Skip to content

Commit

Permalink
FFM-9121: Implement unit tests for feature flag terraform plugin (#676)
Browse files Browse the repository at this point in the history
* Add unit test for feature flag terraform plugin
* Fix unit test
* add code review comments
  • Loading branch information
ribeirophillipe committed Sep 8, 2023
1 parent 4b8ec92 commit 20bb7e1
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package feature_flag

import (
"context"
"io"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -207,11 +206,8 @@ func resourceFeatureFlagCreate(ctx context.Context, d *schema.ResourceData, meta
time.Sleep(1 * time.Second)

resp, httpResp, err = c.FeatureFlagsApi.GetFeatureFlag(ctx, id, c.AccountId, qp.OrganizationId, qp.ProjectId, readOpts)

if err != nil {
body, _ := io.ReadAll(httpResp.Body)
return diag.Errorf("readstatus: %s, \nBody:%s", httpResp.Status, body)
//return helpers.HandleReadApiError(err, d, httpResp)
return helpers.HandleApiError(err, d, httpResp)
}

readFeatureFlag(d, &resp, qp)
Expand Down
183 changes: 183 additions & 0 deletions internal/service/platform/feature_flag/resource_feature_flag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package feature_flag_test

import (
"fmt"
"testing"

"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/harness-go-sdk/harness/utils"
"github.com/harness/terraform-provider-harness/internal/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccResourceFeatureFlag(t *testing.T) {

name := t.Name()
targetName := name
id := fmt.Sprintf("%s_%s", name, utils.RandStringBytes(5))
resourceName := "harness_platform_feature_flag.test"
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccResourceFeatureFlagDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceFeatureFlag(id, name, targetName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "org_id", id),
resource.TestCheckResourceAttr(resourceName, "project_id", id),
resource.TestCheckResourceAttr(resourceName, "name", targetName),
),
},
{
Config: testAccResourceFeatureFlag(id, name, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "org_id", id),
resource.TestCheckResourceAttr(resourceName, "project_id", id),
resource.TestCheckResourceAttr(resourceName, "name", name),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"yaml"},
ImportStateIdFunc: acctest.ProjectResourceImportStateIdFunc(resourceName),
},
},
})
}

func testAccResourceFeatureFlag(id string, name string, updatedName string) string {
return fmt.Sprintf(`
resource "harness_platform_organization" "test" {
identifier = "%[1]s"
name = "%[2]s"
}
resource "harness_platform_project" "test" {
identifier = "%[1]s"
name = "%[2]s"
org_id = harness_platform_organization.test.id
color = "#0063F7"
}
resource "harness_platform_environment" "test" {
identifier = "%[1]s"
name = "%[2]s"
org_id = harness_platform_project.test.org_id
project_id = harness_platform_project.test.id
tags = ["foo:bar", "baz"]
type = "PreProduction"
yaml = <<-EOT
environment:
name: %[2]s
identifier: %[1]s
orgIdentifier: ${harness_platform_project.test.org_id}
projectIdentifier: ${harness_platform_project.test.id}
type: PreProduction
tags:
foo: bar
baz: ""
variables:
- name: envVar1
type: String
value: v1
description: ""
- name: envVar2
type: String
value: v2
description: ""
overrides:
manifests:
- manifest:
identifier: manifestEnv
type: Values
spec:
store:
type: Git
spec:
connectorRef: <+input>
gitFetchType: Branch
paths:
- file1
repoName: <+input>
branch: master
configFiles:
- configFile:
identifier: configFileEnv
spec:
store:
type: Harness
spec:
files:
- account:/Add-ons/svcOverrideTest
secretFiles: []
EOT
}
resource "harness_platform_feature_flag" "test" {
identifier = "%[1]s"
org_id = harness_platform_project.test.org_id
project_id = harness_platform_project.test.id
name = "%[2]s"
kind = "boolean"
permanent = false
default_on_variation = "Enabled"
default_off_variation = "Disabled"
variation {
identifier = "Enabled"
name = "Enabled"
description = "The feature is enabled"
value = "true"
}
variation {
identifier = "Disabled"
name = "Disabled"
description = "The feature is disabled"
value = "false"
}
}
`, id, name, updatedName)
}

func testAccResourceFeatureFlagDestroy(resourceName string) resource.TestCheckFunc {
return func(state *terraform.State) error {
env, _ := testAccGetPlatformFeatureFlag(resourceName, state)
if env != nil {
return fmt.Errorf("Feature Flag Target not found: %s", env.Identifier)
}

return nil
}
}

func testAccGetPlatformFeatureFlag(resourceName string, state *terraform.State) (*nextgen.Feature, error) {
r := acctest.TestAccGetResource(resourceName, state)
c, ctx := acctest.TestAccGetPlatformClientWithContext()
id := r.Primary.ID
orgId := r.Primary.Attributes["org_id"]
projId := r.Primary.Attributes["project_id"]
readOpts := &nextgen.FeatureFlagsApiGetFeatureFlagOpts{
EnvironmentIdentifier: optional.EmptyString(),
}

featureFlag, resp, err := c.FeatureFlagsApi.GetFeatureFlag(ctx, id, c.AccountId, orgId, projId, readOpts)

if err != nil {
return nil, err
}

if resp == nil {
return nil, nil
}

return &featureFlag, nil
}

0 comments on commit 20bb7e1

Please sign in to comment.