|
9 | 9 | "github.com/stretchr/testify/assert" |
10 | 10 | cnstypes "github.com/vmware/govmomi/cns/types" |
11 | 11 | vim25types "github.com/vmware/govmomi/vim25/types" |
| 12 | + "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger" |
12 | 13 | ) |
13 | 14 |
|
14 | 15 | const createVolumeTaskTimeout = 3 * time.Second |
@@ -284,3 +285,248 @@ func TestAttachVolumeFaultHandling(t *testing.T) { |
284 | 285 | }) |
285 | 286 | } |
286 | 287 | } |
| 288 | + |
| 289 | +// TestIsCnsNotRegisteredFault tests the IsCnsNotRegisteredFault helper function |
| 290 | +// that detects CnsNotRegisteredFault from CNS operations. |
| 291 | +func TestIsCnsNotRegisteredFault(t *testing.T) { |
| 292 | + ctx := logger.NewContextWithLogger(context.Background()) |
| 293 | + |
| 294 | + tests := []struct { |
| 295 | + name string |
| 296 | + fault *vim25types.LocalizedMethodFault |
| 297 | + expected bool |
| 298 | + }{ |
| 299 | + { |
| 300 | + name: "nil fault", |
| 301 | + fault: nil, |
| 302 | + expected: false, |
| 303 | + }, |
| 304 | + { |
| 305 | + name: "fault with nil Fault field", |
| 306 | + fault: &vim25types.LocalizedMethodFault{ |
| 307 | + LocalizedMessage: "Some error", |
| 308 | + Fault: nil, |
| 309 | + }, |
| 310 | + expected: false, |
| 311 | + }, |
| 312 | + { |
| 313 | + name: "CnsNotRegisteredFault", |
| 314 | + fault: &vim25types.LocalizedMethodFault{ |
| 315 | + LocalizedMessage: "The input volume is not registered as a CNS volume", |
| 316 | + Fault: &cnstypes.CnsNotRegisteredFault{}, |
| 317 | + }, |
| 318 | + expected: true, |
| 319 | + }, |
| 320 | + { |
| 321 | + name: "CnsFault (not CnsNotRegisteredFault)", |
| 322 | + fault: &vim25types.LocalizedMethodFault{ |
| 323 | + LocalizedMessage: "Generic CNS fault", |
| 324 | + Fault: &cnstypes.CnsFault{ |
| 325 | + Reason: "Generic error", |
| 326 | + }, |
| 327 | + }, |
| 328 | + expected: false, |
| 329 | + }, |
| 330 | + { |
| 331 | + name: "ResourceInUse fault", |
| 332 | + fault: &vim25types.LocalizedMethodFault{ |
| 333 | + LocalizedMessage: "Resource is in use", |
| 334 | + Fault: &vim25types.ResourceInUse{ |
| 335 | + Type: "Disk", |
| 336 | + Name: "test-disk", |
| 337 | + }, |
| 338 | + }, |
| 339 | + expected: false, |
| 340 | + }, |
| 341 | + { |
| 342 | + name: "NotSupported fault", |
| 343 | + fault: &vim25types.LocalizedMethodFault{ |
| 344 | + LocalizedMessage: "Operation not supported", |
| 345 | + Fault: &vim25types.NotSupported{}, |
| 346 | + }, |
| 347 | + expected: false, |
| 348 | + }, |
| 349 | + } |
| 350 | + |
| 351 | + for _, tt := range tests { |
| 352 | + t.Run(tt.name, func(t *testing.T) { |
| 353 | + result := IsCnsNotRegisteredFault(ctx, tt.fault) |
| 354 | + assert.Equal(t, tt.expected, result, |
| 355 | + "IsCnsNotRegisteredFault(%v) = %v, expected %v", tt.fault, result, tt.expected) |
| 356 | + }) |
| 357 | + } |
| 358 | +} |
| 359 | + |
| 360 | +// TestIsCnsVolumeAlreadyExistsFault tests the IsCnsVolumeAlreadyExistsFault helper function |
| 361 | +// that checks if a fault type indicates the volume already exists in CNS. |
| 362 | +func TestIsCnsVolumeAlreadyExistsFault(t *testing.T) { |
| 363 | + ctx := logger.NewContextWithLogger(context.Background()) |
| 364 | + |
| 365 | + tests := []struct { |
| 366 | + name string |
| 367 | + faultType string |
| 368 | + expected bool |
| 369 | + }{ |
| 370 | + { |
| 371 | + name: "CnsVolumeAlreadyExistsFault", |
| 372 | + faultType: "vim.fault.CnsVolumeAlreadyExistsFault", |
| 373 | + expected: true, |
| 374 | + }, |
| 375 | + { |
| 376 | + name: "empty fault type", |
| 377 | + faultType: "", |
| 378 | + expected: false, |
| 379 | + }, |
| 380 | + { |
| 381 | + name: "CnsFault", |
| 382 | + faultType: "vim.fault.CnsFault", |
| 383 | + expected: false, |
| 384 | + }, |
| 385 | + { |
| 386 | + name: "NotSupported", |
| 387 | + faultType: "vim25:NotSupported", |
| 388 | + expected: false, |
| 389 | + }, |
| 390 | + { |
| 391 | + name: "partial match - should not match", |
| 392 | + faultType: "CnsVolumeAlreadyExistsFault", |
| 393 | + expected: false, |
| 394 | + }, |
| 395 | + { |
| 396 | + name: "case sensitive - lowercase should not match", |
| 397 | + faultType: "vim.fault.cnsvolumealreadyexistsfault", |
| 398 | + expected: false, |
| 399 | + }, |
| 400 | + } |
| 401 | + |
| 402 | + for _, tt := range tests { |
| 403 | + t.Run(tt.name, func(t *testing.T) { |
| 404 | + result := IsCnsVolumeAlreadyExistsFault(ctx, tt.faultType) |
| 405 | + assert.Equal(t, tt.expected, result, |
| 406 | + "IsCnsVolumeAlreadyExistsFault(%q) = %v, expected %v", tt.faultType, result, tt.expected) |
| 407 | + }) |
| 408 | + } |
| 409 | +} |
| 410 | + |
| 411 | +// TestCnsNotRegisteredFaultHandlingScenarios tests the scenarios where |
| 412 | +// CnsNotRegisteredFault should trigger re-registration of the volume. |
| 413 | +func TestCnsNotRegisteredFaultHandlingScenarios(t *testing.T) { |
| 414 | + ctx := logger.NewContextWithLogger(context.Background()) |
| 415 | + |
| 416 | + tests := []struct { |
| 417 | + name string |
| 418 | + volumeOperationResult *cnstypes.CnsVolumeOperationResult |
| 419 | + expectReRegistrationNeeded bool |
| 420 | + description string |
| 421 | + }{ |
| 422 | + { |
| 423 | + name: "CnsNotRegisteredFault should trigger re-registration", |
| 424 | + volumeOperationResult: &cnstypes.CnsVolumeOperationResult{ |
| 425 | + VolumeId: cnstypes.CnsVolumeId{Id: "test-volume-1"}, |
| 426 | + Fault: &vim25types.LocalizedMethodFault{ |
| 427 | + LocalizedMessage: "The input volume is not registered as a CNS volume", |
| 428 | + Fault: &cnstypes.CnsNotRegisteredFault{}, |
| 429 | + }, |
| 430 | + }, |
| 431 | + expectReRegistrationNeeded: true, |
| 432 | + description: "Volume not registered in CNS should trigger re-registration", |
| 433 | + }, |
| 434 | + { |
| 435 | + name: "CnsFault should not trigger re-registration", |
| 436 | + volumeOperationResult: &cnstypes.CnsVolumeOperationResult{ |
| 437 | + VolumeId: cnstypes.CnsVolumeId{Id: "test-volume-2"}, |
| 438 | + Fault: &vim25types.LocalizedMethodFault{ |
| 439 | + LocalizedMessage: "Generic CNS error", |
| 440 | + Fault: &cnstypes.CnsFault{ |
| 441 | + Reason: "Some other error", |
| 442 | + }, |
| 443 | + }, |
| 444 | + }, |
| 445 | + expectReRegistrationNeeded: false, |
| 446 | + description: "Generic CnsFault should not trigger re-registration", |
| 447 | + }, |
| 448 | + { |
| 449 | + name: "No fault should not trigger re-registration", |
| 450 | + volumeOperationResult: &cnstypes.CnsVolumeOperationResult{ |
| 451 | + VolumeId: cnstypes.CnsVolumeId{Id: "test-volume-3"}, |
| 452 | + Fault: nil, |
| 453 | + }, |
| 454 | + expectReRegistrationNeeded: false, |
| 455 | + description: "Successful operation should not trigger re-registration", |
| 456 | + }, |
| 457 | + { |
| 458 | + name: "ResourceInUse should not trigger re-registration", |
| 459 | + volumeOperationResult: &cnstypes.CnsVolumeOperationResult{ |
| 460 | + VolumeId: cnstypes.CnsVolumeId{Id: "test-volume-4"}, |
| 461 | + Fault: &vim25types.LocalizedMethodFault{ |
| 462 | + LocalizedMessage: "Resource is in use", |
| 463 | + Fault: &vim25types.ResourceInUse{ |
| 464 | + Type: "Disk", |
| 465 | + Name: "test-disk", |
| 466 | + }, |
| 467 | + }, |
| 468 | + }, |
| 469 | + expectReRegistrationNeeded: false, |
| 470 | + description: "ResourceInUse should not trigger re-registration", |
| 471 | + }, |
| 472 | + } |
| 473 | + |
| 474 | + for _, tt := range tests { |
| 475 | + t.Run(tt.name, func(t *testing.T) { |
| 476 | + var reRegistrationNeeded bool |
| 477 | + if tt.volumeOperationResult.Fault != nil { |
| 478 | + reRegistrationNeeded = IsCnsNotRegisteredFault(ctx, tt.volumeOperationResult.Fault) |
| 479 | + } |
| 480 | + assert.Equal(t, tt.expectReRegistrationNeeded, reRegistrationNeeded, |
| 481 | + "Test: %s - %s", tt.name, tt.description) |
| 482 | + }) |
| 483 | + } |
| 484 | +} |
| 485 | + |
| 486 | +// TestReRegistrationIdempotency tests that re-registration handles |
| 487 | +// CnsVolumeAlreadyExistsFault gracefully (idempotent behavior). |
| 488 | +func TestReRegistrationIdempotency(t *testing.T) { |
| 489 | + ctx := logger.NewContextWithLogger(context.Background()) |
| 490 | + |
| 491 | + tests := []struct { |
| 492 | + name string |
| 493 | + faultTypeAfterReRegister string |
| 494 | + expectSuccess bool |
| 495 | + description string |
| 496 | + }{ |
| 497 | + { |
| 498 | + name: "Re-registration succeeds", |
| 499 | + faultTypeAfterReRegister: "", |
| 500 | + expectSuccess: true, |
| 501 | + description: "Re-registration with no fault should succeed", |
| 502 | + }, |
| 503 | + { |
| 504 | + name: "Volume already exists (idempotent)", |
| 505 | + faultTypeAfterReRegister: "vim.fault.CnsVolumeAlreadyExistsFault", |
| 506 | + expectSuccess: true, |
| 507 | + description: "CnsVolumeAlreadyExistsFault should be treated as success", |
| 508 | + }, |
| 509 | + { |
| 510 | + name: "Other fault during re-registration", |
| 511 | + faultTypeAfterReRegister: "vim.fault.CnsFault", |
| 512 | + expectSuccess: false, |
| 513 | + description: "Other faults should be treated as failure", |
| 514 | + }, |
| 515 | + } |
| 516 | + |
| 517 | + for _, tt := range tests { |
| 518 | + t.Run(tt.name, func(t *testing.T) { |
| 519 | + var success bool |
| 520 | + if tt.faultTypeAfterReRegister == "" { |
| 521 | + success = true |
| 522 | + } else if IsCnsVolumeAlreadyExistsFault(ctx, tt.faultTypeAfterReRegister) { |
| 523 | + // CnsVolumeAlreadyExistsFault means volume is already registered |
| 524 | + success = true |
| 525 | + } else { |
| 526 | + success = false |
| 527 | + } |
| 528 | + assert.Equal(t, tt.expectSuccess, success, |
| 529 | + "Test: %s - %s", tt.name, tt.description) |
| 530 | + }) |
| 531 | + } |
| 532 | +} |
0 commit comments