github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudstack/resource_cloudstack_template_test.go (about) 1 package cloudstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/xanzy/go-cloudstack/cloudstack" 10 ) 11 12 func TestAccCloudStackTemplate_basic(t *testing.T) { 13 var template cloudstack.Template 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckCloudStackTemplateDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCloudStackTemplate_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckCloudStackTemplateExists("cloudstack_template.foo", &template), 24 testAccCheckCloudStackTemplateBasicAttributes(&template), 25 resource.TestCheckResourceAttr( 26 "cloudstack_template.foo", "display_text", "terraform-test"), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func TestAccCloudStackTemplate_update(t *testing.T) { 34 var template cloudstack.Template 35 36 resource.Test(t, resource.TestCase{ 37 PreCheck: func() { testAccPreCheck(t) }, 38 Providers: testAccProviders, 39 CheckDestroy: testAccCheckCloudStackTemplateDestroy, 40 Steps: []resource.TestStep{ 41 resource.TestStep{ 42 Config: testAccCloudStackTemplate_basic, 43 Check: resource.ComposeTestCheckFunc( 44 testAccCheckCloudStackTemplateExists("cloudstack_template.foo", &template), 45 testAccCheckCloudStackTemplateBasicAttributes(&template), 46 ), 47 }, 48 49 resource.TestStep{ 50 Config: testAccCloudStackTemplate_update, 51 Check: resource.ComposeTestCheckFunc( 52 testAccCheckCloudStackTemplateExists( 53 "cloudstack_template.foo", &template), 54 testAccCheckCloudStackTemplateUpdatedAttributes(&template), 55 resource.TestCheckResourceAttr( 56 "cloudstack_template.foo", "display_text", "terraform-updated"), 57 ), 58 }, 59 }, 60 }) 61 } 62 63 func testAccCheckCloudStackTemplateExists( 64 n string, template *cloudstack.Template) resource.TestCheckFunc { 65 return func(s *terraform.State) error { 66 rs, ok := s.RootModule().Resources[n] 67 if !ok { 68 return fmt.Errorf("Not found: %s", n) 69 } 70 71 if rs.Primary.ID == "" { 72 return fmt.Errorf("No template ID is set") 73 } 74 75 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 76 tmpl, _, err := cs.Template.GetTemplateByID(rs.Primary.ID, "executable") 77 78 if err != nil { 79 return err 80 } 81 82 if tmpl.Id != rs.Primary.ID { 83 return fmt.Errorf("Template not found") 84 } 85 86 *template = *tmpl 87 88 return nil 89 } 90 } 91 92 func testAccCheckCloudStackTemplateBasicAttributes( 93 template *cloudstack.Template) resource.TestCheckFunc { 94 return func(s *terraform.State) error { 95 96 if template.Name != "terraform-test" { 97 return fmt.Errorf("Bad name: %s", template.Name) 98 } 99 100 if template.Format != CLOUDSTACK_TEMPLATE_FORMAT { 101 return fmt.Errorf("Bad format: %s", template.Format) 102 } 103 104 if template.Hypervisor != CLOUDSTACK_HYPERVISOR { 105 return fmt.Errorf("Bad hypervisor: %s", template.Hypervisor) 106 } 107 108 if template.Ostypename != CLOUDSTACK_TEMPLATE_OS_TYPE { 109 return fmt.Errorf("Bad os type: %s", template.Ostypename) 110 } 111 112 if template.Zonename != CLOUDSTACK_ZONE { 113 return fmt.Errorf("Bad zone: %s", template.Zonename) 114 } 115 116 return nil 117 } 118 } 119 120 func testAccCheckCloudStackTemplateUpdatedAttributes( 121 template *cloudstack.Template) resource.TestCheckFunc { 122 return func(s *terraform.State) error { 123 124 if template.Displaytext != "terraform-updated" { 125 return fmt.Errorf("Bad name: %s", template.Displaytext) 126 } 127 128 if !template.Isdynamicallyscalable { 129 return fmt.Errorf("Bad is_dynamically_scalable: %t", template.Isdynamicallyscalable) 130 } 131 132 if !template.Passwordenabled { 133 return fmt.Errorf("Bad password_enabled: %t", template.Passwordenabled) 134 } 135 136 return nil 137 } 138 } 139 140 func testAccCheckCloudStackTemplateDestroy(s *terraform.State) error { 141 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 142 143 for _, rs := range s.RootModule().Resources { 144 if rs.Type != "cloudstack_template" { 145 continue 146 } 147 148 if rs.Primary.ID == "" { 149 return fmt.Errorf("No template ID is set") 150 } 151 152 _, _, err := cs.Template.GetTemplateByID(rs.Primary.ID, "executable") 153 if err == nil { 154 return fmt.Errorf("Template %s still exists", rs.Primary.ID) 155 } 156 } 157 158 return nil 159 } 160 161 var testAccCloudStackTemplate_basic = fmt.Sprintf(` 162 resource "cloudstack_template" "foo" { 163 name = "terraform-test" 164 format = "%s" 165 hypervisor = "%s" 166 os_type = "%s" 167 url = "%s" 168 zone = "%s" 169 } 170 `, 171 CLOUDSTACK_TEMPLATE_FORMAT, 172 CLOUDSTACK_HYPERVISOR, 173 CLOUDSTACK_TEMPLATE_OS_TYPE, 174 CLOUDSTACK_TEMPLATE_URL, 175 CLOUDSTACK_ZONE) 176 177 var testAccCloudStackTemplate_update = fmt.Sprintf(` 178 resource "cloudstack_template" "foo" { 179 name = "terraform-test" 180 display_text = "terraform-updated" 181 format = "%s" 182 hypervisor = "%s" 183 os_type = "%s" 184 url = "%s" 185 zone = "%s" 186 is_dynamically_scalable = true 187 password_enabled = true 188 } 189 `, 190 CLOUDSTACK_TEMPLATE_FORMAT, 191 CLOUDSTACK_HYPERVISOR, 192 CLOUDSTACK_TEMPLATE_OS_TYPE, 193 CLOUDSTACK_TEMPLATE_URL, 194 CLOUDSTACK_ZONE)