|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 10 | + "github.com/xanzy/go-gitlab" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccGitlabInstanceVariable_basic(t *testing.T) { |
| 14 | + var instanceVariable gitlab.InstanceVariable |
| 15 | + rString := acctest.RandString(5) |
| 16 | + |
| 17 | + resource.Test(t, resource.TestCase{ |
| 18 | + PreCheck: func() { testAccPreCheck(t) }, |
| 19 | + Providers: testAccProviders, |
| 20 | + Steps: []resource.TestStep{ |
| 21 | + // Create a variable with default options |
| 22 | + { |
| 23 | + Config: testAccGitlabInstanceVariableConfig(rString), |
| 24 | + Check: resource.ComposeTestCheckFunc( |
| 25 | + testAccCheckGitlabInstanceVariableExists("gitlab_instance_variable.foo", &instanceVariable), |
| 26 | + testAccCheckGitlabInstanceVariableAttributes(&instanceVariable, &testAccGitlabInstanceVariableExpectedAttributes{ |
| 27 | + Key: fmt.Sprintf("key_%s", rString), |
| 28 | + Value: fmt.Sprintf("value-%s", rString), |
| 29 | + }), |
| 30 | + ), |
| 31 | + }, |
| 32 | + // Update the instance variable to toggle all the values to their inverse |
| 33 | + { |
| 34 | + Config: testAccGitlabInstanceVariableUpdateConfig(rString), |
| 35 | + Check: resource.ComposeTestCheckFunc( |
| 36 | + testAccCheckGitlabInstanceVariableExists("gitlab_instance_variable.foo", &instanceVariable), |
| 37 | + testAccCheckGitlabInstanceVariableAttributes(&instanceVariable, &testAccGitlabInstanceVariableExpectedAttributes{ |
| 38 | + Key: fmt.Sprintf("key_%s", rString), |
| 39 | + Value: fmt.Sprintf("value-inverse-%s", rString), |
| 40 | + Protected: true, |
| 41 | + }), |
| 42 | + ), |
| 43 | + }, |
| 44 | + // Update the instance variable to toggle the options back |
| 45 | + { |
| 46 | + Config: testAccGitlabInstanceVariableConfig(rString), |
| 47 | + Check: resource.ComposeTestCheckFunc( |
| 48 | + testAccCheckGitlabInstanceVariableExists("gitlab_instance_variable.foo", &instanceVariable), |
| 49 | + testAccCheckGitlabInstanceVariableAttributes(&instanceVariable, &testAccGitlabInstanceVariableExpectedAttributes{ |
| 50 | + Key: fmt.Sprintf("key_%s", rString), |
| 51 | + Value: fmt.Sprintf("value-%s", rString), |
| 52 | + Protected: false, |
| 53 | + }), |
| 54 | + ), |
| 55 | + }, |
| 56 | + }, |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +func testAccCheckGitlabInstanceVariableExists(n string, instanceVariable *gitlab.InstanceVariable) resource.TestCheckFunc { |
| 61 | + return func(s *terraform.State) error { |
| 62 | + rs, ok := s.RootModule().Resources[n] |
| 63 | + if !ok { |
| 64 | + return fmt.Errorf("Not Found: %s", n) |
| 65 | + } |
| 66 | + |
| 67 | + key := rs.Primary.Attributes["key"] |
| 68 | + if key == "" { |
| 69 | + return fmt.Errorf("No variable key is set") |
| 70 | + } |
| 71 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 72 | + |
| 73 | + gotVariable, _, err := conn.InstanceVariables.GetVariable(key) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + *instanceVariable = *gotVariable |
| 78 | + return nil |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +type testAccGitlabInstanceVariableExpectedAttributes struct { |
| 83 | + Key string |
| 84 | + Value string |
| 85 | + Protected bool |
| 86 | + Masked bool |
| 87 | +} |
| 88 | + |
| 89 | +func testAccCheckGitlabInstanceVariableAttributes(variable *gitlab.InstanceVariable, want *testAccGitlabInstanceVariableExpectedAttributes) resource.TestCheckFunc { |
| 90 | + return func(s *terraform.State) error { |
| 91 | + if variable.Key != want.Key { |
| 92 | + return fmt.Errorf("got key %s; want %s", variable.Key, want.Key) |
| 93 | + } |
| 94 | + |
| 95 | + if variable.Value != want.Value { |
| 96 | + return fmt.Errorf("got value %s; value %s", variable.Value, want.Value) |
| 97 | + } |
| 98 | + |
| 99 | + if variable.Protected != want.Protected { |
| 100 | + return fmt.Errorf("got protected %t; want %t", variable.Protected, want.Protected) |
| 101 | + } |
| 102 | + |
| 103 | + if variable.Masked != want.Masked { |
| 104 | + return fmt.Errorf("got masked %t; want %t", variable.Masked, want.Masked) |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func testAccGitlabInstanceVariableConfig(rString string) string { |
| 112 | + return fmt.Sprintf(` |
| 113 | +resource "gitlab_instance_variable" "foo" { |
| 114 | + key = "key_%s" |
| 115 | + value = "value-%s" |
| 116 | + variable_type = "file" |
| 117 | + masked = false |
| 118 | +} |
| 119 | + `, rString, rString) |
| 120 | +} |
| 121 | + |
| 122 | +func testAccGitlabInstanceVariableUpdateConfig(rString string) string { |
| 123 | + return fmt.Sprintf(` |
| 124 | +resource "gitlab_instance_variable" "foo" { |
| 125 | + key = "key_%s" |
| 126 | + value = "value-inverse-%s" |
| 127 | + protected = true |
| 128 | + masked = false |
| 129 | +} |
| 130 | + `, rString, rString) |
| 131 | +} |
0 commit comments