Skip to content

Commit

Permalink
fix lxd_network_zone_record tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis Mandravellos committed Sep 8, 2023
1 parent 69955bc commit eb4e59f
Showing 1 changed file with 70 additions and 53 deletions.
123 changes: 70 additions & 53 deletions lxd/resource_lxd_network_zone_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ import (
"testing"

"github.com/canonical/lxd/shared/api"
petname "github.com/dustinkirkland/golang-petname"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccNetworkZoneRecord_basic(t *testing.T) {
var record api.NetworkZoneRecord

recordName := petname.Name()
zoneName := petname.Generate(3, ".")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNetworkZoneRecord_basic(),
Config: testAccNetworkZoneRecord(zoneName, recordName),
Check: resource.ComposeTestCheckFunc(
testAccNetworkZoneRecordExists(t, "lxd_network_zone_record.record", &record),
resource.TestCheckResourceAttr(
"lxd_network_zone_record.record",
"name",
"ns",
),
"lxd_network_zone_record.record", "name", recordName),
),
},
},
Expand All @@ -34,16 +35,18 @@ func TestAccNetworkZoneRecord_basic(t *testing.T) {
func TestAccNetworkZoneRecord_description(t *testing.T) {
var record api.NetworkZoneRecord

recordName := petname.Name()
zoneName := petname.Generate(3, ".")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNetworkZoneRecord_desc(),
Config: testAccNetworkZoneRecord(zoneName, recordName),
Check: resource.ComposeTestCheckFunc(
testAccNetworkZoneRecordExists(t, "lxd_network_record.record", &record),
resource.TestCheckResourceAttr(
"lxd_network_record.record",
testAccNetworkZoneRecordExists(t, "lxd_network_zone_record.record", &record),
resource.TestCheckResourceAttr("lxd_network_zone_record.record",
"description",
"descriptive",
),
Expand All @@ -53,6 +56,55 @@ func TestAccNetworkZoneRecord_description(t *testing.T) {
})
}

func TestAccNetworkZoneRecord_entry(t *testing.T) {
var record api.NetworkZoneRecord

recordName := petname.Name()
zoneName := petname.Generate(3, ".")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNetworkZoneRecord(zoneName, recordName),
Check: resource.ComposeTestCheckFunc(
testAccNetworkZoneRecordExists(t, "lxd_network_zone_record.record", &record),
resource.TestCheckTypeSetElemNestedAttrs(
"lxd_network_zone_record.record",
"entry.*",
map[string]string{"type": "CNAME", "value": "another", "ttl": "3600"},
),
),
},
},
})
}

func TestAccNetworkZoneRecord_zone(t *testing.T) {
var record api.NetworkZoneRecord

recordName := petname.Name()
zoneName := petname.Generate(3, ".")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNetworkZoneRecord(zoneName, recordName),
Check: resource.ComposeTestCheckFunc(
testAccNetworkZoneRecordExists(t, "lxd_network_zone_record.record", &record),
resource.TestCheckResourceAttr("lxd_network_zone_record.record",
"zone",
zoneName,
),
),
},
},
})
}

func testAccNetworkZoneRecordExists(
t *testing.T,
n string,
Expand Down Expand Up @@ -83,64 +135,29 @@ func testAccNetworkZoneRecordExists(
}
}

func testAccNetworkZoneRecordConfig(
record *api.NetworkZoneRecord,
k, v string,
) resource.TestCheckFunc {
return func(s *terraform.State) error {
if record.Config == nil {
return fmt.Errorf("No config")
}

for key, value := range record.Config {
if k != key {
continue
}

if v == value {
return nil
}

return fmt.Errorf("Bad value for %s: %s", k, value)
}

return fmt.Errorf("Config not found: %s", k)
}
}

func testAccNetworkZoneRecord_basic() string {
return `
func testAccNetworkZoneRecord(zoneName, recordName string) string {
return fmt.Sprintf(`
resource "lxd_network_zone" "zone" {
name = "custom.example.org"
description = "descriptive"
name = "%[1]s"
config = {
"dns.nameservers" = "ns.custom.example.org"
"dns.nameservers" = "ns.%[1]s"
"peers.ns.address" = "127.0.0.1"
}
}
resource "lxd_network_zone_record" "record" {
name = "ns"
zone = lxd_network_record.zone.id
name = "%[2]s"
zone = lxd_network_zone.zone.id
description = "descriptive"
config = {}
entry {
type = "CNAME"
value = "another"
ttl = 3600
}
}
`
}

func testAccNetworkZoneRecord_desc() string {
return `
resource "lxd_network_zone_record" "record" {
name = "ns"
description = "descriptive"
config = {}
}
`
`, zoneName, recordName)
}

0 comments on commit eb4e59f

Please sign in to comment.