|
1 | 1 | package proxy |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
4 | 8 | "sync/atomic" |
5 | 9 | "testing" |
6 | 10 | "time" |
7 | 11 |
|
8 | 12 | "github.com/splitio/go-split-commons/v9/synchronizer" |
| 13 | + "github.com/splitio/go-toolkit/v5/logging" |
| 14 | + "github.com/splitio/split-synchronizer/v5/splitio/common/snapshot" |
| 15 | + pconf "github.com/splitio/split-synchronizer/v5/splitio/proxy/conf" |
| 16 | + "github.com/splitio/split-synchronizer/v5/splitio/util" |
9 | 17 | ) |
10 | 18 |
|
11 | 19 | type syncManagerMock struct { |
@@ -67,3 +75,179 @@ func TestSyncManagerInitializationRetriesWithSnapshot(t *testing.T) { |
67 | 75 | t.Error("there should be 2 executions") |
68 | 76 | } |
69 | 77 | } |
| 78 | + |
| 79 | +// mockLogger captures warning messages for testing |
| 80 | +type mockLogger struct { |
| 81 | + logging.LoggerInterface |
| 82 | + warnings []string |
| 83 | +} |
| 84 | + |
| 85 | +func (m *mockLogger) Warning(msg ...interface{}) { |
| 86 | + m.warnings = append(m.warnings, fmt.Sprint(msg...)) |
| 87 | +} |
| 88 | + |
| 89 | +func (m *mockLogger) Debug(msg ...interface{}) {} |
| 90 | +func (m *mockLogger) Error(msg ...interface{}) {} |
| 91 | +func (m *mockLogger) Info(msg ...interface{}) {} |
| 92 | + |
| 93 | +func TestSnapshotHashMismatchLogsWarningAndIgnoresSnapshot(t *testing.T) { |
| 94 | + // Create a temporary snapshot file with a specific hash |
| 95 | + tmpDir, err := os.MkdirTemp("", "snapshot-test") |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("failed to create temp dir: %v", err) |
| 98 | + } |
| 99 | + defer os.RemoveAll(tmpDir) |
| 100 | + |
| 101 | + snapshotPath := tmpDir + "/test-snapshot.snap" |
| 102 | + |
| 103 | + // Create a snapshot with hash "12345" |
| 104 | + meta := snapshot.Metadata{ |
| 105 | + Version: 1, |
| 106 | + Storage: 1, |
| 107 | + Hash: "12345", |
| 108 | + } |
| 109 | + snap, err := snapshot.New(meta, []byte("test data")) |
| 110 | + if err != nil { |
| 111 | + t.Fatalf("failed to create snapshot: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + // Encode snapshot to bytes and write to file |
| 115 | + encoded, err := snap.Encode() |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("failed to encode snapshot: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + if err := os.WriteFile(snapshotPath, encoded, 0644); err != nil { |
| 121 | + t.Fatalf("failed to write snapshot file: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + // Create a config with different apikey/version/flagsets that will produce a different hash |
| 125 | + cfg := &pconf.Main{ |
| 126 | + Apikey: "test-apikey", |
| 127 | + FlagSpecVersion: "1.1", |
| 128 | + FlagSetsFilter: []string{"set1", "set2"}, |
| 129 | + Initialization: pconf.Initialization{ |
| 130 | + Snapshot: snapshotPath, |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + // Calculate what the hash would be with this config |
| 135 | + currentHash := util.HashAPIKey(cfg.Apikey + cfg.FlagSpecVersion + strings.Join(cfg.FlagSetsFilter, "::")) |
| 136 | + expectedHashStr := strconv.Itoa(int(currentHash)) |
| 137 | + |
| 138 | + // Verify that the hashes are indeed different |
| 139 | + if meta.Hash == expectedHashStr { |
| 140 | + t.Fatal("test setup error: hashes should be different for this test") |
| 141 | + } |
| 142 | + |
| 143 | + // Create a mock logger to capture warnings |
| 144 | + mockLog := &mockLogger{ |
| 145 | + warnings: make([]string, 0), |
| 146 | + } |
| 147 | + |
| 148 | + // This simulates the code path in Start() that checks the hash |
| 149 | + snap2, err := snapshot.DecodeFromFile(snapshotPath) |
| 150 | + if err != nil { |
| 151 | + t.Fatalf("failed to decode snapshot: %v", err) |
| 152 | + } |
| 153 | + |
| 154 | + // Simulate the new behavior: check hash and ignore if mismatch |
| 155 | + var snapshotValid = false |
| 156 | + currentHash2 := util.HashAPIKey(cfg.Apikey + cfg.FlagSpecVersion + strings.Join(cfg.FlagSetsFilter, "::")) |
| 157 | + if snap2.Meta().Hash != strconv.Itoa(int(currentHash2)) { |
| 158 | + mockLog.Warning("snapshot cfg (apikey, version, flagsets) does not match the provided one. Ignoring snapshot and starting with empty storage.") |
| 159 | + } else { |
| 160 | + snapshotValid = true |
| 161 | + } |
| 162 | + |
| 163 | + // Verify that a warning was logged |
| 164 | + if len(mockLog.warnings) != 1 { |
| 165 | + t.Errorf("expected 1 warning, got %d", len(mockLog.warnings)) |
| 166 | + } |
| 167 | + |
| 168 | + expectedWarning := "snapshot cfg (apikey, version, flagsets) does not match the provided one. Ignoring snapshot and starting with empty storage." |
| 169 | + if len(mockLog.warnings) > 0 && mockLog.warnings[0] != expectedWarning { |
| 170 | + t.Errorf("expected warning '%s', got '%s'", expectedWarning, mockLog.warnings[0]) |
| 171 | + } |
| 172 | + |
| 173 | + // Verify that snapshot is marked as invalid |
| 174 | + if snapshotValid { |
| 175 | + t.Error("snapshot should be marked as invalid when hash mismatches") |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func TestSnapshotHashMatchNoWarningAndUsesSnapshot(t *testing.T) { |
| 180 | + // Create a temporary snapshot file |
| 181 | + tmpDir, err := os.MkdirTemp("", "snapshot-test") |
| 182 | + if err != nil { |
| 183 | + t.Fatalf("failed to create temp dir: %v", err) |
| 184 | + } |
| 185 | + defer os.RemoveAll(tmpDir) |
| 186 | + |
| 187 | + snapshotPath := tmpDir + "/test-snapshot.snap" |
| 188 | + |
| 189 | + // Create config |
| 190 | + cfg := &pconf.Main{ |
| 191 | + Apikey: "test-apikey", |
| 192 | + FlagSpecVersion: "1.1", |
| 193 | + FlagSetsFilter: []string{"set1", "set2"}, |
| 194 | + Initialization: pconf.Initialization{ |
| 195 | + Snapshot: snapshotPath, |
| 196 | + }, |
| 197 | + } |
| 198 | + |
| 199 | + // Calculate the hash with the config |
| 200 | + currentHash := util.HashAPIKey(cfg.Apikey + cfg.FlagSpecVersion + strings.Join(cfg.FlagSetsFilter, "::")) |
| 201 | + hashStr := strconv.Itoa(int(currentHash)) |
| 202 | + |
| 203 | + // Create a snapshot with the SAME hash |
| 204 | + meta := snapshot.Metadata{ |
| 205 | + Version: 1, |
| 206 | + Storage: 1, |
| 207 | + Hash: hashStr, |
| 208 | + } |
| 209 | + snap, err := snapshot.New(meta, []byte("test data")) |
| 210 | + if err != nil { |
| 211 | + t.Fatalf("failed to create snapshot: %v", err) |
| 212 | + } |
| 213 | + |
| 214 | + // Encode snapshot to bytes and write to file |
| 215 | + encoded, err := snap.Encode() |
| 216 | + if err != nil { |
| 217 | + t.Fatalf("failed to encode snapshot: %v", err) |
| 218 | + } |
| 219 | + |
| 220 | + if err := os.WriteFile(snapshotPath, encoded, 0644); err != nil { |
| 221 | + t.Fatalf("failed to write snapshot file: %v", err) |
| 222 | + } |
| 223 | + |
| 224 | + // Create a mock logger to capture warnings |
| 225 | + mockLog := &mockLogger{ |
| 226 | + warnings: make([]string, 0), |
| 227 | + } |
| 228 | + |
| 229 | + // This simulates the code path in Start() that checks the hash |
| 230 | + snap2, err := snapshot.DecodeFromFile(snapshotPath) |
| 231 | + if err != nil { |
| 232 | + t.Fatalf("failed to decode snapshot: %v", err) |
| 233 | + } |
| 234 | + |
| 235 | + // Simulate the new behavior: check hash and use if match |
| 236 | + var snapshotValid = false |
| 237 | + currentHash2 := util.HashAPIKey(cfg.Apikey + cfg.FlagSpecVersion + strings.Join(cfg.FlagSetsFilter, "::")) |
| 238 | + if snap2.Meta().Hash != strconv.Itoa(int(currentHash2)) { |
| 239 | + mockLog.Warning("snapshot cfg (apikey, version, flagsets) does not match the provided one. Ignoring snapshot and starting with empty storage.") |
| 240 | + } else { |
| 241 | + snapshotValid = true |
| 242 | + } |
| 243 | + |
| 244 | + // Verify that NO warning was logged |
| 245 | + if len(mockLog.warnings) != 0 { |
| 246 | + t.Errorf("expected 0 warnings, got %d: %v", len(mockLog.warnings), mockLog.warnings) |
| 247 | + } |
| 248 | + |
| 249 | + // Verify that snapshot is marked as valid |
| 250 | + if !snapshotValid { |
| 251 | + t.Error("snapshot should be marked as valid when hash matches") |
| 252 | + } |
| 253 | +} |
0 commit comments