|
1 | 1 | package github |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
| 6 | + "net/url" |
5 | 7 | "strings" |
6 | 8 | "testing" |
7 | 9 |
|
| 10 | + "github.com/google/go-github/v66/github" |
8 | 11 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
9 | | - |
10 | 12 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
11 | 14 | ) |
12 | 15 |
|
13 | 16 | func TestAccGithubActionsEnvironmentVariable(t *testing.T) { |
@@ -195,3 +198,86 @@ func TestAccGithubActionsEnvironmentVariable(t *testing.T) { |
195 | 198 | }) |
196 | 199 | }) |
197 | 200 | } |
| 201 | + |
| 202 | +func TestAccGithubActionsEnvironmentVariable_alreadyExists(t *testing.T) { |
| 203 | + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) |
| 204 | + repoName := fmt.Sprintf("tf-acc-test-%s", randomID) |
| 205 | + envName := "environment / test" |
| 206 | + varName := "test_variable" |
| 207 | + value := "my_variable_value" |
| 208 | + |
| 209 | + config := fmt.Sprintf(` |
| 210 | + resource "github_repository" "test" { |
| 211 | + name = "%s" |
| 212 | + vulnerability_alerts = true |
| 213 | + } |
| 214 | +
|
| 215 | + resource "github_repository_environment" "test" { |
| 216 | + repository = github_repository.test.name |
| 217 | + environment = "%s" |
| 218 | + } |
| 219 | +
|
| 220 | + resource "github_actions_environment_variable" "variable" { |
| 221 | + repository = github_repository.test.name |
| 222 | + environment = github_repository_environment.test.environment |
| 223 | + variable_name = "%s" |
| 224 | + value = "%s" |
| 225 | + } |
| 226 | + `, repoName, envName, varName, value) |
| 227 | + |
| 228 | + testCase := func(t *testing.T, mode string) { |
| 229 | + resource.Test(t, resource.TestCase{ |
| 230 | + PreCheck: func() { |
| 231 | + skipUnlessMode(t, mode) |
| 232 | + }, |
| 233 | + Providers: testAccProviders, |
| 234 | + Steps: []resource.TestStep{ |
| 235 | + { |
| 236 | + // First, create the repository and environment. |
| 237 | + Config: fmt.Sprintf(` |
| 238 | + resource "github_repository" "test" { |
| 239 | + name = "%s" |
| 240 | + vulnerability_alerts = true |
| 241 | + } |
| 242 | +
|
| 243 | + resource "github_repository_environment" "test" { |
| 244 | + repository = github_repository.test.name |
| 245 | + environment = "%s" |
| 246 | + } |
| 247 | + `, repoName, envName), |
| 248 | + Check: resource.ComposeTestCheckFunc( |
| 249 | + func(s *terraform.State) error { |
| 250 | + // Now that the repo and env are created, create the variable using the API. |
| 251 | + client := testAccProvider.Meta().(*Owner).v3client |
| 252 | + owner := testAccProvider.Meta().(*Owner).name |
| 253 | + ctx := context.Background() |
| 254 | + escapedEnvName := url.PathEscape(envName) |
| 255 | + |
| 256 | + variable := &github.ActionsVariable{ |
| 257 | + Name: varName, |
| 258 | + Value: value, |
| 259 | + } |
| 260 | + _, err := client.Actions.CreateEnvVariable(ctx, owner, repoName, escapedEnvName, variable) |
| 261 | + return err |
| 262 | + }, |
| 263 | + ), |
| 264 | + }, |
| 265 | + { |
| 266 | + // Now, run the full config. Terraform should detect the existing variable and "adopt" it. |
| 267 | + Config: config, |
| 268 | + Check: resource.ComposeTestCheckFunc( |
| 269 | + resource.TestCheckResourceAttr("github_actions_environment_variable.variable", "value", value), |
| 270 | + ), |
| 271 | + }, |
| 272 | + }, |
| 273 | + }) |
| 274 | + } |
| 275 | + |
| 276 | + t.Run("with an individual account", func(t *testing.T) { |
| 277 | + testCase(t, individual) |
| 278 | + }) |
| 279 | + |
| 280 | + t.Run("with an organization account", func(t *testing.T) { |
| 281 | + testCase(t, organization) |
| 282 | + }) |
| 283 | +} |
0 commit comments