Skip to content

Commit a04c0d1

Browse files
mristoktenthirtyam
authored andcommitted
feat(pci-dev): add host_pci_device tests
1 parent e05c821 commit a04c0d1

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

docs/data-sources/host_pci_device.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The following arguments are supported:
7070
[docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider
7171

7272
~> **NOTE:** `name_regex`, `class_id`, and `vendor_id` can all be used together.
73-
They are evaluated and filter PCI Device results in the above order.
73+
The above arguments are evaluated and filter PCI Device results in the above order.
7474

7575
## Attribute Reference
7676

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package vsphere
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"regexp"
10+
"strconv"
11+
"testing"
12+
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
14+
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/testhelper"
15+
)
16+
17+
func TestAccDataSourceVSphereHostPciDevice_basic(t *testing.T) {
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() {
20+
testAccPreCheck(t)
21+
testAccDataSourceVSphereHostPciDevicePreCheck(t)
22+
},
23+
Providers: testAccProviders,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccDataSourceVSphereHostPciDeviceConfig(),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttrWith(
29+
"data.vsphere_host_pci_device.device",
30+
"pci_devices.#",
31+
func(value string) error {
32+
valueInt, err := strconv.Atoi(value)
33+
if err != nil {
34+
return err
35+
}
36+
37+
if valueInt <= 0 {
38+
return fmt.Errorf("number of PCI devices should be greater than 0")
39+
}
40+
return nil
41+
},
42+
),
43+
),
44+
},
45+
{
46+
Config: testAccDataSourceVSphereHostPciDeviceConfig(),
47+
Check: resource.ComposeTestCheckFunc(
48+
resource.TestMatchResourceAttr(
49+
"data.vsphere_host_pci_device.device",
50+
"pci_devices.0.name",
51+
regexp.MustCompile("(.*?)"),
52+
),
53+
),
54+
},
55+
},
56+
})
57+
}
58+
59+
func testAccDataSourceVSphereHostPciDevicePreCheck(t *testing.T) {
60+
if os.Getenv("TF_VAR_VSPHERE_DATACENTER") == "" {
61+
t.Skip("set TF_VAR_VSPHERE_DATACENTER to run vsphere_host_pci_device acceptance tests")
62+
}
63+
if os.Getenv("TF_VAR_VSPHERE_ESXI1") == "" {
64+
t.Skip("set TF_VAR_VSPHERE_ESXI1 to run vsphere_host_pci_device acceptance tests")
65+
}
66+
}
67+
68+
func testAccDataSourceVSphereHostPciDeviceConfig() string {
69+
return fmt.Sprintf(`
70+
%s
71+
72+
data "vsphere_host" "host" {
73+
name = "%s"
74+
datacenter_id = "${data.vsphere_datacenter.rootdc1.id}"
75+
}
76+
77+
data "vsphere_host_pci_device" "device" {
78+
host_id = "${data.vsphere_host.host.id}"
79+
name_regex = ""
80+
}
81+
`, testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()), os.Getenv("TF_VAR_VSPHERE_ESXI1"))
82+
}

0 commit comments

Comments
 (0)