Change integration.Test from a func to a interface

Using an interface instead of a func is more flexible while achieving
the same effect. It allows you to succintly define a large number of
test cases as structs, as is common in table-driven testing.

A helper func is added that converts the existing test funcs into the
interface, so the change is fairly seamless.

Signed-off-by: Erik Sipsma <erik@sipsma.dev>
master
Erik Sipsma 2021-11-17 12:00:17 -08:00
parent 0539181757
commit 2bfad4b0dc
14 changed files with 65 additions and 40 deletions

View File

@ -33,7 +33,7 @@ import (
) )
func TestClientGatewayIntegration(t *testing.T) { func TestClientGatewayIntegration(t *testing.T) {
integration.Run(t, []integration.Test{ integration.Run(t, integration.TestFuncs(
testClientGatewaySolve, testClientGatewaySolve,
testClientGatewayFailedSolve, testClientGatewayFailedSolve,
testClientGatewayEmptySolve, testClientGatewayEmptySolve,
@ -53,20 +53,20 @@ func TestClientGatewayIntegration(t *testing.T) {
testClientGatewayExecFileActionError, testClientGatewayExecFileActionError,
testClientGatewayContainerExtraHosts, testClientGatewayContainerExtraHosts,
testWarnings, testWarnings,
}, integration.WithMirroredImages(integration.OfficialImages("busybox:latest"))) ), integration.WithMirroredImages(integration.OfficialImages("busybox:latest")))
integration.Run(t, []integration.Test{ integration.Run(t, integration.TestFuncs(
testClientGatewayContainerSecurityMode, testClientGatewayContainerSecurityMode,
}, integration.WithMirroredImages(integration.OfficialImages("busybox:latest")), ), integration.WithMirroredImages(integration.OfficialImages("busybox:latest")),
integration.WithMatrix("secmode", map[string]interface{}{ integration.WithMatrix("secmode", map[string]interface{}{
"sandbox": securitySandbox, "sandbox": securitySandbox,
"insecure": securityInsecure, "insecure": securityInsecure,
}), }),
) )
integration.Run(t, []integration.Test{ integration.Run(t, integration.TestFuncs(
testClientGatewayContainerHostNetworking, testClientGatewayContainerHostNetworking,
}, ),
integration.WithMirroredImages(integration.OfficialImages("busybox:latest")), integration.WithMirroredImages(integration.OfficialImages("busybox:latest")),
integration.WithMatrix("netmode", map[string]interface{}{ integration.WithMatrix("netmode", map[string]interface{}{
"default": defaultNetwork, "default": defaultNetwork,

View File

@ -72,7 +72,7 @@ func TestIntegration(t *testing.T) {
mirroredImages["tonistiigi/test:nolayers"] = "docker.io/tonistiigi/test:nolayers" mirroredImages["tonistiigi/test:nolayers"] = "docker.io/tonistiigi/test:nolayers"
mirrors := integration.WithMirroredImages(mirroredImages) mirrors := integration.WithMirroredImages(mirroredImages)
integration.Run(t, []integration.Test{ integration.Run(t, integration.TestFuncs(
testCacheExportCacheKeyLoop, testCacheExportCacheKeyLoop,
testRelativeWorkDir, testRelativeWorkDir,
testFileOpMkdirMkfile, testFileOpMkdirMkfile,
@ -140,13 +140,13 @@ func TestIntegration(t *testing.T) {
testMergeOp, testMergeOp,
testMergeOpCache, testMergeOpCache,
testRmSymlink, testRmSymlink,
}, mirrors) ), mirrors)
integration.Run(t, []integration.Test{ integration.Run(t, integration.TestFuncs(
testSecurityMode, testSecurityMode,
testSecurityModeSysfs, testSecurityModeSysfs,
testSecurityModeErrors, testSecurityModeErrors,
}, ),
mirrors, mirrors,
integration.WithMatrix("secmode", map[string]interface{}{ integration.WithMatrix("secmode", map[string]interface{}{
"sandbox": securitySandbox, "sandbox": securitySandbox,
@ -154,9 +154,9 @@ func TestIntegration(t *testing.T) {
}), }),
) )
integration.Run(t, []integration.Test{ integration.Run(t, integration.TestFuncs(
testHostNetworking, testHostNetworking,
}, ),
mirrors, mirrors,
integration.WithMatrix("netmode", map[string]interface{}{ integration.WithMatrix("netmode", map[string]interface{}{
"default": defaultNetwork, "default": defaultNetwork,

View File

@ -13,7 +13,7 @@ func init() {
} }
func TestCLIIntegration(t *testing.T) { func TestCLIIntegration(t *testing.T) {
integration.Run(t, []integration.Test{ integration.Run(t, integration.TestFuncs(
testDiskUsage, testDiskUsage,
testBuildWithLocalFiles, testBuildWithLocalFiles,
testBuildLocalExporter, testBuildLocalExporter,
@ -21,7 +21,7 @@ func TestCLIIntegration(t *testing.T) {
testBuildMetadataFile, testBuildMetadataFile,
testPrune, testPrune,
testUsage, testUsage,
}, ),
integration.WithMirroredImages(integration.OfficialImages("busybox:latest")), integration.WithMirroredImages(integration.OfficialImages("busybox:latest")),
) )
} }

View File

@ -18,7 +18,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var hdTests = []integration.Test{ var hdTests = integration.TestFuncs(
testCopyHeredoc, testCopyHeredoc,
testRunBasicHeredoc, testRunBasicHeredoc,
testRunFakeHeredoc, testRunFakeHeredoc,
@ -27,7 +27,7 @@ var hdTests = []integration.Test{
testHeredocIndent, testHeredocIndent,
testHeredocVarSubstitution, testHeredocVarSubstitution,
testOnBuildHeredoc, testOnBuildHeredoc,
} )
func init() { func init() {
heredocTests = append(heredocTests, hdTests...) heredocTests = append(heredocTests, hdTests...)

View File

@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var mountTests = []integration.Test{ var mountTests = integration.TestFuncs(
testMountContext, testMountContext,
testMountTmpfs, testMountTmpfs,
testMountRWCache, testMountRWCache,
@ -26,12 +26,12 @@ var mountTests = []integration.Test{
testMountFromError, testMountFromError,
testMountInvalid, testMountInvalid,
testMountTmpfsSize, testMountTmpfsSize,
} )
func init() { func init() {
allTests = append(allTests, mountTests...) allTests = append(allTests, mountTests...)
fileOpTests = append(fileOpTests, testCacheMountUser) fileOpTests = append(fileOpTests, integration.TestFuncs(testCacheMountUser)...)
} }
func testMountContext(t *testing.T, sb integration.Sandbox) { func testMountContext(t *testing.T, sb integration.Sandbox) {

View File

@ -15,12 +15,12 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var runNetworkTests = []integration.Test{ var runNetworkTests = integration.TestFuncs(
testRunDefaultNetwork, testRunDefaultNetwork,
testRunNoNetwork, testRunNoNetwork,
testRunHostNetwork, testRunHostNetwork,
testRunGlobalNetwork, testRunGlobalNetwork,
} )
func init() { func init() {
networkTests = append(networkTests, runNetworkTests...) networkTests = append(networkTests, runNetworkTests...)

View File

@ -15,12 +15,12 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var runSecurityTests = []integration.Test{ var runSecurityTests = integration.TestFuncs(
testRunSecurityInsecure, testRunSecurityInsecure,
testRunSecuritySandbox, testRunSecuritySandbox,
testRunSecurityDefault, testRunSecurityDefault,
testInsecureDevicesWhitelist, testInsecureDevicesWhitelist,
} )
func init() { func init() {
securityOpts = []integration.TestOpt{ securityOpts = []integration.TestOpt{

View File

@ -13,10 +13,10 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var secretsTests = []integration.Test{ var secretsTests = integration.TestFuncs(
testSecretFileParams, testSecretFileParams,
testSecretRequiredWithoutValue, testSecretRequiredWithoutValue,
} )
func init() { func init() {
allTests = append(allTests, secretsTests...) allTests = append(allTests, secretsTests...)

View File

@ -19,9 +19,9 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var sshTests = []integration.Test{ var sshTests = integration.TestFuncs(
testSSHSocketParams, testSSHSocketParams,
} )
func init() { func init() {
allTests = append(allTests, sshTests...) allTests = append(allTests, sshTests...)

View File

@ -58,7 +58,7 @@ func init() {
} }
} }
var allTests = []integration.Test{ var allTests = integration.TestFuncs(
testCmdShell, testCmdShell,
testGlobalArg, testGlobalArg,
testDockerfileDirs, testDockerfileDirs,
@ -118,9 +118,9 @@ var allTests = []integration.Test{
testShmSize, testShmSize,
testUlimit, testUlimit,
testCgroupParent, testCgroupParent,
} )
var fileOpTests = []integration.Test{ var fileOpTests = integration.TestFuncs(
testEmptyDestDir, testEmptyDestDir,
testCopyChownCreateDest, testCopyChownCreateDest,
testCopyThroughSymlinkContext, testCopyThroughSymlinkContext,
@ -143,7 +143,7 @@ var fileOpTests = []integration.Test{
testWorkdirCopyIgnoreRelative, testWorkdirCopyIgnoreRelative,
testCopyFollowAllSymlinks, testCopyFollowAllSymlinks,
testDockerfileAddChownExpand, testDockerfileAddChownExpand,
} )
// Tests that depend on the `security.*` entitlements // Tests that depend on the `security.*` entitlements
var securityTests = []integration.Test{} var securityTests = []integration.Test{}

View File

@ -28,12 +28,12 @@ func init() {
} }
func TestFrontendIntegration(t *testing.T) { func TestFrontendIntegration(t *testing.T) {
integration.Run(t, []integration.Test{ integration.Run(t, integration.TestFuncs(
testRefReadFile, testRefReadFile,
testRefReadDir, testRefReadDir,
testRefStatFile, testRefStatFile,
testReturnNil, testReturnNil,
}) ))
} }
func testReturnNil(t *testing.T, sb integration.Sandbox) { func testReturnNil(t *testing.T, sb integration.Sandbox) {

View File

@ -20,9 +20,9 @@ func init() {
func TestJobsIntegration(t *testing.T) { func TestJobsIntegration(t *testing.T) {
mirrors := integration.WithMirroredImages(integration.OfficialImages("busybox:latest")) mirrors := integration.WithMirroredImages(integration.OfficialImages("busybox:latest"))
integration.Run(t, []integration.Test{ integration.Run(t, integration.TestFuncs(
testParallelism, testParallelism,
}, ),
mirrors, mirrors,
integration.WithMatrix("max-parallelism", map[string]interface{}{ integration.WithMatrix("max-parallelism", map[string]interface{}{
"single": maxParallelismSingle, "single": maxParallelismSingle,

View File

@ -68,7 +68,32 @@ type ConfigUpdater interface {
UpdateConfigFile(string) string UpdateConfigFile(string) string
} }
type Test func(*testing.T, Sandbox) type Test interface {
Name() string
Run(t *testing.T, sb Sandbox)
}
type testFunc struct {
name string
run func(t *testing.T, sb Sandbox)
}
func (f testFunc) Name() string {
return f.name
}
func (f testFunc) Run(t *testing.T, sb Sandbox) {
t.Helper()
f.run(t, sb)
}
func TestFuncs(funcs ...func(t *testing.T, sb Sandbox)) []Test {
var tests []Test
for _, f := range funcs {
tests = append(tests, testFunc{name: getFunctionName(f), run: f})
}
return tests
}
var defaultWorkers []Worker var defaultWorkers []Worker
@ -152,7 +177,7 @@ func Run(t *testing.T, testCases []Test, opt ...TestOpt) {
for _, br := range list { for _, br := range list {
for _, tc := range testCases { for _, tc := range testCases {
for _, mv := range matrix { for _, mv := range matrix {
fn := getFunctionName(tc) fn := tc.Name()
name := fn + "/worker=" + br.Name() + mv.functionSuffix() name := fn + "/worker=" + br.Name() + mv.functionSuffix()
func(fn, testName string, br Worker, tc Test, mv matrixValue) { func(fn, testName string, br Worker, tc Test, mv matrixValue) {
ok := t.Run(testName, func(t *testing.T) { ok := t.Run(testName, func(t *testing.T) {
@ -173,7 +198,7 @@ func Run(t *testing.T, testCases []Test, opt ...TestOpt) {
sb.PrintLogs(t) sb.PrintLogs(t)
} }
}() }()
tc(t, sb) tc.Run(t, sb)
}) })
require.True(t, ok) require.True(t, ok)
}(fn, name, br, tc, mv) }(fn, name, br, tc, mv)

View File

@ -22,10 +22,10 @@ func init() {
func TestContainerdWorkerIntegration(t *testing.T) { func TestContainerdWorkerIntegration(t *testing.T) {
checkRequirement(t) checkRequirement(t)
integration.Run(t, []integration.Test{ integration.Run(t, integration.TestFuncs(
testContainerdWorkerExec, testContainerdWorkerExec,
testContainerdWorkerExecFailures, testContainerdWorkerExecFailures,
}) ))
} }
func newWorkerOpt(t *testing.T, addr string) (base.WorkerOpt, func()) { func newWorkerOpt(t *testing.T, addr string) (base.WorkerOpt, func()) {