|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform/helper/acctest" |
| 8 | + "github.com/hashicorp/terraform/helper/resource" |
| 9 | + "github.com/hashicorp/terraform/terraform" |
| 10 | + "github.com/xanzy/go-gitlab" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccGitlabGroupVariable_basic(t *testing.T) { |
| 14 | + var groupVariable gitlab.GroupVariable |
| 15 | + rString := acctest.RandString(5) |
| 16 | + |
| 17 | + resource.Test(t, resource.TestCase{ |
| 18 | + PreCheck: func() { testAccPreCheck(t) }, |
| 19 | + Providers: testAccProviders, |
| 20 | + CheckDestroy: testAccCheckGitlabGroupVariableDestroy, |
| 21 | + Steps: []resource.TestStep{ |
| 22 | + // Create a group and variable with default options |
| 23 | + { |
| 24 | + Config: testAccGitlabGroupVariableConfig(rString), |
| 25 | + Check: resource.ComposeTestCheckFunc( |
| 26 | + testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable), |
| 27 | + testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{ |
| 28 | + Key: fmt.Sprintf("key_%s", rString), |
| 29 | + Value: fmt.Sprintf("value-%s", rString), |
| 30 | + }), |
| 31 | + ), |
| 32 | + }, |
| 33 | + // Update the group variable to toggle all the values to their inverse |
| 34 | + { |
| 35 | + Config: testAccGitlabGroupVariableUpdateConfig(rString), |
| 36 | + Check: resource.ComposeTestCheckFunc( |
| 37 | + testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable), |
| 38 | + testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{ |
| 39 | + Key: fmt.Sprintf("key_%s", rString), |
| 40 | + Value: fmt.Sprintf("value-inverse-%s", rString), |
| 41 | + Protected: true, |
| 42 | + }), |
| 43 | + ), |
| 44 | + }, |
| 45 | + // Update the group variable to toggle the options back |
| 46 | + { |
| 47 | + Config: testAccGitlabGroupVariableConfig(rString), |
| 48 | + Check: resource.ComposeTestCheckFunc( |
| 49 | + testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable), |
| 50 | + testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{ |
| 51 | + Key: fmt.Sprintf("key_%s", rString), |
| 52 | + Value: fmt.Sprintf("value-%s", rString), |
| 53 | + Protected: false, |
| 54 | + }), |
| 55 | + ), |
| 56 | + }, |
| 57 | + }, |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +func testAccCheckGitlabGroupVariableExists(n string, groupVariable *gitlab.GroupVariable) resource.TestCheckFunc { |
| 62 | + return func(s *terraform.State) error { |
| 63 | + rs, ok := s.RootModule().Resources[n] |
| 64 | + if !ok { |
| 65 | + return fmt.Errorf("Not Found: %s", n) |
| 66 | + } |
| 67 | + |
| 68 | + repoName := rs.Primary.Attributes["group"] |
| 69 | + if repoName == "" { |
| 70 | + return fmt.Errorf("No group ID is set") |
| 71 | + } |
| 72 | + key := rs.Primary.Attributes["key"] |
| 73 | + if key == "" { |
| 74 | + return fmt.Errorf("No variable key is set") |
| 75 | + } |
| 76 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 77 | + |
| 78 | + gotVariable, _, err := conn.GroupVariables.GetVariable(repoName, key) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + *groupVariable = *gotVariable |
| 83 | + return nil |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +type testAccGitlabGroupVariableExpectedAttributes struct { |
| 88 | + Key string |
| 89 | + Value string |
| 90 | + Protected bool |
| 91 | +} |
| 92 | + |
| 93 | +func testAccCheckGitlabGroupVariableAttributes(variable *gitlab.GroupVariable, want *testAccGitlabGroupVariableExpectedAttributes) resource.TestCheckFunc { |
| 94 | + return func(s *terraform.State) error { |
| 95 | + if variable.Key != want.Key { |
| 96 | + return fmt.Errorf("got key %s; want %s", variable.Key, want.Key) |
| 97 | + } |
| 98 | + |
| 99 | + if variable.Value != want.Value { |
| 100 | + return fmt.Errorf("got value %s; value %s", variable.Value, want.Value) |
| 101 | + } |
| 102 | + |
| 103 | + if variable.Protected != want.Protected { |
| 104 | + return fmt.Errorf("got protected %t; want %t", variable.Protected, want.Protected) |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func testAccCheckGitlabGroupVariableDestroy(s *terraform.State) error { |
| 112 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 113 | + |
| 114 | + for _, rs := range s.RootModule().Resources { |
| 115 | + if rs.Type != "gitlab_group" { |
| 116 | + continue |
| 117 | + } |
| 118 | + |
| 119 | + _, resp, err := conn.Groups.GetGroup(rs.Primary.ID) |
| 120 | + if err == nil { |
| 121 | + //if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID { |
| 122 | + // return fmt.Errorf("Repository still exists") |
| 123 | + //} |
| 124 | + } |
| 125 | + if resp.StatusCode != 404 { |
| 126 | + return err |
| 127 | + } |
| 128 | + return nil |
| 129 | + } |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +func testAccGitlabGroupVariableConfig(rString string) string { |
| 134 | + return fmt.Sprintf(` |
| 135 | +resource "gitlab_group" "foo" { |
| 136 | +name = "foo%v" |
| 137 | +path = "foo%v" |
| 138 | +} |
| 139 | +
|
| 140 | +resource "gitlab_group_variable" "foo" { |
| 141 | + group = "${gitlab_group.foo.id}" |
| 142 | + key = "key_%s" |
| 143 | + value = "value-%s" |
| 144 | +} |
| 145 | + `, rString, rString, rString, rString) |
| 146 | +} |
| 147 | + |
| 148 | +func testAccGitlabGroupVariableUpdateConfig(rString string) string { |
| 149 | + return fmt.Sprintf(` |
| 150 | +resource "gitlab_group" "foo" { |
| 151 | +name = "foo%v" |
| 152 | +path = "foo%v" |
| 153 | +} |
| 154 | +
|
| 155 | +resource "gitlab_group_variable" "foo" { |
| 156 | + group = "${gitlab_group.foo.id}" |
| 157 | + key = "key_%s" |
| 158 | + value = "value-inverse-%s" |
| 159 | + protected = true |
| 160 | +} |
| 161 | + `, rString, rString, rString, rString) |
| 162 | +} |
0 commit comments