|
| 1 | +package acctest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +type fakeExtensionCleaner struct { |
| 10 | + defaultProjectID string |
| 11 | + delete func(context.Context, string, string) error |
| 12 | +} |
| 13 | + |
| 14 | +func (f fakeExtensionCleaner) DefaultProjectID() string { |
| 15 | + return f.defaultProjectID |
| 16 | +} |
| 17 | + |
| 18 | +func (f fakeExtensionCleaner) DeleteExtension(ctx context.Context, projectID, id string) error { |
| 19 | + return f.delete(ctx, projectID, id) |
| 20 | +} |
| 21 | + |
| 22 | +func TestCleanupExtension(t *testing.T) { |
| 23 | + tests := map[string]struct { |
| 24 | + acceptance string |
| 25 | + apiKey string |
| 26 | + projectID string |
| 27 | + defaultProject string |
| 28 | + id string |
| 29 | + deleteErr error |
| 30 | + wantProjectID string |
| 31 | + wantCleanups int |
| 32 | + wantDelete bool |
| 33 | + wantFailure bool |
| 34 | + }{ |
| 35 | + "empty ID is ignored": {}, |
| 36 | + "acceptance disabled": { |
| 37 | + apiKey: "test-key", |
| 38 | + id: "extension_123", |
| 39 | + wantFailure: true, |
| 40 | + }, |
| 41 | + "API key missing": { |
| 42 | + acceptance: "1", |
| 43 | + id: "extension_123", |
| 44 | + wantFailure: true, |
| 45 | + }, |
| 46 | + "default project is resolved": { |
| 47 | + acceptance: "1", |
| 48 | + apiKey: "test-key", |
| 49 | + defaultProject: "project_default", |
| 50 | + id: "extension_123", |
| 51 | + wantProjectID: "project_default", |
| 52 | + wantCleanups: 1, |
| 53 | + wantDelete: true, |
| 54 | + }, |
| 55 | + "not found is already clean": { |
| 56 | + acceptance: "1", |
| 57 | + apiKey: "test-key", |
| 58 | + projectID: "project_explicit", |
| 59 | + id: "extension_123", |
| 60 | + deleteErr: notFoundAPIError(), |
| 61 | + wantProjectID: "project_explicit", |
| 62 | + wantCleanups: 1, |
| 63 | + wantDelete: true, |
| 64 | + }, |
| 65 | + "delete error is reported": { |
| 66 | + acceptance: "1", |
| 67 | + apiKey: "test-key", |
| 68 | + projectID: "project_explicit", |
| 69 | + id: "extension_123", |
| 70 | + deleteErr: errors.New("connection reset"), |
| 71 | + wantProjectID: "project_explicit", |
| 72 | + wantCleanups: 1, |
| 73 | + wantDelete: true, |
| 74 | + wantFailure: true, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for name, test := range tests { |
| 79 | + t.Run(name, func(t *testing.T) { |
| 80 | + t.Setenv(EnvAcceptance, test.acceptance) |
| 81 | + t.Setenv(EnvAPIKey, test.apiKey) |
| 82 | + |
| 83 | + var gotID, gotProjectID string |
| 84 | + deleteCalled := false |
| 85 | + deadlineSet := false |
| 86 | + recorder := &testRecorder{TB: t} |
| 87 | + cleanupExtension(recorder, fakeExtensionCleaner{ |
| 88 | + defaultProjectID: test.defaultProject, |
| 89 | + delete: func(ctx context.Context, projectID, id string) error { |
| 90 | + deleteCalled = true |
| 91 | + _, deadlineSet = ctx.Deadline() |
| 92 | + gotProjectID = projectID |
| 93 | + gotID = id |
| 94 | + return test.deleteErr |
| 95 | + }, |
| 96 | + }, test.projectID, test.id) |
| 97 | + |
| 98 | + if got, want := len(recorder.cleanups), test.wantCleanups; got != want { |
| 99 | + t.Fatalf("cleanupExtension registered %d cleanups, want %d", got, want) |
| 100 | + } |
| 101 | + if test.wantCleanups == 1 { |
| 102 | + recorder.cleanups[0]() |
| 103 | + } |
| 104 | + if recorder.failed != test.wantFailure { |
| 105 | + t.Fatalf("cleanupExtension failure = %t, want %t", recorder.failed, test.wantFailure) |
| 106 | + } |
| 107 | + if deleteCalled != test.wantDelete { |
| 108 | + t.Fatalf("cleanupExtension called delete = %t, want %t", deleteCalled, test.wantDelete) |
| 109 | + } |
| 110 | + if deleteCalled && !deadlineSet { |
| 111 | + t.Fatal("cleanupExtension called delete without a context deadline") |
| 112 | + } |
| 113 | + if test.wantDelete && (gotID != test.id || gotProjectID != test.wantProjectID) { |
| 114 | + t.Fatalf("cleanup extension scope/id = %q/%q, want %q/%q", gotProjectID, gotID, test.wantProjectID, test.id) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments