Skip to content

Commit

Permalink
Merge pull request #2341 from crazy-max/tests-refactor-worker-handling
Browse files Browse the repository at this point in the history
tests: refactor worker handling in sandbox
  • Loading branch information
tonistiigi authored Mar 15, 2024
2 parents c0c3a55 + e2be765 commit 324afe6
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 46 deletions.
8 changes: 4 additions & 4 deletions tests/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
36 changes: 18 additions & 18 deletions tests/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -301,21 +301,21 @@ 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")

// progress=plain
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")
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions tests/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions tests/imagetools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions tests/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions tests/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,27 @@ func dockerCmd(sb integration.Sandbox, opts ...cmdOpt) *exec.Cmd {
return cmd
}

func isMobyWorker(sb integration.Sandbox) bool {
name, hasFeature := driverName(sb.Name())
return name == "docker" && !hasFeature
}

func isDockerWorker(sb integration.Sandbox) bool {
sbDriver, _, _ := strings.Cut(sb.Name(), "+")
return sbDriver == "docker"
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
}
2 changes: 1 addition & 1 deletion tests/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions tests/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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
Expand Down

0 comments on commit 324afe6

Please sign in to comment.