Skip to content

Commit bbc1548

Browse files
committed
Fix TestAccCloudStackRole_basic test
1. Add required 'type' parameter to role resource configuration in both resource and data source tests 2. Modify testAccCheckCloudStackRoleDestroy function to handle potential panic when accessing l.Roles[0]
1 parent 55ff8c1 commit bbc1548

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

cloudstack/data_source_cloudstack_role_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ resource "cloudstack_role" "foo" {
5050
name = "terraform-role"
5151
description = "terraform test role"
5252
is_public = true
53+
type = "User"
5354
}
5455
5556
data "cloudstack_role" "role" {

cloudstack/resource_cloudstack_role_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,23 @@ func testAccCheckCloudStackRoleDestroy(s *terraform.State) error {
9292
return fmt.Errorf("No Role ID is set")
9393
}
9494

95-
_, _, err := cs.Role.GetRoleByID(rs.Primary.ID)
96-
if err == nil {
97-
return fmt.Errorf("Role %s still exists", rs.Primary.ID)
95+
// Use a defer/recover to catch the panic that might occur when trying to access l.Roles[0]
96+
var err error
97+
func() {
98+
defer func() {
99+
if r := recover(); r != nil {
100+
// If a panic occurs, it means the role doesn't exist, which is what we want
101+
err = nil
102+
}
103+
}()
104+
r, _, e := cs.Role.GetRoleByID(rs.Primary.ID)
105+
if e == nil && r != nil && r.Id == rs.Primary.ID {
106+
err = fmt.Errorf("Role %s still exists", rs.Primary.ID)
107+
}
108+
}()
109+
110+
if err != nil {
111+
return err
98112
}
99113
}
100114

@@ -106,5 +120,6 @@ resource "cloudstack_role" "foo" {
106120
name = "terraform-role"
107121
description = "terraform test role"
108122
is_public = true
123+
type = "User"
109124
}
110125
`

0 commit comments

Comments
 (0)