github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/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 p := cs.Template.NewDeleteTemplateParams(rs.Primary.ID) 153 _, err := cs.Template.DeleteTemplate(p) 154 155 if err != nil { 156 return fmt.Errorf( 157 "Error deleting template (%s): %s", 158 rs.Primary.ID, err) 159 } 160 } 161 162 return nil 163 } 164 165 var testAccCloudStackTemplate_basic = fmt.Sprintf(` 166 resource "cloudstack_template" "foo" { 167 name = "terraform-test" 168 format = "%s" 169 hypervisor = "%s" 170 os_type = "%s" 171 url = "%s" 172 zone = "%s" 173 } 174 `, 175 CLOUDSTACK_TEMPLATE_FORMAT, 176 CLOUDSTACK_HYPERVISOR, 177 CLOUDSTACK_TEMPLATE_OS_TYPE, 178 CLOUDSTACK_TEMPLATE_URL, 179 CLOUDSTACK_ZONE) 180 181 var testAccCloudStackTemplate_update = fmt.Sprintf(` 182 resource "cloudstack_template" "foo" { 183 name = "terraform-test" 184 display_text = "terraform-updated" 185 format = "%s" 186 hypervisor = "%s" 187 os_type = "%s" 188 url = "%s" 189 zone = "%s" 190 is_dynamically_scalable = true 191 password_enabled = true 192 } 193 `, 194 CLOUDSTACK_TEMPLATE_FORMAT, 195 CLOUDSTACK_HYPERVISOR, 196 CLOUDSTACK_TEMPLATE_OS_TYPE, 197 CLOUDSTACK_TEMPLATE_URL, 198 CLOUDSTACK_ZONE)