Skip to content

Commit

Permalink
r/host_virtual_switch: Move test check variable gathering to a helper
Browse files Browse the repository at this point in the history
This will actually help when I refactor #139, which actually has
multiple check calls fetching these kinds of variables.
  • Loading branch information
vancluever committed Aug 25, 2017
1 parent 5cf0908 commit b55ef8d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
43 changes: 43 additions & 0 deletions vsphere/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package vsphere

import (
"fmt"
"os"
"time"

"github.com/hashicorp/terraform/terraform"
"github.com/vmware/govmomi"
)

// testCheckVariables bundles common variables needed by various test checkers.
type testCheckVariables struct {
// A client for various operations.
client *govmomi.Client

// The subject resource's ID.
resourceID string

// The ESXi host that a various API call is directed at.
esxiHost string

// The datacenter that a various API call is directed at.
datacenter string

// A timeout to pass to various context creation calls.
timeout time.Duration
}

func testClientVariablesForResource(s *terraform.State, addr string) (testCheckVariables, error) {
rs, ok := s.RootModule().Resources[addr]
if !ok {
return testCheckVariables{}, fmt.Errorf("%s not found in state", addr)
}

return testCheckVariables{
client: testAccProvider.Meta().(*govmomi.Client),
resourceID: rs.Primary.ID,
esxiHost: os.Getenv("VSPHERE_ESXI_HOST"),
datacenter: os.Getenv("VSPHERE_DATACENTER"),
timeout: time.Minute * 5,
}, nil
}
1 change: 0 additions & 1 deletion vsphere/resource_vsphere_host_virtual_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,5 @@ func resourceVSphereHostVirtualSwitchDelete(d *schema.ResourceData, meta interfa
return fmt.Errorf("error deleting host vSwitch: %s", err)
}

d.SetId("")
return nil
}
17 changes: 5 additions & 12 deletions vsphere/resource_vsphere_host_virtual_switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/vmware/govmomi"
)

func TestAccResourceVSphereHostVirtualSwitch(t *testing.T) {
Expand Down Expand Up @@ -130,26 +128,21 @@ func testAccResourceVSphereHostVirtualSwitchPreCheck(t *testing.T) {

func testAccResourceVSphereHostVirtualSwitchExists(expected bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources["vsphere_host_virtual_switch.switch"]
if !ok {
vars, err := testClientVariablesForResource(s, "vsphere_host_virtual_switch.switch")
if err != nil {
return errors.New("vsphere_host_virtual_switch.switch not found in state")
}

client := testAccProvider.Meta().(*govmomi.Client)
id := rs.Primary.ID
host := os.Getenv("VSPHERE_ESXI_HOST")
datacenter := os.Getenv("VSPHERE_DATACENTER")
timeout := time.Minute * 5
_, err := hostVSwitchFromName(client, id, host, datacenter, timeout)
_, err = hostVSwitchFromName(vars.client, vars.resourceID, vars.esxiHost, vars.datacenter, vars.timeout)
if err != nil {
if err.Error() == fmt.Sprintf("vSwitch %s not found on host %s", id, host) && !expected {
if err.Error() == fmt.Sprintf("vSwitch %s not found on host %s", vars.resourceID, vars.esxiHost) && !expected {
// Expected missing
return nil
}
return err
}
if !expected {
return fmt.Errorf("expected vSwitch %s to still be missing", id)
return fmt.Errorf("expected vSwitch %s to still be missing", vars.resourceID)
}
return nil
}
Expand Down

0 comments on commit b55ef8d

Please # to comment.