|
| 1 | +package sqlserverflex |
| 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/stackit-sdk-go/core/utils" |
| 12 | + "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" |
| 13 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil" |
| 14 | +) |
| 15 | + |
| 16 | +func TestSQLServerFlexInstanceSavesIDsOnError(t *testing.T) { |
| 17 | + projectId := uuid.NewString() |
| 18 | + instanceId := uuid.NewString() |
| 19 | + const ( |
| 20 | + name = "instance-name" |
| 21 | + flavorCpu = 4 |
| 22 | + flavorRam = 16 |
| 23 | + flavorId = "4.16-Single" |
| 24 | + region = "eu01" |
| 25 | + ) |
| 26 | + s := testutil.NewMockServer(t) |
| 27 | + defer s.Server.Close() |
| 28 | + tfConfig := fmt.Sprintf(` |
| 29 | +provider "stackit" { |
| 30 | + default_region = "%s" |
| 31 | + sqlserverflex_custom_endpoint = "%s" |
| 32 | + service_account_token = "mock-server-needs-no-auth" |
| 33 | +} |
| 34 | +
|
| 35 | +resource "stackit_sqlserverflex_instance" "instance" { |
| 36 | + project_id = "%s" |
| 37 | + name = "%s" |
| 38 | + flavor = { |
| 39 | + cpu = %d |
| 40 | + ram = %d |
| 41 | + } |
| 42 | +} |
| 43 | +
|
| 44 | +`, region, s.Server.URL, projectId, name, flavorCpu, flavorRam) |
| 45 | + flavor := testutil.MockResponse{ |
| 46 | + ToJsonBody: &sqlserverflex.ListFlavorsResponse{ |
| 47 | + Flavors: &[]sqlserverflex.InstanceFlavorEntry{ |
| 48 | + { |
| 49 | + Cpu: utils.Ptr(int64(flavorCpu)), |
| 50 | + Memory: utils.Ptr(int64(flavorRam)), |
| 51 | + Id: utils.Ptr(flavorId), |
| 52 | + Description: utils.Ptr("test-flavor-id"), |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + resource.UnitTest(t, resource.TestCase{ |
| 59 | + ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, |
| 60 | + Steps: []resource.TestStep{ |
| 61 | + { |
| 62 | + PreConfig: func() { |
| 63 | + s.Reset( |
| 64 | + flavor, |
| 65 | + testutil.MockResponse{ |
| 66 | + Description: "create", |
| 67 | + ToJsonBody: sqlserverflex.CreateInstanceResponse{ |
| 68 | + Id: utils.Ptr(instanceId), |
| 69 | + }, |
| 70 | + }, |
| 71 | + testutil.MockResponse{ |
| 72 | + Description: "failing waiter", |
| 73 | + StatusCode: http.StatusInternalServerError, |
| 74 | + }, |
| 75 | + ) |
| 76 | + }, |
| 77 | + Config: tfConfig, |
| 78 | + ExpectError: regexp.MustCompile("Error creating instance.*"), |
| 79 | + }, |
| 80 | + { |
| 81 | + PreConfig: func() { |
| 82 | + s.Reset( |
| 83 | + testutil.MockResponse{ |
| 84 | + Description: "refresh", |
| 85 | + Handler: func(w http.ResponseWriter, req *http.Request) { |
| 86 | + expected := fmt.Sprintf("/v2/projects/%s/regions/%s/instances/%s", projectId, region, instanceId) |
| 87 | + if req.URL.Path != expected { |
| 88 | + t.Errorf("expected request to %s, got %s", expected, req.URL.Path) |
| 89 | + } |
| 90 | + w.WriteHeader(http.StatusInternalServerError) |
| 91 | + }, |
| 92 | + }, |
| 93 | + testutil.MockResponse{Description: "delete", StatusCode: http.StatusAccepted}, |
| 94 | + testutil.MockResponse{Description: "delete waiter", StatusCode: http.StatusNotFound}, |
| 95 | + ) |
| 96 | + }, |
| 97 | + RefreshState: true, |
| 98 | + ExpectError: regexp.MustCompile("Error reading instance*"), |
| 99 | + }, |
| 100 | + }, |
| 101 | + }) |
| 102 | +} |
0 commit comments