github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/misc/graceful_test.go (about) 1 /* 2 Copyright 2019 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package misc 18 19 import ( 20 "context" 21 "os/exec" 22 "runtime" 23 "testing" 24 "time" 25 26 "github.com/GoogleContainerTools/skaffold/testutil" 27 ) 28 29 func TestGracefulBuildCancel(t *testing.T) { 30 if runtime.GOOS == "windows" { 31 t.Skip("graceful cancel doesn't work on windows") 32 } 33 34 tests := []struct { 35 description string 36 command string 37 shouldErr bool 38 }{ 39 { 40 description: "terminate before timeout", 41 command: "echo done", 42 }, 43 { 44 description: "terminate gracefully and exit 0", 45 command: "trap 'echo trap' INT; sleep .1", 46 }, 47 { 48 description: "kill process after sigint", 49 command: "trap 'echo trap' INT; sleep 100", 50 shouldErr: true, 51 }, 52 } 53 54 for _, test := range tests { 55 testutil.Run(t, test.description, func(t *testutil.T) { 56 t.Override(&gracePeriod, 200*time.Millisecond) 57 ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) 58 defer cancel() 59 60 cmd := exec.Command("bash", "-c", test.command) 61 t.CheckNoError(cmd.Start()) 62 63 err := HandleGracefulTermination(ctx, cmd) 64 t.CheckError(test.shouldErr, err) 65 }) 66 } 67 }