-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathedge_acc_test.go
More file actions
372 lines (355 loc) · 18.8 KB
/
edge_acc_test.go
File metadata and controls
372 lines (355 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package edgecloud_test
import (
"context"
_ "embed"
"errors"
"fmt"
"net/http"
"regexp"
"strings"
"testing"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
edge "github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api"
"github.com/stackitcloud/stackit-sdk-go/services/edge/v1beta1api/wait"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
)
var (
// Currently the API does not verify if the UUID belongs to a valid plan
// This will change with GA, which will require replacing the random UUIDs with real plan UUIDs
testPlanId = uuid.NewString()
testPlanIdUpdated = uuid.NewString()
minTestName = "min-" + acctest.RandStringFromCharSet(4, acctest.CharSetAlpha)
testDescription = "test description"
testDescriptionUpdated = "test description updated"
testExpiration = 1800
testRecreateBefore = 120
testRecreateBeforeUpdated = 100
)
//go:embed testdata/resource-min.tf
var resourceMin string
//go:embed testdata/resource-max.tf
var resourceMax string
// Minimal configuration
var testConfigVarsMin = config.Variables{
// region is unset, to verify it is picked up from the provider config
"project_id": config.StringVariable(testutil.ProjectId),
"display_name": config.StringVariable(minTestName),
"plan_id": config.StringVariable(testPlanId),
}
// Maximal configuration
func configVarsMax(displayName, planId, description string, expiration, recreateBefore int) config.Variables {
return config.Variables{
"project_id": config.StringVariable(testutil.ProjectId),
"region": config.StringVariable(testutil.Region),
"display_name": config.StringVariable(displayName),
"plan_id": config.StringVariable(planId),
"description": config.StringVariable(description),
"expiration": config.IntegerVariable(expiration),
"recreate_before": config.IntegerVariable(recreateBefore),
}
}
func TestAccEdgeCloudInstanceMin(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckEdgeCloudInstanceDestroy,
Steps: []resource.TestStep{
// resources
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMin,
ConfigVariables: testConfigVarsMin,
Check: resource.ComposeAggregateTestCheckFunc(
// instance
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "project_id", testutil.ProjectId),
// testutil.Region is also used in testutils.EdgeCloudProviderConfig to define a default_region
// this checks that this is successfully used for the resource, even if no region is specifically set
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "region", testutil.Region),
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "display_name", minTestName),
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "plan_id", testPlanId),
resource.TestCheckResourceAttrSet("stackit_edgecloud_instance.test_instance", "instance_id"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_instance.test_instance", "frontend_url"),
// token
resource.TestCheckResourceAttr("stackit_edgecloud_token.this", "region", testutil.Region),
resource.TestCheckResourceAttr("stackit_edgecloud_token.this", "project_id", testutil.ProjectId),
resource.TestCheckResourceAttrSet("stackit_edgecloud_token.this", "instance_id"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_token.this", "token"),
// kubeconfig
resource.TestCheckResourceAttr("stackit_edgecloud_kubeconfig.this", "region", testutil.Region),
resource.TestCheckResourceAttr("stackit_edgecloud_kubeconfig.this", "project_id", testutil.ProjectId),
resource.TestCheckResourceAttrSet("stackit_edgecloud_kubeconfig.this", "instance_id"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_kubeconfig.this", "kubeconfig"),
),
},
// data sources
{
ConfigVariables: testConfigVarsMin,
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMin,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.stackit_edgecloud_instances.this", "id", fmt.Sprintf("%s,%s",
testutil.ProjectId,
testutil.Region,
)),
resource.TestCheckResourceAttr("data.stackit_edgecloud_plans.this", "id", testutil.ProjectId),
resource.TestCheckResourceAttr("data.stackit_edgecloud_plans.this", "project_id", testutil.ProjectId),
),
},
// import
{
ConfigVariables: testConfigVarsMin,
ResourceName: "stackit_edgecloud_instance.test_instance",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_edgecloud_instance.test_instance"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_edgecloud_instance.test_instance")
}
instanceID, ok := r.Primary.Attributes["instance_id"]
if !ok {
return "", fmt.Errorf("couldn't find attribute instance_id")
}
return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, testutil.Region, instanceID), nil
},
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccEdgeCloudMax(t *testing.T) {
displayName := "mx-in-" + acctest.RandStringFromCharSet(2, acctest.CharSetAlpha)
initialVars := configVarsMax(displayName, testPlanId, testDescription, testExpiration, testRecreateBefore)
updatedVars := configVarsMax(displayName, testPlanIdUpdated, testDescriptionUpdated, testExpiration, testRecreateBeforeUpdated)
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckEdgeCloudInstanceDestroy,
Steps: []resource.TestStep{
// Creation
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMax,
ConfigVariables: initialVars,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "project_id", testutil.ProjectId),
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "region", testutil.Region),
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "display_name", displayName),
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "plan_id", testPlanId),
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "description", testDescription),
resource.TestCheckResourceAttrSet("stackit_edgecloud_instance.test_instance", "instance_id"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_instance.test_instance", "frontend_url"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_instance.test_instance", "status"),
),
},
// Data sources
{
ConfigVariables: initialVars,
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMax,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.stackit_edgecloud_instances.this", "id", fmt.Sprintf("%s,%s",
testutil.ProjectId,
testutil.Region,
)),
resource.TestCheckResourceAttrSet("data.stackit_edgecloud_instances.this", "instances.0.created"),
// TestCheckResourceAttrSet fails if the value is "", which is an allowed value for the description. That's why it has to be checked via regex
resource.TestMatchResourceAttr("data.stackit_edgecloud_instances.this", "instances.0.description", regexp.MustCompile("^.*$")),
resource.TestCheckResourceAttrSet("data.stackit_edgecloud_instances.this", "instances.0.display_name"),
resource.TestCheckResourceAttrSet("data.stackit_edgecloud_instances.this", "instances.0.frontend_url"),
resource.TestCheckResourceAttrSet("data.stackit_edgecloud_instances.this", "instances.0.instance_id"),
resource.TestCheckResourceAttrSet("data.stackit_edgecloud_instances.this", "instances.0.plan_id"),
resource.TestCheckResourceAttrSet("data.stackit_edgecloud_instances.this", "instances.0.region"),
resource.TestCheckResourceAttrSet("data.stackit_edgecloud_instances.this", "instances.0.status"),
// check plans data source
resource.TestCheckResourceAttr("data.stackit_edgecloud_plans.this", "id", testutil.ProjectId),
resource.TestCheckResourceAttr("data.stackit_edgecloud_plans.this", "project_id", testutil.ProjectId),
resource.TestCheckResourceAttrSet("data.stackit_edgecloud_plans.this", "plans.0.min_edge_hosts"),
resource.TestCheckResourceAttrSet("data.stackit_edgecloud_plans.this", "plans.0.max_edge_hosts"),
),
},
// Kubeconfig
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMax,
ConfigVariables: initialVars,
Check: resource.ComposeAggregateTestCheckFunc(
// Kubeconfig by name
resource.TestCheckResourceAttr("stackit_edgecloud_kubeconfig.by_name", "project_id", testutil.ProjectId),
resource.TestCheckResourceAttr("stackit_edgecloud_kubeconfig.by_name", "instance_name", displayName),
resource.TestCheckResourceAttrSet("stackit_edgecloud_kubeconfig.by_name", "kubeconfig_id"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_kubeconfig.by_name", "kubeconfig"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_kubeconfig.by_name", "expires_at"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_kubeconfig.by_name", "creation_time"),
resource.TestCheckResourceAttr("stackit_edgecloud_kubeconfig.by_name", "region", testutil.Region),
// Kubeconfig by id
resource.TestCheckResourceAttr("stackit_edgecloud_kubeconfig.by_id", "project_id", testutil.ProjectId),
resource.TestCheckResourceAttrPair(
"stackit_edgecloud_instance.test_instance", "instance_id",
"stackit_edgecloud_kubeconfig.by_id", "instance_id",
),
resource.TestCheckResourceAttrSet("stackit_edgecloud_kubeconfig.by_id", "kubeconfig_id"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_kubeconfig.by_id", "kubeconfig"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_kubeconfig.by_id", "expires_at"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_kubeconfig.by_id", "creation_time"),
resource.TestCheckResourceAttr("stackit_edgecloud_kubeconfig.by_id", "region", testutil.Region),
),
},
// Token
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMax,
ConfigVariables: initialVars,
Check: resource.ComposeAggregateTestCheckFunc(
// Token by name
resource.TestCheckResourceAttr("stackit_edgecloud_token.by_name", "project_id", testutil.ProjectId),
resource.TestCheckResourceAttr("stackit_edgecloud_token.by_name", "instance_name", displayName),
resource.TestCheckResourceAttrSet("stackit_edgecloud_token.by_name", "token_id"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_token.by_name", "token"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_token.by_name", "expires_at"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_token.by_name", "creation_time"),
resource.TestCheckResourceAttr("stackit_edgecloud_token.by_name", "region", testutil.Region),
// Token by id
resource.TestCheckResourceAttr("stackit_edgecloud_token.by_id", "project_id", testutil.ProjectId),
resource.TestCheckResourceAttrPair(
"stackit_edgecloud_instance.test_instance", "instance_id",
"stackit_edgecloud_token.by_id", "instance_id",
),
resource.TestCheckResourceAttrSet("stackit_edgecloud_token.by_id", "token_id"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_token.by_id", "token"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_token.by_id", "expires_at"),
resource.TestCheckResourceAttrSet("stackit_edgecloud_token.by_id", "creation_time"),
resource.TestCheckResourceAttr("stackit_edgecloud_token.by_id", "region", testutil.Region),
),
},
// Update
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMax,
ConfigVariables: updatedVars,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "plan_id", testPlanIdUpdated),
resource.TestCheckResourceAttr("stackit_edgecloud_instance.test_instance", "description", testDescriptionUpdated),
),
},
// Import
{
ConfigVariables: updatedVars,
ResourceName: "stackit_edgecloud_instance.test_instance",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_edgecloud_instance.test_instance"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_edgecloud_instance.test_instance")
}
instanceID, ok := r.Primary.Attributes["instance_id"]
if !ok {
return "", fmt.Errorf("couldn't find attribute instance_id")
}
return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, testutil.Region, instanceID), nil
},
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccEdgeCloudInstance_validation(t *testing.T) {
validDisplayName := "mx-v-" + acctest.RandStringFromCharSet(2, acctest.CharSetAlpha)
tooShortDisplayName := "abc" // Invalid (3 chars)
tooLongDisplayName := "too-long-name" // Invalid (13 chars)
invalidUUID := "not-a-uuid"
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Display Name Too Short
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMin,
ConfigVariables: configVarsMax(tooShortDisplayName, testPlanId, testDescription, testExpiration, testRecreateBefore),
ExpectError: regexp.MustCompile(fmt.Sprintf(`string length must be between 4 and 8, got: %d`, len(tooShortDisplayName))),
},
// Display Name Too Long
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMin,
ConfigVariables: configVarsMax(tooLongDisplayName, testPlanId, testDescription, testExpiration, testRecreateBefore),
ExpectError: regexp.MustCompile(fmt.Sprintf(`string length must be between 4 and 8, got: %d`, len(tooLongDisplayName))),
},
// Invalid Project ID
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMin,
ConfigVariables: config.Variables{
"project_id": config.StringVariable(invalidUUID),
"region": config.StringVariable(testutil.Region),
"display_name": config.StringVariable(minTestName),
"plan_id": config.StringVariable(testPlanId),
},
ExpectError: regexp.MustCompile(fmt.Sprintf(`Attribute project_id value must be an UUID, got: %s`, invalidUUID)),
},
// Invalid Plan ID
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMin,
ConfigVariables: configVarsMax(validDisplayName, invalidUUID, testDescription, testExpiration, testRecreateBefore),
ExpectError: regexp.MustCompile(fmt.Sprintf(`Attribute plan_id value must be an UUID, got: %s`, invalidUUID)),
},
// Description Too Long
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMax,
ConfigVariables: configVarsMax(validDisplayName, testPlanId, acctest.RandString(257), testExpiration, testRecreateBefore),
ExpectError: regexp.MustCompile(`Attribute description string length must be at most 256`),
},
},
})
}
func TestAccEdgeCloudKubeconfigToken_validation(t *testing.T) {
displayName := "mx-v-" + acctest.RandStringFromCharSet(2, acctest.CharSetAlpha)
tooShortExpiration := 599
tooLongExpiration := 15552001
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Expiration too short
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMax,
ConfigVariables: configVarsMax(displayName, testPlanId, testDescription, tooShortExpiration, testRecreateBefore),
ExpectError: regexp.MustCompile(fmt.Sprintf(`Attribute expiration value must be between 600 and 15552000, got: %d`, tooShortExpiration)),
},
// Expiration Too Long
{
Config: testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig() + "\n" + resourceMax,
ConfigVariables: configVarsMax(displayName, testPlanId, testDescription, tooLongExpiration, testRecreateBefore),
ExpectError: regexp.MustCompile(fmt.Sprintf(`Attribute expiration value must be between 600 and 15552000, got: %d`, tooLongExpiration)),
},
},
})
}
// testAccCheckEdgeCloudInstanceDestroy verifies that test resources are properly destroyed
func testAccCheckEdgeCloudInstanceDestroy(s *terraform.State) error {
ctx := context.Background()
client, err := edge.NewAPIClient(testutil.NewConfigBuilder().BuildClientOptions(testutil.EdgeCloudCustomEndpoint, false)...)
if err != nil {
return fmt.Errorf("creating client: %w", err)
}
for _, rs := range s.RootModule().Resources {
if rs.Type != "stackit_edgecloud_instance" {
continue
}
idParts := strings.Split(rs.Primary.ID, core.Separator)
if len(idParts) != 3 {
return fmt.Errorf("invalid resource ID format: %s", rs.Primary.ID)
}
projectId, region, instanceId := idParts[0], idParts[1], idParts[2]
_, err := client.DefaultAPI.GetInstance(ctx, projectId, region, instanceId).Execute()
if err == nil {
return fmt.Errorf("edge instance %q still exists", instanceId)
}
// If the error is a 404, the resource was successfully deleted
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
if !ok || oapiErr.StatusCode != http.StatusNotFound {
err := client.DefaultAPI.DeleteInstance(ctx, projectId, region, instanceId).Execute()
if err != nil {
return fmt.Errorf("deleting instance %s during CheckDestroy: %w", instanceId, err)
}
_, err = wait.DeleteInstanceWaitHandler(ctx, client.DefaultAPI, projectId, region, instanceId).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("waiting for instance deletion %s during CheckDestroy: %w", instanceId, err)
}
}
}
return nil
}