From e2be765e7b2b5232d3219ecb594ea0faf234f9f4 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Sun, 10 Mar 2024 00:25:32 +0100 Subject: [PATCH] tests: refactor worker handling in sandbox Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- tests/bake.go | 8 ++++---- tests/build.go | 36 ++++++++++++++++++------------------ tests/create.go | 8 ++++---- tests/imagetools.go | 16 ++++++++-------- tests/inspect.go | 10 +++++----- tests/integration.go | 26 +++++++++++++++++++++++--- tests/ls.go | 2 +- tests/rm.go | 8 ++++---- 8 files changed, 67 insertions(+), 47 deletions(-) diff --git a/tests/bake.go b/tests/bake.go index 4cadd559..520662f7 100644 --- a/tests/bake.go +++ b/tests/bake.go @@ -641,8 +641,8 @@ target "default" { } func testBakeMultiExporters(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker" { - t.Skip("skipping test for non-docker workers") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } registry, err := sb.NewRegistry() @@ -722,8 +722,8 @@ target "default" { } func testBakeLoadPush(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker" { - t.Skip("skipping test for non-docker workers") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } registry, err := sb.NewRegistry() diff --git a/tests/build.go b/tests/build.go index 5bf14fd1..7d20bacd 100644 --- a/tests/build.go +++ b/tests/build.go @@ -125,7 +125,7 @@ func testBuildRegistryExportAttestations(t *testing.T, sb integration.Sandbox) { target := registry + "/buildx/registry:latest" out, err := buildCmd(sb, withArgs(fmt.Sprintf("--output=type=image,name=%s,push=true", target), "--provenance=true", dir)) - if sb.Name() == "docker" { + if isMobyWorker(sb) { require.Error(t, err) require.Contains(t, out, "Attestation is not supported") return @@ -200,7 +200,7 @@ func testImageIDOutput(t *testing.T, sb integration.Sandbox) { func testBuildMobyFromLocalImage(t *testing.T, sb integration.Sandbox) { if !isDockerWorker(sb) { - t.Skip("skipping test for non-docker workers") + t.Skip("only testing with docker workers") } // pull image @@ -290,7 +290,7 @@ RUN exit 1`) func testBuildProgress(t *testing.T, sb integration.Sandbox) { dir := createTestProject(t) - driver, _, _ := strings.Cut(sb.Name(), "+") + sbDriver, _ := driverName(sb.Name()) name := sb.Address() // progress=tty @@ -301,7 +301,7 @@ func testBuildProgress(t *testing.T, sb integration.Sandbox) { io.Copy(buf, f) ttyOutput := buf.String() require.Contains(t, ttyOutput, "[+] Building") - require.Contains(t, ttyOutput, fmt.Sprintf("%s:%s", driver, name)) + require.Contains(t, ttyOutput, fmt.Sprintf("%s:%s", sbDriver, name)) require.Contains(t, ttyOutput, "=> [internal] load build definition from Dockerfile") require.Contains(t, ttyOutput, "=> [base 1/3] FROM docker.io/library/busybox:latest") @@ -309,13 +309,13 @@ func testBuildProgress(t *testing.T, sb integration.Sandbox) { cmd = buildxCmd(sb, withArgs("build", "--progress=plain", "--output=type=cacheonly", dir)) plainOutput, err := cmd.CombinedOutput() require.NoError(t, err) - require.Contains(t, string(plainOutput), fmt.Sprintf(`#0 building with "%s" instance using %s driver`, name, driver)) + require.Contains(t, string(plainOutput), fmt.Sprintf(`#0 building with "%s" instance using %s driver`, name, sbDriver)) require.Contains(t, string(plainOutput), "[internal] load build definition from Dockerfile") require.Contains(t, string(plainOutput), "[base 1/3] FROM docker.io/library/busybox:latest") } func testBuildAnnotations(t *testing.T, sb integration.Sandbox) { - if sb.Name() == "docker" { + if isMobyWorker(sb) { t.Skip("annotations not supported on docker worker") } @@ -374,8 +374,8 @@ func testBuildLabelNoKey(t *testing.T, sb integration.Sandbox) { } func testBuildCacheExportNotSupported(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker" { - t.Skip("skipping test for non-docker workers") + if !isMobyWorker(sb) { + t.Skip("only testing with docker worker") } dir := createTestProject(t) @@ -386,8 +386,8 @@ func testBuildCacheExportNotSupported(t *testing.T, sb integration.Sandbox) { } func testBuildOCIExportNotSupported(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker" { - t.Skip("skipping test for non-docker workers") + if !isMobyWorker(sb) { + t.Skip("only testing with docker worker") } dir := createTestProject(t) @@ -398,8 +398,8 @@ func testBuildOCIExportNotSupported(t *testing.T, sb integration.Sandbox) { } func testBuildMultiPlatformNotSupported(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker" { - t.Skip("skipping test for non-docker workers") + if !isMobyWorker(sb) { + t.Skip("only testing with docker worker") } dir := createTestProject(t) @@ -426,8 +426,8 @@ RUN ping -c 1 buildx.host-gateway-ip.local } func testBuildNetworkModeBridge(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker" { - t.Skip("skipping test for non-docker workers") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } var builderName string @@ -546,8 +546,8 @@ func testBuildRef(t *testing.T, sb integration.Sandbox) { } func testBuildMultiExporters(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker" { - t.Skip("skipping test for non-docker workers") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } registry, err := sb.NewRegistry() @@ -614,8 +614,8 @@ func testBuildMultiExporters(t *testing.T, sb integration.Sandbox) { } func testBuildLoadPush(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker" { - t.Skip("skipping test for non-docker workers") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } registry, err := sb.NewRegistry() diff --git a/tests/create.go b/tests/create.go index 1a2757fc..195691fb 100644 --- a/tests/create.go +++ b/tests/create.go @@ -26,8 +26,8 @@ var createTests = []func(t *testing.T, sb integration.Sandbox){ } func testCreateMemoryLimit(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker-container" { - t.Skip("only testing for docker-container driver") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } var builderName string @@ -45,8 +45,8 @@ func testCreateMemoryLimit(t *testing.T, sb integration.Sandbox) { } func testCreateRestartAlways(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker-container" { - t.Skip("only testing for docker-container driver") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } var builderName string diff --git a/tests/imagetools.go b/tests/imagetools.go index 59d06944..b6d21b49 100644 --- a/tests/imagetools.go +++ b/tests/imagetools.go @@ -22,8 +22,8 @@ var imagetoolsTests = []func(t *testing.T, sb integration.Sandbox){ } func testImagetoolsCopyManifest(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker-container" { - t.Skip("imagetools tests are not driver specific and only run on docker-container") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker, imagetools only runs on docker-container") } dir := createDockerfile(t) @@ -81,8 +81,8 @@ func testImagetoolsCopyManifest(t *testing.T, sb integration.Sandbox) { } func testImagetoolsCopyIndex(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker-container" { - t.Skip("imagetools tests are not driver specific and only run on docker-container") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker, imagetools only runs on docker-container") } dir := createDockerfile(t) @@ -130,8 +130,8 @@ func testImagetoolsCopyIndex(t *testing.T, sb integration.Sandbox) { } func testImagetoolsInspectAndFilter(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker-container" { - t.Skip("imagetools tests are not driver specific and only run on docker-container") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker, imagetools only runs on docker-container") } dir := createDockerfile(t) @@ -181,8 +181,8 @@ func testImagetoolsInspectAndFilter(t *testing.T, sb integration.Sandbox) { } func testImagetoolsAnnotation(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker-container" { - t.Skip("imagetools tests are not driver specific and only run on docker-container") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker, imagetools only runs on docker-container") } dir := createDockerfile(t) diff --git a/tests/inspect.go b/tests/inspect.go index 755ac165..8dea100f 100644 --- a/tests/inspect.go +++ b/tests/inspect.go @@ -41,7 +41,7 @@ func testInspect(t *testing.T, sb integration.Sandbox) { } require.Equal(t, sb.Address(), name) - sbDriver, _, _ := strings.Cut(sb.Name(), "+") + sbDriver, _ := driverName(sb.Name()) require.Equal(t, sbDriver, driver) if isDockerWorker(sb) { require.NotEmpty(t, hostGatewayIP, "host-gateway-ip worker label should be set with docker driver") @@ -51,8 +51,8 @@ func testInspect(t *testing.T, sb integration.Sandbox) { } func testInspectBuildkitdFlags(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker-container" { - t.Skip("only testing for docker-container driver") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } var builderName string @@ -81,8 +81,8 @@ func testInspectBuildkitdFlags(t *testing.T, sb integration.Sandbox) { } func testInspectNetworkHostEntitlement(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker-container" { - t.Skip("only testing for docker-container driver") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } var builderName string diff --git a/tests/integration.go b/tests/integration.go index 335b757c..38cdafbd 100644 --- a/tests/integration.go +++ b/tests/integration.go @@ -71,7 +71,27 @@ func dockerCmd(sb integration.Sandbox, opts ...cmdOpt) *exec.Cmd { return cmd } -func isDockerWorker(sb integration.Sandbox) bool { - sbDriver, _, _ := strings.Cut(sb.Name(), "+") - return sbDriver == "docker" +func isMobyWorker(sb integration.Sandbox) bool { + name, hasFeature := driverName(sb.Name()) + return name == "docker" && !hasFeature +} + +func isDockerWorker(sb integration.Sandbox) bool { + name, _ := driverName(sb.Name()) + return name == "docker" +} + +func isDockerContainerWorker(sb integration.Sandbox) bool { + name, _ := driverName(sb.Name()) + return name == "docker-container" +} + +func driverName(sbName string) (string, bool) { + name := sbName + var hasFeature bool + if b, _, ok := strings.Cut(name, "+"); ok { + name = b + hasFeature = true + } + return name, hasFeature } diff --git a/tests/ls.go b/tests/ls.go index 53246d98..1e147fe0 100644 --- a/tests/ls.go +++ b/tests/ls.go @@ -34,7 +34,7 @@ func testLs(t *testing.T, sb integration.Sandbox) { }, } - sbDriver, _, _ := strings.Cut(sb.Name(), "+") + sbDriver, _ := driverName(sb.Name()) for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { diff --git a/tests/rm.go b/tests/rm.go index 40cf8c94..5f7d4556 100644 --- a/tests/rm.go +++ b/tests/rm.go @@ -21,8 +21,8 @@ var rmTests = []func(t *testing.T, sb integration.Sandbox){ } func testRm(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker-container" { - t.Skip("only testing for docker-container driver") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } out, err := rmCmd(sb, withArgs("default")) @@ -40,8 +40,8 @@ func testRm(t *testing.T, sb integration.Sandbox) { } func testRmMulti(t *testing.T, sb integration.Sandbox) { - if sb.Name() != "docker-container" { - t.Skip("only testing for docker-container driver") + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") } var builderNames []string