Adds source for cross platform killable process to test

As a way to clean the minikube repo dir.
pull/15782/head
x7upLime 2023-02-10 21:08:00 +02:00
parent e91f041802
commit badc971403
2 changed files with 36 additions and 42 deletions

View File

@ -227,7 +227,42 @@ func TestDeleteAllProfiles(t *testing.T) {
// then tries to execute the tryKillOne function on it;
// if after tryKillOne the process still exists, we consider it a failure
func TestTryKillOne(t *testing.T) {
dontkillmeProc := exec.Command("go", "run", "tools/pleasedontkillme/main.go")
var pleasedontkillmeSource = []byte(`
package main
import (
"os"
"os/signal"
"syscall"
)
// This is used to unittest functions that kill processes,
// in a cross-platform way.
func main() {
ch := make(chan os.Signal, 1)
done := make(chan struct{})
defer close(ch)
signal.Notify(ch, syscall.SIGHUP)
defer signal.Stop(ch)
go func() {
<-ch
close(done)
}()
<-done
}
`)
td := t.TempDir()
tmpfile := filepath.Join(td, "pleasedontkillme.go")
if err := os.WriteFile(tmpfile, pleasedontkillmeSource, 0o700); err != nil {
t.Fatalf("copying source to %s", tmpfile)
}
dontkillmeProc := exec.Command("go", "run", tmpfile)
err := dontkillmeProc.Start()
if err != nil {
t.Fatalf("while execing child process")

View File

@ -1,41 +0,0 @@
/*
Copyright 2023 The Kubernetes Authors All rights reserved.
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 main
import (
"os"
"os/signal"
"syscall"
)
// This is used to unittest functions that kill processes,
// in a cross-platform way.
func main() {
ch := make(chan os.Signal, 1)
done := make(chan struct{})
defer close(ch)
signal.Notify(ch, syscall.SIGHUP)
defer signal.Stop(ch)
go func() {
<-ch
close(done)
}()
<-done
}