Skip to content

Commit 49285b3

Browse files
committed
fix(mongodbflex) move SavesIDsOnError tests into new file
1 parent fef2dcd commit 49285b3

2 files changed

Lines changed: 169 additions & 158 deletions

File tree

stackit/internal/services/mongodbflex/mongodbflex_acc_test.go

Lines changed: 0 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package mongodbflex_test
33
import (
44
"context"
55
"fmt"
6-
"net/http"
7-
"regexp"
86
"strings"
97
"testing"
108

11-
"github.com/google/uuid"
129
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1310
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1411
"github.com/hashicorp/terraform-plugin-testing/terraform"
@@ -299,161 +296,6 @@ func TestAccMongoDBFlexFlexResource(t *testing.T) {
299296
})
300297
}
301298

302-
// slow test, delete has a 30s sleep...
303-
func TestMongoDBInstanceSavesIDsOnError(t *testing.T) {
304-
var (
305-
projectId = uuid.NewString()
306-
instanceId = uuid.NewString()
307-
)
308-
const (
309-
name = "instance-test"
310-
region = "eu01"
311-
)
312-
s := testutil.NewMockServer(t)
313-
defer s.Server.Close()
314-
tfConfig := fmt.Sprintf(`
315-
provider "stackit" {
316-
mongodbflex_custom_endpoint = "%s"
317-
service_account_token = "mock-server-needs-no-auth"
318-
}
319-
320-
resource "stackit_mongodbflex_instance" "instance" {
321-
project_id = "%s"
322-
name = "%s"
323-
options = {
324-
type = "Replica"
325-
snapshot_retention_days = 1
326-
daily_snapshot_retention_days = 1
327-
point_in_time_window_hours = 1
328-
}
329-
storage = {
330-
class = "premium-perf2-mongodb"
331-
size = 10
332-
}
333-
replicas = 1
334-
acl = ["192.168.0.0/16"]
335-
flavor = {
336-
cpu =2
337-
ram =4
338-
}
339-
version = "7.0"
340-
backup_schedule = "00 6 * * *"
341-
}
342-
`, s.Server.URL, projectId, name)
343-
344-
resource.UnitTest(t, resource.TestCase{
345-
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
346-
Steps: []resource.TestStep{
347-
{
348-
PreConfig: func() {
349-
s.Reset(
350-
testutil.MockResponse{
351-
Description: "ListFlavors",
352-
ToJsonBody: &mongodbflex.ListFlavorsResponse{Flavors: &[]mongodbflex.InstanceFlavor{
353-
{
354-
Description: utils.Ptr("flava-flav"),
355-
Cpu: utils.Ptr(int64(2)),
356-
Id: utils.Ptr("flavor-id"),
357-
Memory: utils.Ptr(int64(4)),
358-
},
359-
}},
360-
},
361-
testutil.MockResponse{
362-
Description: "create instance",
363-
ToJsonBody: &mongodbflex.CreateInstanceResponse{Id: utils.Ptr(instanceId)},
364-
},
365-
testutil.MockResponse{Description: "create waiter", StatusCode: http.StatusInternalServerError},
366-
)
367-
},
368-
Config: tfConfig,
369-
ExpectError: regexp.MustCompile("Error creating instance.*"),
370-
},
371-
{
372-
PreConfig: func() {
373-
s.Reset(
374-
testutil.MockResponse{
375-
Description: "refresh",
376-
Handler: func(w http.ResponseWriter, req *http.Request) {
377-
expected := fmt.Sprintf("/v2/projects/%s/regions/%s/instances/%s", projectId, region, instanceId)
378-
if req.URL.Path != expected {
379-
t.Errorf("expected request to %s, got %s", expected, req.URL.Path)
380-
}
381-
w.WriteHeader(http.StatusInternalServerError)
382-
},
383-
},
384-
testutil.MockResponse{Description: "delete"},
385-
testutil.MockResponse{Description: "delete waiter", StatusCode: http.StatusNotFound},
386-
)
387-
},
388-
RefreshState: true,
389-
ExpectError: regexp.MustCompile("Error reading instance.*"),
390-
},
391-
},
392-
})
393-
}
394-
395-
func TestMongoDBUserSavesIDsOnError(t *testing.T) {
396-
projectId := uuid.NewString()
397-
instanceId := uuid.NewString()
398-
userId := uuid.NewString()
399-
const region = "eu01"
400-
s := testutil.NewMockServer(t)
401-
defer s.Server.Close()
402-
tfConfig := fmt.Sprintf(`
403-
provider "stackit" {
404-
mongodbflex_custom_endpoint = "%s"
405-
service_account_token = "mock-server-needs-no-auth"
406-
}
407-
408-
resource "stackit_mongodbflex_user" "user" {
409-
project_id = "%s"
410-
instance_id = "%s"
411-
username = "username"
412-
roles = ["read"]
413-
database = "db-name"
414-
}
415-
`, s.Server.URL, projectId, instanceId)
416-
417-
resource.UnitTest(t, resource.TestCase{
418-
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
419-
Steps: []resource.TestStep{
420-
{
421-
PreConfig: func() {
422-
s.Reset(
423-
testutil.MockResponse{
424-
Description: "create user",
425-
ToJsonBody: &mongodbflex.CreateUserResponse{Item: &mongodbflex.User{Id: utils.Ptr(userId)}},
426-
},
427-
testutil.MockResponse{Description: "failing waiter", StatusCode: http.StatusInternalServerError},
428-
)
429-
},
430-
Config: tfConfig,
431-
ExpectError: regexp.MustCompile("Error creating user.*"),
432-
},
433-
{
434-
PreConfig: func() {
435-
s.Reset(
436-
testutil.MockResponse{
437-
Description: "refresh user",
438-
Handler: func(w http.ResponseWriter, req *http.Request) {
439-
expected := fmt.Sprintf("/v2/projects/%s/regions/%s/instances/%s/users/%s", projectId, region, instanceId, userId)
440-
if req.URL.Path != expected {
441-
t.Errorf("expected request to %s, got %s", expected, req.URL.Path)
442-
}
443-
w.WriteHeader(http.StatusInternalServerError)
444-
},
445-
},
446-
testutil.MockResponse{Description: "delete user"},
447-
testutil.MockResponse{Description: "delete user waiter", StatusCode: http.StatusNotFound},
448-
)
449-
},
450-
RefreshState: true,
451-
ExpectError: regexp.MustCompile("Error reading user.*"),
452-
},
453-
},
454-
})
455-
}
456-
457299
func testAccCheckMongoDBFlexDestroy(s *terraform.State) error {
458300
ctx := context.Background()
459301
var client *mongodbflex.APIClient
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package mongodbflex
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/mongodbflex"
13+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
14+
)
15+
16+
// slow test, delete has a 30s sleep...
17+
func TestMongoDBInstanceSavesIDsOnError(t *testing.T) {
18+
var (
19+
projectId = uuid.NewString()
20+
instanceId = uuid.NewString()
21+
)
22+
const (
23+
name = "instance-test"
24+
region = "eu01"
25+
)
26+
s := testutil.NewMockServer(t)
27+
defer s.Server.Close()
28+
tfConfig := fmt.Sprintf(`
29+
provider "stackit" {
30+
mongodbflex_custom_endpoint = "%s"
31+
service_account_token = "mock-server-needs-no-auth"
32+
}
33+
34+
resource "stackit_mongodbflex_instance" "instance" {
35+
project_id = "%s"
36+
name = "%s"
37+
options = {
38+
type = "Replica"
39+
snapshot_retention_days = 1
40+
daily_snapshot_retention_days = 1
41+
point_in_time_window_hours = 1
42+
}
43+
storage = {
44+
class = "premium-perf2-mongodb"
45+
size = 10
46+
}
47+
replicas = 1
48+
acl = ["192.168.0.0/16"]
49+
flavor = {
50+
cpu =2
51+
ram =4
52+
}
53+
version = "7.0"
54+
backup_schedule = "00 6 * * *"
55+
}
56+
`, s.Server.URL, projectId, name)
57+
58+
resource.UnitTest(t, resource.TestCase{
59+
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
60+
Steps: []resource.TestStep{
61+
{
62+
PreConfig: func() {
63+
s.Reset(
64+
testutil.MockResponse{
65+
Description: "ListFlavors",
66+
ToJsonBody: &mongodbflex.ListFlavorsResponse{Flavors: &[]mongodbflex.InstanceFlavor{
67+
{
68+
Description: utils.Ptr("flava-flav"),
69+
Cpu: utils.Ptr(int64(2)),
70+
Id: utils.Ptr("flavor-id"),
71+
Memory: utils.Ptr(int64(4)),
72+
},
73+
}},
74+
},
75+
testutil.MockResponse{
76+
Description: "create instance",
77+
ToJsonBody: &mongodbflex.CreateInstanceResponse{Id: utils.Ptr(instanceId)},
78+
},
79+
testutil.MockResponse{Description: "create waiter", StatusCode: http.StatusInternalServerError},
80+
)
81+
},
82+
Config: tfConfig,
83+
ExpectError: regexp.MustCompile("Error creating instance.*"),
84+
},
85+
{
86+
PreConfig: func() {
87+
s.Reset(
88+
testutil.MockResponse{
89+
Description: "refresh",
90+
Handler: func(w http.ResponseWriter, req *http.Request) {
91+
expected := fmt.Sprintf("/v2/projects/%s/regions/%s/instances/%s", projectId, region, instanceId)
92+
if req.URL.Path != expected {
93+
t.Errorf("expected request to %s, got %s", expected, req.URL.Path)
94+
}
95+
w.WriteHeader(http.StatusInternalServerError)
96+
},
97+
},
98+
testutil.MockResponse{Description: "delete"},
99+
testutil.MockResponse{Description: "delete waiter", StatusCode: http.StatusNotFound},
100+
)
101+
},
102+
RefreshState: true,
103+
ExpectError: regexp.MustCompile("Error reading instance.*"),
104+
},
105+
},
106+
})
107+
}
108+
109+
func TestMongoDBUserSavesIDsOnError(t *testing.T) {
110+
projectId := uuid.NewString()
111+
instanceId := uuid.NewString()
112+
userId := uuid.NewString()
113+
const region = "eu01"
114+
s := testutil.NewMockServer(t)
115+
defer s.Server.Close()
116+
tfConfig := fmt.Sprintf(`
117+
provider "stackit" {
118+
mongodbflex_custom_endpoint = "%s"
119+
service_account_token = "mock-server-needs-no-auth"
120+
}
121+
122+
resource "stackit_mongodbflex_user" "user" {
123+
project_id = "%s"
124+
instance_id = "%s"
125+
username = "username"
126+
roles = ["read"]
127+
database = "db-name"
128+
}
129+
`, s.Server.URL, projectId, instanceId)
130+
131+
resource.UnitTest(t, resource.TestCase{
132+
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
133+
Steps: []resource.TestStep{
134+
{
135+
PreConfig: func() {
136+
s.Reset(
137+
testutil.MockResponse{
138+
Description: "create user",
139+
ToJsonBody: &mongodbflex.CreateUserResponse{Item: &mongodbflex.User{Id: utils.Ptr(userId)}},
140+
},
141+
testutil.MockResponse{Description: "failing waiter", StatusCode: http.StatusInternalServerError},
142+
)
143+
},
144+
Config: tfConfig,
145+
ExpectError: regexp.MustCompile("Error creating user.*"),
146+
},
147+
{
148+
PreConfig: func() {
149+
s.Reset(
150+
testutil.MockResponse{
151+
Description: "refresh user",
152+
Handler: func(w http.ResponseWriter, req *http.Request) {
153+
expected := fmt.Sprintf("/v2/projects/%s/regions/%s/instances/%s/users/%s", projectId, region, instanceId, userId)
154+
if req.URL.Path != expected {
155+
t.Errorf("expected request to %s, got %s", expected, req.URL.Path)
156+
}
157+
w.WriteHeader(http.StatusInternalServerError)
158+
},
159+
},
160+
testutil.MockResponse{Description: "delete user"},
161+
testutil.MockResponse{Description: "delete user waiter", StatusCode: http.StatusNotFound},
162+
)
163+
},
164+
RefreshState: true,
165+
ExpectError: regexp.MustCompile("Error reading user.*"),
166+
},
167+
},
168+
})
169+
}

0 commit comments

Comments
 (0)