mirror of
				https://github.com/fluxcd/flux2.git
				synced 2025-11-01 01:25:53 +08:00 
			
		
		
		
	 d45501a129
			
		
	
	d45501a129
	
	
	
		
			
			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>
		
			
				
	
	
		
			52 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // +build unit
 | |
| 
 | |
| package main
 | |
| 
 | |
| import (
 | |
| 	"testing"
 | |
| )
 | |
| 
 | |
| func TestTraceNoArgs(t *testing.T) {
 | |
| 	cmd := cmdTestCase{
 | |
| 		args:   "trace",
 | |
| 		assert: assertError("object name is required"),
 | |
| 	}
 | |
| 	cmd.runTestCmd(t)
 | |
| }
 | |
| 
 | |
| 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",
 | |
| 		},
 | |
| 	}
 | |
| 	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)
 | |
| 		})
 | |
| 	}
 | |
| }
 |