Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 envtest: improve process cleanup #2560

Merged
merged 1 commit into from Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/internal/testing/controlplane/kubectl.go
Expand Up @@ -112,6 +112,7 @@ func (k *KubeCtl) Run(args ...string) (stdout, stderr io.Reader, err error) {
cmd := exec.Command(k.Path, allArgs...)
cmd.Stdout = stdoutBuffer
cmd.Stderr = stderrBuffer
cmd.SysProcAttr = process.GetSysProcAttr()

err = cmd.Run()

Expand Down
28 changes: 28 additions & 0 deletions pkg/internal/testing/process/procattr_other.go
@@ -0,0 +1,28 @@
//go:build !aix && !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris && !zos
// +build !aix,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris,!zos

/*
Copyright 2016 The Kubernetes 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 process

import "syscall"

// GetSysProcAttr returns the SysProcAttr to use for the process,
// for non-unix systems this returns nil.
func GetSysProcAttr() *syscall.SysProcAttr {
return nil
}
33 changes: 33 additions & 0 deletions pkg/internal/testing/process/procattr_unix.go
@@ -0,0 +1,33 @@
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos

/*
Copyright 2023 The Kubernetes 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 process

import (
"golang.org/x/sys/unix"
)

// GetSysProcAttr returns the SysProcAttr to use for the process,
// for unix systems this returns a SysProcAttr with Setpgid set to true,
// which inherits the parent's process group id.
func GetSysProcAttr() *unix.SysProcAttr {
return &unix.SysProcAttr{
Setpgid: true,
}
}
4 changes: 4 additions & 0 deletions pkg/internal/testing/process/process.go
Expand Up @@ -155,6 +155,7 @@ func (ps *State) Start(stdout, stderr io.Writer) (err error) {
ps.Cmd = exec.Command(ps.Path, ps.Args...)
ps.Cmd.Stdout = stdout
ps.Cmd.Stderr = stderr
ps.Cmd.SysProcAttr = GetSysProcAttr()

ready := make(chan bool)
timedOut := time.After(ps.StartTimeout)
Expand Down Expand Up @@ -265,6 +266,9 @@ func (ps *State) Stop() error {
case <-ps.waitDone:
break
case <-timedOut:
if err := ps.Cmd.Process.Signal(syscall.SIGKILL); err != nil {
vincepri marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("unable to kill process %s: %w", ps.Path, err)
}
return fmt.Errorf("timeout waiting for process %s to stop", path.Base(ps.Path))
}
ps.ready = false
Expand Down
11 changes: 1 addition & 10 deletions pkg/internal/testing/process/process_test.go
Expand Up @@ -352,16 +352,7 @@ var _ = Describe("Init", func() {
})

var simpleBashScript = []string{
"-c",
`
i=0
while true
do
echo "loop $i" >&2
let 'i += 1'
sleep 0.2
done
`,
"-c", "tail -f /dev/null",
vincepri marked this conversation as resolved.
Show resolved Hide resolved
}

func getServerURL(server *ghttp.Server) url.URL {
Expand Down