Skip to content

Commit fbdf921

Browse files
committed
test(cmd): add TestSetupAllModes test
- add tests for container setup across all modes AI-Generated: true AI-Model: claude-sonnet-4 Signed-off-by: AtomicFS <vojtech.vesely@9elements.com>
1 parent 1d71157 commit fbdf921

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

cmd/firmware-action/container/container_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestDetectMode(t *testing.T) {
3232
dockerfileDirExists := filepath.Join(tmpDir, "dockerfile-exists")
3333
err = os.MkdirAll(dockerfileDirExists, 0o755)
3434
dockerfileExists := filepath.Join(dockerfileDirExists, "Dockerfile")
35+
3536
assert.NoError(t, err)
3637

3738
dockerfileNotExists := filepath.Join(tmpDir, "dockerfile-not-exists")
@@ -101,6 +102,7 @@ func TestDetectMode(t *testing.T) {
101102

102103
assert.Equal(t, tc.expectedURL, actualURL)
103104
assert.Equal(t, tc.expectedMode, actualMode)
105+
104106
if tc.wantErr != nil {
105107
assert.ErrorIs(t, actualErr, tc.wantErr)
106108
} else {
@@ -292,6 +294,110 @@ func TestSetup(t *testing.T) {
292294
}
293295
}
294296

297+
func TestSetupAllModes(t *testing.T) {
298+
if testing.Short() {
299+
t.Skip("skipping test in short mode")
300+
}
301+
302+
ctx := t.Context()
303+
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
304+
assert.NoError(t, err)
305+
306+
defer client.Close()
307+
308+
tmpDir := t.TempDir()
309+
t.Chdir(tmpDir)
310+
311+
// Create test Dockerfile
312+
dockerfileDir := filepath.Join(tmpDir, "dockerfile-test")
313+
err = os.MkdirAll(dockerfileDir, 0o755)
314+
assert.NoError(t, err)
315+
316+
dockerfileContent := "FROM ubuntu:latest"
317+
err = os.WriteFile(filepath.Join(dockerfileDir, "Dockerfile"), []byte(dockerfileContent), 0o644)
318+
assert.NoError(t, err)
319+
320+
tarfilePath := filepath.Join(tmpDir, "ubuntu-latest.tar")
321+
322+
testCases := []struct {
323+
name string
324+
containerURL string
325+
mode containerURLtype
326+
saveAs string
327+
}{
328+
{
329+
name: "URL mode - ubuntu latest",
330+
containerURL: "ubuntu:latest",
331+
mode: ModeURL,
332+
saveAs: tarfilePath,
333+
},
334+
{
335+
name: "Dockerfile mode",
336+
containerURL: "file://" + dockerfileDir,
337+
mode: ModeDockerfile,
338+
saveAs: "",
339+
},
340+
{
341+
name: "Dockerfile mode with Dockerfile suffix",
342+
containerURL: "file://" + filepath.Join(dockerfileDir, "Dockerfile"),
343+
mode: ModeDockerfile,
344+
saveAs: "",
345+
},
346+
{
347+
// Note: Tarfile test depends on URL test creating the tar file first
348+
name: "Tarfile mode",
349+
containerURL: "file://" + tarfilePath,
350+
mode: ModeTarfile,
351+
saveAs: "",
352+
},
353+
}
354+
355+
for _, tc := range testCases {
356+
t.Run(tc.name, func(t *testing.T) {
357+
// Verify mode detection
358+
detectedURL, detectedMode, err := detectMode(tc.containerURL)
359+
assert.NoError(t, err)
360+
assert.Equal(t, tc.mode, detectedMode)
361+
362+
// Setup container options
363+
opts := SetupOpts{
364+
ContainerURL: tc.containerURL,
365+
MountContainerDir: "/src",
366+
MountHostDir: tmpDir,
367+
WorkdirContainer: "/src",
368+
ContainerInputDir: "inputs",
369+
InputFiles: []string{},
370+
InputDirs: []string{},
371+
}
372+
373+
// Test container setup
374+
container, err := Setup(ctx, client, &opts)
375+
assert.NoError(t, err)
376+
assert.NotNil(t, container)
377+
378+
if tc.saveAs != "" {
379+
_, err = container.Export(ctx, tc.saveAs)
380+
assert.NoError(t, err)
381+
}
382+
383+
// Verify container is working by running a simple command
384+
_, err = container.WithExec([]string{"echo", "test"}).Sync(ctx)
385+
assert.NoError(t, err)
386+
387+
// Verify working directory is set correctly
388+
output, err := container.WithExec([]string{"pwd"}).Stdout(ctx)
389+
assert.NoError(t, err)
390+
assert.Equal(t, "/src\n", output)
391+
392+
// Verify mount point exists
393+
_, err = container.WithExec([]string{"ls", "/src"}).Sync(ctx)
394+
assert.NoError(t, err)
395+
396+
t.Logf("Successfully tested %s mode with URL: %s", tc.name, detectedURL)
397+
})
398+
}
399+
}
400+
295401
func TestGetArtifacts(t *testing.T) {
296402
// This test is rather slow (between 10s and 20s)
297403
if testing.Short() {

0 commit comments

Comments
 (0)