Skip to content

Commit

Permalink
Add tests for prefix resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanverik committed Oct 27, 2021
1 parent a7c488d commit 7454208
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions netbox/resource_netbox_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func resourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error {
if res.GetPayload().Vlan != nil {
d.Set("vlan_id", res.GetPayload().Vlan.ID)
} else {
d.Set("vlan_id", nil)
}
d.Set("vlan_id", nil)
}

if res.GetPayload().Role != nil {
d.Set("role_id", res.GetPayload().Role.ID)
Expand Down
37 changes: 28 additions & 9 deletions netbox/resource_netbox_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func testAccNetboxPrefixFullDependencies(testName string) string {
func testAccNetboxPrefixFullDependencies(testName string, testSlug string, testVid string) string {
return fmt.Sprintf(`
resource "netbox_tag" "test" {
name = "%[1]s"
Expand All @@ -30,20 +30,35 @@ resource "netbox_site" "test" {
name = "%[1]s"
status = "active"
}
`, testName)
resource "netbox_ipam_role" "test_basic" {
name = "%[1]s"
slug = "%[2]s"
}
resource "netbox_vlan" "test_basic" {
name = "%[1]s"
vid = "%[3]s"
status = "active"
description = "Test"
tags = []
}
`, testName, testSlug, testVid)
}

func TestAccNetboxPrefix_basic(t *testing.T) {

testPrefix := "1.1.1.0/25"
testSlug := "prefix"
testVid := "123"
randomSlug := testAccGetTestName(testSlug)
testDesc := "test prefix"
testName := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(`
Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(`
resource "netbox_prefix" "test" {
prefix = "%s"
description = "%s"
Expand All @@ -59,7 +74,7 @@ resource "netbox_prefix" "test" {
),
},
{
Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(`
Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(`
resource "netbox_prefix" "test" {
prefix = "%s"
description = "%s"
Expand All @@ -69,7 +84,7 @@ resource "netbox_prefix" "test" {
ExpectError: regexp.MustCompile("expected status to be one of .*"),
},
{
Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(`
Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(`
resource "netbox_prefix" "test" {
prefix = "%s"
description = "%s"
Expand All @@ -82,7 +97,7 @@ resource "netbox_prefix" "test" {
),
},
{
Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(`
Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(`
resource "netbox_prefix" "test" {
prefix = "%s"
description = "%s"
Expand All @@ -95,7 +110,7 @@ resource "netbox_prefix" "test" {
),
},
{
Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(`
Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(`
resource "netbox_prefix" "test" {
prefix = "%s"
description = "%s 2"
Expand All @@ -114,7 +129,7 @@ resource "netbox_prefix" "test" {
),
},
{
Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(`
Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(`
resource "netbox_prefix" "test" {
prefix = "%s"
description = "%s 2"
Expand All @@ -134,14 +149,16 @@ resource "netbox_prefix" "test" {
),
},
{
Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(`
Config: testAccNetboxPrefixFullDependencies(testName, randomSlug, testVid) + fmt.Sprintf(`
resource "netbox_prefix" "test" {
prefix = "%s"
description = "%s 2"
status = "active"
vrf_id = netbox_vrf.test.id
tenant_id = netbox_tenant.test.id
site_id = netbox_site.test.id
vlan_id = netbox_vlan.test.id
role_id = netbox_ipam_role.test.id
tags = [netbox_tag.test.name]
}`, testPrefix, testDesc),
Check: resource.ComposeTestCheckFunc(
Expand All @@ -151,6 +168,8 @@ resource "netbox_prefix" "test" {
resource.TestCheckResourceAttrPair("netbox_prefix.test", "vrf_id", "netbox_vrf.test", "id"),
resource.TestCheckResourceAttrPair("netbox_prefix.test", "tenant_id", "netbox_tenant.test", "id"),
resource.TestCheckResourceAttrPair("netbox_prefix.test", "site_id", "netbox_site.test", "id"),
resource.TestCheckResourceAttrPair("netbox_prefix.test", "vlan_id", "netbox_vlan.test", "id"),
resource.TestCheckResourceAttrPair("netbox_prefix.test", "role_id", "netbox_ipam_role.test", "id"),
resource.TestCheckResourceAttr("netbox_prefix.test", "tags.#", "1"),
resource.TestCheckResourceAttr("netbox_prefix.test", "tags.0", testName),
),
Expand Down
4 changes: 2 additions & 2 deletions netbox/resource_netbox_vlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestAccNetboxVlan_basic(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(`
Config: testAccNetboxVlanFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_vlan" "test_basic" {
name = "%s"
vid = "%s"
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestAccNetboxVlan_with_dependencies(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: testAccNetboxPrefixFullDependencies(testName) + fmt.Sprintf(`
Config: testAccNetboxVlanFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_vlan" "test_with_dependencies" {
name = "%s"
vid = "%s"
Expand Down

0 comments on commit 7454208

Please sign in to comment.