|
| 1 | +package nodeconfigutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + "testing" |
| 10 | + |
| 11 | + aksnodeconfigv1 "github.com/Azure/agentbaker/aks-node-controller/pkg/gen/aksnodeconfig/v1" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // Commit from 2025-11-08 when the first version of Config was released. |
| 17 | + // CAREFUL: Test failure may indicate backward compatibility breakage. |
| 18 | + // This commit should be 6-8 months old to account for VHD release cycles and deployment timelines. |
| 19 | + // Investigate thoroughly before updating. |
| 20 | + backwardCompatCommit = "c4ddd55f7dd72bfb541313fe586f586dfa7bd114" |
| 21 | + |
| 22 | + // Commit when PopulateAllFields was first introduced. |
| 23 | + // CAREFUL: Test failure may indicate forward compatibility breakage. |
| 24 | + // Only active RP releases (potentially multiple) need to be supported. |
| 25 | + // Investigate thoroughly before updating. |
| 26 | + forwardCompatCommit = "878718c80db175859a6d7cf377f7b7d40c438413" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + fetchOnce sync.Once |
| 31 | +) |
| 32 | + |
| 33 | +// TestMain fetches required commits before running tests to avoid git lock conflicts in CI. |
| 34 | +func TestMain(m *testing.M) { |
| 35 | + // Fetch commits needed for compatibility tests (only matters in CI with shallow clones) |
| 36 | + fetchCommitsOnce() |
| 37 | + os.Exit(m.Run()) |
| 38 | +} |
| 39 | + |
| 40 | +// fetchCommitsOnce fetches required commits exactly once to avoid git lock conflicts. |
| 41 | +func fetchCommitsOnce() { |
| 42 | + fetchOnce.Do(func() { |
| 43 | + fetchCommitsForTests(backwardCompatCommit, forwardCompatCommit) |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +// setupWorktree creates a git worktree at the specified commit and returns its path. |
| 48 | +func setupWorktree(t *testing.T, targetCommit string) string { |
| 49 | + worktreePath := t.TempDir() |
| 50 | + cmd := exec.Command("git", "worktree", "add", worktreePath, targetCommit) |
| 51 | + output, err := cmd.CombinedOutput() |
| 52 | + require.NoError(t, err, "Failed to create worktree: %s", output) |
| 53 | + |
| 54 | + t.Cleanup(func() { |
| 55 | + _ = exec.Command("git", "worktree", "remove", "--force", worktreePath).Run() |
| 56 | + }) |
| 57 | + |
| 58 | + // Log commit info |
| 59 | + cmd = exec.Command("git", "log", "-1", "--oneline", targetCommit) |
| 60 | + if commitInfo, err := cmd.CombinedOutput(); err == nil { |
| 61 | + t.Logf("Testing against: %q", strings.TrimSpace(string(commitInfo))) |
| 62 | + } |
| 63 | + |
| 64 | + return worktreePath |
| 65 | +} |
| 66 | + |
| 67 | +// fetchCommitsForTests fetches required commits for compatibility tests in CI shallow clones. |
| 68 | +// This is called once before tests run to avoid git lock conflicts between parallel fetches. |
| 69 | +func fetchCommitsForTests(commits ...string) { |
| 70 | + for _, commit := range commits { |
| 71 | + cmd := exec.Command("git", "fetch", "--depth=1", "origin", commit) |
| 72 | + _ = cmd.Run() // Ignore errors - commit might already exist |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// TestBackwardCompatibility tests that old code can unmarshal configs generated by current code. |
| 77 | +func TestBackwardCompatibility(t *testing.T) { |
| 78 | + t.Logf("Testing backward compatibility with commit: %s", backwardCompatCommit) |
| 79 | + |
| 80 | + // Generate JSON with current code |
| 81 | + cfg := &aksnodeconfigv1.Configuration{} |
| 82 | + PopulateAllFields(cfg) |
| 83 | + |
| 84 | + currentJSON, err := MarshalConfigurationV1(cfg) |
| 85 | + require.NoError(t, err) |
| 86 | + t.Logf("Generated %d bytes of JSON with current code", len(currentJSON)) |
| 87 | + |
| 88 | + // Create git worktree at target commit |
| 89 | + worktreePath := setupWorktree(t, backwardCompatCommit) |
| 90 | + |
| 91 | + // Write JSON to worktree |
| 92 | + pkgPath := filepath.Join(worktreePath, "aks-node-controller", "pkg", "nodeconfigutils") |
| 93 | + jsonPath := filepath.Join(pkgPath, "test_compat.json") |
| 94 | + require.NoError(t, os.WriteFile(jsonPath, currentJSON, 0644)) |
| 95 | + |
| 96 | + // Create test file to unmarshal with old code |
| 97 | + testCode := `package nodeconfigutils |
| 98 | +import ( |
| 99 | + "os" |
| 100 | + "testing" |
| 101 | + "github.com/stretchr/testify/require" |
| 102 | +) |
| 103 | +func TestUnmarshalCompat(t *testing.T) { |
| 104 | + data, err := os.ReadFile("test_compat.json") |
| 105 | + require.NoError(t, err) |
| 106 | + cfg, err := UnmarshalConfigurationV1(data) |
| 107 | + require.NoError(t, err, "Old code should unmarshal current JSON") |
| 108 | + require.NotNil(t, cfg) |
| 109 | + t.Logf("✓ Successfully unmarshaled %d bytes", len(data)) |
| 110 | +} |
| 111 | +` |
| 112 | + testFile := filepath.Join(pkgPath, "compat_test.go") |
| 113 | + require.NoError(t, os.WriteFile(testFile, []byte(testCode), 0644)) |
| 114 | + |
| 115 | + // Run test with old code |
| 116 | + cmd := exec.Command("go", "test", "-v", "-run", "TestUnmarshalCompat", "./pkg/nodeconfigutils") |
| 117 | + cmd.Dir = filepath.Join(worktreePath, "aks-node-controller") |
| 118 | + output, err := cmd.CombinedOutput() |
| 119 | + |
| 120 | + t.Logf("Old code output:\n%s", output) |
| 121 | + require.NoError(t, err, "❌ BACKWARD COMPATIBILITY BROKEN: Old code (%s) cannot unmarshal current JSON", backwardCompatCommit) |
| 122 | +} |
| 123 | + |
| 124 | +// TestForwardCompatibility tests that current code can unmarshal configs generated by old code. |
| 125 | +func TestForwardCompatibility(t *testing.T) { |
| 126 | + t.Logf("Testing forward compatibility with commit: %s", forwardCompatCommit) |
| 127 | + |
| 128 | + // Create git worktree at target commit |
| 129 | + worktreePath := setupWorktree(t, forwardCompatCommit) |
| 130 | + pkgPath := filepath.Join(worktreePath, "aks-node-controller", "pkg", "nodeconfigutils") |
| 131 | + |
| 132 | + // Try to generate JSON with old code using PopulateAllFields |
| 133 | + generateCode := `package nodeconfigutils |
| 134 | +import ( |
| 135 | + "os" |
| 136 | + "testing" |
| 137 | + aksnodeconfigv1 "github.com/Azure/agentbaker/aks-node-controller/pkg/gen/aksnodeconfig/v1" |
| 138 | + "github.com/stretchr/testify/require" |
| 139 | +) |
| 140 | +func TestGenerateCompat(t *testing.T) { |
| 141 | + cfg := &aksnodeconfigv1.Configuration{} |
| 142 | + PopulateAllFields(cfg) |
| 143 | + data, err := MarshalConfigurationV1(cfg) |
| 144 | + require.NoError(t, err) |
| 145 | + require.NoError(t, os.WriteFile("old_config.json", data, 0644)) |
| 146 | + t.Logf("Generated %d bytes", len(data)) |
| 147 | +} |
| 148 | +` |
| 149 | + generateFile := filepath.Join(pkgPath, "generate_compat_test.go") |
| 150 | + require.NoError(t, os.WriteFile(generateFile, []byte(generateCode), 0644)) |
| 151 | + |
| 152 | + // Run the generation test |
| 153 | + cmd := exec.Command("go", "test", "-v", "-run", "TestGenerateCompat", "./pkg/nodeconfigutils") |
| 154 | + cmd.Dir = filepath.Join(worktreePath, "aks-node-controller") |
| 155 | + output, err := cmd.CombinedOutput() |
| 156 | + |
| 157 | + t.Logf("Old code generation output:\n%s", output) |
| 158 | + require.NoError(t, err, "Old code failed to generate JSON") |
| 159 | + |
| 160 | + // Read generated JSON |
| 161 | + jsonPath := filepath.Join(pkgPath, "old_config.json") |
| 162 | + oldJSON, err := os.ReadFile(jsonPath) |
| 163 | + require.NoError(t, err) |
| 164 | + t.Logf("Generated %d bytes of JSON with old code", len(oldJSON)) |
| 165 | + |
| 166 | + // Unmarshal with current code |
| 167 | + cfg, err := UnmarshalConfigurationV1(oldJSON) |
| 168 | + require.NoError(t, err, "❌ FORWARD COMPATIBILITY BROKEN: Current code cannot unmarshal old JSON (%s)", forwardCompatCommit) |
| 169 | + require.NotNil(t, cfg) |
| 170 | +} |
0 commit comments