Skip to content

Commit

Permalink
Use ephemeral workspace for Block test
Browse files Browse the repository at this point in the history
Uses the new ephemeral workspace helpers to create the new, random
workspace.
  • Loading branch information
mitchnielsen committed Jun 18, 2024
1 parent b6fc019 commit 58b8093
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions internal/provider/resources/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,36 @@ import (
"testing"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/testutils"
)

func fixtureAccBlock(name string, value string) string {
func fixtureAccBlock(workspace, workspaceName, blockName, blockValue string) string {
return fmt.Sprintf(`
resource "prefect_workspace" "workspace" {
name = "%s"
handle = "%s"
}
%s
resource "prefect_block" "block" {
name = "%s"
type_slug = "secret"
workspace_id = prefect_workspace.workspace.id
data = jsonencode({
"value" = "%s"
})
depends_on = [prefect_workspace.workspace]
}
`, name, name, name, value)
workspace_id = prefect_workspace.%s.id
depends_on = [prefect_workspace.%s]
}`, workspace, blockName, blockValue, workspaceName, workspaceName)
}

//nolint:paralleltest // we use the resource.ParallelTest helper instead
func TestAccResource_block(t *testing.T) {
blockResourceName := "prefect_block.block"
const workspaceResourceName = "prefect_workspace.workspace"
randomName := testutils.TestAccPrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
randomValue := testutils.TestAccPrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
randomValue2 := testutils.TestAccPrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
randomName := testutils.NewRandomPrefixedString()
randomValue := testutils.NewRandomPrefixedString()
randomValue2 := testutils.NewRandomPrefixedString()

workspace, workspaceName := testutils.NewEphemeralWorkspace()

blockResourceName := fmt.Sprintf("prefect_block.%s", randomName)
workspaceResourceName := fmt.Sprintf("prefect_workspace.%s", workspaceName)

// We use this variable to store the fetched block document resource from the API
// and it will be shared between the TestSteps via pointer.
Expand All @@ -50,9 +48,9 @@ func TestAccResource_block(t *testing.T) {
Steps: []resource.TestStep{
// Check creation + existence of the block resource
{
Config: fixtureAccBlock(randomName, randomValue),
Config: fixtureAccBlock(workspace, workspaceName, randomName, randomValue),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBlockExists(blockResourceName, workspaceResourceName, &blockDocument),
testAccCheckBlockExists(blockResourceName, workspaceName, &blockDocument),
testAccCheckBlockValues(&blockDocument, ExpectedBlockValues{
Name: randomName,
TypeSlug: "secret",
Expand All @@ -65,9 +63,9 @@ func TestAccResource_block(t *testing.T) {
},
// Check updating the value of the block resource
{
Config: fixtureAccBlock(randomName, randomValue2),
Config: fixtureAccBlock(workspace, workspaceName, randomName, randomValue2),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBlockExists(blockResourceName, workspaceResourceName, &blockDocument),
testAccCheckBlockExists(blockResourceName, workspaceName, &blockDocument),
testAccCheckBlockValues(&blockDocument, ExpectedBlockValues{
Name: randomName,
TypeSlug: "secret",
Expand Down

0 comments on commit 58b8093

Please sign in to comment.