Use shared envTest for unit tests
Speed up unit tests by using a shared envTest. This requires each test to use its own namespace to avoid clobbering objects for other tests. Tests previously took around 8 seconds each, and now the initial test takes 2 seconds with follow up tests taking less than a second each. Also update existing tests that use a fixed namespace to use a generated namespace. Share gold file template function with yaml files. Remove the testClusterMode, and instead rely on MainTest to do the appropriate test setup and rootArgs flag setup. Move the rootArg flag setup out of NewTestEnvKubeManager to avoid side effects. A follow up change can be to push the individual setups from NewTestEnvKubeManager() into their respective TestMain since the harness share little code. Signed-off-by: Allen Porter <allen@thebends.org>
This commit is contained in:
@@ -8,29 +8,44 @@ import (
|
||||
|
||||
func TestTraceNoArgs(t *testing.T) {
|
||||
cmd := cmdTestCase{
|
||||
args: "trace",
|
||||
testClusterMode: TestEnvClusterMode,
|
||||
assert: assertError("object name is required"),
|
||||
args: "trace",
|
||||
assert: assertError("object name is required"),
|
||||
}
|
||||
cmd.runTestCmd(t)
|
||||
}
|
||||
|
||||
func TestTraceDeployment(t *testing.T) {
|
||||
cmd := cmdTestCase{
|
||||
args: "trace podinfo -n podinfo --kind deployment --api-version=apps/v1",
|
||||
testClusterMode: TestEnvClusterMode,
|
||||
assert: assertGoldenFile("testdata/trace/deployment.golden"),
|
||||
objectFile: "testdata/trace/deployment.yaml",
|
||||
func TestTrace(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
args string
|
||||
objectFile string
|
||||
goldenFile string
|
||||
}{
|
||||
{
|
||||
"Deployment",
|
||||
"trace podinfo --kind deployment --api-version=apps/v1",
|
||||
"testdata/trace/deployment.yaml",
|
||||
"testdata/trace/deployment.golden",
|
||||
},
|
||||
{
|
||||
"HelmRelease",
|
||||
"trace podinfo --kind HelmRelease --api-version=helm.toolkit.fluxcd.io/v2beta1",
|
||||
"testdata/trace/helmrelease.yaml",
|
||||
"testdata/trace/helmrelease.golden",
|
||||
},
|
||||
}
|
||||
cmd.runTestCmd(t)
|
||||
}
|
||||
|
||||
func TestTraceHelmRelease(t *testing.T) {
|
||||
cmd := cmdTestCase{
|
||||
args: "trace podinfo -n podinfo --kind HelmRelease --api-version=helm.toolkit.fluxcd.io/v2beta1",
|
||||
testClusterMode: TestEnvClusterMode,
|
||||
assert: assertGoldenFile("testdata/trace/helmrelease.golden"),
|
||||
objectFile: "testdata/trace/helmrelease.yaml",
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
tmpl := map[string]string{
|
||||
"ns": allocateNamespace("podinfo"),
|
||||
"fluxns": allocateNamespace("flux-system"),
|
||||
}
|
||||
testEnv.CreateObjectFile(tc.objectFile, tmpl, t)
|
||||
cmd := cmdTestCase{
|
||||
args: tc.args + " -n=" + tmpl["ns"],
|
||||
assert: assertGoldenTemplateFile(tc.goldenFile, tmpl),
|
||||
}
|
||||
cmd.runTestCmd(t)
|
||||
})
|
||||
}
|
||||
cmd.runTestCmd(t)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user