Skip to content

Commit

Permalink
Test data source
Browse files Browse the repository at this point in the history
  • Loading branch information
mraerino committed Jul 20, 2024
1 parent 745fe34 commit d67fdd9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
41 changes: 41 additions & 0 deletions internal/provider/bgpsession_datasource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccSessionDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
ExternalProviders: testExternalProviders,
Steps: []resource.TestStep{
// Read testing
{
Config: fmt.Sprintf(`%s
resource "netboxbgp_session" "test" {
name = "My session"
status = "active"
device_id = netbox_device.test.id
local_address_id = netbox_ip_address.local.id
remote_address_id = netbox_ip_address.remote.id
local_as_id = netbox_asn.test.id
remote_as_id = netbox_asn.test.id
}
data "netboxbgp_session" "test" {
depends_on = [netboxbgp_session.test]
id = netboxbgp_session.test.id
}
`, baseResources(t)),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.netboxbgp_session.test", "name", "My session"),
resource.TestCheckResourceAttrPair("data.netboxbgp_session.test", "device.name", "netbox_device.test", "name"),
),
},
},
})
}
31 changes: 21 additions & 10 deletions internal/provider/bgpsession_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"fmt"
"hash/fnv"
"testing"

"github.com/google/uuid"
Expand All @@ -11,11 +12,26 @@ import (
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

var testExternalProviders = map[string]resource.ExternalProvider{
"netbox": {
VersionConstraint: "~> 3.8.7",
Source: "registry.terraform.io/e-breuninger/netbox",
},
}

func testName(t *testing.T) string {
return t.Name() + "_" + uuid.NewString()
}

func testNum(t *testing.T) uint64 {
h := fnv.New64()
fmt.Fprint(h, testName(t))
return h.Sum64()
}

func baseResources(t *testing.T) string {
num := testNum(t)
shortNum := num % 250
return fmt.Sprintf(`
resource "netbox_tag" "test" {
name = "%[1]s"
Expand Down Expand Up @@ -54,14 +70,14 @@ resource "netbox_device_interface" "test" {
}
resource "netbox_ip_address" "local" {
ip_address = "203.0.113.10/24"
ip_address = "203.0.113.%[2]d/24"
status = "active"
interface_id = netbox_device_interface.test.id
object_type = "dcim.interface"
}
resource "netbox_ip_address" "remote" {
ip_address = "203.0.113.11/24"
ip_address = "203.0.113.%[3]d/24"
status = "active"
}
Expand All @@ -70,21 +86,16 @@ resource "netbox_rir" "test" {
}
resource "netbox_asn" "test" {
asn = 1337
asn = %[4]d
rir_id = netbox_rir.test.id
}`, testName(t))
}`, testName(t), shortNum, shortNum+1, shortNum+1337)
}

func TestAccSessionResource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
ExternalProviders: map[string]resource.ExternalProvider{
"netbox": {
VersionConstraint: "~> 3.8.7",
Source: "registry.terraform.io/e-breuninger/netbox",
},
},
ExternalProviders: testExternalProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`%s
Expand Down

0 comments on commit d67fdd9

Please sign in to comment.