github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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_SecurityGroup(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: testAccCheckScalewayServerConfig_SecurityGroup, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckScalewayServerExists("scaleway_server.base"), 44 testAccCheckScalewayServerSecurityGroup("scaleway_server.base", "blue"), 45 ), 46 }, 47 resource.TestStep{ 48 Config: testAccCheckScalewayServerConfig_SecurityGroup_Update, 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckScalewayServerExists("scaleway_server.base"), 51 testAccCheckScalewayServerSecurityGroup("scaleway_server.base", "red"), 52 ), 53 }, 54 }, 55 }) 56 } 57 58 func testAccCheckScalewayServerDestroy(s *terraform.State) error { 59 client := testAccProvider.Meta().(*Client).scaleway 60 61 for _, rs := range s.RootModule().Resources { 62 if rs.Type != "scaleway" { 63 continue 64 } 65 66 _, err := client.GetServer(rs.Primary.ID) 67 68 if err == nil { 69 return fmt.Errorf("Server still exists") 70 } 71 } 72 73 return nil 74 } 75 76 func testAccCheckScalewayServerAttributes(n string) resource.TestCheckFunc { 77 return func(s *terraform.State) error { 78 rs, ok := s.RootModule().Resources[n] 79 if !ok { 80 return fmt.Errorf("Unknown resource: %s", n) 81 } 82 83 client := testAccProvider.Meta().(*Client).scaleway 84 server, err := client.GetServer(rs.Primary.ID) 85 86 if err != nil { 87 return err 88 } 89 90 if server.Name != "test" { 91 return fmt.Errorf("Server has wrong name") 92 } 93 if server.Image.Identifier != armImageIdentifier { 94 return fmt.Errorf("Wrong server image") 95 } 96 if server.CommercialType != "C1" { 97 return fmt.Errorf("Wrong server type") 98 } 99 100 return nil 101 } 102 } 103 104 func testAccCheckScalewayServerSecurityGroup(n, securityGroupName string) resource.TestCheckFunc { 105 return func(s *terraform.State) error { 106 rs, ok := s.RootModule().Resources[n] 107 if !ok { 108 return fmt.Errorf("Unknown resource: %s", n) 109 } 110 111 client := testAccProvider.Meta().(*Client).scaleway 112 server, err := client.GetServer(rs.Primary.ID) 113 114 if err != nil { 115 return err 116 } 117 118 if server.SecurityGroup.Name != securityGroupName { 119 return fmt.Errorf("Server has wrong security_group") 120 } 121 122 return nil 123 } 124 } 125 126 func testAccCheckScalewayServerExists(n string) resource.TestCheckFunc { 127 return func(s *terraform.State) error { 128 rs, ok := s.RootModule().Resources[n] 129 130 if !ok { 131 return fmt.Errorf("Not found: %s", n) 132 } 133 134 if rs.Primary.ID == "" { 135 return fmt.Errorf("No Server ID is set") 136 } 137 138 client := testAccProvider.Meta().(*Client).scaleway 139 server, err := client.GetServer(rs.Primary.ID) 140 141 if err != nil { 142 return err 143 } 144 145 if server.Identifier != rs.Primary.ID { 146 return fmt.Errorf("Record not found") 147 } 148 149 return nil 150 } 151 } 152 153 var armImageIdentifier = "5faef9cd-ea9b-4a63-9171-9e26bec03dbc" 154 155 var testAccCheckScalewayServerConfig = fmt.Sprintf(` 156 resource "scaleway_server" "base" { 157 name = "test" 158 # ubuntu 14.04 159 image = "%s" 160 type = "C1" 161 tags = [ "terraform-test" ] 162 }`, armImageIdentifier) 163 164 var testAccCheckScalewayServerConfig_SecurityGroup = fmt.Sprintf(` 165 resource "scaleway_security_group" "blue" { 166 name = "blue" 167 description = "blue" 168 } 169 170 resource "scaleway_security_group" "red" { 171 name = "red" 172 description = "red" 173 } 174 175 resource "scaleway_server" "base" { 176 name = "test" 177 # ubuntu 14.04 178 image = "%s" 179 type = "C1" 180 tags = [ "terraform-test" ] 181 security_group = "${scaleway_security_group.blue.id}" 182 }`, armImageIdentifier) 183 184 var testAccCheckScalewayServerConfig_SecurityGroup_Update = fmt.Sprintf(` 185 resource "scaleway_security_group" "blue" { 186 name = "blue" 187 description = "blue" 188 } 189 190 resource "scaleway_security_group" "red" { 191 name = "red" 192 description = "red" 193 } 194 195 resource "scaleway_server" "base" { 196 name = "test" 197 # ubuntu 14.04 198 image = "%s" 199 type = "C1" 200 tags = [ "terraform-test" ] 201 security_group = "${scaleway_security_group.red.id}" 202 }`, armImageIdentifier)