diff --git a/.github/workflows/acc-tests.yml b/.github/workflows/acc-tests.yml new file mode 100644 index 00000000..b9de93b2 --- /dev/null +++ b/.github/workflows/acc-tests.yml @@ -0,0 +1,43 @@ +name: Acceptance tests +on: + workflow_call: + inputs: + clientId: + description: Client ID to use for authentication + required: true + type: string + oktaOrgUrl: + description: Okta organization URL + required: false + type: string + oktaAuthServer: + description: Okta authentication server identifier + required: false + type: string + project: + description: Project name to create the tested objects in + required: false + type: string + secrets: + clientSecret: + description: Client secret to use for authentication + required: true +jobs: + test: + name: Run acceptance tests + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: false + - name: Run acceptance tests + env: + TERRAFORM_NOBL9_CLIENT_ID: "${{ secrets.clientId }}" + TERRAFORM_NOBL9_CLIENT_SECRET: "${{ secrets.clientSecret }}" + TERRAFORM_NOBL9_OKTA_ORG_URL: "${{ inputs.oktaOrgUrl }}" + TERRAFORM_NOBL9_OKTA_AUTH_SERVER: "${{ inputs.oktaAuthServer }}" + TERRAFORM_NOBL9_PROJECT: "${{ inputs.project }}" + run: make test/acc diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 48a9aaad..f281bce1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,6 +35,12 @@ jobs: passphrase: ${{ secrets.PASSPHRASE }} - name: setup github run: git config --global url."https://n9-machine-user:${{ secrets.GH_TOKEN }}@github.com".insteadOf "https://github.com" + - name: Run acceptance tests + uses: ./.github/workflows/acc-tests.yml + with: + clientId: ${{ vars.TERRAFORM_NOBL9_CLIENT_ID }} + clientSecret: ${{ secrets.TERRAFORM_NOBL9_CLIENT_SECRET }} + project: ${{ vars.TERRAFORM_NOBL9_PROJECT }} - name: Run GoReleaser uses: goreleaser/goreleaser-action@v5.0.0 with: diff --git a/.github/workflows/tests.yml b/.github/workflows/unit-tests.yml similarity index 88% rename from .github/workflows/tests.yml rename to .github/workflows/unit-tests.yml index 15acc2d8..578150fc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/unit-tests.yml @@ -1,4 +1,4 @@ -name: Tests +name: Unit tests on: push: branches: @@ -18,4 +18,4 @@ jobs: go-version-file: go.mod cache: false - name: Run unit tests - run: make test + run: make test/unit diff --git a/Makefile b/Makefile index 12cb9c04..72e4f335 100644 --- a/Makefile +++ b/Makefile @@ -53,16 +53,18 @@ install: build build: go build -ldflags $(BUILD_FLAGS) -o $(BINARY) -.PHONY: test +.PHONY: test test/unit test/acc +## Run all tests. +test: test/unit test/acc + ## Run Go unit tests. -test: +test/unit: go test -i $(TEST) || exit 1 echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4 -.PHONY: testacc -## Run acceptance tests. -testacc: - cd nobl9 && TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m +## Run Terraform acceptance tests. +test/acc: + TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m nobl9/ .PHONY: release-dry-run ## Run Goreleaser in dry-run mode. diff --git a/codefresh.yml b/codefresh.yml deleted file mode 100644 index 26f5f499..00000000 --- a/codefresh.yml +++ /dev/null @@ -1,41 +0,0 @@ -version: "1.0" - -hooks: - on_fail: - steps: - # Notify about pipeline failure only for main - notify: - when: - branch: - only: - - main - image: codefresh/slacknotifier - environment: - - 'SLACK_HOOK_URL=${{SLACK_WEBHOOK}}' - - 'SLACK_TEXT=Pipeline failed for Nobl9 Terraform Provider repository' - - 'SLACK_ATTACHMENTS=[{ "color":"#d00000", "blocks":[{"type":"context", "elements":[ { "type":"plain_text","text":"Pipeline failed for Nobl9 Terraform Provider repository", "emoji":true }]}]}]' - - 'SLACK_USER_NAME=Nobl9 Terraform Provider' - - 'SLACK_ICON_EMOJI=:terraform:' - -stages: - - "clone" - - "test" - -steps: - clone: - title: "Cloning repository" - type: "git-clone" - repo: "nobl9/terraform-provider-nobl9" - revision: "${{CF_REVISION}}" - git: "github" - stage: "clone" - - test: - title: "Running test" - type: "freestyle" - image: "hashicorp/terraform:1.2.5" - working_directory: "${{clone}}" - commands: - - "apk add go make" - - "make testacc" - stage: "test" diff --git a/nobl9/data_source_test.go b/nobl9/data_source_test.go index dbb995d8..3e73e2ab 100644 --- a/nobl9/data_source_test.go +++ b/nobl9/data_source_test.go @@ -18,7 +18,6 @@ func TestAcc_Nobl9DataSource(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), Steps: []resource.TestStep{ { diff --git a/nobl9/provider.go b/nobl9/provider.go index 1cb1ce77..260c7ac0 100644 --- a/nobl9/provider.go +++ b/nobl9/provider.go @@ -22,7 +22,7 @@ func Provider() *schema.Provider { "ingest_url": { Type: schema.TypeString, Optional: true, - DefaultFunc: schema.EnvDefaultFunc("NOBL9_URL", "https://app.nobl9.com/api"), + DefaultFunc: schema.EnvDefaultFunc("NOBL9_URL", nil), Description: "Nobl9 API URL.", }, @@ -36,7 +36,7 @@ func Provider() *schema.Provider { "project": { Type: schema.TypeString, Optional: true, - DefaultFunc: schema.EnvDefaultFunc("NOBL9_PROJECT", sdk.DefaultProject), + DefaultFunc: schema.EnvDefaultFunc("NOBL9_PROJECT", nil), Description: "Nobl9 project used when importing resources.", }, @@ -58,7 +58,7 @@ func Provider() *schema.Provider { "okta_org_url": { Type: schema.TypeString, Optional: true, - DefaultFunc: schema.EnvDefaultFunc("NOBL9_OKTA_URL", "https://accounts.nobl9.com"), + DefaultFunc: schema.EnvDefaultFunc("NOBL9_OKTA_URL", nil), Description: "Authorization service URL.", }, @@ -66,7 +66,7 @@ func Provider() *schema.Provider { Type: schema.TypeString, Optional: true, //cspell:ignore auseg9kiegWKEtJZC416 - DefaultFunc: schema.EnvDefaultFunc("NOBL9_OKTA_AUTH", "auseg9kiegWKEtJZC416"), + DefaultFunc: schema.EnvDefaultFunc("NOBL9_OKTA_AUTH", nil), Description: "Authorization service configuration.", }, }, diff --git a/nobl9/provider_test.go b/nobl9/provider_test.go index 8e2cb371..921bc0d2 100644 --- a/nobl9/provider_test.go +++ b/nobl9/provider_test.go @@ -26,6 +26,9 @@ var ( //nolint:gochecknoinits func init() { testProject = os.Getenv("NOBL9_PROJECT") + if testProject == "" { + testProject = "default" + } } func ProviderFactory() map[string]func() (*schema.Provider, error) { @@ -43,30 +46,6 @@ func TestProvider(t *testing.T) { } } -func testAccPreCheck(t *testing.T) { - if err := os.Getenv("NOBL9_URL"); err == "" { - t.Fatal("NOBL9_URL must be set for acceptance tests") - } - if err := os.Getenv("NOBL9_ORG"); err == "" { - t.Fatal("NOBL9_ORG must be set for acceptance tests") - } - if err := os.Getenv("NOBL9_PROJECT"); err == "" { - t.Fatal("NOBL9_PROJECT must be set for acceptance tests") - } - if err := os.Getenv("NOBL9_CLIENT_ID"); err == "" { - t.Fatal("NOBL9_CLIENT_ID must be set for acceptance tests") - } - if err := os.Getenv("NOBL9_CLIENT_SECRET"); err == "" { - t.Fatal("NOBL9_CLIENT_SECRET must be set for acceptance tests") - } - if err := os.Getenv("NOBL9_OKTA_URL"); err == "" { - t.Fatal("NOBL9_OKTA_URL must be set for acceptance tests") - } - if err := os.Getenv("NOBL9_OKTA_AUTH"); err == "" { - t.Fatal("NOBL9_OKTA_AUTH must be set for acceptance tests") - } -} - func CheckObjectCreated(name string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] diff --git a/nobl9/resource_agent_test.go b/nobl9/resource_agent_test.go index 495fe859..b0809f68 100644 --- a/nobl9/resource_agent_test.go +++ b/nobl9/resource_agent_test.go @@ -45,7 +45,6 @@ func TestAcc_Nobl9Agent(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), CheckDestroy: CheckDestroy("nobl9_agent", manifest.KindAgent), Steps: []resource.TestStep{ diff --git a/nobl9/resource_alert_policy_test.go b/nobl9/resource_alert_policy_test.go index 5e7c60ed..ca1e574a 100644 --- a/nobl9/resource_alert_policy_test.go +++ b/nobl9/resource_alert_policy_test.go @@ -26,7 +26,6 @@ func TestAcc_Nobl9AlertPolicy(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), CheckDestroy: destroyMultiple( []string{"nobl9_alert_policy", "nobl9_alert_method_webhook"}, diff --git a/nobl9/resource_alertmethod_test.go b/nobl9/resource_alertmethod_test.go index 2d066be1..7d143d98 100644 --- a/nobl9/resource_alertmethod_test.go +++ b/nobl9/resource_alertmethod_test.go @@ -33,7 +33,6 @@ func TestAcc_Nobl9AlertMethod(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), CheckDestroy: CheckDestroy("nobl9_alert_method_"+tc.resourceSuffix, manifest.KindAlertMethod), Steps: []resource.TestStep{ diff --git a/nobl9/resource_direct_test.go b/nobl9/resource_direct_test.go index d7c93c90..3d46e7bc 100644 --- a/nobl9/resource_direct_test.go +++ b/nobl9/resource_direct_test.go @@ -39,7 +39,6 @@ func TestAcc_Nobl9Direct(t *testing.T) { t.Run(tc.directType, func(t *testing.T) { testName := strings.ReplaceAll("test-"+tc.directType, "_", "") resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), CheckDestroy: CheckDestroy("nobl9_direct_%s", manifest.KindDirect), Steps: []resource.TestStep{ diff --git a/nobl9/resource_project_test.go b/nobl9/resource_project_test.go index 5183cb51..22283b8c 100644 --- a/nobl9/resource_project_test.go +++ b/nobl9/resource_project_test.go @@ -13,7 +13,6 @@ func TestAcc_Nobl9Project(t *testing.T) { name := "test-project" resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), CheckDestroy: CheckDestroy("nobl9_project", manifest.KindProject), Steps: []resource.TestStep{ @@ -33,7 +32,6 @@ func TestAcc_NewNobl9ProjectReference(t *testing.T) { name := "test-project" resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), CheckDestroy: resource.ComposeTestCheckFunc( CheckDestroy("nobl9_agent", manifest.KindAgent), diff --git a/nobl9/resource_role_binding_test.go b/nobl9/resource_role_binding_test.go index d1e8e044..c91fecf7 100644 --- a/nobl9/resource_role_binding_test.go +++ b/nobl9/resource_role_binding_test.go @@ -26,7 +26,6 @@ func TestAcc_Nobl9RoleBinding(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), CheckDestroy: CheckDestroy("nobl9_role_binding", manifest.KindRoleBinding), Steps: []resource.TestStep{ diff --git a/nobl9/resource_service_test.go b/nobl9/resource_service_test.go index 129a716d..0db64b2f 100644 --- a/nobl9/resource_service_test.go +++ b/nobl9/resource_service_test.go @@ -12,7 +12,6 @@ import ( func TestAcc_Nobl9Service(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), CheckDestroy: CheckDestroy("nobl9_service", manifest.KindService), Steps: []resource.TestStep{ diff --git a/nobl9/resource_slo_test.go b/nobl9/resource_slo_test.go index 4bcabd43..ff8793d3 100644 --- a/nobl9/resource_slo_test.go +++ b/nobl9/resource_slo_test.go @@ -63,7 +63,6 @@ func TestAcc_Nobl9SLO(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), CheckDestroy: CheckDestroy("nobl9_slo", manifest.KindSLO), Steps: []resource.TestStep{ @@ -98,7 +97,6 @@ func TestAcc_Nobl9SLOErrors(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProviderFactories: ProviderFactory(), CheckDestroy: CheckDestroy("nobl9_slo.", manifest.KindSLO), Steps: []resource.TestStep{