Skip to content

Commit

Permalink
fix: resource definition test
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjenek committed May 27, 2024
1 parent 34c5b09 commit 91dd0d9
Showing 1 changed file with 72 additions and 15 deletions.
87 changes: 72 additions & 15 deletions internal/provider/resource_definition_resource_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package provider

import (
"encoding/json"
"fmt"
"os"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccResourceDefinition(t *testing.T) {
Expand Down Expand Up @@ -155,21 +158,6 @@ func TestAccResourceDefinition(t *testing.T) {
resourceAttrNameUpdateValue2: "{\"aws_access_key_id\":{\"value\":\"accessKeyId2\"},\"aws_secret_access_key\":{\"value\":\"secretAccessKey2\"}}",
importStateVerifyIgnore: []string{"driver_inputs.secret_refs", "force_delete"},
},
{
name: "S3 static - secrets",
configCreate: func() string {
return testAccResourceDefinitionS3taticResourceWithSecrets("accessKeyId1", "secretAccessKey1")
},
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")),
resourceAttrName: "humanitec_resource_definition.s3_test_with_secrets",
configUpdate: func() string {
return testAccResourceDefinitionS3taticResourceWithSecrets("accessKeyId2", "secretAccessKey2")
},
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")),
importStateVerifyIgnore: []string{"driver_inputs.secrets_string", "force_delete"},
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -207,6 +195,71 @@ func TestAccResourceDefinition(t *testing.T) {
}
}

func TestAccResourceDefinition_S3_static_secrets(t *testing.T) {
var expectedSecretRef string
var expectedSecretRefAfterUpdate string
t.Run("S3 static - secrets", func(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and Read testing
{
Config: testAccResourceDefinitionS3taticResourceWithSecrets("accessKeyId1", "secretAccessKey1"),
Check: resource.ComposeAggregateTestCheckFunc(
func(s *terraform.State) error {
secretRefsRaw := s.Modules[0].Resources["humanitec_resource_definition.s3_test_with_secrets"].Primary.Attributes["driver_inputs.secret_refs"]
var secretRefs struct {
AWSAccessKey struct {
Ref string `json:"ref"`
Store string `json:"store"`
Version string `json:"version"`
} `json:"aws_access_key_id"`
AWSSecretKey struct {
Ref string `json:"ref"`
Store string `json:"store"`
Version string `json:"version"`
} `json:"aws_secret_access_key"`
}

err := json.Unmarshal([]byte(secretRefsRaw), &secretRefs)
if err != nil {
return err
}

currentVersion, err := strconv.Atoi(secretRefs.AWSAccessKey.Version)
if err != nil {
return err
}

expectedSecretRef = getDefinitionSecretRef(currentVersion)
expectedSecretRefAfterUpdate = getDefinitionSecretRef(currentVersion + 1)
return nil
},
resource.TestCheckResourceAttr("humanitec_resource_definition.s3_test_with_secrets", "id", "s3-test-with-secrets"),
resource.TestCheckResourceAttrPtr("humanitec_resource_definition.s3_test_with_secrets", "driver_inputs.secret_refs", &expectedSecretRef),
),
},
// ImportState testing
{
ResourceName: "humanitec_resource_definition.s3_test_with_secrets",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"driver_inputs.secrets_string", "force_delete"},
},
// Update and Read testing
{
Config: testAccResourceDefinitionS3taticResourceWithSecrets("accessKeyId2", "secretAccessKey2"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPtr("humanitec_resource_definition.s3_test_with_secrets", "driver_inputs.secret_refs", &expectedSecretRefAfterUpdate),
),
},
// Delete testing automatically occurs in TestCase
},
})
})
}

func testAccResourceDefinitionS3Resource(region string) string {
return fmt.Sprintf(`
resource "humanitec_resource_definition" "s3_test" {
Expand Down Expand Up @@ -424,3 +477,7 @@ func getDefinitionSecretPath(defID string) string {
orgID := os.Getenv("HUMANITEC_ORG")
return fmt.Sprintf("orgs/%s/resources/defs/%s/driver_secrets", orgID, defID)
}

func getDefinitionSecretRef(version int) string {
return fmt.Sprintf("{\"aws_access_key_id\":{\"ref\":\"%s/aws_access_key_id/.value\",\"store\":\"humanitec\",\"version\":\"%d\"},\"aws_secret_access_key\":{\"ref\":\"%s/aws_secret_access_key/.value\",\"store\":\"humanitec\",\"version\":\"%d\"}}", getDefinitionSecretPath("s3-test-with-secrets"), version, getDefinitionSecretPath("s3-test-with-secrets"), version)
}

0 comments on commit 91dd0d9

Please sign in to comment.