Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use-test-tables for testing flags #2041

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 47 additions & 51 deletions tests/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){
testBuildDetailsLink,
testBuildProgress,
testBuildAnnotations,
testBuildBuildArgNoKey,
testBuildLabelNoKey,
testBuildCacheExportNotSupported,
testBuildOCIExportNotSupported,
testBuildMultiPlatformNotSupported,
Comment on lines -45 to -49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want test tables I think we should group testBuildBuildArgNoKey, testBuildLabelNoKey and name it testBuildKeyPairNoKey and for testBuildCacheExportNotSupported, testBuildOCIExportNotSupported, testBuildMultiPlatformNotSupported have another one named testBuildDockerNotSupported.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah missed your comment #1998 (comment). So I think we should split them because they don't have the same input and purpose.

testInvalidArgs,
}

func testBuild(t *testing.T, sb integration.Sandbox) {
Expand Down Expand Up @@ -364,54 +360,54 @@ func testBuildAnnotations(t *testing.T, sb integration.Sandbox) {
assert.Equal(t, "zzz", img.Desc.Annotations["example4"])
}

func testBuildBuildArgNoKey(t *testing.T, sb integration.Sandbox) {
func testInvalidArgs(t *testing.T, sb integration.Sandbox) {
dir := createTestProject(t)
cmd := buildxCmd(sb, withArgs("build", "--build-arg", "=TEST_STRING", dir))
out, err := cmd.CombinedOutput()
require.Error(t, err, string(out))
require.Equal(t, strings.TrimSpace(string(out)), `ERROR: invalid key-value pair "=TEST_STRING": empty key`)
}

func testBuildLabelNoKey(t *testing.T, sb integration.Sandbox) {
dir := createTestProject(t)
cmd := buildxCmd(sb, withArgs("build", "--label", "=TEST_STRING", dir))
out, err := cmd.CombinedOutput()
require.Error(t, err, string(out))
require.Equal(t, strings.TrimSpace(string(out)), `ERROR: invalid key-value pair "=TEST_STRING": empty key`)
}

func testBuildCacheExportNotSupported(t *testing.T, sb integration.Sandbox) {
if sb.Name() != "docker" {
t.Skip("skipping test for non-docker workers")
}

dir := createTestProject(t)
cmd := buildxCmd(sb, withArgs("build", "--cache-to=type=registry", dir))
out, err := cmd.CombinedOutput()
require.Error(t, err, string(out))
require.Contains(t, string(out), "Cache export is not supported")
}

func testBuildOCIExportNotSupported(t *testing.T, sb integration.Sandbox) {
if sb.Name() != "docker" {
t.Skip("skipping test for non-docker workers")
tests := []struct {
name string
args []string
expectedErr string
driver string
}{
{
name: "build-arg without key",
args: []string{"--build-arg", "=TEST_STRING", dir},
expectedErr: `ERROR: invalid key-value pair "=TEST_STRING": empty key`,
},
{
name: "label without key",
args: []string{"--label", "=TEST_STRING", dir},
expectedErr: `ERROR: invalid key-value pair "=TEST_STRING": empty key`,
},
{
name: "cache-to not supported",
args: []string{"--cache-to=type=registry", dir},
expectedErr: "Cache export is not supported",
driver: "docker",
},
{
name: "oci not supported",
args: []string{fmt.Sprintf("--output=type=oci,dest=%s/result", dir), dir},
expectedErr: "OCI exporter is not supported",
driver: "docker",
},
{
name: "multiple platforms not supported",
args: []string{"--platform=linux/amd64,linux/arm64", dir},
expectedErr: "Multi-platform build is not supported",
driver: "docker",
},
}

dir := createTestProject(t)
cmd := buildxCmd(sb, withArgs("build", fmt.Sprintf("--output=type=oci,dest=%s/result", dir), dir))
out, err := cmd.CombinedOutput()
require.Error(t, err, string(out))
require.Contains(t, string(out), "OCI exporter is not supported")
}

func testBuildMultiPlatformNotSupported(t *testing.T, sb integration.Sandbox) {
if sb.Name() != "docker" {
t.Skip("skipping test for non-docker workers")
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if tc.driver != "" && sb.Name() != tc.driver {
t.Skipf("skipping test for non-%s workers", tc.driver)
}

out, err := buildxCmd(sb, withCommandAndArgs("build", tc.args...)).CombinedOutput()
require.Error(t, err, string(out))
require.Contains(t, string(out), tc.expectedErr)
})
}

dir := createTestProject(t)
cmd := buildxCmd(sb, withArgs("build", "--platform=linux/amd64,linux/arm64", dir))
out, err := cmd.CombinedOutput()
require.Error(t, err, string(out))
require.Contains(t, string(out), "Multi-platform build is not supported")
}
7 changes: 7 additions & 0 deletions tests/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ func withEnv(env ...string) cmdOpt {
}
}

func withCommandAndArgs(command string, args ...string) cmdOpt {
return func(cmd *exec.Cmd) {
cmd.Args = append(cmd.Args, command)
cmd.Args = append(cmd.Args, args...)
}
}

func withArgs(args ...string) cmdOpt {
return func(cmd *exec.Cmd) {
cmd.Args = append(cmd.Args, args...)
Expand Down