@@ -30,8 +30,10 @@ import (
30
30
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
31
31
"github.com/sirupsen/logrus"
32
32
appsv1 "k8s.io/api/apps/v1"
33
+ batchv1 "k8s.io/api/batch/v1"
33
34
"k8s.io/api/core/v1"
34
35
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36
+ "k8s.io/apimachinery/pkg/util/wait"
35
37
"k8s.io/client-go/kubernetes"
36
38
)
37
39
@@ -185,19 +187,116 @@ func TestRun(t *testing.T) {
185
187
}
186
188
187
189
// Cleanup
188
- args = []string {"delete" , "--namespace" , ns .Name }
189
- if testCase .filename != "" {
190
- args = append (args , "-f" , testCase .filename )
191
- }
192
- cmd = exec .Command ("skaffold" , args ... )
190
+ cleanupTest (t , ns , testCase .dir , testCase .filename )
191
+ })
192
+ }
193
+ }
194
+
195
+ func TestDev (t * testing.T ) {
196
+ type testDevCase struct {
197
+ description string
198
+ dir string
199
+ args []string
200
+ setup func (t * testing.T ) func (t * testing.T )
201
+ jobs []string
202
+ jobValidation func (t * testing.T , ns * v1.Namespace , j * batchv1.Job )
203
+ }
204
+
205
+ testCases := []testDevCase {
206
+ {
207
+ description : "delete and redeploy job" ,
208
+ dir : "../examples/test-dev-job" ,
209
+ args : []string {"dev" },
210
+ setup : func (t * testing.T ) func (t * testing.T ) {
211
+ // create foo
212
+ cmd := exec .Command ("touch" , "../examples/test-dev-job/foo" )
213
+ if output , err := util .RunCmdOut (cmd ); err != nil {
214
+ t .Fatalf ("creating foo: %s %v" , output , err )
215
+ }
216
+ return func (t * testing.T ) {
217
+ // delete foo
218
+ cmd := exec .Command ("rm" , "../examples/test-dev-job/foo" )
219
+ if output , err := util .RunCmdOut (cmd ); err != nil {
220
+ t .Fatalf ("creating foo: %s %v" , output , err )
221
+ }
222
+ }
223
+ },
224
+ jobs : []string {
225
+ "test-dev-job" ,
226
+ },
227
+ jobValidation : func (t * testing.T , ns * v1.Namespace , j * batchv1.Job ) {
228
+ originalUID := j .GetUID ()
229
+ // Make a change to foo so that dev is forced to delete the job and redeploy
230
+ cmd := exec .Command ("sh" , "-c" , "echo bar > ../examples/test-dev-job/foo" )
231
+ if output , err := util .RunCmdOut (cmd ); err != nil {
232
+ t .Fatalf ("creating bar: %s %v" , output , err )
233
+ }
234
+ // Make sure the UID of the old Job and the UID of the new Job is different
235
+ err := wait .PollImmediate (time .Millisecond * 500 , 10 * time .Minute , func () (bool , error ) {
236
+ newJob , err := client .BatchV1 ().Jobs (ns .Name ).Get (j .Name , meta_v1.GetOptions {})
237
+ if err != nil {
238
+ return false , nil
239
+ }
240
+ return originalUID != newJob .GetUID (), nil
241
+ })
242
+ if err != nil {
243
+ t .Fatalf ("original UID and new UID are the same, redeploy failed" )
244
+ }
245
+ },
246
+ },
247
+ }
248
+
249
+ for _ , testCase := range testCases {
250
+ t .Run (testCase .description , func (t * testing.T ) {
251
+ ns , deleteNs := setupNamespace (t )
252
+ defer deleteNs ()
253
+
254
+ cleanupTC := testCase .setup (t )
255
+ defer cleanupTC (t )
256
+
257
+ args := []string {}
258
+ args = append (args , testCase .args ... )
259
+ args = append (args , "--namespace" , ns .Name )
260
+
261
+ cmd := exec .Command ("skaffold" , args ... )
193
262
cmd .Dir = testCase .dir
194
- if output , err := util .RunCmdOut (cmd ); err != nil {
195
- t .Fatalf ("skaffold delete: %s %v" , output , err )
263
+ go func () {
264
+ if output , err := util .RunCmdOut (cmd ); err != nil {
265
+ t .Fatalf ("skaffold: %s %v" , output , err )
266
+ }
267
+ }()
268
+
269
+ for _ , j := range testCase .jobs {
270
+ if err := kubernetesutil .WaitForJobToStabilize (client , ns .Name , j , 10 * time .Minute ); err != nil {
271
+ t .Fatalf ("Timed out waiting for job to stabilize" )
272
+ }
273
+ if testCase .jobValidation != nil {
274
+ job , err := client .BatchV1 ().Jobs (ns .Name ).Get (j , meta_v1.GetOptions {})
275
+ if err != nil {
276
+ t .Fatalf ("Could not find job: %s %s" , ns .Name , j )
277
+ }
278
+ testCase .jobValidation (t , ns , job )
279
+ }
196
280
}
281
+
282
+ // Cleanup
283
+ cleanupTest (t , ns , testCase .dir , "" )
197
284
})
198
285
}
199
286
}
200
287
288
+ func cleanupTest (t * testing.T , ns * v1.Namespace , dir , filename string ) {
289
+ args := []string {"delete" , "--namespace" , ns .Name }
290
+ if filename != "" {
291
+ args = append (args , "-f" , filename )
292
+ }
293
+ cmd := exec .Command ("skaffold" , args ... )
294
+ cmd .Dir = dir
295
+ if output , err := util .RunCmdOut (cmd ); err != nil {
296
+ t .Fatalf ("skaffold delete: %s %v" , output , err )
297
+ }
298
+ }
299
+
201
300
func setupNamespace (t * testing.T ) (* v1.Namespace , func ()) {
202
301
ns , err := client .CoreV1 ().Namespaces ().Create (& v1.Namespace {
203
302
ObjectMeta : meta_v1.ObjectMeta {
0 commit comments