-
Notifications
You must be signed in to change notification settings - Fork 3
/
apply_test.go
174 lines (152 loc) · 3.47 KB
/
apply_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"os/exec"
"reflect"
"testing"
)
func TestKubernetes_Apply_Command(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
Path: "~/.kube/config",
}
a := &Apply{
DryRun: "false",
Files: []string{"apply.yml"},
Output: "json",
}
// nolint: gosec // testing purposes
for _, file := range a.Files {
want := exec.Command(
_kubectl,
fmt.Sprintf("--kubeconfig=%s", c.Path),
fmt.Sprintf("--cluster=%s", c.Cluster),
fmt.Sprintf("--context=%s", c.Context),
fmt.Sprintf("--namespace=%s", c.Namespace),
"apply",
"--dry-run=none",
fmt.Sprintf("--filename=%s", file),
fmt.Sprintf("--output=%s", a.Output),
)
got := a.Command(c, file)
if !reflect.DeepEqual(got, want) {
t.Errorf("Command is %v, want %v", got, want)
}
}
}
func TestKubernetes_Apply_Command_WithDryRunTrue(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
Path: "~/.kube/config",
}
a := &Apply{
DryRun: "true",
Files: []string{"apply.yml"},
Output: "json",
}
// nolint: gosec // testing purposes
for _, file := range a.Files {
want := exec.Command(
_kubectl,
fmt.Sprintf("--kubeconfig=%s", c.Path),
fmt.Sprintf("--cluster=%s", c.Cluster),
fmt.Sprintf("--context=%s", c.Context),
fmt.Sprintf("--namespace=%s", c.Namespace),
"apply",
"--dry-run=client",
fmt.Sprintf("--filename=%s", file),
fmt.Sprintf("--output=%s", a.Output),
)
got := a.Command(c, file)
if !reflect.DeepEqual(got, want) {
t.Errorf("Command is %v, want %v", got, want)
}
}
}
func TestKubernetes_Apply_Command_WithDryRunAnythingNonBoolean(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
Path: "~/.kube/config",
}
a := &Apply{
DryRun: "server",
Files: []string{"apply.yml"},
Output: "json",
}
// nolint: gosec // testing purposes
for _, file := range a.Files {
want := exec.Command(
_kubectl,
fmt.Sprintf("--kubeconfig=%s", c.Path),
fmt.Sprintf("--cluster=%s", c.Cluster),
fmt.Sprintf("--context=%s", c.Context),
fmt.Sprintf("--namespace=%s", c.Namespace),
"apply",
fmt.Sprintf("--dry-run=%s", a.DryRun),
fmt.Sprintf("--filename=%s", file),
fmt.Sprintf("--output=%s", a.Output),
)
got := a.Command(c, file)
if !reflect.DeepEqual(got, want) {
t.Errorf("Command is %v, want %v", got, want)
}
}
}
func TestKubernetes_Apply_Exec_Error(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
Namespace: "namespace",
}
a := &Apply{
DryRun: "false",
Files: []string{"apply.yml"},
Output: "json",
}
err := a.Exec(c)
if err == nil {
t.Errorf("Exec should have returned err")
}
}
func TestKubernetes_Apply_Validate(t *testing.T) {
// setup types
a := &Apply{
DryRun: "false",
Files: []string{"apply.yml"},
Output: "json",
}
err := a.Validate()
if err != nil {
t.Errorf("Validate returned err: %v", err)
}
}
func TestKubernetes_Apply_Validate_NoFiles(t *testing.T) {
// setup types
a := &Apply{
DryRun: "false",
Output: "json",
}
err := a.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}