Skip to content

Commit e015a11

Browse files
committed
added unit test for filterBootDiskFromDisks function
1 parent 0457797 commit e015a11

2 files changed

Lines changed: 87 additions & 5 deletions

File tree

internal/provider/resource_instance.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,14 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
406406
}
407407
params.Body.AntiAffinityGroups = antiAffinityGroupIDs
408408

409-
// The control plane API counts the BootDisk and the Disk attachments when it calculates the limit on disk attachments.
410-
// If bootdisk is set explicitly, we don't want it to be in the API call, but we need it in the state entry.
411409
disks, diags := newDiskAttachmentsOnCreate(ctx, r.client, plan.DiskAttachments)
412410
resp.Diagnostics.Append(diags...)
413411
if resp.Diagnostics.HasError() {
414412
return
415413
}
414+
415+
// The control plane API counts the BootDisk and the Disk attachments when it calculates the limit on disk attachments.
416+
// If bootdisk is set explicitly, we don't want it to be in the API call, but we need it in the state entry.
416417
params.Body.Disks = filterBootDiskFromDisks(disks, params.Body.BootDisk)
417418

418419
externalIPs := newExternalIPsOnCreate(plan.ExternalIPs)

internal/provider/resource_instance_test.go

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package provider
77
import (
88
"context"
99
"fmt"
10+
"reflect"
1011
"testing"
1112
"time"
1213

@@ -382,13 +383,13 @@ func TestAccCloudResourceInstance_nic(t *testing.T) {
382383
data "oxide_project" "{{.SupportBlockName}}" {
383384
name = "tf-acc-test"
384385
}
385-
386+
386387
data "oxide_vpc_subnet" "{{.SubnetBlockName}}" {
387388
project_name = data.oxide_project.{{.SupportBlockName}}.name
388389
vpc_name = "default"
389390
name = "default"
390391
}
391-
392+
392393
resource "oxide_instance" "{{.BlockName}}" {
393394
project_id = data.oxide_project.{{.SupportBlockName}}.id
394395
description = "a test instance"
@@ -412,7 +413,7 @@ resource "oxide_instance" "{{.BlockName}}" {
412413
data "oxide_project" "{{.SupportBlockName}}" {
413414
name = "tf-acc-test"
414415
}
415-
416+
416417
resource "oxide_instance" "{{.BlockName}}" {
417418
project_id = data.oxide_project.{{.SupportBlockName}}.id
418419
description = "a test instance"
@@ -1227,3 +1228,83 @@ func testAccInstanceDestroy(s *terraform.State) error {
12271228

12281229
return nil
12291230
}
1231+
1232+
func TestFilterBootDiskFromDisks(t *testing.T) {
1233+
boot_disk := oxide.InstanceDiskAttachment{
1234+
Name: "testboot01",
1235+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1236+
}
1237+
1238+
tests := []struct {
1239+
boot_disk oxide.InstanceDiskAttachment
1240+
disks []oxide.InstanceDiskAttachment
1241+
want []oxide.InstanceDiskAttachment
1242+
}{
1243+
{
1244+
boot_disk: boot_disk,
1245+
disks: []oxide.InstanceDiskAttachment{
1246+
boot_disk,
1247+
{
1248+
Name: "testdisk01",
1249+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1250+
},
1251+
{
1252+
Name: "testdisk01",
1253+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1254+
},
1255+
},
1256+
want: []oxide.InstanceDiskAttachment{
1257+
{
1258+
Name: "testdisk01",
1259+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1260+
},
1261+
{
1262+
Name: "testdisk01",
1263+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1264+
},
1265+
},
1266+
},
1267+
{
1268+
boot_disk: oxide.InstanceDiskAttachment{},
1269+
disks: []oxide.InstanceDiskAttachment{
1270+
{
1271+
Name: "testdisk01",
1272+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1273+
},
1274+
{
1275+
Name: "testdisk01",
1276+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1277+
},
1278+
{
1279+
Name: "testboot01",
1280+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1281+
},
1282+
},
1283+
want: []oxide.InstanceDiskAttachment{
1284+
{
1285+
Name: "testdisk01",
1286+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1287+
},
1288+
{
1289+
Name: "testdisk01",
1290+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1291+
},
1292+
{
1293+
Name: "testboot01",
1294+
Type: oxide.InstanceDiskAttachmentTypeAttach,
1295+
},
1296+
},
1297+
},
1298+
{
1299+
boot_disk: oxide.InstanceDiskAttachment{},
1300+
disks: []oxide.InstanceDiskAttachment{},
1301+
want: []oxide.InstanceDiskAttachment{},
1302+
},
1303+
}
1304+
for _, tt := range tests {
1305+
disks := filterBootDiskFromDisks(tt.disks, &tt.boot_disk)
1306+
if !reflect.DeepEqual(disks, tt.want) {
1307+
t.Errorf("want: %+v, got: %+v", tt.want, disks)
1308+
}
1309+
}
1310+
}

0 commit comments

Comments
 (0)