|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" |
| 8 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAccGithubRepositoryCustomProperties(t *testing.T) { |
| 12 | + t.Run("sets custom_properties at creation time", func(t *testing.T) { |
| 13 | + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) |
| 14 | + propertyName := fmt.Sprintf("tfacc%s", randomID) |
| 15 | + repoName := fmt.Sprintf("%scustomprops-%s", testResourcePrefix, randomID) |
| 16 | + |
| 17 | + config := fmt.Sprintf(` |
| 18 | + resource "github_organization_custom_properties" "test" { |
| 19 | + allowed_values = ["alpha", "beta"] |
| 20 | + property_name = "%[1]s" |
| 21 | + value_type = "single_select" |
| 22 | + } |
| 23 | +
|
| 24 | + resource "github_repository" "test" { |
| 25 | + name = "%[2]s" |
| 26 | + auto_init = true |
| 27 | +
|
| 28 | + custom_properties = { |
| 29 | + (github_organization_custom_properties.test.property_name) = "alpha" |
| 30 | + } |
| 31 | + } |
| 32 | + `, propertyName, repoName) |
| 33 | + |
| 34 | + check := resource.ComposeTestCheckFunc( |
| 35 | + resource.TestCheckResourceAttr("github_repository.test", "custom_properties.%", "1"), |
| 36 | + resource.TestCheckResourceAttr( |
| 37 | + "github_repository.test", |
| 38 | + fmt.Sprintf("custom_properties.%s", propertyName), |
| 39 | + "alpha", |
| 40 | + ), |
| 41 | + ) |
| 42 | + |
| 43 | + resource.Test(t, resource.TestCase{ |
| 44 | + PreCheck: func() { skipUnlessHasOrgs(t) }, |
| 45 | + ProviderFactories: providerFactories, |
| 46 | + Steps: []resource.TestStep{ |
| 47 | + { |
| 48 | + Config: config, |
| 49 | + Check: check, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("updates a custom property value", func(t *testing.T) { |
| 56 | + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) |
| 57 | + propertyName := fmt.Sprintf("tfacc%s", randomID) |
| 58 | + repoName := fmt.Sprintf("%scustomprops-%s", testResourcePrefix, randomID) |
| 59 | + |
| 60 | + configTemplate := ` |
| 61 | + resource "github_organization_custom_properties" "test" { |
| 62 | + allowed_values = ["alpha", "beta"] |
| 63 | + property_name = "%[1]s" |
| 64 | + value_type = "single_select" |
| 65 | + } |
| 66 | +
|
| 67 | + resource "github_repository" "test" { |
| 68 | + name = "%[2]s" |
| 69 | + auto_init = true |
| 70 | +
|
| 71 | + custom_properties = { |
| 72 | + (github_organization_custom_properties.test.property_name) = "%[3]s" |
| 73 | + } |
| 74 | + } |
| 75 | + ` |
| 76 | + |
| 77 | + resource.Test(t, resource.TestCase{ |
| 78 | + PreCheck: func() { skipUnlessHasOrgs(t) }, |
| 79 | + ProviderFactories: providerFactories, |
| 80 | + Steps: []resource.TestStep{ |
| 81 | + { |
| 82 | + Config: fmt.Sprintf(configTemplate, propertyName, repoName, "alpha"), |
| 83 | + Check: resource.TestCheckResourceAttr( |
| 84 | + "github_repository.test", |
| 85 | + fmt.Sprintf("custom_properties.%s", propertyName), |
| 86 | + "alpha", |
| 87 | + ), |
| 88 | + }, |
| 89 | + { |
| 90 | + Config: fmt.Sprintf(configTemplate, propertyName, repoName, "beta"), |
| 91 | + Check: resource.TestCheckResourceAttr( |
| 92 | + "github_repository.test", |
| 93 | + fmt.Sprintf("custom_properties.%s", propertyName), |
| 94 | + "beta", |
| 95 | + ), |
| 96 | + }, |
| 97 | + }, |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("removes a specific property key", func(t *testing.T) { |
| 102 | + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) |
| 103 | + propertyA := fmt.Sprintf("tfacca%s", randomID) |
| 104 | + propertyB := fmt.Sprintf("tfaccb%s", randomID) |
| 105 | + repoName := fmt.Sprintf("%scustomprops-%s", testResourcePrefix, randomID) |
| 106 | + |
| 107 | + propertiesConfig := fmt.Sprintf(` |
| 108 | + resource "github_organization_custom_properties" "a" { |
| 109 | + allowed_values = ["one"] |
| 110 | + property_name = "%[1]s" |
| 111 | + value_type = "single_select" |
| 112 | + } |
| 113 | +
|
| 114 | + resource "github_organization_custom_properties" "b" { |
| 115 | + allowed_values = ["two"] |
| 116 | + property_name = "%[2]s" |
| 117 | + value_type = "single_select" |
| 118 | + } |
| 119 | + `, propertyA, propertyB) |
| 120 | + |
| 121 | + configBoth := propertiesConfig + fmt.Sprintf(` |
| 122 | + resource "github_repository" "test" { |
| 123 | + name = "%[1]s" |
| 124 | + auto_init = true |
| 125 | +
|
| 126 | + custom_properties = { |
| 127 | + (github_organization_custom_properties.a.property_name) = "one" |
| 128 | + (github_organization_custom_properties.b.property_name) = "two" |
| 129 | + } |
| 130 | + } |
| 131 | + `, repoName) |
| 132 | + |
| 133 | + configOnlyA := propertiesConfig + fmt.Sprintf(` |
| 134 | + resource "github_repository" "test" { |
| 135 | + name = "%[1]s" |
| 136 | + auto_init = true |
| 137 | +
|
| 138 | + custom_properties = { |
| 139 | + (github_organization_custom_properties.a.property_name) = "one" |
| 140 | + } |
| 141 | + } |
| 142 | + `, repoName) |
| 143 | + |
| 144 | + resource.Test(t, resource.TestCase{ |
| 145 | + PreCheck: func() { skipUnlessHasOrgs(t) }, |
| 146 | + ProviderFactories: providerFactories, |
| 147 | + Steps: []resource.TestStep{ |
| 148 | + { |
| 149 | + Config: configBoth, |
| 150 | + Check: resource.ComposeTestCheckFunc( |
| 151 | + resource.TestCheckResourceAttr("github_repository.test", "custom_properties.%", "2"), |
| 152 | + ), |
| 153 | + }, |
| 154 | + { |
| 155 | + Config: configOnlyA, |
| 156 | + Check: resource.ComposeTestCheckFunc( |
| 157 | + resource.TestCheckResourceAttr("github_repository.test", "custom_properties.%", "1"), |
| 158 | + resource.TestCheckResourceAttr( |
| 159 | + "github_repository.test", |
| 160 | + fmt.Sprintf("custom_properties.%s", propertyA), |
| 161 | + "one", |
| 162 | + ), |
| 163 | + resource.TestCheckNoResourceAttr( |
| 164 | + "github_repository.test", |
| 165 | + fmt.Sprintf("custom_properties.%s", propertyB), |
| 166 | + ), |
| 167 | + ), |
| 168 | + }, |
| 169 | + }, |
| 170 | + }) |
| 171 | + }) |
| 172 | + |
| 173 | + t.Run("clears all properties with empty map", func(t *testing.T) { |
| 174 | + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) |
| 175 | + propertyName := fmt.Sprintf("tfacc%s", randomID) |
| 176 | + repoName := fmt.Sprintf("%scustomprops-%s", testResourcePrefix, randomID) |
| 177 | + |
| 178 | + propertiesConfig := fmt.Sprintf(` |
| 179 | + resource "github_organization_custom_properties" "test" { |
| 180 | + allowed_values = ["alpha"] |
| 181 | + property_name = "%[1]s" |
| 182 | + value_type = "single_select" |
| 183 | + } |
| 184 | + `, propertyName) |
| 185 | + |
| 186 | + configWithValue := propertiesConfig + fmt.Sprintf(` |
| 187 | + resource "github_repository" "test" { |
| 188 | + name = "%[1]s" |
| 189 | + auto_init = true |
| 190 | +
|
| 191 | + custom_properties = { |
| 192 | + (github_organization_custom_properties.test.property_name) = "alpha" |
| 193 | + } |
| 194 | + } |
| 195 | + `, repoName) |
| 196 | + |
| 197 | + configEmpty := propertiesConfig + fmt.Sprintf(` |
| 198 | + resource "github_repository" "test" { |
| 199 | + name = "%[1]s" |
| 200 | + auto_init = true |
| 201 | +
|
| 202 | + custom_properties = {} |
| 203 | + } |
| 204 | + `, repoName) |
| 205 | + |
| 206 | + resource.Test(t, resource.TestCase{ |
| 207 | + PreCheck: func() { skipUnlessHasOrgs(t) }, |
| 208 | + ProviderFactories: providerFactories, |
| 209 | + Steps: []resource.TestStep{ |
| 210 | + { |
| 211 | + Config: configWithValue, |
| 212 | + Check: resource.TestCheckResourceAttr("github_repository.test", "custom_properties.%", "1"), |
| 213 | + }, |
| 214 | + { |
| 215 | + Config: configEmpty, |
| 216 | + Check: resource.TestCheckResourceAttr("github_repository.test", "custom_properties.%", "0"), |
| 217 | + }, |
| 218 | + }, |
| 219 | + }) |
| 220 | + }) |
| 221 | + |
| 222 | + t.Run("removing the block leaves values untouched", func(t *testing.T) { |
| 223 | + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) |
| 224 | + propertyName := fmt.Sprintf("tfacc%s", randomID) |
| 225 | + repoName := fmt.Sprintf("%scustomprops-%s", testResourcePrefix, randomID) |
| 226 | + |
| 227 | + propertiesConfig := fmt.Sprintf(` |
| 228 | + resource "github_organization_custom_properties" "test" { |
| 229 | + allowed_values = ["alpha"] |
| 230 | + property_name = "%[1]s" |
| 231 | + value_type = "single_select" |
| 232 | + } |
| 233 | + `, propertyName) |
| 234 | + |
| 235 | + configWithValue := propertiesConfig + fmt.Sprintf(` |
| 236 | + resource "github_repository" "test" { |
| 237 | + name = "%[1]s" |
| 238 | + auto_init = true |
| 239 | +
|
| 240 | + custom_properties = { |
| 241 | + (github_organization_custom_properties.test.property_name) = "alpha" |
| 242 | + } |
| 243 | + } |
| 244 | + `, repoName) |
| 245 | + |
| 246 | + configNoBlock := propertiesConfig + fmt.Sprintf(` |
| 247 | + resource "github_repository" "test" { |
| 248 | + name = "%[1]s" |
| 249 | + auto_init = true |
| 250 | + } |
| 251 | + `, repoName) |
| 252 | + |
| 253 | + resource.Test(t, resource.TestCase{ |
| 254 | + PreCheck: func() { skipUnlessHasOrgs(t) }, |
| 255 | + ProviderFactories: providerFactories, |
| 256 | + Steps: []resource.TestStep{ |
| 257 | + { |
| 258 | + Config: configWithValue, |
| 259 | + Check: resource.TestCheckResourceAttr( |
| 260 | + "github_repository.test", |
| 261 | + fmt.Sprintf("custom_properties.%s", propertyName), |
| 262 | + "alpha", |
| 263 | + ), |
| 264 | + }, |
| 265 | + { |
| 266 | + Config: configNoBlock, |
| 267 | + PlanOnly: true, |
| 268 | + ExpectNonEmptyPlan: false, |
| 269 | + }, |
| 270 | + }, |
| 271 | + }) |
| 272 | + }) |
| 273 | +} |
0 commit comments