From 0b3e93bfb625ad5c2c396d3a26586223dc60f821 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Tue, 21 Aug 2018 15:33:18 +0200 Subject: [PATCH] Add test for signal handling Signed-off-by: David Gageot --- cmd/skaffold/app/cmd/dev.go | 13 --------- cmd/skaffold/app/cmd/signals.go | 38 ++++++++++++++++++++++++ cmd/skaffold/app/cmd/signals_test.go | 43 ++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 cmd/skaffold/app/cmd/signals.go create mode 100644 cmd/skaffold/app/cmd/signals_test.go diff --git a/cmd/skaffold/app/cmd/dev.go b/cmd/skaffold/app/cmd/dev.go index 87935c1d5c5..394b786aedf 100644 --- a/cmd/skaffold/app/cmd/dev.go +++ b/cmd/skaffold/app/cmd/dev.go @@ -19,9 +19,6 @@ package cmd import ( "context" "io" - "os" - "os/signal" - "syscall" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner" "github.com/pkg/errors" @@ -76,13 +73,3 @@ func dev(out io.Writer) error { } } } - -func catchCtrlC(cancel context.CancelFunc) { - signals := make(chan os.Signal, 1) - signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGPIPE) - - go func() { - <-signals - cancel() - }() -} diff --git a/cmd/skaffold/app/cmd/signals.go b/cmd/skaffold/app/cmd/signals.go new file mode 100644 index 00000000000..5491201f2fe --- /dev/null +++ b/cmd/skaffold/app/cmd/signals.go @@ -0,0 +1,38 @@ +/* +Copyright 2018 The Skaffold Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "context" + "os" + "os/signal" + "syscall" +) + +func catchCtrlC(cancel context.CancelFunc) { + signals := make(chan os.Signal, 1) + signal.Notify(signals, + syscall.SIGTERM, + syscall.SIGINT, + syscall.SIGPIPE, + ) + + go func() { + <-signals + cancel() + }() +} diff --git a/cmd/skaffold/app/cmd/signals_test.go b/cmd/skaffold/app/cmd/signals_test.go new file mode 100644 index 00000000000..6a3ec465944 --- /dev/null +++ b/cmd/skaffold/app/cmd/signals_test.go @@ -0,0 +1,43 @@ +// +build !windows + +/* +Copyright 2018 The Skaffold Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "context" + "sync" + "syscall" + "testing" +) + +func TestCatchCtrlC(t *testing.T) { + var wg sync.WaitGroup + wg.Add(1) + + ctx, cancel := context.WithCancel(context.Background()) + catchCtrlC(cancel) + + go func() { + <-ctx.Done() + wg.Done() + }() + + syscall.Kill(syscall.Getpid(), syscall.SIGINT) + + wg.Wait() +}