Skip to content

Commit

Permalink
Fix: [CDS-101657]: Fix TF Provider Tests (#1112)
Browse files Browse the repository at this point in the history
* Fix TestAccResourceGitopsProjectAccLevel

* Fix TestAccDataSourceGitopsProjectAccLevel

* Improve fix for gitops_app_project_test

* Fix TestAccDataSourceGitopsCluster

* Fix TestAccDataSourceGitopsApplication


Signed-off-by: Mirko Teodorovic <[email protected]>

---------

Signed-off-by: Mirko Teodorovic <[email protected]>
Co-authored-by: Mirko Teodorovic <[email protected]>
  • Loading branch information
ivbalan-harness and mteodor authored Nov 15, 2024
1 parent cd9ad28 commit 1a9e879
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ website/node_modules
.vscode
*debug*
website/vendor
test-report.xml

# Test exclusions
!command/test-fixtures/**/*.tfstate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,36 @@ 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{
PreCheck: func() { acctest.TestAccPreCheck(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),
),
},
},
})

}

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"
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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)

}
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
Expand All @@ -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
}
Expand All @@ -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{}{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ 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) },
ProviderFactories: acctest.ProviderFactories,
//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),
),
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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),
),
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -226,5 +227,5 @@ func testAccResourceGitopsProjectAccountLevelUpdate(agentId string, accountId st
}
}
}
`, accountId, agentId, name, namespace)
`, accountId, agentId, name, namespace, agentNamespace)
}

0 comments on commit 1a9e879

Please sign in to comment.