diff --git a/internal/provider/resource_definition_resource_test.go b/internal/provider/resource_definition_resource_test.go index 635740b..fa9f966 100644 --- a/internal/provider/resource_definition_resource_test.go +++ b/internal/provider/resource_definition_resource_test.go @@ -118,19 +118,67 @@ func TestAccResourceDefinition(t *testing.T) { resourceAttrNameUpdateValue2: "test-2", }, { - name: "S3 static - secret refs", + name: "S3 static - secrets", configCreate: func() string { - return testAccResourceDefinitionS3taticResourceWithSecretRefs("accessKeyIdPath1", "secretAccessKeyPath1") + return testAccResourceDefinitionS3taticResourceWithSecrets("accessKeyId1", "secretAccessKey1") }, resourceAttrNameIDValue: "s3-test-with-secrets", resourceAttrNameUpdateKey: "driver_inputs.secret_refs", - resourceAttrNameUpdateValue1: "{\"aws_access_key_id\":{\"ref\":\"accessKeyIdPath1\",\"store\":\"external-secret-store\",\"version\":\"1\"},\"aws_secret_access_key\":{\"ref\":\"secretAccessKeyPath1\",\"store\":\"external-secret-store\",\"version\":\"1\"}}", + resourceAttrNameUpdateValue1: fmt.Sprintf("{\"aws_access_key_id\":{\"ref\":\"%s/aws_access_key_id/.value\",\"store\":\"humanitec\",\"version\":\"1\"},\"aws_secret_access_key\":{\"ref\":\"%s/aws_secret_access_key/.value\",\"store\":\"humanitec\",\"version\":\"1\"}}", getDefinitionSecretPath("s3-test-with-secrets"), getDefinitionSecretPath("s3-test-with-secrets")), resourceAttrName: "humanitec_resource_definition.s3_test_with_secrets", configUpdate: func() string { - return testAccResourceDefinitionS3taticResourceWithSecretRefs("accessKeyIdPath2", "secretAccessKeyPath2") + return testAccResourceDefinitionS3taticResourceWithSecrets("accessKeyId2", "secretAccessKey2") }, - resourceAttrNameUpdateValue2: "{\"aws_access_key_id\":{\"ref\":\"accessKeyIdPath2\",\"store\":\"external-secret-store\",\"version\":\"1\"},\"aws_secret_access_key\":{\"ref\":\"secretAccessKeyPath2\",\"store\":\"external-secret-store\",\"version\":\"1\"}}", + resourceAttrNameUpdateValue2: fmt.Sprintf("{\"aws_access_key_id\":{\"ref\":\"%s/aws_access_key_id/.value\",\"store\":\"humanitec\",\"version\":\"2\"},\"aws_secret_access_key\":{\"ref\":\"%s/aws_secret_access_key/.value\",\"store\":\"humanitec\",\"version\":\"2\"}}", getDefinitionSecretPath("s3-test-with-secrets"), getDefinitionSecretPath("s3-test-with-secrets")), }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Create and Read testing + { + Config: tc.configCreate(), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(tc.resourceAttrName, "id", tc.resourceAttrNameIDValue), + resource.TestCheckResourceAttr(tc.resourceAttrName, tc.resourceAttrNameUpdateKey, tc.resourceAttrNameUpdateValue1), + ), + }, + // ImportState testing + { + ResourceName: tc.resourceAttrName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"driver_inputs.secrets_string", "force_delete"}, + }, + // Update and Read testing + { + Config: tc.configUpdate(), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(tc.resourceAttrName, tc.resourceAttrNameUpdateKey, tc.resourceAttrNameUpdateValue2), + ), + }, + // Delete testing automatically occurs in TestCase + }, + }) + }) + } +} + +func TestAccResourceDefinitionSecretRefsWithValues(t *testing.T) { + tests := []struct { + name string + configCreate func() string + configUpdate func() string + resourceAttrName string + resourceAttrNameIDValue string + resourceAttrNameUpdateKey string + resourceAttrNameUpdateValue1 string + resourceAttrNameUpdateValue2 string + }{ { name: "S3 static - secret ref set values", configCreate: func() string { @@ -145,19 +193,67 @@ func TestAccResourceDefinition(t *testing.T) { }, resourceAttrNameUpdateValue2: "{\"aws_access_key_id\":{\"value\":\"accessKeyId2\"},\"aws_secret_access_key\":{\"value\":\"secretAccessKey2\"}}", }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Create and Read testing + { + Config: tc.configCreate(), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(tc.resourceAttrName, "id", tc.resourceAttrNameIDValue), + resource.TestCheckResourceAttr(tc.resourceAttrName, tc.resourceAttrNameUpdateKey, tc.resourceAttrNameUpdateValue1), + ), + }, + // ImportState testing + { + ResourceName: tc.resourceAttrName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"driver_inputs.secret_refs", "force_delete"}, + }, + // Update and Read testing + { + Config: tc.configUpdate(), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(tc.resourceAttrName, tc.resourceAttrNameUpdateKey, tc.resourceAttrNameUpdateValue2), + ), + }, + // Delete testing automatically occurs in TestCase + }, + }) + }) + } +} + +func TestAccResourceDefinitionSecretRefs(t *testing.T) { + tests := []struct { + name string + configCreate func() string + configUpdate func() string + resourceAttrName string + resourceAttrNameIDValue string + resourceAttrNameUpdateKey string + resourceAttrNameUpdateValue1 string + resourceAttrNameUpdateValue2 string + }{ { - name: "S3 static - secrets", + name: "S3 static - secret refs", configCreate: func() string { - return testAccResourceDefinitionS3taticResourceWithSecrets("accessKeyId1", "secretAccessKey1") + return testAccResourceDefinitionS3taticResourceWithSecretRefs("accessKeyIdPath1", "secretAccessKeyPath1") }, resourceAttrNameIDValue: "s3-test-with-secrets", resourceAttrNameUpdateKey: "driver_inputs.secret_refs", - resourceAttrNameUpdateValue1: fmt.Sprintf("{\"aws_access_key_id\":{\"ref\":\"%s/aws_access_key_id/.value\",\"store\":\"humanitec\",\"version\":\"1\"},\"aws_secret_access_key\":{\"ref\":\"%s/aws_secret_access_key/.value\",\"store\":\"humanitec\",\"version\":\"1\"}}", getDefinitionSecretPath("s3-test-with-secrets"), getDefinitionSecretPath("s3-test-with-secrets")), + resourceAttrNameUpdateValue1: "{\"aws_access_key_id\":{\"ref\":\"accessKeyIdPath1\",\"store\":\"external-secret-store\",\"version\":\"1\"},\"aws_secret_access_key\":{\"ref\":\"secretAccessKeyPath1\",\"store\":\"external-secret-store\",\"version\":\"1\"}}", resourceAttrName: "humanitec_resource_definition.s3_test_with_secrets", configUpdate: func() string { - return testAccResourceDefinitionS3taticResourceWithSecrets("accessKeyId2", "secretAccessKey2") + return testAccResourceDefinitionS3taticResourceWithSecretRefs("accessKeyIdPath2", "secretAccessKeyPath2") }, - resourceAttrNameUpdateValue2: fmt.Sprintf("{\"aws_access_key_id\":{\"ref\":\"%s/aws_access_key_id/.value\",\"store\":\"humanitec\",\"version\":\"2\"},\"aws_secret_access_key\":{\"ref\":\"%s/aws_secret_access_key/.value\",\"store\":\"humanitec\",\"version\":\"2\"}}", getDefinitionSecretPath("s3-test-with-secrets"), getDefinitionSecretPath("s3-test-with-secrets")), + resourceAttrNameUpdateValue2: "{\"aws_access_key_id\":{\"ref\":\"accessKeyIdPath2\",\"store\":\"external-secret-store\",\"version\":\"1\"},\"aws_secret_access_key\":{\"ref\":\"secretAccessKeyPath2\",\"store\":\"external-secret-store\",\"version\":\"1\"}}", }, } @@ -180,7 +276,7 @@ func TestAccResourceDefinition(t *testing.T) { ResourceName: tc.resourceAttrName, ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"driver_inputs.secrets_string", "driver_inputs.secret_refs", "force_delete"}, + ImportStateVerifyIgnore: []string{"force_delete"}, }, // Update and Read testing {