|
| 1 | +package google |
| 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 | + "google.golang.org/api/runtimeconfig/v1beta1" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccRuntimeconfigConfig_basic(t *testing.T) { |
| 14 | + var runtimeConfig runtimeconfig.RuntimeConfig |
| 15 | + configName := fmt.Sprintf("runtimeconfig-test-%s", acctest.RandString(10)) |
| 16 | + description := "my test description" |
| 17 | + |
| 18 | + resource.Test(t, resource.TestCase{ |
| 19 | + PreCheck: func() { testAccPreCheck(t) }, |
| 20 | + Providers: testAccProviders, |
| 21 | + CheckDestroy: testAccCheckRuntimeconfigConfigDestroy, |
| 22 | + Steps: []resource.TestStep{ |
| 23 | + { |
| 24 | + Config: testAccRuntimeconfigConfig_basicDescription(configName, description), |
| 25 | + Check: resource.ComposeTestCheckFunc( |
| 26 | + testAccCheckRuntimeConfigExists( |
| 27 | + "google_runtimeconfig_config.foobar", &runtimeConfig), |
| 28 | + testAccCheckRuntimeConfigDescription(&runtimeConfig, description), |
| 29 | + ), |
| 30 | + }, |
| 31 | + }, |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +func TestAccRuntimeconfig_update(t *testing.T) { |
| 36 | + var runtimeConfig runtimeconfig.RuntimeConfig |
| 37 | + configName := fmt.Sprintf("runtimeconfig-test-%s", acctest.RandString(10)) |
| 38 | + firstDescription := "my test description" |
| 39 | + secondDescription := "my updated test description" |
| 40 | + |
| 41 | + resource.Test(t, resource.TestCase{ |
| 42 | + PreCheck: func() { testAccPreCheck(t) }, |
| 43 | + Providers: testAccProviders, |
| 44 | + CheckDestroy: testAccCheckRuntimeconfigConfigDestroy, |
| 45 | + Steps: []resource.TestStep{ |
| 46 | + { |
| 47 | + Config: testAccRuntimeconfigConfig_basicDescription(configName, firstDescription), |
| 48 | + Check: resource.ComposeTestCheckFunc( |
| 49 | + testAccCheckRuntimeConfigExists( |
| 50 | + "google_runtimeconfig_config.foobar", &runtimeConfig), |
| 51 | + testAccCheckRuntimeConfigDescription(&runtimeConfig, firstDescription), |
| 52 | + ), |
| 53 | + }, { |
| 54 | + Config: testAccRuntimeconfigConfig_basicDescription(configName, secondDescription), |
| 55 | + Check: resource.ComposeTestCheckFunc( |
| 56 | + testAccCheckRuntimeConfigExists( |
| 57 | + "google_runtimeconfig_config.foobar", &runtimeConfig), |
| 58 | + testAccCheckRuntimeConfigDescription(&runtimeConfig, secondDescription), |
| 59 | + ), |
| 60 | + }, |
| 61 | + }, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +func TestAccRuntimeconfig_updateEmptyDescription(t *testing.T) { |
| 66 | + var runtimeConfig runtimeconfig.RuntimeConfig |
| 67 | + configName := fmt.Sprintf("runtimeconfig-test-%s", acctest.RandString(10)) |
| 68 | + description := "my test description" |
| 69 | + |
| 70 | + resource.Test(t, resource.TestCase{ |
| 71 | + PreCheck: func() { testAccPreCheck(t) }, |
| 72 | + Providers: testAccProviders, |
| 73 | + CheckDestroy: testAccCheckRuntimeconfigConfigDestroy, |
| 74 | + Steps: []resource.TestStep{ |
| 75 | + { |
| 76 | + Config: testAccRuntimeconfigConfig_basicDescription(configName, description), |
| 77 | + Check: resource.ComposeTestCheckFunc( |
| 78 | + testAccCheckRuntimeConfigExists( |
| 79 | + "google_runtimeconfig_config.foobar", &runtimeConfig), |
| 80 | + testAccCheckRuntimeConfigDescription(&runtimeConfig, description), |
| 81 | + ), |
| 82 | + }, { |
| 83 | + Config: testAccRuntimeconfigConfig_emptyDescription(configName), |
| 84 | + Check: resource.ComposeTestCheckFunc( |
| 85 | + testAccCheckRuntimeConfigExists( |
| 86 | + "google_runtimeconfig_config.foobar", &runtimeConfig), |
| 87 | + testAccCheckRuntimeConfigDescription(&runtimeConfig, ""), |
| 88 | + ), |
| 89 | + }, |
| 90 | + }, |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +func testAccCheckRuntimeConfigDescription(runtimeConfig *runtimeconfig.RuntimeConfig, description string) resource.TestCheckFunc { |
| 95 | + return func(s *terraform.State) error { |
| 96 | + if runtimeConfig.Description != description { |
| 97 | + return fmt.Errorf("On runtime config '%s', expected description '%s', but found '%s'", |
| 98 | + runtimeConfig.Name, description, runtimeConfig.Description) |
| 99 | + } |
| 100 | + return nil |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func testAccCheckRuntimeConfigExists(resourceName string, runtimeConfig *runtimeconfig.RuntimeConfig) resource.TestCheckFunc { |
| 105 | + return func(s *terraform.State) error { |
| 106 | + rs, ok := s.RootModule().Resources[resourceName] |
| 107 | + if !ok { |
| 108 | + return fmt.Errorf("Not found: %s", resourceName) |
| 109 | + } |
| 110 | + |
| 111 | + if rs.Primary.ID == "" { |
| 112 | + return fmt.Errorf("No ID is set") |
| 113 | + } |
| 114 | + |
| 115 | + config := testAccProvider.Meta().(*Config) |
| 116 | + |
| 117 | + found, err := config.clientRuntimeconfig.Projects.Configs.Get(rs.Primary.ID).Do() |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + *runtimeConfig = *found |
| 123 | + |
| 124 | + return nil |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func testAccCheckRuntimeconfigConfigDestroy(s *terraform.State) error { |
| 129 | + config := testAccProvider.Meta().(*Config) |
| 130 | + |
| 131 | + for _, rs := range s.RootModule().Resources { |
| 132 | + if rs.Type != "google_runtimeconfig_config" { |
| 133 | + continue |
| 134 | + } |
| 135 | + |
| 136 | + _, err := config.clientRuntimeconfig.Projects.Configs.Get(rs.Primary.ID).Do() |
| 137 | + |
| 138 | + if err == nil { |
| 139 | + return fmt.Errorf("Runtimeconfig still exists") |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func testAccRuntimeconfigConfig_basicDescription(name, description string) string { |
| 147 | + return fmt.Sprintf(` |
| 148 | +resource "google_runtimeconfig_config" "foobar" { |
| 149 | + name = "%s" |
| 150 | + description = "%s" |
| 151 | +}`, name, description) |
| 152 | +} |
| 153 | + |
| 154 | +func testAccRuntimeconfigConfig_emptyDescription(name string) string { |
| 155 | + return fmt.Sprintf(` |
| 156 | +resource "google_runtimeconfig_config" "foobar" { |
| 157 | + name = "%s" |
| 158 | +}`, name) |
| 159 | +} |
0 commit comments