Skip to content

Commit e6a9f72

Browse files
unify the tests + remove duplicate tests
1 parent 287b144 commit e6a9f72

1 file changed

Lines changed: 57 additions & 29 deletions

File tree

ns1/resource_zone_test.go

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ func TestAccZone_ManualDelete(t *testing.T) {
506506
})
507507
}
508508

509-
func TestAccZone_EmptyNetworks(t *testing.T) {
509+
func TestAccZone_Networks(t *testing.T) {
510510
var zone dns.Zone
511511
zoneName := fmt.Sprintf("terraform-test-%s.io",
512512
acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum),
@@ -516,13 +516,53 @@ func TestAccZone_EmptyNetworks(t *testing.T) {
516516
Providers: testAccProviders,
517517
CheckDestroy: testAccCheckZoneDestroy,
518518
Steps: []resource.TestStep{
519+
// Create with nil networks (field omitted) - should default to [0]
520+
{
521+
Config: testAccZoneDefaultNetworks(zoneName),
522+
Check: resource.ComposeTestCheckFunc(
523+
testAccCheckZoneExists("ns1_zone.it", &zone),
524+
testAccCheckZoneNetworks(&zone, []int{0}, "initial create with nil networks"),
525+
),
526+
},
527+
// Update with nil networks (no change) - should remain [0]
528+
{
529+
Config: testAccZoneDefaultNetworks(zoneName),
530+
Check: resource.ComposeTestCheckFunc(
531+
testAccCheckZoneExists("ns1_zone.it", &zone),
532+
testAccCheckZoneNetworks(&zone, []int{0}, "update with nil networks"),
533+
),
534+
},
535+
// Update with empty networks [] - should become []
519536
{
520537
Config: testAccZoneEmptyNetworks(zoneName),
521538
Check: resource.ComposeTestCheckFunc(
522539
testAccCheckZoneExists("ns1_zone.it", &zone),
523-
testAccCheckZoneNetworks(&zone, []int{}, "networks=[]"),
540+
testAccCheckZoneNetworks(&zone, []int{}, "update with networks=[]"),
524541
),
525542
},
543+
// Update back to nil networks - should become [0]
544+
{
545+
Config: testAccZoneDefaultNetworks(zoneName),
546+
Check: resource.ComposeTestCheckFunc(
547+
testAccCheckZoneExists("ns1_zone.it", &zone),
548+
testAccCheckZoneNetworks(&zone, []int{0}, "update back to nil networks"),
549+
),
550+
},
551+
// Update with specific networks [1, 2]
552+
{
553+
Config: testAccZoneSpecificNetworks(zoneName, []int{1, 2}),
554+
Check: resource.ComposeTestCheckFunc(
555+
testAccCheckZoneExists("ns1_zone.it", &zone),
556+
testAccCheckZoneNetworks(&zone, []int{1, 2}, "update with networks=[1,2]"),
557+
),
558+
},
559+
// Import verification
560+
{
561+
ResourceName: "ns1_zone.it",
562+
ImportState: true,
563+
ImportStateId: zoneName,
564+
ImportStateVerify: true,
565+
},
526566
},
527567
})
528568
}
@@ -535,25 +575,21 @@ func testAccZoneEmptyNetworks(zoneName string) string {
535575
`, zoneName)
536576
}
537577

538-
func TestAccZone_DefaultNetworks(t *testing.T) {
539-
var zone dns.Zone
540-
zoneName := fmt.Sprintf("terraform-test-%s.io",
541-
acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum),
542-
)
543-
resource.Test(t, resource.TestCase{
544-
PreCheck: func() { testAccPreCheck(t) },
545-
Providers: testAccProviders,
546-
CheckDestroy: testAccCheckZoneDestroy,
547-
Steps: []resource.TestStep{
548-
{
549-
Config: testAccZoneDefaultNetworks(zoneName),
550-
Check: resource.ComposeTestCheckFunc(
551-
testAccCheckZoneExists("ns1_zone.it", &zone),
552-
testAccCheckZoneNetworks(&zone, []int{0}, "networks field omitted"),
553-
),
554-
},
555-
},
556-
})
578+
func testAccZoneSpecificNetworks(zoneName string, networks []int) string {
579+
networksStr := "["
580+
for i, n := range networks {
581+
if i > 0 {
582+
networksStr += ", "
583+
}
584+
networksStr += fmt.Sprintf("%d", n)
585+
}
586+
networksStr += "]"
587+
588+
return fmt.Sprintf(`resource "ns1_zone" "it" {
589+
zone="%s"
590+
networks=%s
591+
}
592+
`, zoneName, networksStr)
557593
}
558594

559595
func testAccZoneDefaultNetworks(zoneName string) string {
@@ -881,14 +917,6 @@ func testAccCheckZoneNetworks(zone *dns.Zone, expected []int, context string) re
881917
}
882918
}
883919

884-
func testAccZoneNoNetworks(zoneName string) string {
885-
return fmt.Sprintf(`resource "ns1_zone" "it" {
886-
zone="%s"
887-
networks=[]
888-
}
889-
`, zoneName)
890-
}
891-
892920
func testAccCheckZoneNotSecondary(z *dns.Zone) resource.TestCheckFunc {
893921
return func(s *terraform.State) error {
894922
if z.Secondary != nil {

0 commit comments

Comments
 (0)