From be63f85a384a83b4b38b634795970d327be781f0 Mon Sep 17 00:00:00 2001 From: hirosassa Date: Sun, 7 Nov 2021 16:00:04 +0900 Subject: [PATCH] Add acc test (#11) * update doc * add acc test * update doc --- docs/resources/group_membership.md | 5 +- .../looker_group_membership/resource.tf | 5 +- pkg/looker/resource_connection.go | 2 +- pkg/looker/resource_connection_test.go | 50 ++++++++++++++++++- pkg/looker/resource_group_membership_test.go | 5 +- pkg/looker/resource_group_test.go | 36 ++++++++++++- pkg/looker/resource_model_set_test.go | 36 ++++++++++++- pkg/looker/resource_permission_set_test.go | 36 ++++++++++++- pkg/looker/resource_role_groups_test.go | 36 ++++++++++++- pkg/looker/resource_role_test.go | 36 ++++++++++++- pkg/looker/resource_user_roles_test.go | 38 +++++++++++++- pkg/looker/resource_user_test.go | 36 ++++++++++++- 12 files changed, 307 insertions(+), 14 deletions(-) diff --git a/docs/resources/group_membership.md b/docs/resources/group_membership.md index b2cc0d4..58d53b7 100644 --- a/docs/resources/group_membership.md +++ b/docs/resources/group_membership.md @@ -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] } ``` diff --git a/examples/resources/looker_group_membership/resource.tf b/examples/resources/looker_group_membership/resource.tf index 2b742ff..3260e03 100644 --- a/examples/resources/looker_group_membership/resource.tf +++ b/examples/resources/looker_group_membership/resource.tf @@ -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] } diff --git a/pkg/looker/resource_connection.go b/pkg/looker/resource_connection.go index 507d1a9..3c662ca 100644 --- a/pkg/looker/resource_connection.go +++ b/pkg/looker/resource_connection.go @@ -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 } diff --git a/pkg/looker/resource_connection_test.go b/pkg/looker/resource_connection_test.go index 628d343..d632485 100644 --- a/pkg/looker/resource_connection_test.go +++ b/pkg/looker/resource_connection_test.go @@ -2,11 +2,14 @@ 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) { @@ -14,11 +17,12 @@ func TestAcc_Connection(t *testing.T) { 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"), ), @@ -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" { diff --git a/pkg/looker/resource_group_membership_test.go b/pkg/looker/resource_group_membership_test.go index 95f6d7a..7453eb0 100644 --- a/pkg/looker/resource_group_membership_test.go +++ b/pkg/looker/resource_group_membership_test.go @@ -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( @@ -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 } diff --git a/pkg/looker/resource_group_test.go b/pkg/looker/resource_group_test.go index b669dba..bd6528f 100644 --- a/pkg/looker/resource_group_test.go +++ b/pkg/looker/resource_group_test.go @@ -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) { @@ -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), @@ -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" { diff --git a/pkg/looker/resource_model_set_test.go b/pkg/looker/resource_model_set_test.go index 51270b8..e0cf83d 100644 --- a/pkg/looker/resource_model_set_test.go +++ b/pkg/looker/resource_model_set_test.go @@ -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) { @@ -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), @@ -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" { diff --git a/pkg/looker/resource_permission_set_test.go b/pkg/looker/resource_permission_set_test.go index 2cf5eee..6d4c351 100644 --- a/pkg/looker/resource_permission_set_test.go +++ b/pkg/looker/resource_permission_set_test.go @@ -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) { @@ -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), @@ -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" { diff --git a/pkg/looker/resource_role_groups_test.go b/pkg/looker/resource_role_groups_test.go index 61bb9ce..5305d75 100644 --- a/pkg/looker/resource_role_groups_test.go +++ b/pkg/looker/resource_role_groups_test.go @@ -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_RoleGroups(t *testing.T) { @@ -14,7 +17,7 @@ func TestAcc_RoleGroups(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - Providers: providers(), + Providers: testAccProviders, Steps: []resource.TestStep{ { Config: roleGroupsConfig(name1), @@ -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" { diff --git a/pkg/looker/resource_role_test.go b/pkg/looker/resource_role_test.go index e15aff1..62f95e6 100644 --- a/pkg/looker/resource_role_test.go +++ b/pkg/looker/resource_role_test.go @@ -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_Role(t *testing.T) { @@ -14,7 +17,7 @@ func TestAcc_Role(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - Providers: providers(), + Providers: testAccProviders, Steps: []resource.TestStep{ { Config: roleConfig(name1), @@ -28,9 +31,40 @@ func TestAcc_Role(t *testing.T) { ImportStateVerify: true, }, }, + CheckDestroy: testAccCheckRoleDestroy, }) } +func testAccCheckRoleDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*apiclient.LookerSDK) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "looker_role" { + continue + } + + roleID, err := strconv.ParseInt(rs.Primary.ID, 10, 64) + if err != nil { + return err + } + + role, err := client.Role(roleID, nil) + if err != nil { + if strings.Contains(err.Error(), "404") { + return nil // successfully destroyed + } + return err + } + + if *role.Name == rs.Primary.Attributes["name"] { + return fmt.Errorf("role '%s' still exists", rs.Primary.ID) + } + + } + + return nil +} + func roleConfig(name string) string { return fmt.Sprintf(` resource "looker_model_set" "role_test" { diff --git a/pkg/looker/resource_user_roles_test.go b/pkg/looker/resource_user_roles_test.go index 1115bb1..14d0032 100644 --- a/pkg/looker/resource_user_roles_test.go +++ b/pkg/looker/resource_user_roles_test.go @@ -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_UserRoles(t *testing.T) { @@ -14,7 +17,7 @@ func TestAcc_UserRoles(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - Providers: providers(), + Providers: testAccProviders, Steps: []resource.TestStep{ { Config: userRolesConfig(name), @@ -28,9 +31,42 @@ func TestAcc_UserRoles(t *testing.T) { ImportStateVerify: true, }, }, + CheckDestroy: testAccCheckUserRoleDestroy, }) } +func testAccCheckUserRoleDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*apiclient.LookerSDK) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "looker_user_role" { + continue + } + + userID, err := strconv.ParseInt(rs.Primary.ID, 10, 64) + if err != nil { + return err + } + + request := apiclient.RequestUserRoles{UserId: userID} + + userRoles, err := client.UserRoles(request, nil) + if err != nil { + if strings.Contains(err.Error(), "404") { + return nil // successfully destroyed + } + return err + } + + if len(userRoles) != 0 { + return fmt.Errorf("user_role '%s' still exists", rs.Primary.ID) + } + + } + + return nil +} + func userRolesConfig(name string) string { return fmt.Sprintf(` resource "looker_user" "user_role_test" { diff --git a/pkg/looker/resource_user_test.go b/pkg/looker/resource_user_test.go index c2d81af..1625333 100644 --- a/pkg/looker/resource_user_test.go +++ b/pkg/looker/resource_user_test.go @@ -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_User(t *testing.T) { @@ -14,7 +17,7 @@ func TestAcc_User(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - Providers: providers(), + Providers: testAccProviders, Steps: []resource.TestStep{ { Config: userConfig(name, name, name), @@ -30,9 +33,40 @@ func TestAcc_User(t *testing.T) { ImportStateVerify: true, }, }, + CheckDestroy: testAccCheckUserDestroy, }) } +func testAccCheckUserDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*apiclient.LookerSDK) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "looker_user" { + continue + } + + userID, err := strconv.ParseInt(rs.Primary.ID, 10, 64) + if err != nil { + return err + } + + user, err := client.User(userID, "", nil) + if err != nil { + if strings.Contains(err.Error(), "404") { + return nil // successfully destroyed + } + return err + } + + if *user.Email == rs.Primary.Attributes["email"] { + return fmt.Errorf("user '%s' still exists", rs.Primary.ID) + } + + } + + return nil +} + func userConfig(firstName, lastName, email string) string { return fmt.Sprintf(` resource "looker_user" "test" {