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