|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors All rights reserved. |
| 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 process_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "bufio" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "os/exec" |
| 25 | + "path/filepath" |
| 26 | + "runtime" |
| 27 | + "testing" |
| 28 | + "time" |
| 29 | + |
| 30 | + "k8s.io/minikube/pkg/minikube/process" |
| 31 | +) |
| 32 | + |
| 33 | +var waitTimeout = 250 * time.Millisecond |
| 34 | + |
| 35 | +func TestPidfile(t *testing.T) { |
| 36 | + pidfile := filepath.Join(t.TempDir(), "pid") |
| 37 | + if err := process.WritePidfile(pidfile, 42); err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + pid, err := process.ReadPidfile(pidfile) |
| 41 | + if err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + if pid != 42 { |
| 45 | + t.Fatalf("expected 42, got %d", pid) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestPidfileMissing(t *testing.T) { |
| 50 | + pidfile := filepath.Join(t.TempDir(), "pid") |
| 51 | + _, err := process.ReadPidfile(pidfile) |
| 52 | + if !errors.Is(err, os.ErrNotExist) { |
| 53 | + t.Fatalf("unexpected error: %s", err) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestPidfileInvalid(t *testing.T) { |
| 58 | + pidfile := filepath.Join(t.TempDir(), "pid") |
| 59 | + if err := os.WriteFile(pidfile, []byte("invalid"), 0o600); err != nil { |
| 60 | + t.Fatal(err) |
| 61 | + } |
| 62 | + _, err := process.ReadPidfile(pidfile) |
| 63 | + if err == nil { |
| 64 | + t.Fatal("parsing invalid pidfile did not fail") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestProcess(t *testing.T) { |
| 69 | + sleep, err := build(t, "sleep") |
| 70 | + if err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + |
| 74 | + noterm, err := build(t, "noterm") |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + |
| 79 | + t.Run("exists", func(t *testing.T) { |
| 80 | + cmd := startProcess(t, sleep) |
| 81 | + exists, err := process.Exists(cmd.Process.Pid, filepath.Base(sleep)) |
| 82 | + if err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + if !exists { |
| 86 | + t.Fatal("existing process not found") |
| 87 | + } |
| 88 | + exists, err = process.Exists(0xfffffffc, filepath.Base(sleep)) |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + if exists { |
| 93 | + t.Fatal("process with non-existing pid found") |
| 94 | + } |
| 95 | + exists, err = process.Exists(cmd.Process.Pid, "no-such-executable") |
| 96 | + if err != nil { |
| 97 | + t.Fatal(err) |
| 98 | + } |
| 99 | + if exists { |
| 100 | + t.Fatal("process with non-existing executable found") |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("terminate", func(t *testing.T) { |
| 105 | + cmd := startProcess(t, sleep) |
| 106 | + err := process.Terminate(cmd.Process.Pid, filepath.Base(sleep)) |
| 107 | + if err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + waitForTermination(t, cmd) |
| 111 | + exists, err := process.Exists(cmd.Process.Pid, filepath.Base(sleep)) |
| 112 | + if err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + if exists { |
| 116 | + t.Fatalf("reaped process exists") |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + t.Run("terminate name mismatch", func(t *testing.T) { |
| 121 | + cmd := startProcess(t, sleep) |
| 122 | + err := process.Terminate(cmd.Process.Pid, "no-such-process") |
| 123 | + if err == nil { |
| 124 | + t.Fatalf("Signaled unrelated process") |
| 125 | + } |
| 126 | + if err != os.ErrProcessDone { |
| 127 | + t.Fatalf("Unexpected error: %s", err) |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("terminate ignored", func(t *testing.T) { |
| 132 | + if runtime.GOOS == "windows" { |
| 133 | + t.Skip("no way to ignore termination on windows") |
| 134 | + } |
| 135 | + cmd := startProcess(t, noterm) |
| 136 | + err := process.Terminate(cmd.Process.Pid, filepath.Base(noterm)) |
| 137 | + if err != nil { |
| 138 | + t.Fatal(err) |
| 139 | + } |
| 140 | + time.Sleep(waitTimeout) |
| 141 | + exists, err := process.Exists(cmd.Process.Pid, filepath.Base(noterm)) |
| 142 | + if err != nil { |
| 143 | + t.Fatal(err) |
| 144 | + } |
| 145 | + if !exists { |
| 146 | + t.Fatalf("process terminated") |
| 147 | + } |
| 148 | + }) |
| 149 | + |
| 150 | + t.Run("kill", func(t *testing.T) { |
| 151 | + cmd := startProcess(t, noterm) |
| 152 | + err := process.Kill(cmd.Process.Pid, filepath.Base(noterm)) |
| 153 | + if err != nil { |
| 154 | + t.Fatal(err) |
| 155 | + } |
| 156 | + waitForTermination(t, cmd) |
| 157 | + exists, err := process.Exists(cmd.Process.Pid, filepath.Base(noterm)) |
| 158 | + if err != nil { |
| 159 | + t.Fatal(err) |
| 160 | + } |
| 161 | + if exists { |
| 162 | + t.Fatalf("reaped process exists") |
| 163 | + } |
| 164 | + }) |
| 165 | + |
| 166 | + t.Run("kill name mismatch", func(t *testing.T) { |
| 167 | + cmd := startProcess(t, noterm) |
| 168 | + err := process.Kill(cmd.Process.Pid, "no-such-process") |
| 169 | + if err == nil { |
| 170 | + t.Fatalf("Killed unrelated process") |
| 171 | + } |
| 172 | + if err != os.ErrProcessDone { |
| 173 | + t.Fatalf("Unexpected error: %s", err) |
| 174 | + } |
| 175 | + }) |
| 176 | +} |
| 177 | + |
| 178 | +func startProcess(t *testing.T, cmd string) *exec.Cmd { |
| 179 | + name := filepath.Base(cmd) |
| 180 | + c := exec.Command(cmd) |
| 181 | + stdout, err := c.StdoutPipe() |
| 182 | + if err != nil { |
| 183 | + t.Fatal(err) |
| 184 | + } |
| 185 | + |
| 186 | + start := time.Now() |
| 187 | + if err := c.Start(); err != nil { |
| 188 | + t.Fatal(err) |
| 189 | + } |
| 190 | + t.Logf("Started process %q (pid=%v)", name, c.Process.Pid) |
| 191 | + |
| 192 | + t.Cleanup(func() { |
| 193 | + _ = c.Process.Kill() |
| 194 | + _ = c.Wait() |
| 195 | + }) |
| 196 | + |
| 197 | + // Synchronize with the process to ensure it set up signal handlers before |
| 198 | + // we send a signal. |
| 199 | + r := bufio.NewReader(stdout) |
| 200 | + line, err := r.ReadString('\n') |
| 201 | + if err != nil { |
| 202 | + t.Fatal(err) |
| 203 | + } |
| 204 | + if line != "READY\n" { |
| 205 | + t.Fatalf("Unexpected response: %q", line) |
| 206 | + } |
| 207 | + t.Logf("Process %q ready in %.6f seconds", name, time.Since(start).Seconds()) |
| 208 | + |
| 209 | + return c |
| 210 | +} |
| 211 | + |
| 212 | +func waitForTermination(t *testing.T, cmd *exec.Cmd) { |
| 213 | + name := filepath.Base(cmd.Path) |
| 214 | + timer := time.AfterFunc(waitTimeout, func() { |
| 215 | + t.Fatalf("Timeout waiting for %q", name) |
| 216 | + }) |
| 217 | + defer timer.Stop() |
| 218 | + start := time.Now() |
| 219 | + err := cmd.Wait() |
| 220 | + t.Logf("Process %q terminated in %.6f seconds: %s", name, time.Since(start).Seconds(), err) |
| 221 | +} |
| 222 | + |
| 223 | +func build(t *testing.T, name string) (string, error) { |
| 224 | + source := fmt.Sprintf("testdata/%s.go", name) |
| 225 | + |
| 226 | + out := filepath.Join(t.TempDir(), name) |
| 227 | + if runtime.GOOS == "windows" { |
| 228 | + out += ".exe" |
| 229 | + } |
| 230 | + |
| 231 | + t.Logf("Building %q", name) |
| 232 | + cmd := exec.Command("go", "build", "-o", out, source) |
| 233 | + cmd.Stdout = os.Stdout |
| 234 | + cmd.Stderr = os.Stderr |
| 235 | + |
| 236 | + if err := cmd.Run(); err != nil { |
| 237 | + return "", err |
| 238 | + } |
| 239 | + return out, nil |
| 240 | +} |
0 commit comments