|
| 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