From 9747f3bae56dcb3cb349ed9554f0fd4693aaf493 Mon Sep 17 00:00:00 2001 From: Victor Moraes Date: Mon, 25 Sep 2023 12:35:01 -0300 Subject: [PATCH] Add tests for data source permission --- cyral/data_source_cyral_permission_test.go | 78 ++++++++++++++++++++ cyral/resource_cyral_service_account_test.go | 22 ++++-- 2 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 cyral/data_source_cyral_permission_test.go diff --git a/cyral/data_source_cyral_permission_test.go b/cyral/data_source_cyral_permission_test.go new file mode 100644 index 00000000..e7223ffb --- /dev/null +++ b/cyral/data_source_cyral_permission_test.go @@ -0,0 +1,78 @@ +package cyral + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +var expectedPermissionNames = []string{ + "Approval Management", + "Modify Policies", + "Modify Roles", + "Modify Sidecars and Repositories", + "Modify Users", + "Repo Crawler", + "View Audit Logs", + "View Datamaps", + "View Integrations", + "View Policies", + "View Roles", + "View Users", + "Modify Integrations", +} + +func TestAccPermissionDataSource(t *testing.T) { + testSteps := []resource.TestStep{} + dataSourceName1 := "permissions_1" + testSteps = append( + testSteps, + []resource.TestStep{ + accTestStepPermissionDataSource_RetrieveAllPermissions(dataSourceName1), + }..., + ) + + resource.ParallelTest(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: testSteps, + }) +} + +func accTestStepPermissionDataSource_RetrieveAllPermissions(dataSourceName string) resource.TestStep { + dataSourceFullName := fmt.Sprintf("data.cyral_permission.%s", dataSourceName) + config := fmt.Sprintf(` + data "cyral_permission" "%s" { + } + `, dataSourceName) + var checks []resource.TestCheckFunc + for index, expectedPermissionName := range expectedPermissionNames { + checks = append(checks, + []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet( + dataSourceFullName, + fmt.Sprintf( + "%s.%d.%s", + PermissionDataSourcePermissionListKey, + index, + IDKey, + ), + ), + resource.TestCheckTypeSetElemNestedAttrs( + dataSourceFullName, + fmt.Sprintf("%s.*", PermissionDataSourcePermissionListKey), + map[string]string{NameKey: expectedPermissionName}, + ), + resource.TestCheckTypeSetElemNestedAttrs( + dataSourceFullName, + fmt.Sprintf("%s.*", PermissionDataSourcePermissionListKey), + map[string]string{DescriptionKey: expectedPermissionName}, + ), + }..., + ) + } + return resource.TestStep{ + Config: config, + Check: resource.ComposeTestCheckFunc(checks...), + } +} diff --git a/cyral/resource_cyral_service_account_test.go b/cyral/resource_cyral_service_account_test.go index 3c70713a..14db8408 100644 --- a/cyral/resource_cyral_service_account_test.go +++ b/cyral/resource_cyral_service_account_test.go @@ -10,17 +10,17 @@ import ( func TestAccServiceAccountResource(t *testing.T) { testSteps := []resource.TestStep{} - serviceAccountName1 := accTestName("service-account", "1") + resourceName1 := "service_account_1" testSteps = append( testSteps, []resource.TestStep{ - accTestStepServiceAccountResource_RequiredArgumentDisplayName(serviceAccountName1), - accTestStepServiceAccountResource_RequiredArgumentPermissions(serviceAccountName1), - accTestStepServiceAccountResource_EmptyPermissions(serviceAccountName1), - accTestStepServiceAccountResource_SinglePermission(serviceAccountName1), - accTestStepServiceAccountResource_DuplicatedPermission(serviceAccountName1), - accTestStepServiceAccountResource_AllPermissions(serviceAccountName1), - accTestStepServiceAccountResource_UpdatedFields(serviceAccountName1), + accTestStepServiceAccountResource_RequiredArgumentDisplayName(resourceName1), + accTestStepServiceAccountResource_RequiredArgumentPermissions(resourceName1), + accTestStepServiceAccountResource_EmptyPermissions(resourceName1), + accTestStepServiceAccountResource_SinglePermission(resourceName1), + accTestStepServiceAccountResource_DuplicatedPermission(resourceName1), + accTestStepServiceAccountResource_AllPermissions(resourceName1), + accTestStepServiceAccountResource_UpdatedFields(resourceName1), }..., ) @@ -212,6 +212,12 @@ func getAccTestStepForServiceAccountResourceFullConfig( fmt.Sprintf("%s.#", serviceAccountResourcePermissionIDsKey), fmt.Sprintf("%d", len(permissionNames)), ), + resource.TestCheckResourceAttrPair( + resourceFullName, + IDKey, + resourceFullName, + serviceAccountResourceClientIDKey, + ), resource.TestCheckResourceAttrSet( resourceFullName, serviceAccountResourceClientIDKey,