Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,14 @@ func createSharedProject(t *testing.T) {
return
}

projectName := fmt.Sprintf("tf-acc-test-%d", time.Now().UnixNano())
// Use prefix from env var (set by scripts/run-acceptance-tests.sh) for auto-cleanup support
prefix := os.Getenv("ORY_TEST_PROJECT_PREFIX")
var projectName string
if prefix != "" {
projectName = fmt.Sprintf("%s-tf-%d", prefix, time.Now().UnixNano())
} else {
projectName = fmt.Sprintf("tf-acc-test-%d", time.Now().UnixNano())
}
t.Logf("Creating test project: %s (environment: prod)", projectName)

// Create as "prod" environment to support all features including organizations
Expand Down
19 changes: 16 additions & 3 deletions internal/resources/project/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"

Expand All @@ -27,16 +28,17 @@ func testAccPreCheck(t *testing.T) {
// WARNING: This test creates and deletes a real Ory project.
// Only run this test if you have quota available and understand the implications.
func TestAccProjectResource_basic(t *testing.T) {
projectName := testProjectName("basic")
acctest.RunTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
// Create and Read
{
Config: testAccProjectResourceConfig("tf-test-project", "dev"),
Config: testAccProjectResourceConfig(projectName, "dev"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("ory_project.test", "id"),
resource.TestCheckResourceAttr("ory_project.test", "name", "tf-test-project"),
resource.TestCheckResourceAttr("ory_project.test", "name", projectName),
resource.TestCheckResourceAttr("ory_project.test", "environment", "dev"),
resource.TestCheckResourceAttrSet("ory_project.test", "slug"),
resource.TestCheckResourceAttr("ory_project.test", "state", "running"),
Expand All @@ -54,12 +56,13 @@ func TestAccProjectResource_basic(t *testing.T) {

// TestAccProjectResource_prodEnvironment tests creating a production project.
func TestAccProjectResource_prodEnvironment(t *testing.T) {
projectName := testProjectName("prod")
acctest.RunTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccProjectResourceConfig("tf-test-prod-project", "prod"),
Config: testAccProjectResourceConfig(projectName, "prod"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("ory_project.test", "id"),
resource.TestCheckResourceAttr("ory_project.test", "environment", "prod"),
Expand All @@ -69,6 +72,16 @@ func TestAccProjectResource_prodEnvironment(t *testing.T) {
})
}

// testProjectName generates a project name with the e2e prefix for hard deletion support.
// The prefix is read from ORY_TEST_PROJECT_PREFIX env var (set by scripts/run-acceptance-tests.sh).
func testProjectName(suffix string) string {
prefix := os.Getenv("ORY_TEST_PROJECT_PREFIX")
if prefix != "" {
return fmt.Sprintf("%s-tf-%s-%d", prefix, suffix, time.Now().UnixNano())
}
return fmt.Sprintf("tf-acc-test-%s-%d", suffix, time.Now().UnixNano())
}

func testAccProjectResourceConfig(name, environment string) string {
return fmt.Sprintf(`
provider "ory" {}
Expand Down
6 changes: 5 additions & 1 deletion scripts/run-acceptance-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ parse_curl_response() {
# Configuration
CONSOLE_API_URL="${ORY_CONSOLE_API_URL:-https://api.console.ory.sh}"
PROJECT_API_URL="${ORY_PROJECT_API_URL:-https://%s.projects.oryapis.com}"
PROJECT_NAME="tf-acc-test-$(date +%s)"

# Projects with names starting with this prefix are automatically purged by the e2e cleanup job.
# DO NOT CHANGE THIS PREFIX - it must match the pattern in cloud/backoffice/backoffice/x/patterns.go
export ORY_TEST_PROJECT_PREFIX="ory-cy-e2e-da2f162d-af61-42dd-90dc-e3fcfa7c84a0"
PROJECT_NAME="${ORY_TEST_PROJECT_PREFIX}-tf-$(date +%s)"

# Validate required environment variables
if [[ -z "${ORY_WORKSPACE_API_KEY:-}" ]]; then
Expand Down