-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathserverbackup_acc_test.go
More file actions
388 lines (359 loc) · 19.1 KB
/
Copy pathserverbackup_acc_test.go
File metadata and controls
388 lines (359 loc) · 19.1 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package serverbackup_test
import (
"context"
_ "embed"
"fmt"
"maps"
"regexp"
"strconv"
"strings"
"testing"
"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"
core_config "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/serverbackup"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
)
var (
//go:embed testdata/resource-min.tf
resourceMinConfig string
//go:embed testdata/resource-max.tf
resourceMaxConfig string
)
var testConfigVarsMin = config.Variables{
"project_id": config.StringVariable(testutil.ProjectId),
"schedule_name": config.StringVariable("tf-acc-" + acctest.RandStringFromCharSet(8, acctest.CharSetAlpha)),
"rrule": config.StringVariable("DTSTART;TZID=Europe/Sofia:20200803T023000 RRULE:FREQ=DAILY;INTERVAL=1"),
"enabled": config.BoolVariable(true),
"backup_name": config.StringVariable("tf-acc-" + acctest.RandStringFromCharSet(8, acctest.CharSetAlpha)),
"retention_period": config.IntegerVariable(14),
"server_name": config.StringVariable(fmt.Sprintf("tf-acc-%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))),
"network_name": config.StringVariable(fmt.Sprintf("tf-acc-%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))),
"machine_type": config.StringVariable("t1.1"),
// image needs to contain the STACKIT Server Agent
"image_id": config.StringVariable("fb5b3fa8-5e20-478a-929a-2b7da1676b18"),
}
var testConfigVarsMax = config.Variables{
"project_id": config.StringVariable(testutil.ProjectId),
"schedule_name": config.StringVariable("tf-acc-" + acctest.RandStringFromCharSet(8, acctest.CharSetAlpha)),
"rrule": config.StringVariable("DTSTART;TZID=Europe/Sofia:20200803T023000 RRULE:FREQ=DAILY;INTERVAL=1"),
"enabled": config.BoolVariable(true),
"backup_name": config.StringVariable("tf-acc-" + acctest.RandStringFromCharSet(8, acctest.CharSetAlpha)),
"retention_period": config.IntegerVariable(14),
"region": config.StringVariable("eu01"),
"server_name": config.StringVariable(fmt.Sprintf("tf-acc-%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))),
"network_name": config.StringVariable(fmt.Sprintf("tf-acc-%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))),
"machine_type": config.StringVariable("t1.1"),
// image needs to contain the STACKIT Server Agent
"image_id": config.StringVariable("fb5b3fa8-5e20-478a-929a-2b7da1676b18"),
}
func configVarsInvalid(vars config.Variables) config.Variables {
tempConfig := maps.Clone(vars)
tempConfig["retention_period"] = config.IntegerVariable(0)
return tempConfig
}
func configVarsMinUpdated() config.Variables {
tempConfig := maps.Clone(testConfigVarsMin)
tempConfig["retention_period"] = config.IntegerVariable(12)
tempConfig["rrule"] = config.StringVariable("DTSTART;TZID=Europe/Berlin:20250430T010000 RRULE:FREQ=DAILY;INTERVAL=3")
return tempConfig
}
func configVarsMaxUpdated() config.Variables {
tempConfig := maps.Clone(testConfigVarsMax)
tempConfig["retention_period"] = config.IntegerVariable(12)
tempConfig["rrule"] = config.StringVariable("DTSTART;TZID=Europe/Berlin:20250430T010000 RRULE:FREQ=DAILY;INTERVAL=3")
return tempConfig
}
func TestAccServerBackupScheduleMinResource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
CheckDestroy: resource.ComposeTestCheckFunc(
testAccCheckServerBackupScheduleDestroy,
testAccCheckServerDestroy,
),
Steps: []resource.TestStep{
// Creation fail
{
Config: testutil.ServerBackupProviderConfig() + "\n" + resourceMinConfig,
ConfigVariables: configVarsInvalid(testConfigVarsMin),
ExpectError: regexp.MustCompile(`.*backup_properties.retention_period value must be at least 1*`),
},
// Creation
{
Config: testutil.ServerBackupProviderConfig() + "\n" + resourceMinConfig,
ConfigVariables: testConfigVarsMin,
Check: resource.ComposeAggregateTestCheckFunc(
// Backup schedule data
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "project_id", testutil.ConvertConfigVariable(testConfigVarsMin["project_id"])),
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "backup_schedule_id"),
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "id"),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "name", testutil.ConvertConfigVariable(testConfigVarsMin["schedule_name"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "rrule", testutil.ConvertConfigVariable(testConfigVarsMin["rrule"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "enabled", strconv.FormatBool(true)),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "backup_properties.name", testutil.ConvertConfigVariable(testConfigVarsMin["backup_name"])),
// server
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "server_id"),
),
},
// data source
{
Config: testutil.ServerBackupProviderConfig() + "\n" + resourceMinConfig,
ConfigVariables: testConfigVarsMin,
Check: resource.ComposeAggregateTestCheckFunc(
// Server backup schedule data
resource.TestCheckResourceAttr("data.stackit_server_backup_schedule.schedule_data_test", "project_id", testutil.ConvertConfigVariable(testConfigVarsMin["project_id"])),
resource.TestCheckResourceAttrSet("data.stackit_server_backup_schedule.schedule_data_test", "backup_schedule_id"),
resource.TestCheckResourceAttrSet("data.stackit_server_backup_schedule.schedule_data_test", "id"),
resource.TestCheckResourceAttr("data.stackit_server_backup_schedule.schedule_data_test", "name", testutil.ConvertConfigVariable(testConfigVarsMin["schedule_name"])),
resource.TestCheckResourceAttr("data.stackit_server_backup_schedule.schedule_data_test", "rrule", testutil.ConvertConfigVariable(testConfigVarsMin["rrule"])),
resource.TestCheckResourceAttr("data.stackit_server_backup_schedule.schedule_data_test", "enabled", strconv.FormatBool(true)),
resource.TestCheckResourceAttr("data.stackit_server_backup_schedule.schedule_data_test", "backup_properties.name", testutil.ConvertConfigVariable(testConfigVarsMin["backup_name"])),
// Server backup schedules data
resource.TestCheckResourceAttr("data.stackit_server_backup_schedules.schedules_data_test", "project_id", testutil.ConvertConfigVariable(testConfigVarsMin["project_id"])),
resource.TestCheckResourceAttrSet("data.stackit_server_backup_schedules.schedules_data_test", "id"),
resource.TestCheckResourceAttrSet("data.stackit_server_backup_schedules.schedules_data_test", "server_id"),
),
},
// Import
{
ResourceName: "stackit_server_backup_schedule.test_schedule",
ConfigVariables: testConfigVarsMin,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_server_backup_schedule.test_schedule"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_server_backup_schedule.test_schedule")
}
scheduleId, ok := r.Primary.Attributes["backup_schedule_id"]
if !ok {
return "", fmt.Errorf("couldn't find attribute backup_schedule_id")
}
serverId, ok := r.Primary.Attributes["server_id"]
if !ok {
return "", fmt.Errorf("couldn't find attribute server_id")
}
return fmt.Sprintf("%s,%s,%s,%s", testutil.ProjectId, testutil.Region, serverId, scheduleId), nil
},
ImportState: true,
ImportStateVerify: true,
},
// Update
{
Config: testutil.ServerBackupProviderConfig() + "\n" + resourceMinConfig,
ConfigVariables: configVarsMinUpdated(),
Check: resource.ComposeAggregateTestCheckFunc(
// Backup schedule data
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "project_id", testutil.ConvertConfigVariable(configVarsMinUpdated()["project_id"])),
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "backup_schedule_id"),
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "id"),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "name", testutil.ConvertConfigVariable(configVarsMinUpdated()["schedule_name"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "rrule", testutil.ConvertConfigVariable(configVarsMinUpdated()["rrule"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "enabled", testutil.ConvertConfigVariable(configVarsMinUpdated()["enabled"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "backup_properties.retention_period", testutil.ConvertConfigVariable(configVarsMinUpdated()["retention_period"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "backup_properties.name", testutil.ConvertConfigVariable(configVarsMinUpdated()["backup_name"])),
// server
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "server_id"),
),
},
// Deletion is done by the framework implicitly
},
})
}
func TestAccServerBackupScheduleMaxResource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
CheckDestroy: resource.ComposeTestCheckFunc(
testAccCheckServerBackupScheduleDestroy,
testAccCheckServerDestroy,
),
Steps: []resource.TestStep{
// Creation fail
{
Config: testutil.ServerBackupProviderConfig() + "\n" + resourceMaxConfig,
ConfigVariables: configVarsInvalid(testConfigVarsMax),
ExpectError: regexp.MustCompile(`.*backup_properties.retention_period value must be at least 1*`),
},
// Creation
{
Config: testutil.ServerBackupProviderConfig() + "\n" + resourceMaxConfig,
ConfigVariables: testConfigVarsMax,
Check: resource.ComposeAggregateTestCheckFunc(
// Backup schedule data
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "project_id", testutil.ConvertConfigVariable(testConfigVarsMax["project_id"])),
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "backup_schedule_id"),
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "id"),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "name", testutil.ConvertConfigVariable(testConfigVarsMax["schedule_name"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "rrule", testutil.ConvertConfigVariable(testConfigVarsMax["rrule"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "enabled", strconv.FormatBool(true)),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "backup_properties.name", testutil.ConvertConfigVariable(testConfigVarsMax["backup_name"])),
// server
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "server_id"),
),
},
// data source
{
Config: testutil.ServerBackupProviderConfig() + "\n" + resourceMaxConfig,
ConfigVariables: testConfigVarsMax,
Check: resource.ComposeAggregateTestCheckFunc(
// Server backup schedule data
resource.TestCheckResourceAttr("data.stackit_server_backup_schedule.schedule_data_test", "project_id", testutil.ConvertConfigVariable(testConfigVarsMax["project_id"])),
resource.TestCheckResourceAttrSet("data.stackit_server_backup_schedule.schedule_data_test", "backup_schedule_id"),
resource.TestCheckResourceAttrSet("data.stackit_server_backup_schedule.schedule_data_test", "id"),
resource.TestCheckResourceAttr("data.stackit_server_backup_schedule.schedule_data_test", "name", testutil.ConvertConfigVariable(testConfigVarsMax["schedule_name"])),
resource.TestCheckResourceAttr("data.stackit_server_backup_schedule.schedule_data_test", "rrule", testutil.ConvertConfigVariable(testConfigVarsMax["rrule"])),
resource.TestCheckResourceAttr("data.stackit_server_backup_schedule.schedule_data_test", "enabled", strconv.FormatBool(true)),
resource.TestCheckResourceAttr("data.stackit_server_backup_schedule.schedule_data_test", "backup_properties.name", testutil.ConvertConfigVariable(testConfigVarsMax["backup_name"])),
// Server backup schedules data
resource.TestCheckResourceAttr("data.stackit_server_backup_schedules.schedules_data_test", "project_id", testutil.ConvertConfigVariable(testConfigVarsMax["project_id"])),
resource.TestCheckResourceAttrSet("data.stackit_server_backup_schedules.schedules_data_test", "id"),
resource.TestCheckResourceAttrSet("data.stackit_server_backup_schedules.schedules_data_test", "server_id"),
),
},
// Import
{
ResourceName: "stackit_server_backup_schedule.test_schedule",
ConfigVariables: testConfigVarsMax,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
r, ok := s.RootModule().Resources["stackit_server_backup_schedule.test_schedule"]
if !ok {
return "", fmt.Errorf("couldn't find resource stackit_server_backup_schedule.test_schedule")
}
scheduleId, ok := r.Primary.Attributes["backup_schedule_id"]
if !ok {
return "", fmt.Errorf("couldn't find attribute backup_schedule_id")
}
serverId, ok := r.Primary.Attributes["server_id"]
if !ok {
return "", fmt.Errorf("couldn't find attribute server_id")
}
return fmt.Sprintf("%s,%s,%s,%s", testutil.ProjectId, testutil.Region, serverId, scheduleId), nil
},
ImportState: true,
ImportStateVerify: true,
},
// Update
{
Config: testutil.ServerBackupProviderConfig() + "\n" + resourceMaxConfig,
ConfigVariables: configVarsMaxUpdated(),
Check: resource.ComposeAggregateTestCheckFunc(
// Backup schedule data
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "project_id", testutil.ConvertConfigVariable(configVarsMaxUpdated()["project_id"])),
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "backup_schedule_id"),
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "id"),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "name", testutil.ConvertConfigVariable(configVarsMaxUpdated()["schedule_name"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "rrule", testutil.ConvertConfigVariable(configVarsMaxUpdated()["rrule"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "enabled", testutil.ConvertConfigVariable(configVarsMaxUpdated()["enabled"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "backup_properties.retention_period", testutil.ConvertConfigVariable(configVarsMaxUpdated()["retention_period"])),
resource.TestCheckResourceAttr("stackit_server_backup_schedule.test_schedule", "backup_properties.name", testutil.ConvertConfigVariable(configVarsMaxUpdated()["backup_name"])),
// server
resource.TestCheckResourceAttrSet("stackit_server_backup_schedule.test_schedule", "server_id"),
),
},
// Deletion is done by the framework implicitly
},
})
}
func testAccCheckServerBackupScheduleDestroy(s *terraform.State) error {
ctx := context.Background()
var client *serverbackup.APIClient
var err error
if testutil.ServerBackupCustomEndpoint == "" {
client, err = serverbackup.NewAPIClient()
} else {
client, err = serverbackup.NewAPIClient(
core_config.WithEndpoint(testutil.ServerBackupCustomEndpoint),
)
}
if err != nil {
return fmt.Errorf("creating serverbackup client: %w", err)
}
var serverId string
for _, rs := range s.RootModule().Resources {
if rs.Type == "stackit_server" {
// server terraform ID: "[project_id],[region],[server_id]"
serverId = strings.Split(rs.Primary.ID, core.Separator)[2]
break
}
}
if serverId == "" {
return fmt.Errorf("could not find server ID in state")
}
schedulesToDestroy := []string{}
for _, rs := range s.RootModule().Resources {
if rs.Type != "stackit_server_backup_schedule" {
continue
}
// server backup schedule terraform ID: "[project_id],[server_id],[backup_schedule_id]"
scheduleId := strings.Split(rs.Primary.ID, core.Separator)[3]
schedulesToDestroy = append(schedulesToDestroy, scheduleId)
}
schedulesResp, err := client.ListBackupSchedules(ctx, testutil.ProjectId, serverId, testutil.Region).Execute()
// The destroy functions are called after all resources are cleaned up.
// If the server was successfully destroyed we should see a 404 here.
if err != nil {
if strings.Contains(err.Error(), "404") || strings.Contains(err.Error(), "Server not found") {
return nil
}
return fmt.Errorf("getting schedulesResp: %w", err)
}
schedules := *schedulesResp.Items
for i := range schedules {
if schedules[i].Id == nil {
continue
}
scheduleId := strconv.FormatInt(*schedules[i].Id, 10)
if utils.Contains(schedulesToDestroy, scheduleId) {
err := client.DeleteBackupScheduleExecute(ctx, testutil.ProjectId, serverId, scheduleId, testutil.Region)
if err != nil {
return fmt.Errorf("destroying server backup schedule %s during CheckDestroy: %w", scheduleId, err)
}
}
}
return nil
}
// Additional function to check if the server was deleted if something went wrong in the first case.
func testAccCheckServerDestroy(s *terraform.State) error {
ctx := context.Background()
var client *iaas.APIClient
var err error
if testutil.IaaSCustomEndpoint == "" {
client, err = iaas.NewAPIClient()
} else {
client, err = iaas.NewAPIClient(
core_config.WithEndpoint(testutil.ServerBackupCustomEndpoint),
)
}
if err != nil {
return fmt.Errorf("creating client: %w", err)
}
serversToDestroy := []string{}
for _, rs := range s.RootModule().Resources {
if rs.Type != "stackit_server" {
continue
}
// server terraform ID: "[project_id],[region],[server_id]"
serverId := strings.Split(rs.Primary.ID, core.Separator)[2]
serversToDestroy = append(serversToDestroy, serverId)
}
serversResp, err := client.ListServersExecute(ctx, testutil.ProjectId, testutil.Region)
if err != nil {
return fmt.Errorf("getting serversResp: %w", err)
}
servers := *serversResp.Items
for i := range servers {
if servers[i].Id == nil {
continue
}
if utils.Contains(serversToDestroy, *servers[i].Id) {
err := client.DeleteServerExecute(ctx, testutil.ProjectId, testutil.Region, *servers[i].Id)
if err != nil {
return fmt.Errorf("destroying server %s during CheckDestroy: %w", *servers[i].Id, err)
}
}
}
return nil
}