github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/rancher/resource_rancher_stack_test.go (about) 1 package rancher 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 rancherClient "github.com/rancher/go-rancher/client" 11 ) 12 13 func TestAccRancherStack_basic(t *testing.T) { 14 var stack rancherClient.Environment 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckRancherStackDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccRancherStackConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckRancherStackExists("rancher_stack.foo", &stack), 25 resource.TestCheckResourceAttr("rancher_stack.foo", "name", "foo"), 26 resource.TestCheckResourceAttr("rancher_stack.foo", "description", "Terraform acc test group"), 27 resource.TestCheckResourceAttr("rancher_stack.foo", "catalog_id", ""), 28 resource.TestCheckResourceAttr("rancher_stack.foo", "docker_compose", ""), 29 resource.TestCheckResourceAttr("rancher_stack.foo", "rancher_compose", ""), 30 testAccCheckRancherStackAttributes(&stack, emptyEnvironment, false), 31 ), 32 }, 33 resource.TestStep{ 34 Config: testAccRancherStackUpdateConfig, 35 Check: resource.ComposeTestCheckFunc( 36 testAccCheckRancherStackExists("rancher_stack.foo", &stack), 37 resource.TestCheckResourceAttr("rancher_stack.foo", "name", "foo2"), 38 resource.TestCheckResourceAttr("rancher_stack.foo", "description", "Terraform acc test group - updated"), 39 resource.TestCheckResourceAttr("rancher_stack.foo", "catalog_id", ""), 40 resource.TestCheckResourceAttr("rancher_stack.foo", "docker_compose", ""), 41 resource.TestCheckResourceAttr("rancher_stack.foo", "rancher_compose", ""), 42 testAccCheckRancherStackAttributes(&stack, emptyEnvironment, false), 43 ), 44 }, 45 }, 46 }) 47 } 48 49 func TestAccRancherStack_compose(t *testing.T) { 50 var stack rancherClient.Environment 51 52 resource.Test(t, resource.TestCase{ 53 PreCheck: func() { testAccPreCheck(t) }, 54 Providers: testAccProviders, 55 CheckDestroy: testAccCheckRancherStackDestroy, 56 Steps: []resource.TestStep{ 57 resource.TestStep{ 58 Config: testAccRancherStackComposeConfig, 59 Check: resource.ComposeTestCheckFunc( 60 testAccCheckRancherStackExists("rancher_stack.compose", &stack), 61 resource.TestCheckResourceAttr("rancher_stack.compose", "name", "compose"), 62 resource.TestCheckResourceAttr("rancher_stack.compose", "description", "Terraform acc test group - compose"), 63 resource.TestCheckResourceAttr("rancher_stack.compose", "catalog_id", ""), 64 resource.TestCheckResourceAttr("rancher_stack.compose", "docker_compose", "web: { image: nginx }"), 65 resource.TestCheckResourceAttr("rancher_stack.compose", "rancher_compose", "web: { scale: 1 }"), 66 testAccCheckRancherStackAttributes(&stack, emptyEnvironment, false), 67 ), 68 }, 69 }, 70 }) 71 } 72 73 //The following tests are run against the Default environment because 74 //upgrading a stack automatically starts the services which never 75 //completes if there is no host available 76 func TestAccRancherStack_catalog(t *testing.T) { 77 var stack rancherClient.Environment 78 79 resource.Test(t, resource.TestCase{ 80 PreCheck: func() { testAccPreCheck(t) }, 81 Providers: testAccProviders, 82 CheckDestroy: testAccCheckRancherStackDestroy, 83 Steps: []resource.TestStep{ 84 resource.TestStep{ 85 Config: testAccRancherStackSystemCatalogConfigInitial, 86 Check: resource.ComposeTestCheckFunc( 87 testAccCheckRancherStackExists("rancher_stack.catalog", &stack), 88 resource.TestCheckResourceAttr("rancher_stack.catalog", "name", "catalogInitial"), 89 resource.TestCheckResourceAttr("rancher_stack.catalog", "description", "Terraform acc test group - catalogInitial"), 90 resource.TestCheckResourceAttr("rancher_stack.catalog", "catalog_id", "community:janitor:0"), 91 resource.TestCheckResourceAttr("rancher_stack.catalog", "scope", "system"), 92 resource.TestCheckResourceAttr("rancher_stack.catalog", "docker_compose", ""), 93 resource.TestCheckResourceAttr("rancher_stack.catalog", "rancher_compose", ""), 94 resource.TestCheckResourceAttr("rancher_stack.catalog", "rendered_docker_compose", catalogDockerComposeInitial), 95 resource.TestCheckResourceAttr("rancher_stack.catalog", "rendered_rancher_compose", catalogRancherComposeInitial), 96 testAccCheckRancherStackAttributes(&stack, catalogEnvironment, true), 97 ), 98 }, 99 resource.TestStep{ 100 Config: testAccRancherStackSystemCatalogConfigUpdate, 101 Check: resource.ComposeTestCheckFunc( 102 testAccCheckRancherStackExists("rancher_stack.catalog", &stack), 103 resource.TestCheckResourceAttr("rancher_stack.catalog", "name", "catalogUpdate"), 104 resource.TestCheckResourceAttr("rancher_stack.catalog", "description", "Terraform acc test group - catalogUpdate"), 105 resource.TestCheckResourceAttr("rancher_stack.catalog", "catalog_id", "community:janitor:1"), 106 resource.TestCheckResourceAttr("rancher_stack.catalog", "scope", "user"), 107 resource.TestCheckResourceAttr("rancher_stack.catalog", "docker_compose", ""), 108 resource.TestCheckResourceAttr("rancher_stack.catalog", "rancher_compose", ""), 109 resource.TestCheckResourceAttr("rancher_stack.catalog", "rendered_docker_compose", catalogDockerComposeUpdate), 110 resource.TestCheckResourceAttr("rancher_stack.catalog", "rendered_rancher_compose", catalogRancherComposeUpdate), 111 testAccCheckRancherStackAttributes(&stack, catalogEnvironmentUpgrade, true), 112 ), 113 }, 114 }, 115 }) 116 } 117 118 func TestAccRancherStack_disappears(t *testing.T) { 119 var stack rancherClient.Environment 120 121 resource.Test(t, resource.TestCase{ 122 PreCheck: func() { testAccPreCheck(t) }, 123 Providers: testAccProviders, 124 CheckDestroy: testAccCheckRancherStackDestroy, 125 Steps: []resource.TestStep{ 126 resource.TestStep{ 127 Config: testAccRancherStackConfig, 128 Check: resource.ComposeTestCheckFunc( 129 testAccCheckRancherStackExists("rancher_stack.foo", &stack), 130 testAccRancherStackDisappears(&stack), 131 ), 132 ExpectNonEmptyPlan: true, 133 }, 134 }, 135 }) 136 } 137 138 func testAccRancherStackDisappears(stack *rancherClient.Environment) resource.TestCheckFunc { 139 return func(s *terraform.State) error { 140 client, err := testAccProvider.Meta().(*Config).EnvironmentClient(stack.AccountId) 141 if err != nil { 142 return err 143 } 144 145 if err := client.Environment.Delete(stack); err != nil { 146 return fmt.Errorf("Error deleting Stack: %s", err) 147 } 148 149 stateConf := &resource.StateChangeConf{ 150 Pending: []string{"active", "removed", "removing"}, 151 Target: []string{"removed"}, 152 Refresh: StackStateRefreshFunc(client, stack.Id), 153 Timeout: 10 * time.Minute, 154 Delay: 1 * time.Second, 155 MinTimeout: 3 * time.Second, 156 } 157 158 _, waitErr := stateConf.WaitForState() 159 if waitErr != nil { 160 return fmt.Errorf( 161 "Error waiting for stack (%s) to be removed: %s", stack.Id, waitErr) 162 } 163 164 return nil 165 } 166 } 167 168 func testAccCheckRancherStackExists(n string, stack *rancherClient.Environment) resource.TestCheckFunc { 169 return func(s *terraform.State) error { 170 rs, ok := s.RootModule().Resources[n] 171 172 if !ok { 173 return fmt.Errorf("Not found: %s", n) 174 } 175 176 if rs.Primary.ID == "" { 177 return fmt.Errorf("No App Name is set") 178 } 179 180 client, err := testAccProvider.Meta().(*Config).EnvironmentClient(rs.Primary.Attributes["environment_id"]) 181 if err != nil { 182 return err 183 } 184 185 foundStack, err := client.Environment.ById(rs.Primary.ID) 186 if err != nil { 187 return err 188 } 189 190 if foundStack.Resource.Id != rs.Primary.ID { 191 return fmt.Errorf("Stack not found") 192 } 193 194 *stack = *foundStack 195 196 return nil 197 } 198 } 199 200 func testAccCheckRancherStackAttributes(stack *rancherClient.Environment, environment map[string]string, startOnCreate bool) resource.TestCheckFunc { 201 return func(s *terraform.State) error { 202 203 if len(stack.Environment) != len(environment) { 204 return fmt.Errorf("Bad environment size: %v should be: %v", len(stack.Environment), environment) 205 } 206 207 for k, v := range stack.Environment { 208 if environment[k] != v { 209 return fmt.Errorf("Bad environment value for %s: %s should be: %s", k, environment[k], v) 210 } 211 } 212 213 if stack.StartOnCreate != startOnCreate { 214 return fmt.Errorf("Bad startOnCreate: %t should be: %t", stack.StartOnCreate, startOnCreate) 215 } 216 217 return nil 218 } 219 } 220 221 func testAccCheckRancherStackDestroy(s *terraform.State) error { 222 for _, rs := range s.RootModule().Resources { 223 if rs.Type != "rancher_stack" { 224 continue 225 } 226 client, err := testAccProvider.Meta().(*Config).GlobalClient() 227 if err != nil { 228 return err 229 } 230 231 stack, err := client.Environment.ById(rs.Primary.ID) 232 233 if err == nil { 234 if stack != nil && 235 stack.Resource.Id == rs.Primary.ID && 236 stack.State != "removed" { 237 return fmt.Errorf("Stack still exists") 238 } 239 } 240 241 return nil 242 } 243 return nil 244 } 245 246 const testAccRancherStackConfig = ` 247 resource "rancher_environment" "foo" { 248 name = "foo" 249 } 250 251 resource "rancher_stack" "foo" { 252 name = "foo" 253 description = "Terraform acc test group" 254 environment_id = "${rancher_environment.foo.id}" 255 } 256 ` 257 258 const testAccRancherStackUpdateConfig = ` 259 resource "rancher_environment" "foo" { 260 name = "foo" 261 } 262 263 resource "rancher_stack" "foo" { 264 name = "foo2" 265 description = "Terraform acc test group - updated" 266 environment_id = "${rancher_environment.foo.id}" 267 } 268 ` 269 270 const testAccRancherStackComposeConfig = ` 271 resource "rancher_environment" "foo" { 272 name = "foo" 273 } 274 275 resource "rancher_stack" "compose" { 276 name = "compose" 277 description = "Terraform acc test group - compose" 278 environment_id = "${rancher_environment.foo.id}" 279 docker_compose = "web: { image: nginx }" 280 rancher_compose = "web: { scale: 1 }" 281 } 282 ` 283 284 const testAccRancherStackSystemCatalogConfigInitial = ` 285 resource "rancher_stack" "catalog" { 286 name = "catalogInitial" 287 description = "Terraform acc test group - catalogInitial" 288 environment_id = "1a5" 289 catalog_id = "community:janitor:0" 290 scope = "system" 291 start_on_create = true 292 environment { 293 EXCLUDE_LABEL = "cleanup=false" 294 FREQUENCY = "60" 295 KEEP = "rancher/agent:*" 296 } 297 } 298 ` 299 300 const testAccRancherStackSystemCatalogConfigUpdate = ` 301 resource "rancher_stack" "catalog" { 302 name = "catalogUpdate" 303 description = "Terraform acc test group - catalogUpdate" 304 environment_id = "1a5" 305 catalog_id = "community:janitor:1" 306 scope = "user" 307 environment { 308 EXCLUDE_LABEL = "cleanup=false" 309 FREQUENCY = "60" 310 KEEP = "rancher/agent:*" 311 KEEPC = "*:*" 312 } 313 } 314 ` 315 316 var catalogDockerComposeInitial = `cleanup: 317 environment: 318 CLEAN_PERIOD: '60' 319 DELAY_TIME: '900' 320 KEEP_IMAGES: rancher/agent:* 321 labels: 322 io.rancher.scheduler.global: 'true' 323 io.rancher.scheduler.affinity:host_label_ne: cleanup=false 324 tty: true 325 image: meltwater/docker-cleanup:1.4.0 326 privileged: true 327 volumes: 328 - /var/run/docker.sock:/var/run/docker.sock 329 - /var/lib/docker:/var/lib/docker 330 stdin_open: true 331 ` 332 333 const catalogRancherComposeInitial = `{} 334 ` 335 336 const catalogDockerComposeUpdate = `cleanup: 337 environment: 338 CLEAN_PERIOD: '60' 339 DELAY_TIME: '900' 340 KEEP_CONTAINERS: '*:*' 341 KEEP_IMAGES: rancher/agent:* 342 labels: 343 io.rancher.scheduler.global: 'true' 344 io.rancher.scheduler.affinity:host_label_ne: cleanup=false 345 image: sshipway/docker-cleanup:1.5.2 346 volumes: 347 - /var/run/docker.sock:/var/run/docker.sock 348 - /var/lib/docker:/var/lib/docker 349 net: none 350 ` 351 352 const catalogRancherComposeUpdate = `{} 353 ` 354 355 var emptyEnvironment = map[string]string{} 356 357 var catalogEnvironment = map[string]string{ 358 "EXCLUDE_LABEL": "cleanup=false", 359 "FREQUENCY": "60", 360 "KEEP": "rancher/agent:*", 361 } 362 363 var catalogEnvironmentUpgrade = map[string]string{ 364 "EXCLUDE_LABEL": "cleanup=false", 365 "FREQUENCY": "60", 366 "KEEP": "rancher/agent:*", 367 "KEEPC": "*:*", 368 }