|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package cloudcontrol |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "net/http" |
| 9 | + "testing" |
| 10 | + |
| 11 | + ccsdk "github.com/volcengine/terraform-provider-volcenginecc/internal/cloudcontrol" |
| 12 | + "github.com/volcengine/terraform-provider-volcenginecc/internal/tfresource" |
| 13 | + "github.com/volcengine/volcengine-go-sdk/volcengine/volcengineerr" |
| 14 | +) |
| 15 | + |
| 16 | +// TestWrapCloudControlNotFound verifies that only explicit Cloud Control |
| 17 | +// resource-not-found responses are converted to tfresource.NotFoundError. |
| 18 | +func TestWrapCloudControlNotFound(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + tests := map[string]struct { |
| 22 | + err error |
| 23 | + expected bool |
| 24 | + }{ |
| 25 | + "exact SDK code": { |
| 26 | + err: volcengineerr.New("NotFound", "resource does not exist", nil), |
| 27 | + expected: true, |
| 28 | + }, |
| 29 | + "HTTP 404": { |
| 30 | + err: volcengineerr.NewRequestFailure( |
| 31 | + volcengineerr.New("UnknownError", "resource does not exist", nil), |
| 32 | + http.StatusNotFound, |
| 33 | + "request-id", |
| 34 | + ), |
| 35 | + expected: true, |
| 36 | + }, |
| 37 | + "exact handler marker": { |
| 38 | + err: errors.New("handler failed (HandlerErrorCode: NotFound)"), |
| 39 | + expected: true, |
| 40 | + }, |
| 41 | + "service-specific NotFound code": { |
| 42 | + err: volcengineerr.New("InvalidNatGateway.NotFound", "resource does not exist", nil), |
| 43 | + expected: true, |
| 44 | + }, |
| 45 | + "partial service code suffix is not enough": { |
| 46 | + err: volcengineerr.New("InvalidNatGateway.NotFoundExtra", "different service error", nil), |
| 47 | + }, |
| 48 | + "partial handler marker is not enough": { |
| 49 | + err: errors.New("handler failed (HandlerErrorCode: NotFoundExtra)"), |
| 50 | + }, |
| 51 | + "ordinary error": { |
| 52 | + err: errors.New("permission denied"), |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for name, test := range tests { |
| 57 | + t.Run(name, func(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + err := wrapCloudControlNotFound(test.err) |
| 61 | + if got := tfresource.NotFound(err); got != test.expected { |
| 62 | + t.Fatalf("tfresource.NotFound() = %t, want %t; error: %v", got, test.expected, err) |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// TestIsCloudControlNotFoundProgressEvent verifies the strict ErrorCode and |
| 69 | +// StatusMessage boundaries used for failed synchronous and asynchronous tasks. |
| 70 | +func TestIsCloudControlNotFoundProgressEvent(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + |
| 73 | + notFound := "NotFound" |
| 74 | + serviceCode := "InvalidNatGateway.NotFound" |
| 75 | + partialServiceCode := "InvalidNatGateway.NotFoundExtra" |
| 76 | + exactMarker := "handler failed (HandlerErrorCode: NotFound)" |
| 77 | + partialMarker := "handler failed (HandlerErrorCode: NotFoundExtra)" |
| 78 | + |
| 79 | + tests := map[string]struct { |
| 80 | + event *ccsdk.ProgressEvent |
| 81 | + expected bool |
| 82 | + }{ |
| 83 | + "nil event": {}, |
| 84 | + "exact error code": { |
| 85 | + event: &ccsdk.ProgressEvent{ErrorCode: ¬Found}, |
| 86 | + expected: true, |
| 87 | + }, |
| 88 | + "service-specific NotFound code": { |
| 89 | + event: &ccsdk.ProgressEvent{ErrorCode: &serviceCode}, |
| 90 | + expected: true, |
| 91 | + }, |
| 92 | + "partial service code suffix is not enough": { |
| 93 | + event: &ccsdk.ProgressEvent{ErrorCode: &partialServiceCode}, |
| 94 | + }, |
| 95 | + "exact status marker": { |
| 96 | + event: &ccsdk.ProgressEvent{StatusMessage: &exactMarker}, |
| 97 | + expected: true, |
| 98 | + }, |
| 99 | + "partial status marker is not enough": { |
| 100 | + event: &ccsdk.ProgressEvent{StatusMessage: &partialMarker}, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for name, test := range tests { |
| 105 | + t.Run(name, func(t *testing.T) { |
| 106 | + t.Parallel() |
| 107 | + |
| 108 | + if got := isCloudControlNotFoundProgressEvent(test.event); got != test.expected { |
| 109 | + t.Fatalf("isCloudControlNotFoundProgressEvent() = %t, want %t", got, test.expected) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments