mirror of
				https://github.com/fluxcd/flux2.git
				synced 2025-10-31 16:26:36 +08:00 
			
		
		
		
	add e2e test for check --pre with templating support
Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
This commit is contained in:
		
							
								
								
									
										40
									
								
								cmd/flux/check_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								cmd/flux/check_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,40 @@ | |||||||
|  | // +build e2e | ||||||
|  |  | ||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"strings" | ||||||
|  | 	"testing" | ||||||
|  |  | ||||||
|  | 	"github.com/fluxcd/flux2/internal/utils" | ||||||
|  | 	"k8s.io/apimachinery/pkg/version" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func TestCheckPre(t *testing.T) { | ||||||
|  | 	jsonOutput, err := utils.ExecKubectlCommand(context.TODO(), utils.ModeCapture, rootArgs.kubeconfig, rootArgs.kubecontext, "version", "--output", "json") | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("Error running utils.ExecKubectlCommand: %v", err.Error()) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	var versions map[string]version.Info | ||||||
|  | 	if err := json.Unmarshal([]byte(jsonOutput), &versions); err != nil { | ||||||
|  | 		t.Fatalf("Error unmarshalling: %v", err.Error()) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	clientVersion := strings.TrimPrefix(versions["clientVersion"].GitVersion, "v") | ||||||
|  | 	serverVersion := strings.TrimPrefix(versions["serverVersion"].GitVersion, "v") | ||||||
|  |  | ||||||
|  | 	cmd := cmdTestCase{ | ||||||
|  | 		args:            "check --pre", | ||||||
|  | 		wantError:       false, | ||||||
|  | 		testClusterMode: ExistingClusterMode, | ||||||
|  | 		templateValues: map[string]string{ | ||||||
|  | 			"clientVersion": clientVersion, | ||||||
|  | 			"serverVersion": serverVersion, | ||||||
|  | 		}, | ||||||
|  | 		goldenFile: "testdata/check/check_pre.golden", | ||||||
|  | 	} | ||||||
|  | 	cmd.runTestCmd(t) | ||||||
|  | } | ||||||
| @ -10,6 +10,7 @@ import ( | |||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"testing" | 	"testing" | ||||||
|  | 	"text/template" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/fluxcd/flux2/internal/utils" | 	"github.com/fluxcd/flux2/internal/utils" | ||||||
| @ -169,6 +170,8 @@ type cmdTestCase struct { | |||||||
| 	objectFile string | 	objectFile string | ||||||
| 	// TestClusterMode to bootstrap and testing, default to Fake | 	// TestClusterMode to bootstrap and testing, default to Fake | ||||||
| 	testClusterMode TestClusterMode | 	testClusterMode TestClusterMode | ||||||
|  | 	// TemplateValues enable template preprocessing for the golden file, or golden value | ||||||
|  | 	templateValues map[string]string | ||||||
| } | } | ||||||
|  |  | ||||||
| func (cmd *cmdTestCase) runTestCmd(t *testing.T) { | func (cmd *cmdTestCase) runTestCmd(t *testing.T) { | ||||||
| @ -203,14 +206,28 @@ func (cmd *cmdTestCase) runTestCmd(t *testing.T) { | |||||||
|  |  | ||||||
| 	var expected string | 	var expected string | ||||||
| 	if cmd.goldenValue != "" { | 	if cmd.goldenValue != "" { | ||||||
| 		expected = cmd.goldenValue | 		if cmd.templateValues != nil { | ||||||
|  | 			expected, err = executeGoldenTemplate(cmd.goldenValue, cmd.templateValues) | ||||||
|  | 			if err != nil { | ||||||
|  | 				t.Fatalf("Error executing golden template: %s", err) | ||||||
|  | 			} | ||||||
|  | 		} else { | ||||||
|  | 			expected = cmd.goldenValue | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 	if cmd.goldenFile != "" { | 	if cmd.goldenFile != "" { | ||||||
| 		expectedOutput, err := os.ReadFile(cmd.goldenFile) | 		goldenFileContent, err := os.ReadFile(cmd.goldenFile) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			t.Fatalf("Error reading golden file: '%s'", err) | 			t.Fatalf("Error reading golden file: '%s'", err) | ||||||
| 		} | 		} | ||||||
| 		expected = string(expectedOutput) | 		if cmd.templateValues != nil { | ||||||
|  | 			expected, err = executeGoldenTemplate(string(goldenFileContent), cmd.templateValues) | ||||||
|  | 			if err != nil { | ||||||
|  | 				t.Fatalf("Error executing golden template file '%s': %s", cmd.goldenFile, err) | ||||||
|  | 			} | ||||||
|  | 		} else { | ||||||
|  | 			expected = string(goldenFileContent) | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	diff := cmp.Diff(expected, actual) | 	diff := cmp.Diff(expected, actual) | ||||||
| @ -219,6 +236,15 @@ func (cmd *cmdTestCase) runTestCmd(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func executeGoldenTemplate(goldenValue string, templateValues map[string]string) (string, error) { | ||||||
|  | 	tmpl := template.Must(template.New("golden").Parse(goldenValue)) | ||||||
|  | 	var out bytes.Buffer | ||||||
|  | 	if err := tmpl.Execute(&out, templateValues); err != nil { | ||||||
|  | 		return "", err | ||||||
|  | 	} | ||||||
|  | 	return out.String(), nil | ||||||
|  | } | ||||||
|  |  | ||||||
| // Run the command and return the captured output. | // Run the command and return the captured output. | ||||||
| func executeCommand(cmd string) (string, error) { | func executeCommand(cmd string) (string, error) { | ||||||
| 	args, err := shellwords.Parse(cmd) | 	args, err := shellwords.Parse(cmd) | ||||||
|  | |||||||
							
								
								
									
										4
									
								
								cmd/flux/testdata/check/check_pre.golden
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								cmd/flux/testdata/check/check_pre.golden
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,4 @@ | |||||||
|  | ► checking prerequisites | ||||||
|  | ✔ kubectl {{ .clientVersion }} >=1.18.0-0 | ||||||
|  | ✔ Kubernetes {{ .serverVersion }} >=1.16.0-0 | ||||||
|  | ✔ prerequisites checks passed | ||||||
		Reference in New Issue
	
	Block a user
	 Chanwit Kaewkasi
					Chanwit Kaewkasi