|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAccGithubEnterpriseOrganization(t *testing.T) { |
| 13 | + |
| 14 | + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) |
| 15 | + orgName := fmt.Sprintf("tf-acc-test-%s", randomID) |
| 16 | + |
| 17 | + desc := "Initial org description" |
| 18 | + updatedDesc := "Updated org description" |
| 19 | + |
| 20 | + config := fmt.Sprintf(` |
| 21 | + data "github_enterprise" "enterprise" { |
| 22 | + slug = "%s" |
| 23 | + } |
| 24 | + |
| 25 | + data "github_user" "current" { |
| 26 | + username = "" |
| 27 | + } |
| 28 | +
|
| 29 | + resource "github_enterprise_organization" "org" { |
| 30 | + enterprise_id = data.github_enterprise.enterprise.id |
| 31 | + name = "%s" |
| 32 | + description = "%s" |
| 33 | + billing_email = data.github_user.current.email |
| 34 | + admin_logins = [ |
| 35 | + data.github_user.current.login |
| 36 | + ] |
| 37 | + } |
| 38 | + `, testEnterprise, orgName, desc) |
| 39 | + |
| 40 | + t.Run("creates and updates an enterprise organization without error", func(t *testing.T) { |
| 41 | + checks := map[string]resource.TestCheckFunc{ |
| 42 | + "before": resource.ComposeTestCheckFunc( |
| 43 | + resource.TestCheckResourceAttrSet( |
| 44 | + "github_enterprise_organization.org", "enterprise_id", |
| 45 | + ), |
| 46 | + resource.TestCheckResourceAttr( |
| 47 | + "github_enterprise_organization.org", "name", |
| 48 | + orgName, |
| 49 | + ), |
| 50 | + resource.TestCheckResourceAttr( |
| 51 | + "github_enterprise_organization.org", "description", |
| 52 | + desc, |
| 53 | + ), |
| 54 | + resource.TestCheckResourceAttrSet( |
| 55 | + "github_enterprise_organization.org", "billing_email", |
| 56 | + ), |
| 57 | + resource.TestCheckResourceAttr( |
| 58 | + "github_enterprise_organization.org", "admin_logins.#", |
| 59 | + "1", |
| 60 | + ), |
| 61 | + ), |
| 62 | + "after": resource.ComposeTestCheckFunc( |
| 63 | + resource.TestCheckResourceAttr( |
| 64 | + "github_enterprise_organization.org", "description", |
| 65 | + updatedDesc, |
| 66 | + ), |
| 67 | + ), |
| 68 | + } |
| 69 | + |
| 70 | + testCase := func(t *testing.T, mode string) { |
| 71 | + resource.Test(t, resource.TestCase{ |
| 72 | + PreCheck: func() { skipUnlessMode(t, mode) }, |
| 73 | + Providers: testAccProviders, |
| 74 | + Steps: []resource.TestStep{ |
| 75 | + { |
| 76 | + Config: config, |
| 77 | + Check: checks["before"], |
| 78 | + }, |
| 79 | + { |
| 80 | + Config: strings.Replace(config, |
| 81 | + desc, |
| 82 | + updatedDesc, 1), |
| 83 | + Check: checks["after"], |
| 84 | + }, |
| 85 | + }, |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + t.Run("with an enterprise account", func(t *testing.T) { |
| 90 | + if isEnterprise != "true" { |
| 91 | + t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") |
| 92 | + } |
| 93 | + if testEnterprise == "" { |
| 94 | + t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") |
| 95 | + } |
| 96 | + testCase(t, enterprise) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("deletes an enterprise organization without error", func(t *testing.T) { |
| 101 | + testCase := func(t *testing.T, mode string) { |
| 102 | + resource.Test(t, resource.TestCase{ |
| 103 | + PreCheck: func() { skipUnlessMode(t, mode) }, |
| 104 | + Providers: testAccProviders, |
| 105 | + Steps: []resource.TestStep{ |
| 106 | + { |
| 107 | + Config: config, |
| 108 | + Destroy: true, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + t.Run("with an enterprise account", func(t *testing.T) { |
| 115 | + if isEnterprise != "true" { |
| 116 | + t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") |
| 117 | + } |
| 118 | + if testEnterprise == "" { |
| 119 | + t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") |
| 120 | + } |
| 121 | + testCase(t, enterprise) |
| 122 | + }) |
| 123 | + }) |
| 124 | +} |
0 commit comments