From 1a9e8798457a8f9a56dee94a20d6287c4d09bd90 Mon Sep 17 00:00:00 2001 From: ivbalan-harness <115148179+ivbalan-harness@users.noreply.github.com> Date: Fri, 15 Nov 2024 11:16:04 +0200 Subject: [PATCH] Fix: [CDS-101657]: Fix TF Provider Tests (#1112) * Fix TestAccResourceGitopsProjectAccLevel * Fix TestAccDataSourceGitopsProjectAccLevel * Improve fix for gitops_app_project_test * Fix TestAccDataSourceGitopsCluster * Fix TestAccDataSourceGitopsApplication Signed-off-by: Mirko Teodorovic --------- Signed-off-by: Mirko Teodorovic Co-authored-by: Mirko Teodorovic --- .gitignore | 1 + .../datasource_gitops_applications_test.go | 69 +++++++++++++++---- .../resource_gitops_applications.go | 21 +++++- .../data_source_gitops_app_project_test.go | 9 +-- .../resource_gitops_app_project_test.go | 17 ++--- 5 files changed, 90 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index f9f1c541a..4d63a98f8 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ website/node_modules .vscode *debug* website/vendor +test-report.xml # Test exclusions !command/test-fixtures/**/*.tfstate diff --git a/internal/service/platform/gitops/applications/datasource_gitops_applications_test.go b/internal/service/platform/gitops/applications/datasource_gitops_applications_test.go index 615cdde17..a8a02e064 100644 --- a/internal/service/platform/gitops/applications/datasource_gitops_applications_test.go +++ b/internal/service/platform/gitops/applications/datasource_gitops_applications_test.go @@ -17,11 +17,17 @@ func TestAccDataSourceGitopsApplication(t *testing.T) { name := id agentId := os.Getenv("HARNESS_TEST_GITOPS_AGENT_ID") accountId := os.Getenv("HARNESS_ACCOUNT_ID") - clusterServer := os.Getenv("HARNESS_TEST_GITOPS_CLUSTER_SERVER_APP") - clusterId := os.Getenv("HARNESS_TEST_GITOPS_CLUSTER_ID") + // clusterServer := os.Getenv("HARNESS_TEST_GITOPS_CLUSTER_SERVER_APP") + // clusterId := os.Getenv("HARNESS_TEST_GITOPS_CLUSTER_ID") repoId := os.Getenv("HARNESS_TEST_GITOPS_REPO_ID") - repo := "https://github.com/argoproj/argocd-example-apps" + repo := "https://github.com/harness-apps/hosted-gitops-example-apps" + clusterName := id + clusterId := id + // clusterResourceName := "data.harness_platform_gitops_cluster.test" + clusterServer := os.Getenv("HARNESS_TEST_GITOPS_CLUSTER_SERVER") + clusterToken := os.Getenv("HARNESS_TEST_GITOPS_CLUSTER_TOKEN") + namespace := "test" resourceName := "harness_platform_gitops_applications.test" resource.UnitTest(t, resource.TestCase{ @@ -29,10 +35,10 @@ func TestAccDataSourceGitopsApplication(t *testing.T) { ProviderFactories: acctest.ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccDataSourceGitopsApplication(id, accountId, name, agentId, clusterName, namespace, clusterServer, clusterId, repo, repoId), + Config: testAccDataSourceGitopsApplication(id, accountId, name, agentId, clusterName, namespace, clusterServer, clusterId, repo, repoId, clusterToken), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "id", id), - resource.TestCheckResourceAttr(resourceName, "identifier", id), + resource.TestCheckResourceAttr(resourceName, "name", name), ), }, }, @@ -40,7 +46,7 @@ func TestAccDataSourceGitopsApplication(t *testing.T) { } -func testAccDataSourceGitopsApplication(id string, accountId string, name string, agentId string, clusterName string, namespace string, clusterServer string, clusterId string, repo string, repoId string) string { +func testAccDataSourceGitopsApplication(id string, accountId string, name string, agentId string, clusterName string, namespace string, clusterServer string, clusterId string, repo string, repoId string, clusterToken string) string { return fmt.Sprintf(` resource "harness_platform_organization" "test" { identifier = "%[1]s" @@ -83,8 +89,45 @@ func testAccDataSourceGitopsApplication(id string, accountId string, name string upsert = true } + resource "harness_platform_gitops_cluster" "test" { + identifier = "%[8]s" + account_id = "%[2]s" + agent_id = "%[4]s" + project_id = harness_platform_project.test.id + org_id = harness_platform_organization.test.id + request { + upsert = true + cluster { + server = "%[7]s" + name = "%[8]s" + config { + bearer_token = "%[11]s" + tls_client_config { + insecure = true + } + cluster_connection_type = "SERVICE_ACCOUNT" + } + } + } + lifecycle { + ignore_changes = [ + request.0.upsert, request.0.cluster.0.config.0.bearer_token, request.0.cluster.0.info, + ] + } + } + + data "harness_platform_gitops_cluster" "test" { + depends_on = [harness_platform_gitops_cluster.test] + identifier = harness_platform_gitops_cluster.test.id + account_id = "%[2]s" + project_id = harness_platform_project.test.id + org_id = harness_platform_organization.test.id + agent_id = "%[4]s" + + } + resource "harness_platform_gitops_applications" "test" { - depends_on = [harness_platform_gitops_repository.test] + depends_on = [harness_platform_gitops_repository.test, harness_platform_gitops_cluster.test] application { metadata { annotations = {} @@ -109,10 +152,9 @@ func testAccDataSourceGitopsApplication(id string, accountId string, name string ] } source { - target_revision = "master" - repo_url = "%[9]s" - path = "helm-guestbook" - + target_revision = "master" + repo_url = "%[9]s" + path = "helm-guestbook" } destination { namespace = "%[6]s" @@ -126,9 +168,10 @@ func testAccDataSourceGitopsApplication(id string, accountId string, name string identifier = "%[1]s" name = "%[3]s" cluster_id = "%[8]s" - repo_id = "%[10]s" + repo_id = "%[1]s" agent_id = "%[4]s" } + data "harness_platform_gitops_applications" "test"{ depends_on = [harness_platform_gitops_applications.test] identifier = harness_platform_gitops_applications.test.id @@ -138,6 +181,6 @@ func testAccDataSourceGitopsApplication(id string, accountId string, name string agent_id = "%[4]s" name = "%[3]s" } - `, id, accountId, name, agentId, clusterName, namespace, clusterServer, clusterId, repo, repoId) + `, id, accountId, name, agentId, clusterName, namespace, clusterServer, clusterId, repo, repoId, clusterToken) } diff --git a/internal/service/platform/gitops/applications/resource_gitops_applications.go b/internal/service/platform/gitops/applications/resource_gitops_applications.go index 2c7913ade..8d16e5a0b 100644 --- a/internal/service/platform/gitops/applications/resource_gitops_applications.go +++ b/internal/service/platform/gitops/applications/resource_gitops_applications.go @@ -1349,7 +1349,9 @@ func setApplication(d *schema.ResourceData, app *nextgen.Servicev1Application) { d.Set("repo_id", app.RepoIdentifier) d.Set("name", app.Name) d.Set("skip_repo_validation", app.SkipRepoValidation) - d.Set("repo_ids", app.RepoIdentifiers) + if app.RepoIdentifiers != nil { + d.Set("repo_ids", app.RepoIdentifiers) + } if app.App != nil { var applicationList = []interface{}{} @@ -1368,6 +1370,21 @@ func setApplication(d *schema.ResourceData, app *nextgen.Servicev1Application) { if app.App.Metadata.Annotations != nil { metadata["annotations"] = app.App.Metadata.Annotations } + if app.App.Metadata.Finalizers != nil { + metadata["finalizers"] = app.App.Metadata.Finalizers + } + if app.App.Metadata.OwnerReferences != nil { + var ownerReferencesList = []interface{}{} + for _, ownerReference := range app.App.Metadata.OwnerReferences { + var ownerReferenceMap = map[string]interface{}{} + ownerReferenceMap["api_version"] = ownerReference.ApiVersion + ownerReferenceMap["kind"] = ownerReference.Kind + ownerReferenceMap["name"] = ownerReference.Name + ownerReferenceMap["uid"] = ownerReference.Uid + ownerReferencesList = append(ownerReferencesList, ownerReferenceMap) + } + metadata["owner_references"] = ownerReferencesList + } metadataList = append(metadataList, metadata) application["metadata"] = metadataList } @@ -1381,7 +1398,7 @@ func setApplication(d *schema.ResourceData, app *nextgen.Servicev1Application) { } if app.App.Spec.Source != nil { source := getSourceForState(app.App.Spec) - spec["source"] = source + spec["source"] = []interface{}{source} } if len(app.App.Spec.Sources) > 0 { var sourcesList = []interface{}{} diff --git a/internal/service/platform/gitops/project/data_source_gitops_app_project_test.go b/internal/service/platform/gitops/project/data_source_gitops_app_project_test.go index 4b566db3b..8b577b464 100644 --- a/internal/service/platform/gitops/project/data_source_gitops_app_project_test.go +++ b/internal/service/platform/gitops/project/data_source_gitops_app_project_test.go @@ -12,6 +12,7 @@ import ( func TestAccDataSourceGitopsProjectAccLevel(t *testing.T) { resourceName := "harness_platform_gitops_app_project.test" agentId := os.Getenv("HARNESS_TEST_GITOPS_AGENT_ID") + agentNamespace := os.Getenv("HARNESS_TEST_GITOPS_AGENT_NAMESPACE") accountId := os.Getenv("HARNESS_ACCOUNT_ID") resource.UnitTest(t, resource.TestCase{ PreCheck: func() { acctest.TestAccPreCheck(t) }, @@ -19,7 +20,7 @@ func TestAccDataSourceGitopsProjectAccLevel(t *testing.T) { //CheckDestroy: testAccResourceGitopsRepositoryDestroy(resourceName), Steps: []resource.TestStep{ { - Config: testAccDataGitopsProjectOrgLevel(agentId, accountId, "my-project-3", "*"), + Config: testAccDataGitopsProjectOrgLevel(agentId, agentNamespace, accountId, "my-project-3", "*"), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "agent_id", agentId), ), @@ -29,7 +30,7 @@ func TestAccDataSourceGitopsProjectAccLevel(t *testing.T) { } -func testAccDataGitopsProjectOrgLevel(agentId string, accountId string, name string, namespace string) string { +func testAccDataGitopsProjectOrgLevel(agentId string, agentNamespace string, accountId string, name string, namespace string) string { return fmt.Sprintf(` resource "harness_platform_gitops_app_project" "test" { account_id = "%[1]s" @@ -38,7 +39,7 @@ func testAccDataGitopsProjectOrgLevel(agentId string, accountId string, name str project { metadata { name = "%[3]s" - namespace = "rollouts" + namespace = "%[5]s" finalizers = ["resources-finalizer.argocd.argoproj.io"] labels = { v1 = "k1" @@ -123,5 +124,5 @@ func testAccDataGitopsProjectOrgLevel(agentId string, accountId string, name str agent_id = harness_platform_gitops_app_project.test.agent_id query_name = harness_platform_gitops_app_project.test.project[0].metadata[0].name } - `, accountId, agentId, name, namespace) + `, accountId, agentId, name, namespace, agentNamespace) } diff --git a/internal/service/platform/gitops/project/resource_gitops_app_project_test.go b/internal/service/platform/gitops/project/resource_gitops_app_project_test.go index 83a551be6..307902ffa 100644 --- a/internal/service/platform/gitops/project/resource_gitops_app_project_test.go +++ b/internal/service/platform/gitops/project/resource_gitops_app_project_test.go @@ -16,6 +16,7 @@ func TestAccResourceGitopsProjectAccLevel(t *testing.T) { id = strings.ReplaceAll(id, "_", "") name := strings.ToLower(id) agentId := os.Getenv("HARNESS_TEST_GITOPS_AGENT_ID") + agentNamespace := os.Getenv("HARNESS_TEST_GITOPS_AGENT_NAMESPACE") resourceName := "harness_platform_gitops_app_project.test" accountId := os.Getenv("HARNESS_ACCOUNT_ID") resource.UnitTest(t, resource.TestCase{ @@ -24,13 +25,13 @@ func TestAccResourceGitopsProjectAccLevel(t *testing.T) { //CheckDestroy: testAccResourceGitopsRepositoryDestroy(resourceName), Steps: []resource.TestStep{ { - Config: testAccResourceGitopsProjectAccountLevel(agentId, accountId, name, "*"), + Config: testAccResourceGitopsProjectAccountLevel(agentId, agentNamespace, accountId, name, "*"), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "agent_id", agentId), ), }, { - Config: testAccResourceGitopsProjectAccountLevelUpdate(agentId, accountId, name, "roll"), + Config: testAccResourceGitopsProjectAccountLevelUpdate(agentId, agentNamespace, accountId, name, "roll"), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "agent_id", agentId), ), @@ -47,7 +48,7 @@ func TestAccResourceGitopsProjectAccLevel(t *testing.T) { } -func testAccResourceGitopsProjectAccountLevel(agentId string, accountId string, name string, namespace string) string { +func testAccResourceGitopsProjectAccountLevel(agentId string, agentNamespace string, accountId string, name string, namespace string) string { return fmt.Sprintf(` resource "harness_platform_gitops_app_project" "test" { account_id = "%[1]s" @@ -56,7 +57,7 @@ func testAccResourceGitopsProjectAccountLevel(agentId string, accountId string, project { metadata { name = "%[3]s" - namespace = "rollouts" + namespace = "%[5]s" finalizers = ["resources-finalizer.argocd.argoproj.io"] labels = { v1 = "k1" @@ -135,10 +136,10 @@ func testAccResourceGitopsProjectAccountLevel(agentId string, accountId string, } } } - `, accountId, agentId, name, namespace) + `, accountId, agentId, name, namespace, agentNamespace) } -func testAccResourceGitopsProjectAccountLevelUpdate(agentId string, accountId string, name string, namespace string) string { +func testAccResourceGitopsProjectAccountLevelUpdate(agentId string, agentNamespace string, accountId string, name string, namespace string) string { return fmt.Sprintf(` resource "harness_platform_gitops_app_project" "test" { account_id = "%[1]s" @@ -147,7 +148,7 @@ func testAccResourceGitopsProjectAccountLevelUpdate(agentId string, accountId st project { metadata { name = "%[3]s" - namespace = "rollouts" + namespace = "%[5]s" finalizers = ["resources-finalizer.argocd.argoproj.io"] labels = { v1 = "k2" @@ -226,5 +227,5 @@ func testAccResourceGitopsProjectAccountLevelUpdate(agentId string, accountId st } } } - `, accountId, agentId, name, namespace) + `, accountId, agentId, name, namespace, agentNamespace) }