|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// TestCreateTestFaultInjectorLogic_WithoutEnv verifies the auto-start logic |
| 9 | +// when REDIS_ENDPOINTS_CONFIG_PATH is not set |
| 10 | +func TestCreateTestFaultInjectorLogic_WithoutEnv(t *testing.T) { |
| 11 | + // Save original environment |
| 12 | + origConfigPath := os.Getenv("REDIS_ENDPOINTS_CONFIG_PATH") |
| 13 | + origFIURL := os.Getenv("FAULT_INJECTION_API_URL") |
| 14 | + |
| 15 | + // Clear environment to simulate no setup |
| 16 | + os.Unsetenv("REDIS_ENDPOINTS_CONFIG_PATH") |
| 17 | + os.Unsetenv("FAULT_INJECTION_API_URL") |
| 18 | + |
| 19 | + // Restore environment after test |
| 20 | + defer func() { |
| 21 | + if origConfigPath != "" { |
| 22 | + os.Setenv("REDIS_ENDPOINTS_CONFIG_PATH", origConfigPath) |
| 23 | + } |
| 24 | + if origFIURL != "" { |
| 25 | + os.Setenv("FAULT_INJECTION_API_URL", origFIURL) |
| 26 | + } |
| 27 | + }() |
| 28 | + |
| 29 | + // Test GetEnvConfig - should fail when REDIS_ENDPOINTS_CONFIG_PATH is not set |
| 30 | + envConfig, err := GetEnvConfig() |
| 31 | + if err == nil { |
| 32 | + t.Fatal("Expected GetEnvConfig() to fail when REDIS_ENDPOINTS_CONFIG_PATH is not set") |
| 33 | + } |
| 34 | + if envConfig != nil { |
| 35 | + t.Fatal("Expected envConfig to be nil when GetEnvConfig() fails") |
| 36 | + } |
| 37 | + |
| 38 | + t.Log("✅ GetEnvConfig() correctly fails when REDIS_ENDPOINTS_CONFIG_PATH is not set") |
| 39 | + t.Log("✅ This means CreateTestFaultInjectorWithCleanup() will auto-start the proxy") |
| 40 | +} |
| 41 | + |
| 42 | +// TestCreateTestFaultInjectorLogic_WithEnv verifies the logic |
| 43 | +// when REDIS_ENDPOINTS_CONFIG_PATH is set |
| 44 | +func TestCreateTestFaultInjectorLogic_WithEnv(t *testing.T) { |
| 45 | + // Create a temporary config file |
| 46 | + tmpFile := "/tmp/test_endpoints.json" |
| 47 | + content := `{ |
| 48 | + "standalone": { |
| 49 | + "endpoints": ["redis://localhost:6379"] |
| 50 | + } |
| 51 | + }` |
| 52 | + |
| 53 | + if err := os.WriteFile(tmpFile, []byte(content), 0644); err != nil { |
| 54 | + t.Fatalf("Failed to create temp file: %v", err) |
| 55 | + } |
| 56 | + defer os.Remove(tmpFile) |
| 57 | + |
| 58 | + // Save original environment |
| 59 | + origConfigPath := os.Getenv("REDIS_ENDPOINTS_CONFIG_PATH") |
| 60 | + origFIURL := os.Getenv("FAULT_INJECTION_API_URL") |
| 61 | + |
| 62 | + // Set environment |
| 63 | + os.Setenv("REDIS_ENDPOINTS_CONFIG_PATH", tmpFile) |
| 64 | + os.Setenv("FAULT_INJECTION_API_URL", "http://test-fi:9999") |
| 65 | + |
| 66 | + // Restore environment after test |
| 67 | + defer func() { |
| 68 | + if origConfigPath != "" { |
| 69 | + os.Setenv("REDIS_ENDPOINTS_CONFIG_PATH", origConfigPath) |
| 70 | + } else { |
| 71 | + os.Unsetenv("REDIS_ENDPOINTS_CONFIG_PATH") |
| 72 | + } |
| 73 | + if origFIURL != "" { |
| 74 | + os.Setenv("FAULT_INJECTION_API_URL", origFIURL) |
| 75 | + } else { |
| 76 | + os.Unsetenv("FAULT_INJECTION_API_URL") |
| 77 | + } |
| 78 | + }() |
| 79 | + |
| 80 | + // Test GetEnvConfig - should succeed when REDIS_ENDPOINTS_CONFIG_PATH is set |
| 81 | + envConfig, err := GetEnvConfig() |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("Expected GetEnvConfig() to succeed when REDIS_ENDPOINTS_CONFIG_PATH is set, got error: %v", err) |
| 84 | + } |
| 85 | + if envConfig == nil { |
| 86 | + t.Fatal("Expected envConfig to be non-nil when GetEnvConfig() succeeds") |
| 87 | + } |
| 88 | + |
| 89 | + // Verify the fault injector URL is correct |
| 90 | + if envConfig.FaultInjectorURL != "http://test-fi:9999" { |
| 91 | + t.Errorf("Expected FaultInjectorURL to be 'http://test-fi:9999', got '%s'", envConfig.FaultInjectorURL) |
| 92 | + } |
| 93 | + |
| 94 | + t.Log("✅ GetEnvConfig() correctly succeeds when REDIS_ENDPOINTS_CONFIG_PATH is set") |
| 95 | + t.Log("✅ This means CreateTestFaultInjectorWithCleanup() will use the real fault injector") |
| 96 | + t.Logf("✅ Fault injector URL: %s", envConfig.FaultInjectorURL) |
| 97 | +} |
| 98 | + |
| 99 | +// TestCreateTestFaultInjectorLogic_DefaultFIURL verifies the default fault injector URL |
| 100 | +func TestCreateTestFaultInjectorLogic_DefaultFIURL(t *testing.T) { |
| 101 | + // Create a temporary config file |
| 102 | + tmpFile := "/tmp/test_endpoints2.json" |
| 103 | + content := `{ |
| 104 | + "standalone": { |
| 105 | + "endpoints": ["redis://localhost:6379"] |
| 106 | + } |
| 107 | + }` |
| 108 | + |
| 109 | + if err := os.WriteFile(tmpFile, []byte(content), 0644); err != nil { |
| 110 | + t.Fatalf("Failed to create temp file: %v", err) |
| 111 | + } |
| 112 | + defer os.Remove(tmpFile) |
| 113 | + |
| 114 | + // Save original environment |
| 115 | + origConfigPath := os.Getenv("REDIS_ENDPOINTS_CONFIG_PATH") |
| 116 | + origFIURL := os.Getenv("FAULT_INJECTION_API_URL") |
| 117 | + |
| 118 | + // Set only config path, not fault injector URL |
| 119 | + os.Setenv("REDIS_ENDPOINTS_CONFIG_PATH", tmpFile) |
| 120 | + os.Unsetenv("FAULT_INJECTION_API_URL") |
| 121 | + |
| 122 | + // Restore environment after test |
| 123 | + defer func() { |
| 124 | + if origConfigPath != "" { |
| 125 | + os.Setenv("REDIS_ENDPOINTS_CONFIG_PATH", origConfigPath) |
| 126 | + } else { |
| 127 | + os.Unsetenv("REDIS_ENDPOINTS_CONFIG_PATH") |
| 128 | + } |
| 129 | + if origFIURL != "" { |
| 130 | + os.Setenv("FAULT_INJECTION_API_URL", origFIURL) |
| 131 | + } |
| 132 | + }() |
| 133 | + |
| 134 | + // Test GetEnvConfig - should succeed and use default FI URL |
| 135 | + envConfig, err := GetEnvConfig() |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("Expected GetEnvConfig() to succeed, got error: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + // Verify the default fault injector URL |
| 141 | + if envConfig.FaultInjectorURL != "http://localhost:8080" { |
| 142 | + t.Errorf("Expected default FaultInjectorURL to be 'http://localhost:8080', got '%s'", envConfig.FaultInjectorURL) |
| 143 | + } |
| 144 | + |
| 145 | + t.Log("✅ GetEnvConfig() uses default fault injector URL when FAULT_INJECTION_API_URL is not set") |
| 146 | + t.Logf("✅ Default fault injector URL: %s", envConfig.FaultInjectorURL) |
| 147 | +} |
| 148 | + |
| 149 | +// TestFaultInjectorClientCreation verifies that FaultInjectorClient can be created |
| 150 | +func TestFaultInjectorClientCreation(t *testing.T) { |
| 151 | + // Test creating client with different URLs |
| 152 | + testCases := []struct { |
| 153 | + name string |
| 154 | + url string |
| 155 | + }{ |
| 156 | + {"localhost", "http://localhost:5000"}, |
| 157 | + {"with port", "http://test:9999"}, |
| 158 | + {"with trailing slash", "http://test:9999/"}, |
| 159 | + } |
| 160 | + |
| 161 | + for _, tc := range testCases { |
| 162 | + t.Run(tc.name, func(t *testing.T) { |
| 163 | + client := NewFaultInjectorClient(tc.url) |
| 164 | + if client == nil { |
| 165 | + t.Fatal("Expected non-nil client") |
| 166 | + } |
| 167 | + |
| 168 | + // Verify the base URL (should have trailing slash removed) |
| 169 | + expectedURL := tc.url |
| 170 | + if expectedURL[len(expectedURL)-1] == '/' { |
| 171 | + expectedURL = expectedURL[:len(expectedURL)-1] |
| 172 | + } |
| 173 | + |
| 174 | + if client.GetBaseURL() != expectedURL { |
| 175 | + t.Errorf("Expected base URL '%s', got '%s'", expectedURL, client.GetBaseURL()) |
| 176 | + } |
| 177 | + |
| 178 | + t.Logf("✅ Client created successfully with URL: %s", client.GetBaseURL()) |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
| 182 | + |
0 commit comments