Skip to content

Commit

Permalink
Add acc test (#11)
Browse files Browse the repository at this point in the history
* update doc

* add acc test

* update doc
  • Loading branch information
hirosassa authored Nov 7, 2021
1 parent a7ac460 commit be63f85
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 14 deletions.
5 changes: 3 additions & 2 deletions docs/resources/group_membership.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ description: |-

```terraform
resource "looker_group_membership" "group_membership" {
group_id = looker_group.group.id
user_id = looker_user.user.id
target_group_id = looker_group.group.id
user_ids = [looker_user.user1.id, looker_user.user2.id, looker_user.user3.id]
group_ids = [looker_group.group1.id, looker_group.group2.id]
}
```

Expand Down
5 changes: 3 additions & 2 deletions examples/resources/looker_group_membership/resource.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "looker_group_membership" "group_membership" {
group_id = looker_group.group.id
user_id = looker_user.user.id
target_group_id = looker_group.group.id
user_ids = [looker_user.user1.id, looker_user.user2.id, looker_user.user3.id]
group_ids = [looker_group.group1.id, looker_group.group2.id]
}
2 changes: 1 addition & 1 deletion pkg/looker/resource_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func resourceConnectionRead(ctx context.Context, d *schema.ResourceData, m inter

connection, err := client.Connection(connectionName, "", nil)
if err != nil {
if strings.Contains(err.Error(), "Not found") {
if strings.Contains(err.Error(), "404") {
d.SetId("")
return nil
}
Expand Down
50 changes: 49 additions & 1 deletion pkg/looker/resource_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ package looker

import (
"fmt"
"log"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
)

func TestAcc_Connection(t *testing.T) {
name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: providers(),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: connectionConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckConnectionExists("looker_connection.test"),
resource.TestCheckResourceAttr("looker_connection.test", "name", strings.ToLower(name)),
resource.TestCheckResourceAttr("looker_connection.test", "host", "test_project"),
),
Expand All @@ -29,9 +33,53 @@ func TestAcc_Connection(t *testing.T) {
ImportStateVerify: true,
},
},
CheckDestroy: testAccCheckConnectionDestroy,
})
}

func testAccCheckConnectionExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("connection setting not found from resources: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("no connection setting ID is set")
}

client := testAccProvider.Meta().(*apiclient.LookerSDK)
connectionName := rs.Primary.ID

_, err := client.Connection(connectionName, "", nil)
if err !=nil {
return err
}

return nil
}
}
func testAccCheckConnectionDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*apiclient.LookerSDK)

for _, rs := range s.RootModule().Resources {
if rs.Type != "looker_connection" {
continue
}

connectionName := rs.Primary.ID
_, err := client.Connection(connectionName, "", nil)
if err != nil {
log.Printf("[WARN] get connection: %s", err.Error())
if strings.Contains(err.Error(), "404") {
return nil // successfully destroyed
}
return err
}
}

return nil
}

func connectionConfig(name string) string {
return fmt.Sprintf(`
resource "looker_connection" "test" {
Expand Down
5 changes: 4 additions & 1 deletion pkg/looker/resource_group_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestAcc_GroupMembership(t *testing.T) {
resource.TestCheckResourceAttr("looker_group_membership.test", "user_ids.#", "2"),
),
},
// Test: Create
// Test: Update
{
Config: groupMembershipConfigUpdate(user3),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -102,6 +102,9 @@ func testAccCheckGroupMembershipDestroy(s *terraform.State) error {

users, err := client.AllGroupUsers(apiclient.RequestAllGroupUsers{GroupId: targetGroupID}, nil)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil // successfully destroyed
}
return err
}

Expand Down
36 changes: 35 additions & 1 deletion pkg/looker/resource_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package looker

import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
)

func TestAcc_Group(t *testing.T) {
Expand All @@ -15,7 +18,7 @@ func TestAcc_Group(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: providers(),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: groupConfig(name1),
Expand All @@ -35,9 +38,40 @@ func TestAcc_Group(t *testing.T) {
ImportStateVerify: true,
},
},
CheckDestroy: testAccCheckGroupDestroy,
})
}

func testAccCheckGroupDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*apiclient.LookerSDK)

for _, rs := range s.RootModule().Resources {
if rs.Type != "looker_group" {
continue
}

groupID, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return err
}

group, err := client.Group(groupID, "", nil)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil // successfully destroyed
}
return err
}

if *group.Name == rs.Primary.Attributes["name"] {
return fmt.Errorf("group still exists: %s", rs.Primary.ID)
}
}

return nil

}

func groupConfig(name string) string {
return fmt.Sprintf(`
resource "looker_group" "test" {
Expand Down
36 changes: 35 additions & 1 deletion pkg/looker/resource_model_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package looker

import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
)

func TestAcc_ModelSet(t *testing.T) {
Expand All @@ -15,7 +18,7 @@ func TestAcc_ModelSet(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: providers(),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: modelSetConfig(name1),
Expand All @@ -37,9 +40,40 @@ func TestAcc_ModelSet(t *testing.T) {
ImportStateVerify: true,
},
},
CheckDestroy: testAccCheckModelSetDestroy,
})
}

func testAccCheckModelSetDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*apiclient.LookerSDK)

for _, rs := range s.RootModule().Resources {
if rs.Type != "looker_model_set" {
continue
}

modelSetID, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return err
}

modelSet, err := client.ModelSet(modelSetID, "", nil)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil // successfully destroyed
}
return err
}

if *modelSet.Name == rs.Primary.Attributes["name"] {
return fmt.Errorf("model_set '%s' still exists", rs.Primary.ID)
}

}

return nil
}

func modelSetConfig(name string) string {
return fmt.Sprintf(`
resource "looker_model_set" "test" {
Expand Down
36 changes: 35 additions & 1 deletion pkg/looker/resource_permission_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package looker

import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
)

func TestAcc_PermissionSet(t *testing.T) {
Expand All @@ -15,7 +18,7 @@ func TestAcc_PermissionSet(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: providers(),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: permissionSetConfig(name1),
Expand All @@ -37,9 +40,40 @@ func TestAcc_PermissionSet(t *testing.T) {
ImportStateVerify: true,
},
},
CheckDestroy: testAccCheckPermissionSetDestroy,
})
}

func testAccCheckPermissionSetDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*apiclient.LookerSDK)

for _, rs := range s.RootModule().Resources {
if rs.Type != "looker_permission_set" {
continue
}

permissionSetID, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return err
}

permissionSet, err := client.PermissionSet(permissionSetID, "", nil)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil // successfully destroyed
}
return err
}

if *permissionSet.Name == rs.Primary.Attributes["name"] {
return fmt.Errorf("permission_set '%s' still exists", rs.Primary.ID)
}

}

return nil
}

func permissionSetConfig(name string) string {
return fmt.Sprintf(`
resource "looker_permission_set" "test" {
Expand Down
36 changes: 35 additions & 1 deletion pkg/looker/resource_role_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ package looker

import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
)

func TestAcc_RoleGroups(t *testing.T) {
name1 := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: providers(),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: roleGroupsConfig(name1),
Expand All @@ -28,9 +31,40 @@ func TestAcc_RoleGroups(t *testing.T) {
ImportStateVerify: true,
},
},
CheckDestroy: testAccCheckRoleGroupsDestroy,
})
}

func testAccCheckRoleGroupsDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*apiclient.LookerSDK)

for _, rs := range s.RootModule().Resources {
if rs.Type != "looker_role_groups" {
continue
}

roleGroupsID, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return err
}

groups, err := client.RoleGroups(roleGroupsID, "", nil)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil // successfully destroyed
}
return err
}

if len(groups) != 0 {
return fmt.Errorf("role_groups '%s' still exists", rs.Primary.ID)
}

}

return nil
}

func roleGroupsConfig(name string) string {
return fmt.Sprintf(`
resource "looker_group" "test" {
Expand Down
Loading

0 comments on commit be63f85

Please sign in to comment.