Skip to content

Commit

Permalink
chore: tidies and documents test
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdemendonca committed Feb 21, 2025
1 parent e99d032 commit a288a5a
Showing 1 changed file with 50 additions and 17 deletions.
67 changes: 50 additions & 17 deletions maas/resource_maas_subnet_ip_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,80 @@ import (
)

func TestAccResourceMaasSubnetIPRange_basic(t *testing.T) {
// Setup ip range attrs
// Setup ip range parameters
var ipRange entity.IPRange

subnet_name := acctest.RandomWithPrefix("test-subnet")
ip_range_attr := "maas_subnet_ip_range.test_ip_range"
ipRangeAttrName := "maas_subnet_ip_range.test_ip_range"
range_type := "reserved"
comment := "test-comment"
start_ip := "10.88.88.1"
end_ip := "10.88.88.50"

pre_check := func() { testutils.PreCheck(t, nil) }
ipStart := "10.88.88.1"
ipEnd := "10.88.88.50"
ipStartMod := "10.88.88.2"
ipEndMod := "10.88.88.49"
commentMod := "a-different-comment"

// Check functions
checks := []resource.TestCheckFunc{
testAccMAASSubnetIPRangeCheckExists(ip_range_attr, &ipRange),
resource.TestCheckResourceAttr(ip_range_attr, "type", range_type),
resource.TestCheckResourceAttr(ip_range_attr, "comment", comment),
resource.TestCheckResourceAttr(ip_range_attr, "start_ip", start_ip),
resource.TestCheckResourceAttr(ip_range_attr, "end_ip", end_ip),
testAccMAASSubnetIPRangeCheckExists(ipRangeAttrName, &ipRange),
resource.TestCheckResourceAttr(ipRangeAttrName, "type", range_type),
resource.TestCheckResourceAttr(ipRangeAttrName, "comment", comment),
resource.TestCheckResourceAttr(ipRangeAttrName, "start_ip", ipStart),
resource.TestCheckResourceAttr(ipRangeAttrName, "end_ip", ipEnd),
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: pre_check,
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.TestAccProviders,
CheckDestroy: testAccCheckMAASSubnetIPRangeDestroy,
ErrorCheck: func(err error) error { return err },
Steps: []resource.TestStep{
// Test create
// Test creation
{
Config: testAccSubnetIPRangeExampleResource(
subnet_name,
range_type,
comment,
ipStart,
ipEnd,
),
Check: resource.ComposeTestCheckFunc(checks...),
},
// Test if resource drift is detected by modifying the IP range using the
// GO MAAS client, and then checking if the state is updated correctly on
// refresh
{
Config: testAccSubnetIPRangeExampleResource(subnet_name, range_type, comment, start_ip, end_ip),
Check: resource.ComposeTestCheckFunc(checks...),
PreConfig: func() {
client := testutils.TestAccProvider.Meta().(*maas.ClientConfig).Client
params := entity.IPRangeParams{
Type: range_type,
Comment: commentMod,
StartIP: ipStartMod,
EndIP: ipEndMod,
Subnet: strconv.Itoa(ipRange.Subnet.ID),
}
// Update the IP range to changed values that should be read into state
_, err := client.IPRange.Update(ipRange.ID, &params)
if err != nil {
t.Fatalf("Failed to update IP range: %s", err)
}
},
RefreshState: true,
ExpectNonEmptyPlan: true,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(ipRangeAttrName, "start_ip", ipStartMod),
),
},
// Test import
{
ResourceName: ip_range_attr,
ResourceName: ipRangeAttrName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

// Check if the IP range specified actually exists in MAAS
func testAccMAASSubnetIPRangeCheckExists(rn string, ipRange *entity.IPRange) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn]
Expand Down

0 comments on commit a288a5a

Please sign in to comment.