Skip to content

Commit

Permalink
feat(pci-dev): add host_pci_device tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mristok committed Oct 30, 2023
1 parent b6033b7 commit da35003
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
82 changes: 82 additions & 0 deletions vsphere/data_source_vsphere_host_pci_device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package vsphere

import (
"fmt"
"os"
"regexp"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/testhelper"
)

func TestAccDataSourceVSphereHostPciDevice_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccDataSourceVSphereHostPciDevicePreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereHostPciDeviceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrWith(
"data.vsphere_host_pci_device.device",
"pci_devices.#",
func(value string) error {
valueInt, err := strconv.Atoi(value)
if err != nil {
return err
}

if valueInt <= 0 {
return fmt.Errorf("number of PCI devices should be greater than 0")
}
return nil
},
),
),
},
{
Config: testAccDataSourceVSphereHostPciDeviceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"data.vsphere_host_pci_device.device",
"pci_devices.0.name",
regexp.MustCompile("(.*?)"),
),
),
},
},
})
}

func testAccDataSourceVSphereHostPciDevicePreCheck(t *testing.T) {
if os.Getenv("TF_VAR_VSPHERE_DATACENTER") == "" {
t.Skip("set TF_VAR_VSPHERE_DATACENTER to run vsphere_host_pci_device acceptance tests")
}
if os.Getenv("TF_VAR_VSPHERE_ESXI1") == "" {
t.Skip("set TF_VAR_VSPHERE_ESXI1 to run vsphere_host_pci_device acceptance tests")
}
}

func testAccDataSourceVSphereHostPciDeviceConfig() string {
return fmt.Sprintf(`
%s
data "vsphere_host" "host" {
name = "%s"
datacenter_id = "${data.vsphere_datacenter.rootdc1.id}"
}
data "vsphere_host_pci_device" "device" {
host_id = "${data.vsphere_host.host.id}"
name_regex = ""
}
`, testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()), os.Getenv("TF_VAR_VSPHERE_ESXI1"))
}
2 changes: 1 addition & 1 deletion website/docs/d/host_pci_device.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ The following arguments are supported:
[docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider

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

## Attribute Reference

Expand Down

0 comments on commit da35003

Please sign in to comment.