Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 1 addition & 1 deletion internal/service/chaos/chaos_hub/data_source_chaos_hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func dataSourceChaosHubRead(ctx context.Context, d *schema.ResourceData, meta in
}

func setChaosHubData(d *schema.ResourceData, hub *model.ChaosHubStatus, identifiers ScopedIdentifiersRequest) diag.Diagnostics {
d.SetId(hub.Identity)
d.SetId(hub.ID)
d.Set("org_id", identifiers.OrgIdentifier)
d.Set("project_id", identifiers.ProjectIdentifier)
d.Set("name", hub.Name)
Expand Down
130 changes: 38 additions & 92 deletions internal/service/chaos/chaos_hub/data_source_chaos_hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceChaosHub(t *testing.T) {
// Check for required environment variables
accountId := os.Getenv("HARNESS_ACCOUNT_ID")
if accountId == "" {
t.Skip("Skipping test because HARNESS_ACCOUNT_ID is not set")
func TestAccDataSourceChaosHub_ProjectLevel(t *testing.T) {
chaosGithubToken := os.Getenv("CHAOS_GITHUB_TOKEN")
if chaosGithubToken == "" {
t.Skip("Skipping test because CHAOS_GITHUB_TOKEN is not set")
}

// Generate unique identifiers
Expand All @@ -26,133 +25,80 @@ func TestAccDataSourceChaosHub(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
ExternalProviders: map[string]resource.ExternalProvider{
"time": {},
},
Steps: []resource.TestStep{
{
Config: testAccDataSourceChaosHubConfig(rName, id, "main"),
Config: testAccDataSourceChaosHubProjectLevelConfig(rName, id, "master", chaosGithubToken),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", rName),
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "project_id", resourceName, "project_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "org_id", resourceName, "org_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "connector_id", resourceName, "connector_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "repo_branch", resourceName, "repo_branch"),
resource.TestCheckResourceAttr(dataSourceName, "connector_scope", "ACCOUNT"),
resource.TestCheckResourceAttrSet(dataSourceName, "created_at"),
resource.TestCheckResourceAttrSet(dataSourceName, "is_available"),
resource.TestCheckResourceAttrSet(dataSourceName, "last_synced_at"),
resource.TestCheckResourceAttrSet(dataSourceName, "total_experiments"),
resource.TestCheckResourceAttrSet(dataSourceName, "total_faults"),
resource.TestCheckResourceAttrSet(dataSourceName, "updated_at"),
),
},
},
})
}

func TestAccDataSourceChaosHub_ProjectLevel(t *testing.T) {
// Check for required environment variables
accountId := os.Getenv("HARNESS_ACCOUNT_ID")
if accountId == "" {
t.Skip("Skipping test because HARNESS_ACCOUNT_ID is not set")
}

// Generate unique identifiers
id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
rName := id
dataSourceName := "data.harness_chaos_hub.test"
resourceName := "harness_chaos_hub.test"

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceChaosHubProjectLevelConfig(rName, id, "develop"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", rName),
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "project_id", resourceName, "project_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "org_id", resourceName, "org_id"),
resource.TestCheckResourceAttr(dataSourceName, "connector_scope", "PROJECT"),
),
},
},
})
}

func testAccDataSourceChaosHubConfig(name, id, branch string) string {
// Use the account ID from environment variables
accountId := os.Getenv("HARNESS_ACCOUNT_ID")
if accountId == "" {
accountId = "test" // Default for test cases when not set
}

return fmt.Sprintf(`
resource "harness_platform_connector_github" "test" {
identifier = "%[2]s"
name = "%[1]s"
description = "Test connector"
url = "https://github.com"
connection_type = "Account"
validation_repo = "harness/chaos-hub"

credentials {
http {
username = "test"
token_ref = "account.do_not_delete_harness_platform_qa_token"
}
}
}

resource "harness_chaos_hub" "test" {
name = "%[1]s"
connector_id = harness_platform_connector_github.test.id
connector_scope = "ACCOUNT"
repo_branch = "%[3]s"
description = "Test chaos hub"
tags = ["test:true", "chaos:true"]
}

data "harness_chaos_hub" "test" {
name = harness_chaos_hub.test.name
}
`, name, id, branch)
}

func testAccDataSourceChaosHubProjectLevelConfig(name, id, branch string) string {
// Use the account ID from environment variables
accountId := os.Getenv("HARNESS_ACCOUNT_ID")
if accountId == "" {
accountId = "test" // Default for test cases when not set
}
// Terraform Configurations

func testAccDataSourceChaosHubProjectLevelConfig(name, id, branch, githubToken string) string {
return fmt.Sprintf(`
resource "harness_platform_organization" "test" {
identifier = "%[2]s"
name = "%[1]s"
account_id = "%[4]s"
}

resource "harness_platform_project" "test" {
identifier = "%[2]s"
name = "%[1]s"
org_id = harness_platform_organization.test.id
account_id = "%[4]s"
color = "#0063F7"
description = "Test project for Chaos Hub"
tags = ["foo:bar", "baz:qux"]
}

resource "harness_platform_secret_text" "test" {
identifier = "%[2]s"
name = "%[1]s"
description = "test"
tags = ["foo:bar"]

secret_manager_identifier = "harnessSecretManager"
value_type = "Inline"
value = "%[4]s"
}

resource "harness_platform_connector_github" "test" {
identifier = "%[2]s"
name = "%[1]s"
description = "Test connector"
url = "https://github.com"
connection_type = "Project"
validation_repo = "harness/chaos-hub"
url = "https://github.com/litmuschaos/chaos-charts"
connection_type = "Repo"
project_id = harness_platform_project.test.id
org_id = harness_platform_organization.test.id

credentials {
http {
username = "test"
token_ref = "account.do_not_delete_harness_platform_qa_token"
username = "admin"
token_ref = "account.${harness_platform_secret_text.test.id}"
}
}
depends_on = [time_sleep.wait_4_seconds]
}

resource "time_sleep" "wait_4_seconds" {
depends_on = [harness_platform_secret_text.test]
destroy_duration = "4s"
}

resource "harness_chaos_hub" "test" {
Expand All @@ -171,5 +117,5 @@ func testAccDataSourceChaosHubProjectLevelConfig(name, id, branch string) string
org_id = harness_platform_organization.test.id
project_id = harness_platform_project.test.id
}
`, name, id, branch, accountId)
`, name, id, branch, githubToken)
}
Loading