-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
) * add service account import test * add import state tests * minor doc things * add variable resource test * add variable resource test * add tiny account test * add account and variable datasource tests
- Loading branch information
1 parent
7f5acb9
commit b4c5f78
Showing
16 changed files
with
304 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# Prefect Accounts can be imported using the account's UUID | ||
terraform import prefect_account.example account-uuid | ||
terraform import prefect_account.example 00000000-0000-0000-0000-000000000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# Prefect Workspace Roles can be imported using the workspace role's UUID | ||
terraform import prefect_workspace_role.example workspace-role-uuid | ||
terraform import prefect_workspace_role.example 00000000-0000-0000-0000-000000000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package datasources_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/prefecthq/terraform-provider-prefect/internal/testutils" | ||
) | ||
|
||
func fixtureAccAccount() string { | ||
return fmt.Sprintf(` | ||
data "prefect_account" "test" { | ||
id = "%s" | ||
} | ||
`, os.Getenv("PREFECT_CLOUD_ACCOUNT_ID")) | ||
} | ||
|
||
//nolint:paralleltest // we use the resource.ParallelTest helper instead | ||
func TestAccDatasource_account(t *testing.T) { | ||
datasourceName := "data.prefect_account.test" | ||
resource.ParallelTest(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, | ||
PreCheck: func() { testutils.AccTestPreCheck(t) }, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: fixtureAccAccount(), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(datasourceName, "id", os.Getenv("PREFECT_CLOUD_ACCOUNT_ID")), | ||
resource.TestCheckResourceAttrSet(datasourceName, "name"), | ||
resource.TestCheckResourceAttrSet(datasourceName, "handle"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package datasources_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/prefecthq/terraform-provider-prefect/internal/testutils" | ||
) | ||
|
||
func fixtureAccVariableByName(name string) string { | ||
return fmt.Sprintf(` | ||
data "prefect_workspace" "evergreen" { | ||
handle = "evergreen-workspace" | ||
} | ||
data "prefect_variable" "test" { | ||
name = "%s" | ||
workspace_id = data.prefect_workspace.evergreen.id | ||
} | ||
`, name) | ||
} | ||
|
||
//nolint:paralleltest // we use the resource.ParallelTest helper instead | ||
func TestAccDatasource_variable(t *testing.T) { | ||
datasourceName := "data.prefect_variable.test" | ||
variableName := "my_variable" | ||
variableValue := "variable value goes here" | ||
resource.ParallelTest(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, | ||
PreCheck: func() { testutils.AccTestPreCheck(t) }, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: fixtureAccVariableByName(variableName), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet(datasourceName, "id"), | ||
resource.TestCheckResourceAttr(datasourceName, "name", variableName), | ||
resource.TestCheckResourceAttr(datasourceName, "value", variableValue), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package resources_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/prefecthq/terraform-provider-prefect/internal/testutils" | ||
) | ||
|
||
//nolint:paralleltest // we use the resource.ParallelTest helper instead | ||
func TestAccResource_account(t *testing.T) { | ||
resourceName := "prefect_account.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, | ||
PreCheck: func() { testutils.AccTestPreCheck(t) }, | ||
Steps: []resource.TestStep{ | ||
// Import State checks - import by ID (from environment) | ||
// NOTE: the prefect_account resource is a little special in that | ||
// we cannot create an account via API, meaning the TF lifecycle | ||
// will be challenging to test. Instead, we'll ensure that the | ||
// resource can be found and properly imported. Note that | ||
// ImportStateVerify is set to false, as the resource can't be | ||
// saved to state after a Create. | ||
{ | ||
Config: `resource "prefect_account" "test" {}`, | ||
ImportStateId: os.Getenv("PREFECT_CLOUD_ACCOUNT_ID"), | ||
ImportState: true, | ||
ResourceName: resourceName, | ||
ImportStateVerify: false, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package resources_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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 fixtureAccVariableResource(name string, value string) string { | ||
return fmt.Sprintf(` | ||
data "prefect_workspace" "evergreen" { | ||
handle = "evergreen-workspace" | ||
} | ||
resource "prefect_variable" "test" { | ||
workspace_id = data.prefect_workspace.evergreen.id | ||
name = "%s" | ||
value = "%s" | ||
} | ||
`, name, value) | ||
} | ||
|
||
func fixtureAccVariableResourceWithTags(name string, value string) string { | ||
return fmt.Sprintf(` | ||
data "prefect_workspace" "evergreen" { | ||
handle = "evergreen-workspace" | ||
} | ||
resource "prefect_variable" "test" { | ||
workspace_id = data.prefect_workspace.evergreen.id | ||
name = "%s" | ||
value = "%s" | ||
tags = ["foo", "bar"] | ||
} | ||
`, name, value) | ||
} | ||
|
||
//nolint:paralleltest // we use the resource.ParallelTest helper instead | ||
func TestAccResource_variable(t *testing.T) { | ||
resourceName := "prefect_variable.test" | ||
const workspaceDatsourceName = "data.prefect_workspace.evergreen" | ||
|
||
randomName := testutils.TestAccPrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
randomName2 := testutils.TestAccPrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
|
||
randomValue := testutils.TestAccPrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
randomValue2 := testutils.TestAccPrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
|
||
// We use this variable to store the fetched resource from the API | ||
// and it will be shared between TestSteps via a pointer. | ||
var variable api.Variable | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, | ||
PreCheck: func() { testutils.AccTestPreCheck(t) }, | ||
Steps: []resource.TestStep{ | ||
{ | ||
// Check creation + existence of the variable resource | ||
Config: fixtureAccVariableResource(randomName, randomValue), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
testAccCheckVariableExists(resourceName, workspaceDatsourceName, &variable), | ||
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName, Value: randomValue}), | ||
resource.TestCheckResourceAttr(resourceName, "name", randomName), | ||
resource.TestCheckResourceAttr(resourceName, "value", randomValue), | ||
), | ||
}, | ||
{ | ||
// Check updating name + value of the variable resource | ||
Config: fixtureAccVariableResource(randomName2, randomValue2), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
testAccCheckVariableExists(resourceName, workspaceDatsourceName, &variable), | ||
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName2, Value: randomValue2}), | ||
resource.TestCheckResourceAttr(resourceName, "name", randomName2), | ||
resource.TestCheckResourceAttr(resourceName, "value", randomValue2), | ||
), | ||
}, | ||
{ | ||
// Check adding tags | ||
Config: fixtureAccVariableResourceWithTags(randomName2, randomValue2), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
testAccCheckVariableExists(resourceName, workspaceDatsourceName, &variable), | ||
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName2, Value: randomValue2}), | ||
resource.TestCheckResourceAttr(resourceName, "name", randomName2), | ||
resource.TestCheckResourceAttr(resourceName, "value", randomValue2), | ||
resource.TestCheckResourceAttr(resourceName, "tags.#", "2"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.0", "foo"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.1", "bar"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckVariableExists(variableResourceName string, workspaceDatasourceName string, variable *api.Variable) resource.TestCheckFunc { | ||
return func(state *terraform.State) error { | ||
variableResource, exists := state.RootModule().Resources[variableResourceName] | ||
if !exists { | ||
return fmt.Errorf("Resource not found in state: %s", variableResourceName) | ||
} | ||
variableResourceID, _ := uuid.Parse(variableResource.Primary.ID) | ||
|
||
workspaceDatsource, exists := state.RootModule().Resources[workspaceDatasourceName] | ||
if !exists { | ||
return fmt.Errorf("Resource not found in state: %s", workspaceDatasourceName) | ||
} | ||
workspaceID, _ := uuid.Parse(workspaceDatsource.Primary.ID) | ||
|
||
// Create a new client, and use the default configurations from the environment | ||
c, _ := testutils.NewTestClient() | ||
variablesClient, _ := c.Variables(uuid.Nil, workspaceID) | ||
|
||
variableName := variableResource.Primary.Attributes["name"] | ||
|
||
fetchedVariable, err := variablesClient.Get(context.Background(), variableResourceID) | ||
if err != nil { | ||
return fmt.Errorf("Error fetching variable: %w", err) | ||
} | ||
if fetchedVariable == nil { | ||
return fmt.Errorf("Variable not found for name: %s", variableName) | ||
} | ||
|
||
*variable = *fetchedVariable | ||
|
||
return nil | ||
} | ||
} | ||
func testAccCheckVariableValues(fetchedVariable *api.Variable, valuesToCheck *api.Variable) resource.TestCheckFunc { | ||
return func(state *terraform.State) error { | ||
if fetchedVariable.Name != valuesToCheck.Name { | ||
return fmt.Errorf("Expected variable name to be %s, got %s", valuesToCheck.Name, fetchedVariable.Name) | ||
} | ||
if fetchedVariable.Value != valuesToCheck.Value { | ||
return fmt.Errorf("Expected variable value to be %s, got %s", valuesToCheck.Name, fetchedVariable.Name) | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters