|
| 1 | +package codefresh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccCodefreshGitopsEnvironmentsResource(t *testing.T) { |
| 14 | + resourceName := "codefresh_gitops_environment.test" |
| 15 | + |
| 16 | + resource.ParallelTest(t, resource.TestCase{ |
| 17 | + PreCheck: func() { testAccPreCheck(t) }, |
| 18 | + Providers: testAccProviders, |
| 19 | + CheckDestroy: testAccCheckCodefreshGitopsEnvironmentDestroy, |
| 20 | + Steps: []resource.TestStep{ |
| 21 | + { |
| 22 | + Config: testAccCodefreshGitopsEnvironmentConfig( |
| 23 | + "test-for-tf", |
| 24 | + "NON_PROD", |
| 25 | + []cfclient.GitopsEnvironmentCluster{{ |
| 26 | + Name: "in-cluster2", |
| 27 | + Server: "https://kubernetes.default.svc", |
| 28 | + RuntimeName: "test-runtime", |
| 29 | + Namespaces: []string{"test-ns-1", "test-ns2"}, |
| 30 | + }}, |
| 31 | + []string{"codefresh.io/environment=test-for-tf", "codefresh.io/environment-1=test-for-tf1"}, |
| 32 | + ), |
| 33 | + Check: resource.ComposeTestCheckFunc( |
| 34 | + testAccCheckCodefreshGitopsEnvironmentExists(resourceName), |
| 35 | + resource.TestCheckResourceAttr(resourceName, "name", "test-for-tf"), |
| 36 | + resource.TestCheckResourceAttr(resourceName, "kind", "NON_PROD"), |
| 37 | + resource.TestCheckResourceAttr(resourceName, "cluster.0.name", "in-cluster2"), |
| 38 | + resource.TestCheckResourceAttr(resourceName, "cluster.0.server", "https://kubernetes.default.svc"), |
| 39 | + resource.TestCheckResourceAttr(resourceName, "cluster.0.runtime_name", "test-runtime"), |
| 40 | + resource.TestCheckResourceAttr(resourceName, "cluster.0.namespaces.0", "test-ns-1"), |
| 41 | + resource.TestCheckResourceAttr(resourceName, "cluster.0.namespaces.1", "test-ns2"), |
| 42 | + resource.TestCheckResourceAttr(resourceName, "label_pairs.0", "codefresh.io/environment=test-for-tf"), |
| 43 | + resource.TestCheckResourceAttr(resourceName, "label_pairs.1", "codefresh.io/environment-1=test-for-tf1"), |
| 44 | + ), |
| 45 | + }, |
| 46 | + { |
| 47 | + Config: testAccCodefreshGitopsEnvironmentConfig( |
| 48 | + "test-for-tf", |
| 49 | + "NON_PROD", |
| 50 | + []cfclient.GitopsEnvironmentCluster{ |
| 51 | + { |
| 52 | + Name: "in-cluster2", |
| 53 | + Server: "https://kubernetes.default.svc", |
| 54 | + RuntimeName: "test-runtime", |
| 55 | + Namespaces: []string{"test-ns-1", "test-ns2"}, |
| 56 | + }, |
| 57 | + { |
| 58 | + Name: "in-cluster3", |
| 59 | + Server: "https://kubernetes2.default.svc", |
| 60 | + RuntimeName: "test-runtime-2", |
| 61 | + Namespaces: []string{"test-ns-3"}, |
| 62 | + }, |
| 63 | + }, |
| 64 | + []string{"codefresh.io/environment=test-for-tf", "codefresh.io/environment-1=test-for-tf1"}, |
| 65 | + ), |
| 66 | + Check: resource.ComposeTestCheckFunc( |
| 67 | + testAccCheckCodefreshGitopsEnvironmentExists(resourceName), |
| 68 | + resource.TestCheckResourceAttr(resourceName, "name", "test-for-tf"), |
| 69 | + resource.TestCheckResourceAttr(resourceName, "kind", "NON_PROD"), |
| 70 | + resource.TestCheckResourceAttr(resourceName, "cluster.0.name", "in-cluster2"), |
| 71 | + resource.TestCheckResourceAttr(resourceName, "cluster.1.name", "in-cluster3"), |
| 72 | + resource.TestCheckResourceAttr(resourceName, "cluster.1.server", "https://kubernetes2.default.svc"), |
| 73 | + resource.TestCheckResourceAttr(resourceName, "cluster.1.runtime_name", "test-runtime-2"), |
| 74 | + resource.TestCheckResourceAttr(resourceName, "cluster.1.namespaces.0", "test-ns-3"), |
| 75 | + ), |
| 76 | + }, |
| 77 | + { |
| 78 | + ResourceName: resourceName, |
| 79 | + ImportState: true, |
| 80 | + ImportStateVerify: true, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +func testAccCheckCodefreshGitopsEnvironmentExists(resource string) resource.TestCheckFunc { |
| 87 | + return func(state *terraform.State) error { |
| 88 | + rs, ok := state.RootModule().Resources[resource] |
| 89 | + if !ok { |
| 90 | + return fmt.Errorf("Not found: %s", resource) |
| 91 | + } |
| 92 | + if rs.Primary.ID == "" { |
| 93 | + return fmt.Errorf("No Record ID is set") |
| 94 | + } |
| 95 | + |
| 96 | + envID := rs.Primary.ID |
| 97 | + apiClient := testAccProvider.Meta().(*cfclient.Client) |
| 98 | + _, err := apiClient.GetGitopsEnvironmentById(envID) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("error fetching gitops environment with ID %s. %s", envID, err) |
| 101 | + } |
| 102 | + return nil |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func testAccCheckCodefreshGitopsEnvironmentDestroy(state *terraform.State) error { |
| 107 | + // Implement destroy check if needed |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// CONFIG |
| 112 | +func testAccCodefreshGitopsEnvironmentConfig(name, kind string, clusters []cfclient.GitopsEnvironmentCluster, labelPairs []string) string { |
| 113 | + var clusterBlocks []string |
| 114 | + for _, c := range clusters { |
| 115 | + ns := fmt.Sprintf("[\"%s\"]", strings.Join(c.Namespaces, "\", \"")) |
| 116 | + block := fmt.Sprintf(` cluster { |
| 117 | + name = "%s" |
| 118 | + server = "%s" |
| 119 | + runtime_name = "%s" |
| 120 | + namespaces = %s |
| 121 | + }`, c.Name, c.Server, c.RuntimeName, ns) |
| 122 | + clusterBlocks = append(clusterBlocks, block) |
| 123 | + } |
| 124 | + labelsStr := fmt.Sprintf("[\"%s\"]", strings.Join(labelPairs, "\", \"")) |
| 125 | + return fmt.Sprintf(` |
| 126 | +resource "codefresh_gitops_environment" "test" { |
| 127 | + name = "%s" |
| 128 | + kind = "%s" |
| 129 | +%s |
| 130 | + label_pairs = %s |
| 131 | +} |
| 132 | +`, name, kind, strings.Join(clusterBlocks, "\n"), labelsStr) |
| 133 | +} |
0 commit comments