Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases for a record next-available-ip #391

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion infoblox/resource_infoblox_a_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func resourceARecordCreate(d *schema.ResourceData, m interface{}) error {
if err != nil {
return fmt.Errorf("error unmarshalling extra attributes of network container: %s", err)
}
rec, err := objMgr.AllocateNextAvailableIp(fqdn, "record:a", eaMap, nil, false, false, extAttrs, comment, false, nil)
rec, err := objMgr.AllocateNextAvailableIp(fqdn, "record:a", eaMap, nil, false, extAttrs, comment, false, nil, "IPV4")
if err != nil {
return fmt.Errorf("error allocating next available IP: %w", err)
}
Expand Down
80 changes: 66 additions & 14 deletions infoblox/resource_infoblox_a_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func testAccARecordCompare(
resPath string,
expectedRec *ibclient.RecordA,
notExpectedIpAddr string,
expectedCidr string) resource.TestCheckFunc {
expectedCidr string,
expectedFilterParams string) resource.TestCheckFunc {

return func(s *terraform.State) error {
res, found := s.RootModule().Resources[resPath]
Expand Down Expand Up @@ -148,13 +149,26 @@ func testAccARecordCompare(
return fmt.Errorf("'comment' is expected to be defined but it is not")
}

actualFilterParams, exists := res.Primary.Attributes["filter_params"]
if expectedFilterParams != "" {
if !exists {
return fmt.Errorf("'filter_params' is expected to be defined but it is not")
}
if actualFilterParams != expectedFilterParams {
return fmt.Errorf(
"'filter_params' does not match: got '%s', expected '%s'",
actualFilterParams, expectedFilterParams)
}
} else if exists {
return fmt.Errorf("'filter_params' is expected to be undefined but it is not")
}
return validateEAs(rec.Ea, expectedRec.Ea)
}
}

var (
regexpRequiredMissingIPv4 = regexp.MustCompile("either of 'ip_addr' and 'cidr' values is required")
regexpCidrIpAddrConflictIPv4 = regexp.MustCompile("only one of 'ip_addr' and 'cidr' values is allowed to be defined")
regexpRequiredMissingIPv4 = regexp.MustCompile("either of 'ip_addr' or 'cidr' or 'filter_values' values is required")
regexpCidrIpAddrConflictIPv4 = regexp.MustCompile("only one of 'ip_addr' or 'cidr' or 'filter_values' values is allowed to be defined")

regexpUpdateConflictIPv4 = regexp.MustCompile("only one of 'ip_addr' and 'cidr' values is allowed to update")
regexpNetviewUpdateNotAllowed = regexp.MustCompile("changing the value of 'network_view' field is not allowed")
Expand Down Expand Up @@ -225,7 +239,7 @@ func TestAccResourceARecord(t *testing.T) {
UseTtl: utils.BoolPtr(false),
Comment: nil,
Ea: nil,
}, "", ""),
}, "", "", ""),
),
},
{
Expand Down Expand Up @@ -262,7 +276,7 @@ func TestAccResourceARecord(t *testing.T) {
"Location": "New York",
"Site": "HQ",
},
}, "", ""),
}, "", "", ""),
),
},
{
Expand All @@ -289,7 +303,7 @@ func TestAccResourceARecord(t *testing.T) {
Ttl: utils.Uint32Ptr(155),
UseTtl: utils.BoolPtr(true),
Comment: utils.StringPtr("test comment 2"),
}, "", ""),
}, "", "", ""),
),
},
{
Expand All @@ -312,7 +326,7 @@ func TestAccResourceARecord(t *testing.T) {
Name: utils.StringPtr("name3.test.com"),
View: "nondefault_view",
UseTtl: utils.BoolPtr(false),
}, "", ""),
}, "", "", ""),
),
},
{
Expand All @@ -339,7 +353,7 @@ func TestAccResourceARecord(t *testing.T) {
Name: utils.StringPtr("name3.test.com"),
View: "nondefault_view",
UseTtl: utils.BoolPtr(false),
}, "10.10.0.1", "10.20.30.0/24"),
}, "10.10.0.1", "10.20.30.0/24", ""),
),
},
{
Expand Down Expand Up @@ -388,7 +402,7 @@ func TestAccResourceARecord(t *testing.T) {
Name: utils.StringPtr("name3.test.com"),
View: "nondefault_view",
UseTtl: utils.BoolPtr(false),
}, "", "10.20.33.0/24"),
}, "", "10.20.33.0/24", ""),
),
},
{
Expand Down Expand Up @@ -433,7 +447,7 @@ func TestAccResourceARecord(t *testing.T) {
Name: utils.StringPtr("name3.test.com"),
View: "nondefault_view",
UseTtl: utils.BoolPtr(false),
}, "", ""),
}, "", "", ""),
),
},
{
Expand All @@ -449,6 +463,44 @@ func TestAccResourceARecord(t *testing.T) {
}`),
ExpectError: regexpDnsviewUpdateNotAllowed,
},
{
Config: fmt.Sprintf(`
resource "infoblox_zone_auth" "zone1" {
fqdn = "test1.com"
}
resource "infoblox_ipv4_network" "net2" {
cidr = "10.1.0.0/24"
ext_attrs = jsonencode({
"Site" = "Blr"
})
}
resource "infoblox_a_record" "rec4" {
fqdn = "dynamic.test1.com"
filter_params = jsonencode({
"*Site" = "Blr"})
dns_view = "default"
}`),
Check: resource.ComposeTestCheckFunc(
testAccARecordCompare(t, "infoblox_a_record.rec4", &ibclient.RecordA{
Name: utils.StringPtr("dynamic.test1.com"),
View: "default",
UseTtl: utils.BoolPtr(false),
}, "", "", `{"*Site":"Blr"}`),
),
},
{
Config: fmt.Sprintf(`
resource "infoblox_zone_auth" "zone1" {
fqdn = "test1.com"
}
resource "infoblox_a_record" "rec1" {
fqdn = "missing_fields.test1.com"
comment = "missing required fields"
dns_view = "default"
depends_on = [infoblox_zone_auth.zone1]
}`),
ExpectError: regexpRequiredMissingIPv4,
},
},
})
}
Expand Down Expand Up @@ -484,7 +536,7 @@ func TestAcc_resourceARecord_ea_inheritance(t *testing.T) {
Ea: ibclient.EA{
"Location": "test location A",
},
}, "", ""),
}, "", "", ""),
},
// When extensible attributes are added by another tool,
// terraform shouldn't remove those EAs
Expand Down Expand Up @@ -547,7 +599,7 @@ func TestAcc_resourceARecord_ea_inheritance(t *testing.T) {
"Location": "test location A",
"Site": "Test site",
},
}, "", ""),
}, "", "", ""),
),
},
// Validate that inherited EA won't be removed if some field is updated in the resource
Expand Down Expand Up @@ -576,7 +628,7 @@ func TestAcc_resourceARecord_ea_inheritance(t *testing.T) {
"Location": "test location A",
"Site": "Test site",
},
}, "", ""),
}, "", "", ""),
},
// Validate that inherited EA can be updated
{
Expand Down Expand Up @@ -605,7 +657,7 @@ func TestAcc_resourceARecord_ea_inheritance(t *testing.T) {
"Location": "test location A",
"Site": "new extensible site",
},
}, "", ""),
}, "", "", ""),
},
// Validate that inherited EA can be removed, if updated
{
Expand Down
81 changes: 67 additions & 14 deletions infoblox/resource_infoblox_aaaa_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func testAccAAAARecordCompare(
resPath string,
expectedRec *ibclient.RecordAAAA,
notExpectedIpAddr string,
expectedCidr string) resource.TestCheckFunc {
expectedCidr string,
expectedFilterParams string) resource.TestCheckFunc {

return func(s *terraform.State) error {
res, found := s.RootModule().Resources[resPath]
Expand Down Expand Up @@ -147,13 +148,27 @@ func testAccAAAARecordCompare(
return fmt.Errorf("'comment' is expected to be defined but it is not")
}

actualFilterParams, exists := res.Primary.Attributes["filter_params"]
if expectedFilterParams != "" {
if !exists {
return fmt.Errorf("'filter_params' is expected to be defined but it is not")
}
if actualFilterParams != expectedFilterParams {
return fmt.Errorf(
"'filter_params' does not match: got '%s', expected '%s'",
actualFilterParams, expectedFilterParams)
}
} else if exists {
return fmt.Errorf("'filter_params' is expected to be undefined but it is not")
}

return validateEAs(rec.Ea, expectedRec.Ea)
}
}

var (
regexpRequiredMissingIPv6 = regexp.MustCompile("either of 'ipv6_addr' and 'cidr' values is required")
regexpCidrIpAddrConflictIPv6 = regexp.MustCompile("only one of 'ipv6_addr' and 'cidr' values is allowed to be defined")
regexpRequiredMissingIPv6 = regexp.MustCompile("any one of 'ipv6_addr', 'cidr' and 'filter_params' values is required")
regexpCidrIpAddrConflictIPv6 = regexp.MustCompile("only one of 'ipv6_addr', 'cidr' and 'filter_params' values is allowed to be defined")
regexpUpdateConflictIPv6 = regexp.MustCompile("only one of 'ipv6_addr' and 'cidr' values is allowed to update")
)

Expand Down Expand Up @@ -208,7 +223,7 @@ func TestAccResourceAAAARecord(t *testing.T) {
UseTtl: utils.BoolPtr(false),
Comment: nil,
Ea: nil,
}, "", ""),
}, "", "", ""),
),
},
{
Expand Down Expand Up @@ -245,7 +260,7 @@ func TestAccResourceAAAARecord(t *testing.T) {
"Location": "New York",
"Site": "HQ",
},
}, "", ""),
}, "", "", ""),
),
},
{
Expand Down Expand Up @@ -274,7 +289,7 @@ func TestAccResourceAAAARecord(t *testing.T) {
Ttl: utils.Uint32Ptr(155),
UseTtl: utils.BoolPtr(true),
Comment: utils.StringPtr("test comment 2"),
}, "", ""),
}, "", "", ""),
),
},
{
Expand All @@ -299,7 +314,7 @@ func TestAccResourceAAAARecord(t *testing.T) {
Name: utils.StringPtr("name3.test.com"),
View: "nondefault_view",
UseTtl: utils.BoolPtr(false),
}, "", ""),
}, "", "", ""),
),
},
{
Expand Down Expand Up @@ -328,7 +343,7 @@ func TestAccResourceAAAARecord(t *testing.T) {
Name: utils.StringPtr("name3.test.com"),
View: "nondefault_view",
UseTtl: utils.BoolPtr(false),
}, "2000::1", "2000:1fde::/96"),
}, "2000::1", "2000:1fde::/96", ""),
),
},
{
Expand Down Expand Up @@ -381,7 +396,7 @@ func TestAccResourceAAAARecord(t *testing.T) {
Name: utils.StringPtr("name3.test.com"),
View: "nondefault_view",
UseTtl: utils.BoolPtr(false),
}, "", "2000:1fcc::/96"),
}, "", "2000:1fcc::/96", ""),
),
},
{
Expand Down Expand Up @@ -435,7 +450,7 @@ func TestAccResourceAAAARecord(t *testing.T) {
Name: utils.StringPtr("name3.test.com"),
View: "nondefault_view",
UseTtl: utils.BoolPtr(false),
}, "", ""),
}, "", "", ""),
),
},
{
Expand All @@ -452,6 +467,44 @@ func TestAccResourceAAAARecord(t *testing.T) {
}`),
ExpectError: regexpDnsviewUpdateNotAllowed,
},
{
Config: fmt.Sprintf(`
resource "infoblox_zone_auth" "zone1" {
fqdn = "test1.com"
}
resource "infoblox_ipv6_network" "net2" {
cidr = "2001:db8:abcd::/64"
ext_attrs = jsonencode({
"Site" = "Blr"
})
}
resource "infoblox_aaaa_record" "rec4" {
fqdn = "dynamic.test1.com"
filter_params = jsonencode({
"*Site" = "Blr"})
dns_view = "default"
}`),
Check: resource.ComposeTestCheckFunc(
testAccAAAARecordCompare(t, "infoblox_aaaa_record.rec4", &ibclient.RecordAAAA{
Name: utils.StringPtr("dynamic.test1.com"),
View: "default",
UseTtl: utils.BoolPtr(false),
}, "", "", `{"*Site":"Blr"}`),
),
},
{
Config: fmt.Sprintf(`
resource "infoblox_zone_auth" "zone1" {
fqdn = "test1.com"
}
resource "infoblox_aaaa_record" "rec1" {
fqdn = "missing_fields.test1.com"
comment = "missing required fields"
dns_view = "default"
depends_on = [infoblox_zone_auth.zone1]
}`),
ExpectError: regexpRequiredMissingIPv6,
},
},
})
}
Expand Down Expand Up @@ -486,7 +539,7 @@ func TestAcc_resourceAAAARecord_ea_inheritance(t *testing.T) {
Ea: ibclient.EA{
"Location": "test AAAA location",
},
}, "", ""),
}, "", "", ""),
},
// When extensible attributes are added by another tool,
// terraform shouldn't remove those EAs
Expand Down Expand Up @@ -549,7 +602,7 @@ func TestAcc_resourceAAAARecord_ea_inheritance(t *testing.T) {
"Location": "test AAAA location",
"Site": "Testing Site",
},
}, "", ""),
}, "", "", ""),
),
},
// Validate that inherited EA won't be removed if some field is updated in the resource
Expand Down Expand Up @@ -578,7 +631,7 @@ func TestAcc_resourceAAAARecord_ea_inheritance(t *testing.T) {
"Location": "test AAAA location",
"Site": "Testing Site",
},
}, "", ""),
}, "", "", ""),
},
// Validate that inherited EA can be updated
{
Expand Down Expand Up @@ -607,7 +660,7 @@ func TestAcc_resourceAAAARecord_ea_inheritance(t *testing.T) {
"Location": "test AAAA location",
"Site": "New Testing Site",
},
}, "", ""),
}, "", "", ""),
},
// Validate that inherited EA can be removed, if updated
{
Expand Down
Loading