github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/scaleway/resource_scaleway_server_test.go (about) 1 package scaleway 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestAccScalewayServer_Basic(t *testing.T) { 12 resource.Test(t, resource.TestCase{ 13 PreCheck: func() { testAccPreCheck(t) }, 14 Providers: testAccProviders, 15 CheckDestroy: testAccCheckScalewayServerDestroy, 16 Steps: []resource.TestStep{ 17 resource.TestStep{ 18 Config: testAccCheckScalewayServerConfig, 19 Check: resource.ComposeTestCheckFunc( 20 testAccCheckScalewayServerExists("scaleway_server.base"), 21 testAccCheckScalewayServerAttributes("scaleway_server.base"), 22 resource.TestCheckResourceAttr( 23 "scaleway_server.base", "type", "C1"), 24 resource.TestCheckResourceAttr( 25 "scaleway_server.base", "name", "test"), 26 resource.TestCheckResourceAttr( 27 "scaleway_server.base", "tags.0", "terraform-test"), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func TestAccScalewayServer_Volumes(t *testing.T) { 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckScalewayServerDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: testAccCheckScalewayServerVolumeConfig, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckScalewayServerExists("scaleway_server.base"), 44 testAccCheckScalewayServerAttributes("scaleway_server.base"), 45 resource.TestCheckResourceAttr( 46 "scaleway_server.base", "type", "C1"), 47 resource.TestCheckResourceAttr( 48 "scaleway_server.base", "volume.#", "2"), 49 resource.TestCheckResourceAttrSet( 50 "scaleway_server.base", "volume.0.volume_id"), 51 resource.TestCheckResourceAttr( 52 "scaleway_server.base", "volume.0.type", "l_ssd"), 53 resource.TestCheckResourceAttr( 54 "scaleway_server.base", "volume.0.size_in_gb", "20"), 55 resource.TestCheckResourceAttrSet( 56 "scaleway_server.base", "volume.1.volume_id"), 57 resource.TestCheckResourceAttr( 58 "scaleway_server.base", "volume.1.type", "l_ssd"), 59 resource.TestCheckResourceAttr( 60 "scaleway_server.base", "volume.1.size_in_gb", "30"), 61 ), 62 }, 63 }, 64 }) 65 } 66 67 func TestAccScalewayServer_SecurityGroup(t *testing.T) { 68 resource.Test(t, resource.TestCase{ 69 PreCheck: func() { testAccPreCheck(t) }, 70 Providers: testAccProviders, 71 CheckDestroy: testAccCheckScalewayServerDestroy, 72 Steps: []resource.TestStep{ 73 resource.TestStep{ 74 Config: testAccCheckScalewayServerConfig_SecurityGroup, 75 Check: resource.ComposeTestCheckFunc( 76 testAccCheckScalewayServerExists("scaleway_server.base"), 77 testAccCheckScalewayServerSecurityGroup("scaleway_server.base", "blue"), 78 ), 79 }, 80 resource.TestStep{ 81 Config: testAccCheckScalewayServerConfig_SecurityGroup_Update, 82 Check: resource.ComposeTestCheckFunc( 83 testAccCheckScalewayServerExists("scaleway_server.base"), 84 testAccCheckScalewayServerSecurityGroup("scaleway_server.base", "red"), 85 ), 86 }, 87 }, 88 }) 89 } 90 91 func testAccCheckScalewayServerDestroy(s *terraform.State) error { 92 client := testAccProvider.Meta().(*Client).scaleway 93 94 for _, rs := range s.RootModule().Resources { 95 if rs.Type != "scaleway" { 96 continue 97 } 98 99 _, err := client.GetServer(rs.Primary.ID) 100 101 if err == nil { 102 return fmt.Errorf("Server still exists") 103 } 104 } 105 106 return nil 107 } 108 109 func testAccCheckScalewayServerAttributes(n string) resource.TestCheckFunc { 110 return func(s *terraform.State) error { 111 rs, ok := s.RootModule().Resources[n] 112 if !ok { 113 return fmt.Errorf("Unknown resource: %s", n) 114 } 115 116 client := testAccProvider.Meta().(*Client).scaleway 117 server, err := client.GetServer(rs.Primary.ID) 118 119 if err != nil { 120 return err 121 } 122 123 if server.Name != "test" { 124 return fmt.Errorf("Server has wrong name") 125 } 126 if server.Image.Identifier != armImageIdentifier { 127 return fmt.Errorf("Wrong server image") 128 } 129 if server.CommercialType != "C1" { 130 return fmt.Errorf("Wrong server type") 131 } 132 133 return nil 134 } 135 } 136 137 func testAccCheckScalewayServerSecurityGroup(n, securityGroupName string) resource.TestCheckFunc { 138 return func(s *terraform.State) error { 139 rs, ok := s.RootModule().Resources[n] 140 if !ok { 141 return fmt.Errorf("Unknown resource: %s", n) 142 } 143 144 client := testAccProvider.Meta().(*Client).scaleway 145 server, err := client.GetServer(rs.Primary.ID) 146 147 if err != nil { 148 return err 149 } 150 151 if server.SecurityGroup.Name != securityGroupName { 152 return fmt.Errorf("Server has wrong security_group") 153 } 154 155 return nil 156 } 157 } 158 159 func testAccCheckScalewayServerExists(n string) resource.TestCheckFunc { 160 return func(s *terraform.State) error { 161 rs, ok := s.RootModule().Resources[n] 162 163 if !ok { 164 return fmt.Errorf("Not found: %s", n) 165 } 166 167 if rs.Primary.ID == "" { 168 return fmt.Errorf("No Server ID is set") 169 } 170 171 client := testAccProvider.Meta().(*Client).scaleway 172 server, err := client.GetServer(rs.Primary.ID) 173 174 if err != nil { 175 return err 176 } 177 178 if server.Identifier != rs.Primary.ID { 179 return fmt.Errorf("Record not found") 180 } 181 182 return nil 183 } 184 } 185 186 var armImageIdentifier = "5faef9cd-ea9b-4a63-9171-9e26bec03dbc" 187 188 var testAccCheckScalewayServerConfig = fmt.Sprintf(` 189 resource "scaleway_server" "base" { 190 name = "test" 191 # ubuntu 14.04 192 image = "%s" 193 type = "C1" 194 tags = [ "terraform-test" ] 195 }`, armImageIdentifier) 196 197 var testAccCheckScalewayServerVolumeConfig = fmt.Sprintf(` 198 resource "scaleway_server" "base" { 199 name = "test" 200 # ubuntu 14.04 201 image = "%s" 202 type = "C1" 203 tags = [ "terraform-test" ] 204 205 volume { 206 size_in_gb = 20 207 type = "l_ssd" 208 } 209 210 volume { 211 size_in_gb = 30 212 type = "l_ssd" 213 } 214 }`, armImageIdentifier) 215 216 var testAccCheckScalewayServerConfig_SecurityGroup = fmt.Sprintf(` 217 resource "scaleway_security_group" "blue" { 218 name = "blue" 219 description = "blue" 220 } 221 222 resource "scaleway_security_group" "red" { 223 name = "red" 224 description = "red" 225 } 226 227 resource "scaleway_server" "base" { 228 name = "test" 229 # ubuntu 14.04 230 image = "%s" 231 type = "C1" 232 tags = [ "terraform-test" ] 233 security_group = "${scaleway_security_group.blue.id}" 234 }`, armImageIdentifier) 235 236 var testAccCheckScalewayServerConfig_SecurityGroup_Update = fmt.Sprintf(` 237 resource "scaleway_security_group" "blue" { 238 name = "blue" 239 description = "blue" 240 } 241 242 resource "scaleway_security_group" "red" { 243 name = "red" 244 description = "red" 245 } 246 247 resource "scaleway_server" "base" { 248 name = "test" 249 # ubuntu 14.04 250 image = "%s" 251 type = "C1" 252 tags = [ "terraform-test" ] 253 security_group = "${scaleway_security_group.red.id}" 254 }`, armImageIdentifier)