Skip to content

Commit

Permalink
✅ Add tests for tag filter
Browse files Browse the repository at this point in the history
  • Loading branch information
twink0r committed Sep 11, 2023
1 parent 0174f5e commit 94df4c6
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions netbox/data_source_netbox_virtual_machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ func TestAccNetboxVirtualMachinesDataSource_basic(t *testing.T) {
})
}

func TestAccNetboxVirtualMachinesDataSource_tags(t *testing.T) {
testSlug := "vm_ds_tags"
testName := testAccGetTestName(testSlug)
dependencies := testAccNetboxVirtualMachineDataSourceDependenciesWithTags(testName)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: dependencies,
},
{
Config: dependencies + testAccNetboxVirtualMachineDataSourceTagA(testName) + testAccNetboxVirtualMachineDataSourceTagB(testName) + testAccNetboxVirtualMachineDataSourceTagAB(testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_virtual_machines.tag-a", "vms.#", "2"),
resource.TestCheckResourceAttr("data.netbox_virtual_machines.tag-b", "vms.#", "2"),
resource.TestCheckResourceAttr("data.netbox_virtual_machines.tag-ab", "vms.#", "1"),
),
},
},
})
}

func testAccNetboxVirtualMachineDataSourceDependencies(testName string) string {
return testAccNetboxVirtualMachineFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_virtual_machine" "test0" {
Expand Down Expand Up @@ -96,6 +118,53 @@ resource "netbox_virtual_machine" "test3" {
`, testName)
}

func testAccNetboxVirtualMachineDataSourceDependenciesWithTags(testName string) string {
return testAccNetboxVirtualMachineFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_tag" "servicea" {
name = "%[1]s_service-a"
}
resource "netbox_tag" "serviceb" {
name = "%[1]s_service-b"
}
resource "netbox_virtual_machine" "test0" {
name = "%[1]s_0"
cluster_id = netbox_cluster.test.id
site_id = netbox_site.test.id
comments = "thisisacomment"
memory_mb = 1024
disk_size_gb = 256
tenant_id = netbox_tenant.test.id
role_id = netbox_device_role.test.id
platform_id = netbox_platform.test.id
vcpus = 4
tags = [
netbox_tag.servicea.name,
netbox_tag.serviceb.name,
]
}
resource "netbox_virtual_machine" "test1" {
name = "%[1]s_1"
cluster_id = netbox_cluster.test.id
site_id = netbox_site.test.id
tags = [
netbox_tag.servicea.name,
]
}
resource "netbox_virtual_machine" "test2" {
name = "%[1]s_2_regex"
cluster_id = netbox_cluster.test.id
site_id = netbox_site.test.id
tags = [
netbox_tag.serviceb.name,
]
}
`, testName)
}

const testAccNetboxVirtualMachineDataSourceFilterCluster = `
data "netbox_virtual_machines" "test" {
filter {
Expand Down Expand Up @@ -127,3 +196,37 @@ data "netbox_virtual_machines" "test" {
value = netbox_cluster.test.id
}
}`

func testAccNetboxVirtualMachineDataSourceTagA(testName string) string {
return fmt.Sprintf(`
data "netbox_virtual_machines" "tag-a" {
filter {
name = "tag"
value = "%[1]s_service-a"
}
}`, testName)
}

func testAccNetboxVirtualMachineDataSourceTagB(testName string) string {
return fmt.Sprintf(`
data "netbox_virtual_machines" "tag-b" {
filter {
name = "tag"
value = "%[1]s_service-b"
}
}`, testName)
}

func testAccNetboxVirtualMachineDataSourceTagAB(testName string) string {
return fmt.Sprintf(`
data "netbox_virtual_machines" "tag-ab" {
filter {
name = "tag"
value = "%[1]s_service-a"
}
filter {
name = "tag"
value = "%[1]s_service-b"
}
}`, testName)
}

0 comments on commit 94df4c6

Please sign in to comment.