@@ -21,7 +21,6 @@ package integration
21
21
import (
22
22
"bytes"
23
23
"flag"
24
- "fmt"
25
24
"os"
26
25
"os/exec"
27
26
"testing"
@@ -34,18 +33,16 @@ import (
34
33
"k8s.io/api/core/v1"
35
34
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36
35
"k8s.io/client-go/kubernetes"
37
- "k8s.io/client-go/tools/clientcmd"
38
- "k8s.io/client-go/tools/clientcmd/api"
39
36
)
40
37
41
- var gkeZone = flag .String ("gke-zone" , "us-central1-a" , "gke zone" )
42
- var gkeClusterName = flag .String ("gke-cluster-name" , "integration-tests" , "name of the integration test cluster" )
43
- var gcpProject = flag .String ("gcp-project" , "k8s-skaffold" , "the gcp project where the integration test cluster lives" )
44
- var remote = flag .Bool ("remote" , false , "if true, run tests on a remote GKE cluster" )
38
+ var (
39
+ gkeZone = flag .String ("gke-zone" , "us-central1-a" , "gke zone" )
40
+ gkeClusterName = flag .String ("gke-cluster-name" , "integration-tests" , "name of the integration test cluster" )
41
+ gcpProject = flag .String ("gcp-project" , "k8s-skaffold" , "the gcp project where the integration test cluster lives" )
42
+ remote = flag .Bool ("remote" , false , "if true, run tests on a remote GKE cluster" )
45
43
46
- var client kubernetes.Interface
47
-
48
- var context * api.Context
44
+ client kubernetes.Interface
45
+ )
49
46
50
47
func TestMain (m * testing.M ) {
51
48
flag .Parse ()
@@ -62,95 +59,57 @@ func TestMain(m *testing.M) {
62
59
logrus .Fatalf ("Test setup error: getting kubernetes client: %s" , err )
63
60
}
64
61
65
- loadingRules := clientcmd .NewDefaultClientConfigLoadingRules ()
66
- kubeConfig := clientcmd .NewNonInteractiveDeferredLoadingClientConfig (loadingRules , & clientcmd.ConfigOverrides {})
67
-
68
- cfg , err := kubeConfig .RawConfig ()
69
- if err != nil {
70
- logrus .Fatalf ("loading kubeconfig: %s" , err )
71
- }
72
-
73
- context = cfg .Contexts [cfg .CurrentContext ]
74
-
75
62
exitCode := m .Run ()
76
63
77
- // Reset default context and namespace
78
- if err := exec .Command ("kubectl" , "config" , "set-context" , context .Cluster , "--namespace" , context .Namespace ).Run (); err != nil {
79
- logrus .Warn (err )
80
- }
81
-
82
64
os .Exit (exitCode )
83
65
}
84
66
85
67
func TestRun (t * testing.T ) {
86
- type testObject struct {
87
- name string
88
- }
89
-
90
68
type testRunCase struct {
91
69
description string
92
70
dir string
71
+ filename string
93
72
args []string
94
- deployments []testObject
95
- pods []testObject
73
+ deployments []string
74
+ pods []string
96
75
deploymentValidation func (t * testing.T , d * appsv1.Deployment )
97
- env map [ string ]string
76
+ env [ ]string
98
77
99
78
remoteOnly bool
100
- cleanup func (t * testing.T )
101
79
}
102
80
103
81
var testCases = []testRunCase {
104
82
{
105
83
description : "getting-started example" ,
106
84
args : []string {"run" },
107
- pods : []testObject {
108
- {
109
- name : "getting-started" ,
110
- },
111
- },
112
- dir : "../examples/getting-started" ,
85
+ pods : []string {"getting-started" },
86
+ dir : "../examples/getting-started" ,
113
87
},
114
88
{
115
89
description : "annotated getting-started example" ,
116
- args : []string {"run" , "-f" , "annotated-skaffold.yaml" },
117
- pods : []testObject {
118
- {
119
- name : "getting-started" ,
120
- },
121
- },
122
- dir : "../examples" ,
90
+ args : []string {"run" },
91
+ filename : "annotated-skaffold.yaml" ,
92
+ pods : []string {"getting-started" },
93
+ dir : "../examples" ,
123
94
},
124
95
{
125
96
description : "getting-started envTagger" ,
126
97
args : []string {"run" },
127
- pods : []testObject {
128
- {
129
- name : "getting-started" ,
130
- },
131
- },
132
- dir : "../examples/tagging-with-environment-variables" ,
133
- env : map [string ]string {"FOO" : "foo" },
98
+ pods : []string {"getting-started" },
99
+ dir : "../examples/tagging-with-environment-variables" ,
100
+ env : []string {"FOO=foo" },
134
101
},
135
102
{
136
103
description : "gcb builder example" ,
137
104
args : []string {"run" , "-p" , "gcb" },
138
- pods : []testObject {
139
- {
140
- name : "getting-started" ,
141
- },
142
- },
143
- dir : "../examples/getting-started" ,
144
- remoteOnly : true ,
105
+ pods : []string {"getting-started" },
106
+ dir : "../examples/getting-started" ,
107
+ remoteOnly : true ,
145
108
},
146
109
{
147
110
description : "deploy kustomize" ,
148
111
args : []string {"deploy" , "--images" , "index.docker.io/library/busybox:1" },
149
- deployments : []testObject {
150
- {
151
- name : "kustomize-test" ,
152
- },
153
- },
112
+ deployments : []string {"kustomize-test" },
154
113
deploymentValidation : func (t * testing.T , d * appsv1.Deployment ) {
155
114
if d == nil {
156
115
t .Fatalf ("Could not find deployment" )
@@ -174,31 +133,16 @@ func TestRun(t *testing.T) {
174
133
{
175
134
description : "kaniko example" ,
176
135
args : []string {"run" },
177
- pods : []testObject {
178
- {
179
- name : "getting-started-kaniko" ,
180
- },
181
- },
182
- dir : "../examples/kaniko" ,
183
- remoteOnly : true ,
136
+ pods : []string {"getting-started-kaniko" },
137
+ dir : "../examples/kaniko" ,
138
+ remoteOnly : true ,
184
139
},
185
140
{
186
141
description : "helm example" ,
187
142
args : []string {"run" },
188
- deployments : []testObject {
189
- {
190
- name : "skaffold-helm" ,
191
- },
192
- },
193
- dir : "../examples/helm-deployment" ,
194
- remoteOnly : true ,
195
- cleanup : func (t * testing.T ) {
196
- cmd := exec .Command ("helm" , "delete" , "--purge" , "skaffold-helm" )
197
- output , err := util .RunCmdOut (cmd )
198
- if err != nil {
199
- t .Fatalf ("skaffold: %s %v" , output , err )
200
- }
201
- },
143
+ deployments : []string {"skaffold-helm" },
144
+ dir : "../examples/helm-deployment" ,
145
+ remoteOnly : true ,
202
146
},
203
147
}
204
148
@@ -211,70 +155,69 @@ func TestRun(t *testing.T) {
211
155
ns , deleteNs := setupNamespace (t )
212
156
defer deleteNs ()
213
157
214
- cmd := exec .Command ("skaffold" , testCase .args ... )
215
- env := os .Environ ()
216
- for k , v := range testCase .env {
217
- env = append (env , fmt .Sprintf ("%s=%s" , k , v ))
158
+ args := []string {}
159
+ args = append (args , testCase .args ... )
160
+ args = append (args , "--namespace" , ns .Name )
161
+ if testCase .filename != "" {
162
+ args = append (args , "-f" , testCase .filename )
218
163
}
219
- cmd .Env = env
164
+
165
+ cmd := exec .Command ("skaffold" , args ... )
166
+ cmd .Env = append (os .Environ (), testCase .env ... )
220
167
cmd .Dir = testCase .dir
221
- output , err := util .RunCmdOut (cmd )
222
- if err != nil {
168
+ if output , err := util .RunCmdOut (cmd ); err != nil {
223
169
t .Fatalf ("skaffold: %s %v" , output , err )
224
170
}
225
171
226
172
for _ , p := range testCase .pods {
227
- if err := kubernetesutil .WaitForPodReady (client .CoreV1 ().Pods (ns .Name ), p . name ); err != nil {
173
+ if err := kubernetesutil .WaitForPodReady (client .CoreV1 ().Pods (ns .Name ), p ); err != nil {
228
174
t .Fatalf ("Timed out waiting for pod ready" )
229
175
}
230
176
}
231
177
232
178
for _ , d := range testCase .deployments {
233
- if err := kubernetesutil .WaitForDeploymentToStabilize (client , ns .Name , d . name , 10 * time .Minute ); err != nil {
179
+ if err := kubernetesutil .WaitForDeploymentToStabilize (client , ns .Name , d , 10 * time .Minute ); err != nil {
234
180
t .Fatalf ("Timed out waiting for deployment to stabilize" )
235
181
}
236
182
if testCase .deploymentValidation != nil {
237
- deployment , err := client .AppsV1 ().Deployments (ns .Name ).Get (d . name , meta_v1.GetOptions {})
183
+ deployment , err := client .AppsV1 ().Deployments (ns .Name ).Get (d , meta_v1.GetOptions {})
238
184
if err != nil {
239
185
t .Fatalf ("Could not find deployment: %s %s" , ns .Name , d )
240
186
}
241
187
testCase .deploymentValidation (t , deployment )
242
188
}
189
+ }
243
190
244
- if testCase .cleanup != nil {
245
- testCase .cleanup (t )
246
- }
191
+ // Cleanup
192
+ args = []string {"delete" , "--namespace" , ns .Name }
193
+ if testCase .filename != "" {
194
+ args = append (args , "-f" , testCase .filename )
195
+ }
196
+ cmd = exec .Command ("skaffold" , args ... )
197
+ cmd .Dir = testCase .dir
198
+ if output , err := util .RunCmdOut (cmd ); err != nil {
199
+ t .Fatalf ("skaffold delete: %s %v" , output , err )
247
200
}
248
201
})
249
202
}
250
203
}
251
204
252
205
func setupNamespace (t * testing.T ) (* v1.Namespace , func ()) {
253
- namespaceName := util .RandomID ()
254
206
ns , err := client .CoreV1 ().Namespaces ().Create (& v1.Namespace {
255
207
ObjectMeta : meta_v1.ObjectMeta {
256
- Name : namespaceName ,
257
- Namespace : namespaceName ,
208
+ GenerateName : "skaffold" ,
258
209
},
259
210
})
260
211
if err != nil {
261
212
t .Fatalf ("creating namespace: %s" , err )
262
213
}
263
214
264
- kubectlCmd := exec .Command ("kubectl" , "config" , "set-context" , context .Cluster , "--namespace" , ns .Name )
265
- if err := util .RunCmd (kubectlCmd ); err != nil {
266
- t .Fatalf ("kubectl config set-context --namespace: %v" , err )
267
- }
268
-
269
- os .Setenv ("SKAFFOLD_DEPLOY_NAMESPACE" , namespaceName )
270
-
271
215
return ns , func () {
272
216
client .CoreV1 ().Namespaces ().Delete (ns .Name , & meta_v1.DeleteOptions {})
273
- os .Setenv ("SKAFFOLD_DEPLOY_NAMESPACE" , "" )
274
217
}
275
218
}
276
219
func TestFix (t * testing.T ) {
277
- _ , deleteNs := setupNamespace (t )
220
+ ns , deleteNs := setupNamespace (t )
278
221
defer deleteNs ()
279
222
280
223
fixCmd := exec .Command ("skaffold" , "fix" , "-f" , "skaffold.yaml" )
@@ -284,7 +227,7 @@ func TestFix(t *testing.T) {
284
227
t .Fatalf ("testing error: %v" , err )
285
228
}
286
229
287
- runCmd := exec .Command ("skaffold" , "run" , "-f" , "-" )
230
+ runCmd := exec .Command ("skaffold" , "run" , "--namespace" , ns . Name , "- f" , "-" )
288
231
runCmd .Dir = "testdata/old-config"
289
232
runCmd .Stdin = bytes .NewReader (out )
290
233
err = util .RunCmd (runCmd )
0 commit comments