From 2a4e4f3c411e875cb2aadc0ed9e200a1b8f33676 Mon Sep 17 00:00:00 2001 From: Chaithra Date: Thu, 24 Oct 2024 10:12:12 +0530 Subject: [PATCH] test cases for host record and network container for next available IP (#398) * test cases for host record and network container for next available IP * revert the changes for cidr and added IPV6 test case for network container --- .../resource_infoblox_ip_allocation_test.go | 175 +++++++++++++++++- ...esource_infoblox_network_container_test.go | 123 +++++++++++- 2 files changed, 293 insertions(+), 5 deletions(-) diff --git a/infoblox/resource_infoblox_ip_allocation_test.go b/infoblox/resource_infoblox_ip_allocation_test.go index 2d62d5015..9592e9fd5 100644 --- a/infoblox/resource_infoblox_ip_allocation_test.go +++ b/infoblox/resource_infoblox_ip_allocation_test.go @@ -2,13 +2,13 @@ package infoblox import ( "fmt" - "github.com/infobloxopen/infoblox-go-client/v2/utils" - "sort" - "testing" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ibclient "github.com/infobloxopen/infoblox-go-client/v2" + "github.com/infobloxopen/infoblox-go-client/v2/utils" + "regexp" + "sort" + "testing" ) type v4addrsType []ibclient.HostRecordIpv4Addr @@ -850,3 +850,170 @@ func testAccCheckIPAllocationDestroy(s *terraform.State) error { } return nil } + +var testResourceIPAllocationIPV4 = `resource "infoblox_ipv4_network" "ipv4_network12" { + cidr = "189.11.0.0/24" + network_view = "default" + comment = "small network for testing" + ext_attrs = jsonencode({ + "Site" = "Darjeeling" + }) +} +resource "infoblox_zone_auth" "zone" { + fqdn = "test.com" + } +resource "infoblox_ip_allocation" "ipv4"{ + fqdn = "testhostnameip5.test.com" + comment = "host record created" + filter_params = jsonencode({ + "*Site" = "Darjeeling" + }) + ext_attrs = jsonencode({ + Site = "Europe" + }) + ip_address_type="IPV4" + depends_on = [infoblox_ipv4_network.ipv4_network12,infoblox_zone_auth.zone] +}` +var testResourceIPAllocationIPV6 = `resource "infoblox_ipv6_network" "ipv6_network12" { + cidr = "2002:1f93:0:4::/96" + network_view = "default" + comment = "small network for testing" + ext_attrs = jsonencode({ + "Site" = "Bengaluru" + }) +} +resource "infoblox_zone_auth" "zone" { + fqdn = "test.com" + } +resource "infoblox_ip_allocation" "ipv6"{ + fqdn = "testhostnameip6.test.com" + comment = "host record created" + filter_params = jsonencode({ + "*Site" = "Bengaluru" + }) + ext_attrs = jsonencode({ + Site = "Europe" + }) + ip_address_type="IPV6" + depends_on = [infoblox_ipv6_network.ipv6_network12,infoblox_zone_auth.zone] +}` +var testResourceIPAllocationIPV6IPV4 = `resource "infoblox_ipv6_network" "ipv6_network12" { + cidr = "2002:1f93:0:4::/96" + network_view = "default" + comment = "small network for testing" + ext_attrs = jsonencode({ + "Site" = "Bengaluru" + }) +} +resource "infoblox_ipv4_network" "ipv4_network12" { + cidr = "189.11.0.0/24" + network_view = "default" + comment = "small network for testing" + ext_attrs = jsonencode({ + "Site" = "Bengaluru" + }) +} +resource "infoblox_zone_auth" "zone" { + fqdn = "test.com" + } +resource "infoblox_ip_allocation" "both"{ + fqdn = "testhostnameip7.test.com" + comment = "host record created" + filter_params = jsonencode({ + "*Site" = "Bengaluru" + }) + ext_attrs = jsonencode({ + Site = "Europe" + }) + ip_address_type="Both" + depends_on = [infoblox_ipv6_network.ipv6_network12,infoblox_zone_auth.zone,infoblox_ipv4_network.ipv4_network12] +}` + +func TestAcc_resourceNetwork_AllocateNetworkByEA_IPV4_IP_Allocation(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckNetworkDestroy, + Steps: []resource.TestStep{ + { + Config: testResourceIPAllocationIPV4, + Check: resource.ComposeTestCheckFunc( + validateIPAllocation( + "infoblox_ip_allocation.ipv4", + &ibclient.HostRecord{ + NetworkView: "default", + View: utils.StringPtr("default"), + EnableDns: utils.BoolPtr(true), + Name: utils.StringPtr("testhostnameip5.test.com"), + Ipv4Addrs: []ibclient.HostRecordIpv4Addr{*ibclient.NewHostRecordIpv4Addr("189.11.0.1", "", false, "")}, + UseTtl: utils.BoolPtr(false), + Comment: utils.StringPtr("host record created"), + Ea: ibclient.EA{ + "Site": "Europe", + }, + }, + ), + ), + }, + { + Config: testResourceIPAllocationIPV6, + Check: resource.ComposeTestCheckFunc( + validateIPAllocation( + "infoblox_ip_allocation.ipv6", + &ibclient.HostRecord{ + NetworkView: "default", + View: utils.StringPtr("default"), + EnableDns: utils.BoolPtr(true), + Name: utils.StringPtr("testhostnameip6.test.com"), + Ipv6Addrs: []ibclient.HostRecordIpv6Addr{*ibclient.NewHostRecordIpv6Addr("2002:1f93:0:4::1", "", false, "")}, + UseTtl: utils.BoolPtr(false), + Comment: utils.StringPtr("host record created"), + Ea: ibclient.EA{ + "Site": "Europe", + }, + }, + ), + ), + }, + { + Config: testResourceIPAllocationIPV6IPV4, + Check: resource.ComposeTestCheckFunc( + validateIPAllocation( + "infoblox_ip_allocation.both", + &ibclient.HostRecord{ + NetworkView: "default", + View: utils.StringPtr("default"), + EnableDns: utils.BoolPtr(true), + Name: utils.StringPtr("testhostnameip7.test.com"), + Ipv4Addrs: []ibclient.HostRecordIpv4Addr{*ibclient.NewHostRecordIpv4Addr("189.11.0.1", "", false, "")}, + Ipv6Addrs: []ibclient.HostRecordIpv6Addr{*ibclient.NewHostRecordIpv6Addr("2002:1f93:0:4::1", "", false, "")}, + UseTtl: utils.BoolPtr(false), + Comment: utils.StringPtr("host record created"), + Ea: ibclient.EA{ + "Site": "Europe", + }, + }, + )), + }, + { + // Negative testcase + Config: ` + resource "infoblox_zone_auth" "zone" { + fqdn = "test.com" + } + resource "infoblox_ip_allocation" "ipv4_1"{ + fqdn = "testhostnameip6.test.com" + network_view="default" + comment = "host record created" + filter_params = jsonencode({ + "*Site" = "Finland" + }) + ext_attrs = jsonencode({ + Site = "Europe" + }) + }`, + ExpectError: regexp.MustCompile("did not return any result"), + }, + }, + }) +} diff --git a/infoblox/resource_infoblox_network_container_test.go b/infoblox/resource_infoblox_network_container_test.go index b77d42ba6..bd16db9f5 100644 --- a/infoblox/resource_infoblox_network_container_test.go +++ b/infoblox/resource_infoblox_network_container_test.go @@ -77,7 +77,6 @@ func validateNetworkContainer( "the value of 'cidr' field is empty, but expected some value") } } - if nc.Cidr != expCidr { return fmt.Errorf( "the value of 'cidr' field is '%s', but expected '%s'", @@ -551,6 +550,128 @@ func TestAcc_resourceNetworkContainer_ipv4_ea_inheritance(t *testing.T) { }) } +var testResourceIPv4NetworkContainer = `resource "infoblox_ipv4_network_container" "ipv4_network12" { + cidr = "25.11.0.0/24" + network_view = "default" + comment = "small network for testing" + ext_attrs = jsonencode({ + "Site" = "Darjeeling" + }) +} +resource "infoblox_ipv4_network_container" "ipv4_network13"{ + allocate_prefix_len = 26 + comment = "network container created" + filter_params = jsonencode({ + "*Site" = "Darjeeling" + }) + ext_attrs = jsonencode({ + Site = "Europe" + }) + depends_on = [infoblox_ipv4_network_container.ipv4_network12] +}` + +func TestAcc_resourceNetworkContainer_AllocateNetworkByEA_IPV4(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckNetworkDestroy, + Steps: []resource.TestStep{ + { + Config: testResourceIPv4NetworkContainer, + Check: resource.ComposeTestCheckFunc( + validateNetworkContainer( + "infoblox_ipv4_network_container.ipv4_network13", + &ibclient.NetworkContainer{ + NetviewName: "default", + Comment: "network container created", + Ea: ibclient.EA{ + "Site": "Europe", + }, + }, + ), + ), + }, + { + // Negative testcase + Config: ` + resource "infoblox_ipv4_network_container" "ipv4_network14"{ + network_view="default" + comment = "network created" + allocate_prefix_len = 26 + filter_params = jsonencode({ + "*Site" = "Finland" + }) + ext_attrs = jsonencode({ + Site = "Europe" + }) + }`, + ExpectError: regexp.MustCompile("did not return any result"), + }, + }, + }) +} + +var testResourceIPv6NetworkContainer = `resource "infoblox_ipv6_network_container" "ipv6_network12" { + cidr = "002:1f93:0:2::/96" + network_view = "default" + comment = "small network for testing" + ext_attrs = jsonencode({ + "Site" = "Darjeeling" + }) +} +resource "infoblox_ipv6_network_container" "ipv6_network13"{ + allocate_prefix_len = 97 + comment = "network container created" + filter_params = jsonencode({ + "*Site" = "Darjeeling" + }) + ext_attrs = jsonencode({ + Site = "Europe" + }) + depends_on = [infoblox_ipv6_network_container.ipv6_network12] +}` + +func TestAcc_resourceNetworkContainer_AllocateNetworkByEA_IPV6(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckNetworkDestroy, + Steps: []resource.TestStep{ + { + Config: testResourceIPv6NetworkContainer, + Check: resource.ComposeTestCheckFunc( + validateNetworkContainer( + "infoblox_ipv6_network_container.ipv6_network13", + &ibclient.NetworkContainer{ + NetviewName: "default", + Comment: "network container created", + Ea: ibclient.EA{ + "Site": "Europe", + }, + }, + ), + ), + }, + { + // Negative testcase + Config: ` + resource "infoblox_ipv6_network_container" "ipv6_network14"{ + network_view="default" + comment = "network created" + allocate_prefix_len = 97 + filter_params = jsonencode({ + "*Site" = "Finland" + }) + ext_attrs = jsonencode({ + Site = "Europe" + }) + }`, + ExpectError: regexp.MustCompile("did not return any result"), + }, + }, + }) +} + func TestAcc_resourceNetworkContainer_ipv6(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) },