|
| 1 | +package foo |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "regexp" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 11 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil" |
| 12 | +) |
| 13 | + |
| 14 | +func TestFooSavesIDsOnError(t *testing.T) { |
| 15 | + /* Setup code: |
| 16 | + - define known values for attributes used in id |
| 17 | + - create mock server |
| 18 | + - define minimal tf config with custom endpoint pointing to mock server |
| 19 | + */ |
| 20 | + var ( |
| 21 | + projectId = uuid.NewString() |
| 22 | + barId = uuid.NewString() |
| 23 | + ) |
| 24 | + const region = "eu01" |
| 25 | + s := testutil.NewMockServer(t) |
| 26 | + defer s.Server.Close() |
| 27 | + tfConfig := fmt.Sprintf(` |
| 28 | +provider "stackit" { |
| 29 | + foo_custom_endpoint = "%s" |
| 30 | + service_account_token = "mock-server-needs-no-auth" |
| 31 | +} |
| 32 | +
|
| 33 | +resource "stackit_foo" "foo" { |
| 34 | + project_id = "%s" |
| 35 | +} |
| 36 | +`, s.Server.URL, projectId) |
| 37 | + |
| 38 | + /* Test steps: |
| 39 | + 1. Create resource with mocked backend |
| 40 | + 2. Verify with a refresh, that IDs are saved to state |
| 41 | + */ |
| 42 | + resource.UnitTest(t, resource.TestCase{ |
| 43 | + ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, |
| 44 | + Steps: []resource.TestStep{ |
| 45 | + { |
| 46 | + PreConfig: func() { |
| 47 | + /* Setup mock responses for create and waiter. |
| 48 | + The create response succeeds and returns the barId, but the waiter fails with an error. |
| 49 | + We can't check the state in this step, because the create returns early due to the waiter error. |
| 50 | + TF won't execute any Checks of the TestStep if there is an error. |
| 51 | + */ |
| 52 | + s.Reset( |
| 53 | + testutil.MockResponse{ |
| 54 | + Description: "create foo", |
| 55 | + ToJsonBody: &BarResponse{ |
| 56 | + BarId: barId, |
| 57 | + }, |
| 58 | + }, |
| 59 | + testutil.MockResponse{Description: "failing waiter", StatusCode: http.StatusInternalServerError}, |
| 60 | + ) |
| 61 | + }, |
| 62 | + Config: tfConfig, |
| 63 | + ExpectError: regexp.MustCompile("Error creating foo.*"), |
| 64 | + }, |
| 65 | + { |
| 66 | + PreConfig: func() { |
| 67 | + /* Setup mock responses for refresh and delete. |
| 68 | + The refresh response fails with an error, but we want to verify that the URL contains the correct IDs. |
| 69 | + After the test TF will automatically destroy the resource. So we set up mocks to simulate a successful delete. |
| 70 | + */ |
| 71 | + s.Reset( |
| 72 | + testutil.MockResponse{ |
| 73 | + Description: "refresh", |
| 74 | + Handler: func(w http.ResponseWriter, req *http.Request) { |
| 75 | + expected := fmt.Sprintf("/v1/projects/%s/regions/%s/foo/%s", projectId, region, barId) |
| 76 | + if req.URL.Path != expected { |
| 77 | + t.Errorf("unexpected URL path: got %s, want %s", req.URL.Path, expected) |
| 78 | + } |
| 79 | + w.WriteHeader(http.StatusInternalServerError) |
| 80 | + }, |
| 81 | + }, |
| 82 | + testutil.MockResponse{Description: "delete"}, |
| 83 | + testutil.MockResponse{Description: "delete waiter", StatusCode: http.StatusGone}, |
| 84 | + ) |
| 85 | + }, |
| 86 | + RefreshState: true, |
| 87 | + ExpectError: regexp.MustCompile("Error reading foo.*"), |
| 88 | + }, |
| 89 | + }, |
| 90 | + }) |
| 91 | +} |
0 commit comments