Skip to content

Commit

Permalink
test: fix all test to work with the current prism version
Browse files Browse the repository at this point in the history
  • Loading branch information
marinsalinas committed Jan 26, 2021
1 parent 911c4b6 commit 9273c14
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 58 deletions.
18 changes: 10 additions & 8 deletions nutanix/data_source_nutanix_access_control_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
func TestAccNutanixAccessControlPolicyDataSourceByID_basic(t *testing.T) {
name := acctest.RandomWithPrefix("accest-access-policy")
description := "Description of my access control policy"
roleName := acctest.RandomWithPrefix("test-acc-role")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAccessControlPolicyDataSourceByIDConfig(name, description),
Config: testAccAccessControlPolicyDataSourceByIDConfig(name, description, roleName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.nutanix_access_control_policy.test", "name", name),
Expand All @@ -34,13 +35,14 @@ func TestAccNutanixAccessControlPolicyDataSourceByID_basic(t *testing.T) {
func TestAccNutanixAccessControlPolicyDataSourceByName_basic(t *testing.T) {
name := acctest.RandomWithPrefix("accest-access-policy")
description := "Description of my access control policy"
roleName := acctest.RandomWithPrefix("test-acc-role")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAccessControlPolicyDataSourceByNameConfig(name, description),
Config: testAccAccessControlPolicyDataSourceByNameConfig(name, description, roleName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.nutanix_access_control_policy.test", "name", name),
Expand All @@ -53,10 +55,10 @@ func TestAccNutanixAccessControlPolicyDataSourceByName_basic(t *testing.T) {
})
}

func testAccAccessControlPolicyDataSourceByIDConfig(name, description string) string {
func testAccAccessControlPolicyDataSourceByIDConfig(name, description, roleName string) string {
return fmt.Sprintf(`
resource "nutanix_role" "test" {
name = "test role"
name = "%[3]s"
description = "description role"
permission_reference_list {
kind = "permission"
Expand All @@ -75,13 +77,13 @@ resource "nutanix_access_control_policy" "test" {
data "nutanix_access_control_policy" "test" {
access_control_policy_id = nutanix_access_control_policy.test.id
}
`, name, description)
`, name, description, roleName)
}

func testAccAccessControlPolicyDataSourceByNameConfig(name, description string) string {
func testAccAccessControlPolicyDataSourceByNameConfig(name, description, roleName string) string {
return fmt.Sprintf(`
resource "nutanix_role" "test" {
name = "test role 2"
name = "%[3]s"
description = "description role"
permission_reference_list {
kind = "permission"
Expand All @@ -100,5 +102,5 @@ resource "nutanix_access_control_policy" "test" {
data "nutanix_access_control_policy" "test" {
access_control_policy_name = nutanix_access_control_policy.test.name
}
`, name, description)
`, name, description, roleName)
}
2 changes: 1 addition & 1 deletion nutanix/data_source_nutanix_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func dataSourceNutanixPermissionRead(d *schema.ResourceData, meta interface{}) e
if rnOk {
resp, err = findPermissionByName(conn, permissionName.(string))
}
utils.PrintToJSON(resp, "Permission: ")

if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions nutanix/data_source_nutanix_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestAccNutanixPermissionsDataSource_basic(t *testing.T) {
Config: testAccPermissionsDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.nutanix_permissions.test", "entities.#"),
resource.TestCheckResourceAttr(
"data.nutanix_permissions.test", "entities.#", "485"),
resource.TestCheckResourceAttrSet(
"data.nutanix_permissions.test", "entities.#"),
),
},
},
Expand Down
1 change: 0 additions & 1 deletion nutanix/data_source_nutanix_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ func dataSourceNutanixUserRead(d *schema.ResourceData, meta interface{}) error {
}

refe := flattenArrayReferenceValues(resp.Status.Resources.AccessControlPolicyReferenceList)
utils.PrintToJSON(refe, "acceess")

if err := d.Set("access_control_policy_reference_list", refe); err != nil {
return fmt.Errorf("error setting state for user UUID(%s), %s", d.Id(), err)
Expand Down
2 changes: 0 additions & 2 deletions nutanix/data_source_nutanix_user_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ func TestAccNutanixUserGroupsDataSource_basic(t *testing.T) {
Config: testAccUserGroupsDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.nutanix_user_groups.test", "entities.#"),
resource.TestCheckResourceAttr(
"data.nutanix_user_groups.test", "entities.#", "3"),
),
},
},
Expand Down
8 changes: 4 additions & 4 deletions nutanix/data_source_nutanix_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

func TestAccNutanixUserDataSource_basic(t *testing.T) {
principalName := "[email protected]"
expectedDisplayName := "dou-user"
directoryServiceUUID := "dd19a896-8e72-4158-b716-98455ceda220"
principalName := "dou-user-3@ntnxlab.local"
expectedDisplayName := "dou-user-3"
directoryServiceUUID := "542d7921-1385-4b6e-ab10-09f2ca4f054d"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -49,7 +49,7 @@ data "nutanix_user" "user" {
func TestAccNutanixUserDataSource_byName(t *testing.T) {
principalName := "[email protected]"
expectedDisplayName := "dou-user"
directoryServiceUUID := "dd19a896-8e72-4158-b716-98455ceda220"
directoryServiceUUID := "542d7921-1385-4b6e-ab10-09f2ca4f054d"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
10 changes: 6 additions & 4 deletions nutanix/data_source_nutanix_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nutanix

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand Down Expand Up @@ -47,13 +48,14 @@ func TestAccNutanixVirtualMachineDataSource_WithDisk(t *testing.T) {
func TestAccNutanixVirtualMachineDataSource_withDiskContainer(t *testing.T) {
datasourceName := "data.nutanix_virtual_machine.nutanix_virtual_machine"
vmName := acctest.RandomWithPrefix("test-dou-vm")
containerUUID := os.Getenv("NUTANIX_STORAGE_CONTAINER")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccVMDataSourceWithDiskContainer(vmName),
Config: testAccVMDataSourceWithDiskContainer(vmName, containerUUID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(datasourceName, "vm_id"),
resource.TestCheckResourceAttrSet(datasourceName, "disk_list.#"),
Expand All @@ -70,7 +72,7 @@ func TestAccNutanixVirtualMachineDataSource_withDiskContainer(t *testing.T) {
})
}

func testAccVMDataSourceWithDiskContainer(vmName string) string {
func testAccVMDataSourceWithDiskContainer(vmName, containerUUID string) string {
return fmt.Sprintf(`
data "nutanix_clusters" "clusters" {}
Expand All @@ -96,7 +98,7 @@ func testAccVMDataSourceWithDiskContainer(vmName string) string {
storage_config {
storage_container_reference {
kind = "storage_container"
uuid = "2bbe77bc-fd14-4697-8de1-6369757f9219"
uuid = "%s"
}
}
}
Expand All @@ -105,7 +107,7 @@ func testAccVMDataSourceWithDiskContainer(vmName string) string {
data "nutanix_virtual_machine" "nutanix_virtual_machine" {
vm_id = nutanix_virtual_machine.vm-disk.id
}
`, vmName)
`, vmName, containerUUID)
}

func testAccVMDataSourceConfig(r int) string {
Expand Down
2 changes: 1 addition & 1 deletion nutanix/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func expandReference(ref map[string]interface{}) *v3.Reference {
r.UUID = utils.StringPtr(v.(string))
hasValue = true
}
if v, ok := ref["name"]; ok {
if v, ok := ref["name"]; ok && v.(string) != "" {
r.Name = utils.StringPtr(v.(string))
hasValue = true
}
Expand Down
5 changes: 3 additions & 2 deletions nutanix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func testAccPreCheck(t *testing.T) {
os.Getenv("NUTANIX_PASSWORD") == "" ||
os.Getenv("NUTANIX_INSECURE") == "" ||
os.Getenv("NUTANIX_PORT") == "" ||
os.Getenv("NUTANIX_ENDPOINT") == "" {
t.Fatal("`NUTANIX_USERNAME`,`NUTANIX_PASSWORD`,`NUTANIX_INSECURE`,`NUTANIX_PORT`,`NUTANIX_ENDPOINT` must be set for acceptance testing")
os.Getenv("NUTANIX_ENDPOINT") == "" ||
os.Getenv("NUTANIX_STORAGE_CONTAINER") == "" {
t.Fatal("`NUTANIX_USERNAME`,`NUTANIX_PASSWORD`,`NUTANIX_INSECURE`,`NUTANIX_PORT`,`NUTANIX_ENDPOINT`, `NUTANIX_STORAGE_CONTAINER` must be set for acceptance testing")
}
}

Expand Down
45 changes: 27 additions & 18 deletions nutanix/resource_nutanix_access_control_policy_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nutanix

import (
"encoding/json"
"fmt"
"strings"
"testing"
Expand All @@ -16,6 +15,7 @@ const resourceAccessPolicy = "nutanix_access_control_policy.test"

func TestAccNutanixAccessControlPolicy_basic(t *testing.T) {
name := acctest.RandomWithPrefix("accest-access-policy")
roleName := acctest.RandomWithPrefix("test-acc-role")
description := "Description of my access control policy"
nameUpdated := acctest.RandomWithPrefix("accest-access-policy")
descriptionUpdated := "Description of my access control policy updated"
Expand All @@ -26,15 +26,15 @@ func TestAccNutanixAccessControlPolicy_basic(t *testing.T) {
CheckDestroy: testAccCheckNutanixAccessControlPolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixAccessControlPolicyConfig(name, description),
Config: testAccNutanixAccessControlPolicyConfig(name, description, roleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixAccessControlPolicyExists(),
resource.TestCheckResourceAttr(resourceAccessPolicy, "name", name),
resource.TestCheckResourceAttr(resourceAccessPolicy, "description", description),
),
},
{
Config: testAccNutanixAccessControlPolicyConfig(nameUpdated, descriptionUpdated),
Config: testAccNutanixAccessControlPolicyConfig(nameUpdated, descriptionUpdated, roleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixAccessControlPolicyExists(),
resource.TestCheckResourceAttr(resourceAccessPolicy, "name", nameUpdated),
Expand All @@ -52,21 +52,32 @@ func TestAccNutanixAccessControlPolicy_basic(t *testing.T) {

func TestAccNutanixAccessControlPolicy_WithUser(t *testing.T) {
name := acctest.RandomWithPrefix("accest-access-policy")
roleName := acctest.RandomWithPrefix("test-acc-role-")
description := "Description of my access control policy"
nameUpdated := acctest.RandomWithPrefix("accest-access-policy")
descriptionUpdated := "Description of my access control policy updated"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixAccessControlPolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixAccessControlPolicyConfigWithUser(name, description),
Config: testAccNutanixAccessControlPolicyConfigWithUser(name, description, roleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixAccessControlPolicyExists(),
resource.TestCheckResourceAttr(resourceAccessPolicy, "name", name),
resource.TestCheckResourceAttr(resourceAccessPolicy, "description", description),
),
},
{
Config: testAccNutanixAccessControlPolicyConfigWithUser(nameUpdated, descriptionUpdated, roleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixAccessControlPolicyExists(),
resource.TestCheckResourceAttr(resourceAccessPolicy, "name", nameUpdated),
resource.TestCheckResourceAttr(resourceAccessPolicy, "description", descriptionUpdated),
),
},
{
ResourceName: resourceAccessPolicy,
ImportState: true,
Expand All @@ -78,6 +89,7 @@ func TestAccNutanixAccessControlPolicy_WithUser(t *testing.T) {

func TestAccNutanixAccessControlPolicy_WithCategory(t *testing.T) {
name := acctest.RandomWithPrefix("accest-access-policy")
roleName := acctest.RandomWithPrefix("test-acc-role-")
description := "Description of my access control policy"
nameUpdated := acctest.RandomWithPrefix("accest-access-policy")
descriptionUpdated := "Description of my access control policy updated"
Expand All @@ -88,7 +100,7 @@ func TestAccNutanixAccessControlPolicy_WithCategory(t *testing.T) {
CheckDestroy: testAccCheckNutanixAccessControlPolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixAccessControlPolicyConfigWithCategory(name, description, "Production"),
Config: testAccNutanixAccessControlPolicyConfigWithCategory(name, description, "Production", roleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixAccessControlPolicyExists(),
testAccCheckNutanixCategories(resourceAccessPolicy),
Expand All @@ -100,7 +112,7 @@ func TestAccNutanixAccessControlPolicy_WithCategory(t *testing.T) {
),
},
{
Config: testAccNutanixAccessControlPolicyConfigWithCategory(nameUpdated, descriptionUpdated, "Staging"),
Config: testAccNutanixAccessControlPolicyConfigWithCategory(nameUpdated, descriptionUpdated, "Staging", roleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixAccessControlPolicyExists(),
resource.TestCheckResourceAttr(resourceAccessPolicy, "categories.#", "1"),
Expand All @@ -126,9 +138,6 @@ func testAccCheckNutanixAccessControlPolicyExists() resource.TestCheckFunc {
return fmt.Errorf("not found: %s", resourceAccessPolicy)
}

pretty, _ := json.MarshalIndent(rs, "", " ")
fmt.Print("\n\n[DEBUG] State of AccessControlPolicy", string(pretty))

if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}
Expand All @@ -155,10 +164,10 @@ func testAccCheckNutanixAccessControlPolicyDestroy(s *terraform.State) error {
return nil
}

func testAccNutanixAccessControlPolicyConfig(name, description string) string {
func testAccNutanixAccessControlPolicyConfig(name, description, roleName string) string {
return fmt.Sprintf(`
resource "nutanix_role" "test" {
name = "test role"
name = "%[3]s"
description = "description role"
permission_reference_list {
kind = "permission"
Expand All @@ -173,13 +182,13 @@ resource "nutanix_access_control_policy" "test" {
uuid = nutanix_role.test.id
}
}
`, name, description)
`, name, description, roleName)
}

func testAccNutanixAccessControlPolicyConfigWithCategory(name, description, categoryValue string) string {
func testAccNutanixAccessControlPolicyConfigWithCategory(name, description, categoryValue, roleName string) string {
return fmt.Sprintf(`
resource "nutanix_role" "test" {
name = "test role"
name = "%[4]s"
description = "description role"
permission_reference_list {
kind = "permission"
Expand All @@ -198,13 +207,13 @@ resource "nutanix_access_control_policy" "test" {
value = "%[3]s"
}
}
`, name, description, categoryValue)
`, name, description, categoryValue, roleName)
}

func testAccNutanixAccessControlPolicyConfigWithUser(name, description string) string {
func testAccNutanixAccessControlPolicyConfigWithUser(name, description, roleName string) string {
return fmt.Sprintf(`
resource "nutanix_role" "test" {
name = "test role 2"
name = "%[3]s"
description = "description role"
permission_reference_list {
kind = "permission"
Expand Down Expand Up @@ -288,5 +297,5 @@ resource "nutanix_access_control_policy" "test" {
}
}
}
`, name, description)
`, name, description, roleName)
}
14 changes: 7 additions & 7 deletions nutanix/resource_nutanix_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,21 +475,21 @@ func resourceNutanixProjectUpdate(d *schema.ResourceData, meta interface{}) erro
return err
}

uuid := *resp.Metadata.UUID
taskUUID := resp.Status.ExecutionContext.TaskUUID.(string)

// Wait for the Image to be available
// Wait for the Project to be available
stateConf := &resource.StateChangeConf{
Pending: []string{"QUEUED", "RUNNING"},
Target: []string{"SUCCEEDED"},
Refresh: taskStateRefreshFunc(conn, taskUUID),
Timeout: subnetTimeout,
Delay: subnetDelay,
MinTimeout: subnetMinTimeout,
Timeout: vmTimeout,
Delay: vmDelay,
MinTimeout: vmMinTimeout,
}

if _, err := stateConf.WaitForState(); err != nil {
d.SetId("")
return fmt.Errorf("error waiting for project (%s) to update: %s", d.Id(), err)
if _, errWaitTask := stateConf.WaitForState(); errWaitTask != nil {
return fmt.Errorf("error waiting for project(%s) to update: %s", uuid, errWaitTask)
}

return resourceNutanixProjectRead(d, meta)
Expand Down
Loading

0 comments on commit 9273c14

Please sign in to comment.