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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -68,7 +68,32 @@ type ConfigUpdater interface {
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
@ -152,7 +177,7 @@ func Run(t *testing.T, testCases []Test, opt ...TestOpt) {
for _, br := range list {
for _, tc := range testCases {
for _, mv := range matrix {
fn := getFunctionName(tc)
fn := tc.Name()
name := fn + "/worker=" + br.Name() + mv.functionSuffix()
func(fn, testName string, br Worker, tc Test, mv matrixValue) {
ok := t.Run(testName, func(t *testing.T) {
@ -173,7 +198,7 @@ func Run(t *testing.T, testCases []Test, opt ...TestOpt) {
sb.PrintLogs(t)
}
}()
tc(t, sb)
tc.Run(t, sb)
})
require.True(t, ok)
}(fn, name, br, tc, mv)

View File

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