@@ -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" )
@@ -164,31 +123,16 @@ func TestRun(t *testing.T) {
164
123
{
165
124
description : "kaniko example" ,
166
125
args : []string {"run" },
167
- pods : []testObject {
168
- {
169
- name : "getting-started-kaniko" ,
170
- },
171
- },
172
- dir : "../examples/kaniko" ,
173
- remoteOnly : true ,
126
+ pods : []string {"getting-started-kaniko" },
127
+ dir : "../examples/kaniko" ,
128
+ remoteOnly : true ,
174
129
},
175
130
{
176
131
description : "helm example" ,
177
132
args : []string {"run" },
178
- deployments : []testObject {
179
- {
180
- name : "skaffold-helm" ,
181
- },
182
- },
183
- dir : "../examples/helm-deployment" ,
184
- remoteOnly : true ,
185
- cleanup : func (t * testing.T ) {
186
- cmd := exec .Command ("helm" , "delete" , "--purge" , "skaffold-helm" )
187
- output , err := util .RunCmdOut (cmd )
188
- if err != nil {
189
- t .Fatalf ("skaffold: %s %v" , output , err )
190
- }
191
- },
133
+ deployments : []string {"skaffold-helm" },
134
+ dir : "../examples/helm-deployment" ,
135
+ remoteOnly : true ,
192
136
},
193
137
}
194
138
@@ -201,42 +145,48 @@ func TestRun(t *testing.T) {
201
145
ns , deleteNs := setupNamespace (t )
202
146
defer deleteNs ()
203
147
204
- args := []string {"--namespace" , ns . Name }
148
+ args := []string {}
205
149
args = append (args , testCase .args ... )
150
+ args = append (args , "--namespace" , ns .Name )
151
+ if testCase .filename != "" {
152
+ args = append (args , "-f" , testCase .filename )
153
+ }
206
154
207
155
cmd := exec .Command ("skaffold" , args ... )
208
- env := os .Environ ()
209
- for k , v := range testCase .env {
210
- env = append (env , fmt .Sprintf ("%s=%s" , k , v ))
211
- }
212
- cmd .Env = env
156
+ cmd .Env = append (os .Environ (), testCase .env ... )
213
157
cmd .Dir = testCase .dir
214
- output , err := util .RunCmdOut (cmd )
215
- if err != nil {
158
+ if output , err := util .RunCmdOut (cmd ); err != nil {
216
159
t .Fatalf ("skaffold: %s %v" , output , err )
217
160
}
218
161
219
162
for _ , p := range testCase .pods {
220
- if err := kubernetesutil .WaitForPodReady (client .CoreV1 ().Pods (ns .Name ), p . name ); err != nil {
163
+ if err := kubernetesutil .WaitForPodReady (client .CoreV1 ().Pods (ns .Name ), p ); err != nil {
221
164
t .Fatalf ("Timed out waiting for pod ready" )
222
165
}
223
166
}
224
167
225
168
for _ , d := range testCase .deployments {
226
- if err := kubernetesutil .WaitForDeploymentToStabilize (client , ns .Name , d . name , 10 * time .Minute ); err != nil {
169
+ if err := kubernetesutil .WaitForDeploymentToStabilize (client , ns .Name , d , 10 * time .Minute ); err != nil {
227
170
t .Fatalf ("Timed out waiting for deployment to stabilize" )
228
171
}
229
172
if testCase .deploymentValidation != nil {
230
- deployment , err := client .AppsV1 ().Deployments (ns .Name ).Get (d . name , meta_v1.GetOptions {})
173
+ deployment , err := client .AppsV1 ().Deployments (ns .Name ).Get (d , meta_v1.GetOptions {})
231
174
if err != nil {
232
175
t .Fatalf ("Could not find deployment: %s %s" , ns .Name , d )
233
176
}
234
177
testCase .deploymentValidation (t , deployment )
235
178
}
179
+ }
236
180
237
- if testCase .cleanup != nil {
238
- testCase .cleanup (t )
239
- }
181
+ // Cleanup
182
+ args = []string {"delete" , "--namespace" , ns .Name }
183
+ if testCase .filename != "" {
184
+ args = append (args , "-f" , testCase .filename )
185
+ }
186
+ cmd = exec .Command ("skaffold" , args ... )
187
+ cmd .Dir = testCase .dir
188
+ if output , err := util .RunCmdOut (cmd ); err != nil {
189
+ t .Fatalf ("skaffold delete: %s %v" , output , err )
240
190
}
241
191
})
242
192
}
0 commit comments