|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "workspace-engine/pkg/oapi" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | +) |
| 11 | + |
| 12 | +func validateRetrievedDeploymentVariables(t *testing.T, actualVars []*oapi.DeploymentVariable, expectedVars []*oapi.DeploymentVariable) { |
| 13 | + t.Helper() |
| 14 | + if len(actualVars) != len(expectedVars) { |
| 15 | + t.Fatalf("expected %d deployment variables, got %d", len(expectedVars), len(actualVars)) |
| 16 | + } |
| 17 | + for _, expected := range expectedVars { |
| 18 | + var actual *oapi.DeploymentVariable |
| 19 | + for _, av := range actualVars { |
| 20 | + if av.Id == expected.Id { |
| 21 | + actual = av |
| 22 | + break |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + if actual == nil { |
| 27 | + t.Fatalf("expected deployment variable with id %s not found", expected.Id) |
| 28 | + } |
| 29 | + if actual.Id != expected.Id { |
| 30 | + t.Fatalf("expected deployment variable id %s, got %s", expected.Id, actual.Id) |
| 31 | + } |
| 32 | + if actual.Key != expected.Key { |
| 33 | + t.Fatalf("expected deployment variable key %s, got %s", expected.Key, actual.Key) |
| 34 | + } |
| 35 | + if actual.DeploymentId != expected.DeploymentId { |
| 36 | + t.Fatalf("expected deployment variable deployment_id %s, got %s", expected.DeploymentId, actual.DeploymentId) |
| 37 | + } |
| 38 | + compareStrPtr(t, actual.Description, expected.Description) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestDBDeploymentVariables_BasicWrite(t *testing.T) { |
| 43 | + workspaceID, conn := setupTestWithWorkspace(t) |
| 44 | + |
| 45 | + tx, err := conn.Begin(t.Context()) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("failed to begin tx: %v", err) |
| 48 | + } |
| 49 | + defer tx.Rollback(t.Context()) |
| 50 | + |
| 51 | + // Create system and deployment |
| 52 | + systemID := uuid.New().String() |
| 53 | + systemDescription := fmt.Sprintf("desc-%s", systemID[:8]) |
| 54 | + sys := &oapi.System{ |
| 55 | + Id: systemID, |
| 56 | + WorkspaceId: workspaceID, |
| 57 | + Name: fmt.Sprintf("test-system-%s", systemID[:8]), |
| 58 | + Description: &systemDescription, |
| 59 | + } |
| 60 | + err = writeSystem(t.Context(), sys, tx) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("failed to create system: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + deploymentID := uuid.New().String() |
| 66 | + deploymentDescription := fmt.Sprintf("deployment-desc-%s", deploymentID[:8]) |
| 67 | + deployment := &oapi.Deployment{ |
| 68 | + Id: deploymentID, |
| 69 | + Name: fmt.Sprintf("test-deployment-%s", deploymentID[:8]), |
| 70 | + Slug: fmt.Sprintf("test-deployment-%s", deploymentID[:8]), |
| 71 | + SystemId: systemID, |
| 72 | + Description: &deploymentDescription, |
| 73 | + JobAgentConfig: map[string]interface{}{}, |
| 74 | + } |
| 75 | + err = writeDeployment(t.Context(), deployment, tx) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("failed to create deployment: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + // Create deployment variable |
| 81 | + varID := uuid.New().String() |
| 82 | + description := "test description" |
| 83 | + variable := &oapi.DeploymentVariable{ |
| 84 | + Id: varID, |
| 85 | + Key: "DATABASE_URL", |
| 86 | + Description: &description, |
| 87 | + DeploymentId: deploymentID, |
| 88 | + } |
| 89 | + |
| 90 | + err = writeDeploymentVariable(t.Context(), variable, tx) |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("expected no errors, got %v", err) |
| 93 | + } |
| 94 | + |
| 95 | + err = tx.Commit(t.Context()) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("failed to commit: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + expectedVars := []*oapi.DeploymentVariable{variable} |
| 101 | + actualVars, err := getDeploymentVariables(t.Context(), workspaceID) |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("expected no errors, got %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + validateRetrievedDeploymentVariables(t, actualVars, expectedVars) |
| 107 | +} |
| 108 | + |
| 109 | +func TestDBDeploymentVariables_BasicWriteAndDelete(t *testing.T) { |
| 110 | + workspaceID, conn := setupTestWithWorkspace(t) |
| 111 | + |
| 112 | + tx, err := conn.Begin(t.Context()) |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("failed to begin tx: %v", err) |
| 115 | + } |
| 116 | + defer tx.Rollback(t.Context()) |
| 117 | + |
| 118 | + // Create system and deployment |
| 119 | + systemID := uuid.New().String() |
| 120 | + systemDescription := fmt.Sprintf("desc-%s", systemID[:8]) |
| 121 | + sys := &oapi.System{ |
| 122 | + Id: systemID, |
| 123 | + WorkspaceId: workspaceID, |
| 124 | + Name: fmt.Sprintf("test-system-%s", systemID[:8]), |
| 125 | + Description: &systemDescription, |
| 126 | + } |
| 127 | + err = writeSystem(t.Context(), sys, tx) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("failed to create system: %v", err) |
| 130 | + } |
| 131 | + |
| 132 | + deploymentID := uuid.New().String() |
| 133 | + deploymentDescription := fmt.Sprintf("deployment-desc-%s", deploymentID[:8]) |
| 134 | + deployment := &oapi.Deployment{ |
| 135 | + Id: deploymentID, |
| 136 | + Name: fmt.Sprintf("test-deployment-%s", deploymentID[:8]), |
| 137 | + Slug: fmt.Sprintf("test-deployment-%s", deploymentID[:8]), |
| 138 | + SystemId: systemID, |
| 139 | + Description: &deploymentDescription, |
| 140 | + JobAgentConfig: map[string]interface{}{}, |
| 141 | + } |
| 142 | + err = writeDeployment(t.Context(), deployment, tx) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("failed to create deployment: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + // Create deployment variable |
| 148 | + varID := uuid.New().String() |
| 149 | + varDescription := "test description" |
| 150 | + variable := &oapi.DeploymentVariable{ |
| 151 | + Id: varID, |
| 152 | + Key: "API_KEY", |
| 153 | + Description: &varDescription, |
| 154 | + DeploymentId: deploymentID, |
| 155 | + } |
| 156 | + |
| 157 | + err = writeDeploymentVariable(t.Context(), variable, tx) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("expected no errors, got %v", err) |
| 160 | + } |
| 161 | + |
| 162 | + err = tx.Commit(t.Context()) |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("failed to commit: %v", err) |
| 165 | + } |
| 166 | + |
| 167 | + // Verify variable exists |
| 168 | + actualVars, err := getDeploymentVariables(t.Context(), workspaceID) |
| 169 | + if err != nil { |
| 170 | + t.Fatalf("expected no errors, got %v", err) |
| 171 | + } |
| 172 | + validateRetrievedDeploymentVariables(t, actualVars, []*oapi.DeploymentVariable{variable}) |
| 173 | + |
| 174 | + // Delete variable |
| 175 | + tx, err = conn.Begin(t.Context()) |
| 176 | + if err != nil { |
| 177 | + t.Fatalf("failed to begin tx: %v", err) |
| 178 | + } |
| 179 | + defer tx.Rollback(t.Context()) |
| 180 | + |
| 181 | + err = deleteDeploymentVariable(t.Context(), varID, tx) |
| 182 | + if err != nil { |
| 183 | + t.Fatalf("expected no errors, got %v", err) |
| 184 | + } |
| 185 | + |
| 186 | + err = tx.Commit(t.Context()) |
| 187 | + if err != nil { |
| 188 | + t.Fatalf("failed to commit: %v", err) |
| 189 | + } |
| 190 | + |
| 191 | + // Verify variable is deleted |
| 192 | + actualVars, err = getDeploymentVariables(t.Context(), workspaceID) |
| 193 | + if err != nil { |
| 194 | + t.Fatalf("expected no errors, got %v", err) |
| 195 | + } |
| 196 | + validateRetrievedDeploymentVariables(t, actualVars, []*oapi.DeploymentVariable{}) |
| 197 | +} |
| 198 | + |
| 199 | +func TestDBDeploymentVariables_BasicWriteAndUpdate(t *testing.T) { |
| 200 | + workspaceID, conn := setupTestWithWorkspace(t) |
| 201 | + |
| 202 | + tx, err := conn.Begin(t.Context()) |
| 203 | + if err != nil { |
| 204 | + t.Fatalf("failed to begin tx: %v", err) |
| 205 | + } |
| 206 | + defer tx.Rollback(t.Context()) |
| 207 | + |
| 208 | + // Create system and deployment |
| 209 | + systemID := uuid.New().String() |
| 210 | + systemDescription := fmt.Sprintf("desc-%s", systemID[:8]) |
| 211 | + sys := &oapi.System{ |
| 212 | + Id: systemID, |
| 213 | + WorkspaceId: workspaceID, |
| 214 | + Name: fmt.Sprintf("test-system-%s", systemID[:8]), |
| 215 | + Description: &systemDescription, |
| 216 | + } |
| 217 | + err = writeSystem(t.Context(), sys, tx) |
| 218 | + if err != nil { |
| 219 | + t.Fatalf("failed to create system: %v", err) |
| 220 | + } |
| 221 | + |
| 222 | + deploymentID := uuid.New().String() |
| 223 | + deploymentDescription := fmt.Sprintf("deployment-desc-%s", deploymentID[:8]) |
| 224 | + deployment := &oapi.Deployment{ |
| 225 | + Id: deploymentID, |
| 226 | + Name: fmt.Sprintf("test-deployment-%s", deploymentID[:8]), |
| 227 | + Slug: fmt.Sprintf("test-deployment-%s", deploymentID[:8]), |
| 228 | + SystemId: systemID, |
| 229 | + Description: &deploymentDescription, |
| 230 | + JobAgentConfig: map[string]interface{}{}, |
| 231 | + } |
| 232 | + err = writeDeployment(t.Context(), deployment, tx) |
| 233 | + if err != nil { |
| 234 | + t.Fatalf("failed to create deployment: %v", err) |
| 235 | + } |
| 236 | + |
| 237 | + // Create deployment variable |
| 238 | + varID := uuid.New().String() |
| 239 | + description := "initial description" |
| 240 | + variable := &oapi.DeploymentVariable{ |
| 241 | + Id: varID, |
| 242 | + Key: "CONFIG_VAR", |
| 243 | + Description: &description, |
| 244 | + DeploymentId: deploymentID, |
| 245 | + } |
| 246 | + |
| 247 | + err = writeDeploymentVariable(t.Context(), variable, tx) |
| 248 | + if err != nil { |
| 249 | + t.Fatalf("expected no errors, got %v", err) |
| 250 | + } |
| 251 | + |
| 252 | + err = tx.Commit(t.Context()) |
| 253 | + if err != nil { |
| 254 | + t.Fatalf("failed to commit: %v", err) |
| 255 | + } |
| 256 | + |
| 257 | + // Update variable |
| 258 | + tx, err = conn.Begin(t.Context()) |
| 259 | + if err != nil { |
| 260 | + t.Fatalf("failed to begin tx: %v", err) |
| 261 | + } |
| 262 | + defer tx.Rollback(t.Context()) |
| 263 | + |
| 264 | + updatedDescription := "updated description" |
| 265 | + variable.Key = "UPDATED_CONFIG_VAR" |
| 266 | + variable.Description = &updatedDescription |
| 267 | + |
| 268 | + err = writeDeploymentVariable(t.Context(), variable, tx) |
| 269 | + if err != nil { |
| 270 | + t.Fatalf("expected no errors, got %v", err) |
| 271 | + } |
| 272 | + |
| 273 | + err = tx.Commit(t.Context()) |
| 274 | + if err != nil { |
| 275 | + t.Fatalf("failed to commit: %v", err) |
| 276 | + } |
| 277 | + |
| 278 | + // Verify update |
| 279 | + actualVars, err := getDeploymentVariables(t.Context(), workspaceID) |
| 280 | + if err != nil { |
| 281 | + t.Fatalf("expected no errors, got %v", err) |
| 282 | + } |
| 283 | + validateRetrievedDeploymentVariables(t, actualVars, []*oapi.DeploymentVariable{variable}) |
| 284 | +} |
| 285 | + |
| 286 | +func TestDBDeploymentVariables_NonexistentDeploymentThrowsError(t *testing.T) { |
| 287 | + workspaceID, conn := setupTestWithWorkspace(t) |
| 288 | + |
| 289 | + tx, err := conn.Begin(t.Context()) |
| 290 | + if err != nil { |
| 291 | + t.Fatalf("failed to begin tx: %v", err) |
| 292 | + } |
| 293 | + defer tx.Rollback(t.Context()) |
| 294 | + |
| 295 | + varDescription := "test" |
| 296 | + variable := &oapi.DeploymentVariable{ |
| 297 | + Id: uuid.New().String(), |
| 298 | + Key: "VAR", |
| 299 | + Description: &varDescription, |
| 300 | + DeploymentId: uuid.New().String(), // Non-existent deployment |
| 301 | + } |
| 302 | + |
| 303 | + err = writeDeploymentVariable(t.Context(), variable, tx) |
| 304 | + // should throw fk constraint error |
| 305 | + if err == nil { |
| 306 | + t.Fatalf("expected FK violation error, got nil") |
| 307 | + } |
| 308 | + |
| 309 | + // Check for foreign key violation (SQLSTATE 23503) |
| 310 | + if !strings.Contains(err.Error(), "23503") && !strings.Contains(err.Error(), "foreign key") { |
| 311 | + t.Fatalf("expected FK violation error, got: %v", err) |
| 312 | + } |
| 313 | + |
| 314 | + // Keep workspaceID to avoid "declared but not used" error |
| 315 | + _ = workspaceID |
| 316 | +} |
| 317 | + |
| 318 | +func TestDBDeploymentVariables_MultipleVariables(t *testing.T) { |
| 319 | + workspaceID, conn := setupTestWithWorkspace(t) |
| 320 | + |
| 321 | + tx, err := conn.Begin(t.Context()) |
| 322 | + if err != nil { |
| 323 | + t.Fatalf("failed to begin tx: %v", err) |
| 324 | + } |
| 325 | + defer tx.Rollback(t.Context()) |
| 326 | + |
| 327 | + // Create system and deployment |
| 328 | + systemID := uuid.New().String() |
| 329 | + systemDescription := fmt.Sprintf("desc-%s", systemID[:8]) |
| 330 | + sys := &oapi.System{ |
| 331 | + Id: systemID, |
| 332 | + WorkspaceId: workspaceID, |
| 333 | + Name: fmt.Sprintf("test-system-%s", systemID[:8]), |
| 334 | + Description: &systemDescription, |
| 335 | + } |
| 336 | + err = writeSystem(t.Context(), sys, tx) |
| 337 | + if err != nil { |
| 338 | + t.Fatalf("failed to create system: %v", err) |
| 339 | + } |
| 340 | + |
| 341 | + deploymentID := uuid.New().String() |
| 342 | + deploymentDescription := fmt.Sprintf("deployment-desc-%s", deploymentID[:8]) |
| 343 | + deployment := &oapi.Deployment{ |
| 344 | + Id: deploymentID, |
| 345 | + Name: fmt.Sprintf("test-deployment-%s", deploymentID[:8]), |
| 346 | + Slug: fmt.Sprintf("test-deployment-%s", deploymentID[:8]), |
| 347 | + SystemId: systemID, |
| 348 | + Description: &deploymentDescription, |
| 349 | + JobAgentConfig: map[string]interface{}{}, |
| 350 | + } |
| 351 | + err = writeDeployment(t.Context(), deployment, tx) |
| 352 | + if err != nil { |
| 353 | + t.Fatalf("failed to create deployment: %v", err) |
| 354 | + } |
| 355 | + |
| 356 | + // Create multiple variables |
| 357 | + desc1 := "desc1" |
| 358 | + desc2 := "desc2" |
| 359 | + desc3 := "desc3" |
| 360 | + variables := []*oapi.DeploymentVariable{ |
| 361 | + { |
| 362 | + Id: uuid.New().String(), |
| 363 | + Key: "DATABASE_URL", |
| 364 | + Description: &desc1, |
| 365 | + DeploymentId: deploymentID, |
| 366 | + }, |
| 367 | + { |
| 368 | + Id: uuid.New().String(), |
| 369 | + Key: "API_KEY", |
| 370 | + Description: &desc2, |
| 371 | + DeploymentId: deploymentID, |
| 372 | + }, |
| 373 | + { |
| 374 | + Id: uuid.New().String(), |
| 375 | + Key: "SECRET_TOKEN", |
| 376 | + Description: &desc3, |
| 377 | + DeploymentId: deploymentID, |
| 378 | + }, |
| 379 | + } |
| 380 | + |
| 381 | + for _, variable := range variables { |
| 382 | + err = writeDeploymentVariable(t.Context(), variable, tx) |
| 383 | + if err != nil { |
| 384 | + t.Fatalf("expected no errors, got %v", err) |
| 385 | + } |
| 386 | + } |
| 387 | + |
| 388 | + err = tx.Commit(t.Context()) |
| 389 | + if err != nil { |
| 390 | + t.Fatalf("failed to commit: %v", err) |
| 391 | + } |
| 392 | + |
| 393 | + // Verify all variables |
| 394 | + actualVars, err := getDeploymentVariables(t.Context(), workspaceID) |
| 395 | + if err != nil { |
| 396 | + t.Fatalf("expected no errors, got %v", err) |
| 397 | + } |
| 398 | + validateRetrievedDeploymentVariables(t, actualVars, variables) |
| 399 | +} |
0 commit comments