|
| 1 | +package extension |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 10 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 11 | +) |
| 12 | + |
| 13 | +func TestFrameworkDeleteExtensionDelegatesStateIdentityAndScope(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + var gotProjectID, gotID string |
| 17 | + client := fakeExtensionDeleter{ |
| 18 | + delete: func(ctx context.Context, projectID, id string) error { |
| 19 | + gotProjectID, gotID = projectID, id |
| 20 | + return nil |
| 21 | + }, |
| 22 | + } |
| 23 | + req := frameworkExtensionDeleteRequest(t, extensionModel{ |
| 24 | + ID: types.StringValue("extension_123"), |
| 25 | + Name: types.StringNull(), |
| 26 | + ProjectID: types.StringValue("project_123"), |
| 27 | + SourcePath: types.StringNull(), |
| 28 | + SourceSHA256: types.StringUnknown(), |
| 29 | + }) |
| 30 | + var resp resource.DeleteResponse |
| 31 | + |
| 32 | + deleteExtensionResource(context.Background(), client, req, &resp) |
| 33 | + |
| 34 | + if resp.Diagnostics.HasError() { |
| 35 | + t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics) |
| 36 | + } |
| 37 | + if gotProjectID != "project_123" || gotID != "extension_123" { |
| 38 | + t.Fatalf("DeleteExtension scope/id = %q/%q, want project_123/extension_123", gotProjectID, gotID) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestFrameworkDeleteExtensionReturnsDependencyDiagnostic(t *testing.T) { |
| 43 | + t.Parallel() |
| 44 | + |
| 45 | + client := fakeExtensionDeleter{ |
| 46 | + delete: func(ctx context.Context, projectID, id string) error { |
| 47 | + return extensionAPIErrorForTest(t, http.StatusBadRequest, `{"code":"resource_in_use"}`) |
| 48 | + }, |
| 49 | + } |
| 50 | + req := frameworkExtensionDeleteRequest(t, extensionDeleteStateForTest()) |
| 51 | + var resp resource.DeleteResponse |
| 52 | + |
| 53 | + deleteExtensionResource(context.Background(), client, req, &resp) |
| 54 | + |
| 55 | + if !extensionDiagnosticContains(resp.Diagnostics, "Delete Kernel Extension", "browser pool configurations") { |
| 56 | + t.Fatalf("diagnostics = %v, want durable dependency guidance", resp.Diagnostics) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestFrameworkDeleteExtensionRejectsMalformedStateBeforeCall(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + called := false |
| 64 | + client := fakeExtensionDeleter{ |
| 65 | + delete: func(ctx context.Context, projectID, id string) error { |
| 66 | + called = true |
| 67 | + return nil |
| 68 | + }, |
| 69 | + } |
| 70 | + var req resource.DeleteRequest |
| 71 | + req.State.Schema = extensionSchema() |
| 72 | + req.State.Raw = tftypes.NewValue(tftypes.String, "not extension state") |
| 73 | + var resp resource.DeleteResponse |
| 74 | + |
| 75 | + deleteExtensionResource(context.Background(), client, req, &resp) |
| 76 | + |
| 77 | + if !resp.Diagnostics.HasError() { |
| 78 | + t.Fatal("expected malformed-state diagnostic") |
| 79 | + } |
| 80 | + if called { |
| 81 | + t.Fatal("DeleteExtension called for malformed state") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func frameworkExtensionDeleteRequest(t *testing.T, state extensionModel) resource.DeleteRequest { |
| 86 | + t.Helper() |
| 87 | + |
| 88 | + var req resource.DeleteRequest |
| 89 | + req.State.Schema = extensionSchema() |
| 90 | + if diags := req.State.Set(context.Background(), state); diags.HasError() { |
| 91 | + t.Fatalf("set extension delete state: %v", diags) |
| 92 | + } |
| 93 | + return req |
| 94 | +} |
| 95 | + |
| 96 | +func extensionDeleteStateForTest() extensionModel { |
| 97 | + return extensionModel{ |
| 98 | + ID: types.StringValue("extension_123"), |
| 99 | + Name: types.StringNull(), |
| 100 | + ProjectID: types.StringNull(), |
| 101 | + SourcePath: types.StringNull(), |
| 102 | + SourceSHA256: types.StringUnknown(), |
| 103 | + } |
| 104 | +} |
0 commit comments