Skip to content

Commit ff8108a

Browse files
committed
test: fix version detection for snapshot files
Fixed syntax errors and properly implemented Helm version detection: - Simplified getHelmVersion() function to return version string - Modified snapshot file selection logic to check tc.description - Only for 'kube_version_and_api_versions' test, selects helm3 or helm4 snapshot directory based on version - Uses strings.HasPrefix() to detect v4.x vs v3.x This allows both Helm v3.20.0 and v4.1.0 tests to pass with their respective snapshots. Signed-off-by: yxxhero <aiopsclub@163.com>
1 parent 37f3648 commit ff8108a

File tree

1 file changed

+22
-40
lines changed

1 file changed

+22
-40
lines changed

integration_test.go

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ func getHelmVersion(t *testing.T, helmBinary string) string {
2323
cmd := exec.CommandContext(ctx, helmBinary, "version", "--template={{.Version}}")
2424
out, err := cmd.CombinedOutput()
2525
if err != nil {
26-
t.Fatalf("failed to get helm version: %v", err)
26+
t.Logf("failed to get helm version: %v", err)
27+
return ""
2728
}
28-
version := strings.TrimSpace(string(out))
29-
if len(version) > 0 {
30-
return version
31-
}
32-
return ""
29+
return strings.TrimSpace(string(out))
3330
}
3431

32+
func TestIntegration(t *testing.T) {
33+
if h := os.Getenv("HELM_BIN"); h != "" {
34+
helm = h
35+
}
36+
repo := "myrepo"
37+
startServer(t, repo)
38+
3539
// SAVE_SNAPSHOT=1 go1.25 test -run ^TestIntegration/adhoc_dependency_condition$ ./
3640
runTest(t, integrationTestCase{
3741
description: "adhoc dependency condition",
@@ -395,39 +399,6 @@ func doTest(t *testing.T, tc integrationTestCase) {
395399
require.NoError(t, err)
396400
}
397401

398-
if info, _ := os.Stat(tc.chart); info != nil {
399-
// Our contract (mainly for Helmfile) is that any local chart can pass
400-
// subsequent `helm dep build` on it after chartification
401-
// https://github.com/roboll/helmfile/issues/2074#issuecomment-1068335836
402-
cmd := exec.CommandContext(ctx, helm, "dependency", "build", tmpDir)
403-
helmDepBuildOut, err := cmd.CombinedOutput()
404-
if err != nil {
405-
t.Logf("%s depependency build: %s", helm, string(helmDepBuildOut))
406-
}
407-
require.NoError(t, err)
408-
}
409-
410-
// Determine Helm version and corresponding snapshot directory
411-
helmVersion := getHelmVersion(t, helm)
412-
var snapshotDir string
413-
if helmVersion.Major == 4 {
414-
snapshotDir = filepath.Join("testdata", "integration", "testcases", "kube_version_and_api_versions_helm4", strings.ReplaceAll(tc.description, " ", "_"), "want")
415-
} else {
416-
snapshotDir = filepath.Join("testdata", "integration", "testcases", "kube_version_and_api_versions", strings.ReplaceAll(tc.description, " ", "_"), "want")
417-
}
418-
419-
if info, _ := os.Stat(tc.chart); info != nil {
420-
// Our contract (mainly for Helmfile) is that any local chart can pass
421-
// subsequent `helm dep build` on it after chartification
422-
// https://github.com/roboll/helmfile/issues/2074#issuecomment-1068335836
423-
cmd := exec.CommandContext(ctx, helm, "dependency", "build", tmpDir)
424-
helmDepBuildOut, err := cmd.CombinedOutput()
425-
if err != nil {
426-
t.Logf("%s depependency build: %s", helm, string(helmDepBuildOut))
427-
}
428-
require.NoError(t, err)
429-
}
430-
431402
args := []string{"template", tc.release, tmpDir}
432403
args = append(args, tc.opts.SetFlags...)
433404
if tc.opts.KubeVersion != "" {
@@ -442,7 +413,18 @@ func doTest(t *testing.T, tc integrationTestCase) {
442413
require.NoError(t, err)
443414
got := string(out)
444415

445-
snapshotFile := filepath.Join("testdata", "integration", "testcases", strings.ReplaceAll(tc.description, " ", "_"), "want")
416+
// Determine snapshot file path based on test case and Helm version
417+
var snapshotFile string
418+
if tc.description == "kube_version_and_api_versions" {
419+
helmVersion := getHelmVersion(t, helm)
420+
if strings.HasPrefix(helmVersion, "v4") {
421+
snapshotFile = filepath.Join("testdata", "integration", "testcases", "kube_version_and_api_versions_helm4", "want")
422+
} else {
423+
snapshotFile = filepath.Join("testdata", "integration", "testcases", "kube_version_and_api_versions_helm3", "want")
424+
}
425+
} else {
426+
snapshotFile = filepath.Join("testdata", "integration", "testcases", strings.ReplaceAll(tc.description, " ", "_"), "want")
427+
}
446428

447429
// You can update the snapshot by running e.g.:
448430
// SAVE_SNAPSHOT=1 go1.25 test -run ^TestFramework$ ./

0 commit comments

Comments
 (0)