|
| 1 | +// Copyright (c) Mondoo, Inc. |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccGithubResource(t *testing.T) { |
| 15 | + resource.Test(t, resource.TestCase{ |
| 16 | + PreCheck: func() { testAccPreCheck(t) }, |
| 17 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 18 | + Steps: []resource.TestStep{ |
| 19 | + // Create and Read testing with space on resource |
| 20 | + { |
| 21 | + Config: testAccGithubResourceConfig(accSpace.ID(), "one", "lunalectric", false), |
| 22 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 23 | + resource.TestCheckResourceAttr("mondoo_integration_github.test", "name", "one"), |
| 24 | + resource.TestCheckResourceAttr("mondoo_integration_github.test", "owner", "lunalectric"), |
| 25 | + resource.TestCheckResourceAttr("mondoo_integration_github.test", "space_id", accSpace.ID()), |
| 26 | + resource.TestCheckResourceAttr("mondoo_integration_github.test", "discovery.terraform", "true"), |
| 27 | + ), |
| 28 | + }, |
| 29 | + // Update and Read testing with space in provider and explicit token |
| 30 | + { |
| 31 | + Config: testAccGithubResourceWithSpaceInProviderConfig(accSpace.ID(), "two", "lunalectric", fakeGithubClassicToken()), |
| 32 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 33 | + resource.TestCheckResourceAttr("mondoo_integration_github.test", "name", "two"), |
| 34 | + resource.TestCheckResourceAttr("mondoo_integration_github.test", "owner", "lunalectric"), |
| 35 | + resource.TestCheckResourceAttr("mondoo_integration_github.test", "space_id", accSpace.ID()), |
| 36 | + resource.TestCheckResourceAttrSet("mondoo_integration_github.test", "credentials.token"), |
| 37 | + ), |
| 38 | + }, |
| 39 | + // Plan-only step: setting force_replace should cause a non-empty plan (replacement) |
| 40 | + { |
| 41 | + PlanOnly: true, |
| 42 | + ExpectNonEmptyPlan: true, |
| 43 | + Config: testAccGithubResourceConfig(accSpace.ID(), "two", "lunalectric", true), |
| 44 | + }, |
| 45 | + // Revert to previous config (without force_replace) |
| 46 | + { |
| 47 | + Config: testAccGithubResourceConfig(accSpace.ID(), "two", "lunalectric", false), |
| 48 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 49 | + resource.TestCheckResourceAttr("mondoo_integration_github.test", "name", "two"), |
| 50 | + resource.TestCheckResourceAttr("mondoo_integration_github.test", "owner", "lunalectric"), |
| 51 | + resource.TestCheckResourceAttr("mondoo_integration_github.test", "space_id", accSpace.ID()), |
| 52 | + ), |
| 53 | + }, |
| 54 | + // Delete testing automatically occurs in TestCase |
| 55 | + }, |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +// fakeGithubClassicToken constructs a valid-looking classic token without embedding the literal pattern in source. |
| 60 | +func fakeGithubClassicToken() string { |
| 61 | + return "gh" + "p_" + strings.Repeat("A", 36) |
| 62 | +} |
| 63 | + |
| 64 | +func testAccGithubResourceConfig(spaceID, intName, owner string, forceReplace bool) string { |
| 65 | + forceReplaceLine := "" |
| 66 | + if forceReplace { |
| 67 | + forceReplaceLine = "\n\tforce_replace = true" |
| 68 | + } |
| 69 | + tok := fakeGithubClassicToken() |
| 70 | + return fmt.Sprintf(` |
| 71 | +resource "mondoo_integration_github" "test" { |
| 72 | + space_id = %q |
| 73 | + name = %q |
| 74 | + owner = %q |
| 75 | +
|
| 76 | + discovery = { |
| 77 | + terraform = true |
| 78 | + k8s_manifests = true |
| 79 | + }%s |
| 80 | +
|
| 81 | + credentials = { |
| 82 | + token = %q |
| 83 | + } |
| 84 | +} |
| 85 | +`, spaceID, intName, owner, forceReplaceLine, tok) |
| 86 | +} |
| 87 | + |
| 88 | +func testAccGithubResourceWithSpaceInProviderConfig(spaceID, intName, owner, token string) string { |
| 89 | + return fmt.Sprintf(` |
| 90 | +provider "mondoo" { |
| 91 | + space = %q |
| 92 | +} |
| 93 | +resource "mondoo_integration_github" "test" { |
| 94 | + name = %q |
| 95 | + owner = %q |
| 96 | +
|
| 97 | + discovery = { |
| 98 | + terraform = true |
| 99 | + k8s_manifests = true |
| 100 | + } |
| 101 | +
|
| 102 | + credentials = { |
| 103 | + token = %q |
| 104 | + } |
| 105 | +} |
| 106 | +`, spaceID, intName, owner, token) |
| 107 | +} |
0 commit comments