|
7 | 7 | "strings" |
8 | 8 | "sync/atomic" |
9 | 9 | "testing" |
| 10 | + |
| 11 | + "github.com/acmore/okdev/internal/kube" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | func TestEnsureDevSSHDRunning(t *testing.T) { |
@@ -160,3 +162,115 @@ func TestRunSSHShellWithReconnectRapidFailures(t *testing.T) { |
160 | 162 | t.Fatalf("expected rapid failure circuit breaker, got %v", err) |
161 | 163 | } |
162 | 164 | } |
| 165 | + |
| 166 | +func TestRunSSHShellWithReconnectDetectsContainerRestart(t *testing.T) { |
| 167 | + t.Setenv("HOME", t.TempDir()) |
| 168 | + runner := &fakeShellRunner{ |
| 169 | + shellErrors: []error{errors.New("connection reset by peer")}, |
| 170 | + } |
| 171 | + runner.connected.Store(true) |
| 172 | + |
| 173 | + checkRestart := func() *kube.ContainerRestartInfo { |
| 174 | + return &kube.ContainerRestartInfo{ |
| 175 | + RestartCount: 2, |
| 176 | + LastReason: "OOMKilled", |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + var errOut bytes.Buffer |
| 181 | + err := runSSHShellWithReconnectAndRestartCheck(context.Background(), runner, "test-sess", &errOut, checkRestart) |
| 182 | + if err != nil { |
| 183 | + t.Fatalf("expected nil return on container restart, got %v", err) |
| 184 | + } |
| 185 | + output := errOut.String() |
| 186 | + if !strings.Contains(output, "OOMKilled") { |
| 187 | + t.Fatalf("expected OOMKilled in output, got %q", output) |
| 188 | + } |
| 189 | + if !strings.Contains(output, "Container restarted") { |
| 190 | + t.Fatalf("expected 'Container restarted' in output, got %q", output) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func TestRunSSHShellWithReconnectNoRestartContinuesRecovery(t *testing.T) { |
| 195 | + t.Setenv("HOME", t.TempDir()) |
| 196 | + runner := &fakeShellRunner{ |
| 197 | + shellErrors: []error{errors.New("connection reset by peer"), nil}, |
| 198 | + } |
| 199 | + runner.connected.Store(true) |
| 200 | + |
| 201 | + // checkRestart returns nil — no restart detected, should continue reconnecting. |
| 202 | + checkRestart := func() *kube.ContainerRestartInfo { |
| 203 | + return nil |
| 204 | + } |
| 205 | + |
| 206 | + var errOut bytes.Buffer |
| 207 | + err := runSSHShellWithReconnectAndRestartCheck(context.Background(), runner, "test-sess", &errOut, checkRestart) |
| 208 | + if err != nil { |
| 209 | + t.Fatalf("expected successful reconnect, got %v", err) |
| 210 | + } |
| 211 | + if !strings.Contains(errOut.String(), "Connection lost") { |
| 212 | + t.Fatalf("expected 'Connection lost' message, got %q", errOut.String()) |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +func TestRunSSHShellWithReconnectDetectsContainerCrashLoop(t *testing.T) { |
| 217 | + t.Setenv("HOME", t.TempDir()) |
| 218 | + runner := &fakeShellRunner{ |
| 219 | + shellErrors: []error{errors.New("connection reset by peer")}, |
| 220 | + } |
| 221 | + runner.connected.Store(true) |
| 222 | + |
| 223 | + checkRestart := func() *kube.ContainerRestartInfo { |
| 224 | + return &kube.ContainerRestartInfo{ |
| 225 | + RestartCount: 5, |
| 226 | + LastReason: "Error", |
| 227 | + LastExitCode: 137, |
| 228 | + CurrentWaiting: "CrashLoopBackOff", |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + var errOut bytes.Buffer |
| 233 | + err := runSSHShellWithReconnectAndRestartCheck(context.Background(), runner, "test-sess", &errOut, checkRestart) |
| 234 | + if err != nil { |
| 235 | + t.Fatalf("expected nil return on container restart, got %v", err) |
| 236 | + } |
| 237 | + output := errOut.String() |
| 238 | + if !strings.Contains(output, "CrashLoopBackOff") { |
| 239 | + t.Fatalf("expected CrashLoopBackOff in output, got %q", output) |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +func TestPrintContainerRestartNotice(t *testing.T) { |
| 244 | + tests := []struct { |
| 245 | + name string |
| 246 | + info kube.ContainerRestartInfo |
| 247 | + contains []string |
| 248 | + }{ |
| 249 | + { |
| 250 | + name: "OOMKilled", |
| 251 | + info: kube.ContainerRestartInfo{RestartCount: 1, LastReason: "OOMKilled"}, |
| 252 | + contains: []string{"Container restarted (OOMKilled)", "tmux sessions"}, |
| 253 | + }, |
| 254 | + { |
| 255 | + name: "CrashLoopBackOff", |
| 256 | + info: kube.ContainerRestartInfo{RestartCount: 5, LastReason: "Error", CurrentWaiting: "CrashLoopBackOff"}, |
| 257 | + contains: []string{"Container restarted (Error)", "CrashLoopBackOff"}, |
| 258 | + }, |
| 259 | + { |
| 260 | + name: "unknown reason", |
| 261 | + info: kube.ContainerRestartInfo{RestartCount: 1}, |
| 262 | + contains: []string{"Container restarted (unknown)"}, |
| 263 | + }, |
| 264 | + } |
| 265 | + for _, tt := range tests { |
| 266 | + t.Run(tt.name, func(t *testing.T) { |
| 267 | + var buf bytes.Buffer |
| 268 | + printContainerRestartNotice(&buf, &tt.info) |
| 269 | + for _, want := range tt.contains { |
| 270 | + if !strings.Contains(buf.String(), want) { |
| 271 | + t.Errorf("expected output to contain %q, got %q", want, buf.String()) |
| 272 | + } |
| 273 | + } |
| 274 | + }) |
| 275 | + } |
| 276 | +} |
0 commit comments