Skip to content

Commit 53eb97d

Browse files
committed
Fix test flake
1 parent 05c164b commit 53eb97d

File tree

1 file changed

+65
-34
lines changed

1 file changed

+65
-34
lines changed

lib/instances/manager_test.go

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -309,47 +309,78 @@ func TestCreateAndDeleteInstance(t *testing.T) {
309309
// Test volume is accessible from inside the guest via exec
310310
t.Log("Testing volume from inside guest via exec...")
311311

312-
// Helper to run command in guest
312+
// Helper to run command in guest with retry (exec agent may need time between connections)
313313
runCmd := func(command ...string) (string, int, error) {
314-
var stdout, stderr bytes.Buffer
315-
exit, err := exec.ExecIntoInstance(ctx, inst.VsockSocket, exec.ExecOptions{
316-
Command: command,
317-
Stdout: &stdout,
318-
Stderr: &stderr,
319-
TTY: false,
320-
})
321-
if err != nil {
322-
return stderr.String(), -1, err
314+
var lastOutput string
315+
var lastExitCode int
316+
var lastErr error
317+
318+
for attempt := 0; attempt < 5; attempt++ {
319+
if attempt > 0 {
320+
time.Sleep(200 * time.Millisecond)
321+
}
322+
323+
var stdout, stderr bytes.Buffer
324+
exit, err := exec.ExecIntoInstance(ctx, inst.VsockSocket, exec.ExecOptions{
325+
Command: command,
326+
Stdout: &stdout,
327+
Stderr: &stderr,
328+
TTY: false,
329+
})
330+
331+
// Combine stdout and stderr
332+
output := stdout.String()
333+
if stderr.Len() > 0 {
334+
output += stderr.String()
335+
}
336+
output = strings.TrimSpace(output)
337+
338+
if err != nil {
339+
lastErr = err
340+
lastOutput = output
341+
lastExitCode = -1
342+
continue
343+
}
344+
345+
lastOutput = output
346+
lastExitCode = exit.Code
347+
lastErr = nil
348+
349+
// Success if we got output or it's a command expected to have no output
350+
if output != "" || exit.Code == 0 {
351+
return output, exit.Code, nil
352+
}
323353
}
324-
return strings.TrimSpace(stdout.String()), exit.Code, nil
354+
355+
return lastOutput, lastExitCode, lastErr
325356
}
326357

327-
// Verify volume mount point exists
328-
output, exitCode, err := runCmd("ls", "-la", "/mnt/data")
329-
require.NoError(t, err, "Should be able to ls /mnt/data")
330-
assert.Equal(t, 0, exitCode, "ls /mnt/data should succeed")
331-
t.Logf("Volume mount contents: %s", output)
332-
333-
// Write a test file to the volume
358+
// Test volume in a single exec call to avoid vsock connection issues
359+
// This verifies: mount exists, can write, can read back, is a real block device
334360
testContent := "hello-from-volume-test"
335-
output, exitCode, err = runCmd("sh", "-c", fmt.Sprintf("echo '%s' > /mnt/data/test.txt", testContent))
336-
require.NoError(t, err, "Should be able to write to volume")
337-
assert.Equal(t, 0, exitCode, "Write to volume should succeed")
338-
339-
// Read the test file back
340-
output, exitCode, err = runCmd("cat", "/mnt/data/test.txt")
341-
require.NoError(t, err, "Should be able to read from volume")
342-
assert.Equal(t, 0, exitCode, "Read from volume should succeed")
343-
assert.Equal(t, testContent, output, "Volume content should match what was written")
361+
script := fmt.Sprintf(`
362+
set -e
363+
echo "=== Volume directory ==="
364+
ls -la /mnt/data
365+
echo "=== Writing test file ==="
366+
echo '%s' > /mnt/data/test.txt
367+
echo "=== Reading test file ==="
368+
cat /mnt/data/test.txt
369+
echo "=== Volume mount info ==="
370+
df -h /mnt/data
371+
`, testContent)
372+
373+
output, exitCode, err := runCmd("sh", "-c", script)
374+
require.NoError(t, err, "Volume test script should execute")
375+
require.Equal(t, 0, exitCode, "Volume test script should succeed")
376+
377+
// Verify all expected output is present
378+
require.Contains(t, output, "lost+found", "Volume should be ext4-formatted")
379+
require.Contains(t, output, testContent, "Should be able to read written content")
380+
require.Contains(t, output, "/dev/vd", "Volume should be mounted from block device")
381+
t.Logf("Volume test output:\n%s", output)
344382
t.Log("Volume read/write test passed!")
345383

346-
// Verify it's a real mount (not just a directory)
347-
output, exitCode, err = runCmd("df", "/mnt/data")
348-
require.NoError(t, err, "Should be able to df /mnt/data")
349-
assert.Equal(t, 0, exitCode, "df /mnt/data should succeed")
350-
assert.Contains(t, output, "/dev/vd", "Volume should be mounted from a block device")
351-
t.Logf("Volume mount info: %s", output)
352-
353384
// Test streaming logs with live updates
354385
t.Log("Testing log streaming with live updates...")
355386
streamCtx, streamCancel := context.WithCancel(ctx)

0 commit comments

Comments
 (0)