github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/scaleway/resource_scaleway_ip_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 TestAccScalewayIP_Basic(t *testing.T) { 12 resource.Test(t, resource.TestCase{ 13 PreCheck: func() { testAccPreCheck(t) }, 14 Providers: testAccProviders, 15 CheckDestroy: testAccCheckScalewayIPDestroy, 16 Steps: []resource.TestStep{ 17 resource.TestStep{ 18 Config: testAccCheckScalewayIPConfig, 19 Check: resource.ComposeTestCheckFunc( 20 testAccCheckScalewayIPExists("scaleway_ip.base"), 21 ), 22 }, 23 resource.TestStep{ 24 Config: testAccCheckScalewayIPAttachConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckScalewayIPExists("scaleway_ip.base"), 27 testAccCheckScalewayIPAttachment("scaleway_ip.base", func(serverID string) bool { 28 return serverID != "" 29 }, "attachment failed"), 30 ), 31 }, 32 resource.TestStep{ 33 Config: testAccCheckScalewayIPConfig, 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckScalewayIPExists("scaleway_ip.base"), 36 testAccCheckScalewayIPAttachment("scaleway_ip.base", func(serverID string) bool { 37 return serverID == "" 38 }, "detachment failed"), 39 ), 40 }, 41 }, 42 }) 43 } 44 45 func testAccCheckScalewayIPDestroy(s *terraform.State) error { 46 client := testAccProvider.Meta().(*Client).scaleway 47 48 for _, rs := range s.RootModule().Resources { 49 if rs.Type != "scaleway" { 50 continue 51 } 52 53 _, err := client.GetIP(rs.Primary.ID) 54 55 if err == nil { 56 return fmt.Errorf("IP still exists") 57 } 58 } 59 60 return nil 61 } 62 63 func testAccCheckScalewayIPAttributes() resource.TestCheckFunc { 64 return func(s *terraform.State) error { 65 return nil 66 } 67 } 68 69 func testAccCheckScalewayIPExists(n string) resource.TestCheckFunc { 70 return func(s *terraform.State) error { 71 rs, ok := s.RootModule().Resources[n] 72 73 if !ok { 74 return fmt.Errorf("Not found: %s", n) 75 } 76 77 if rs.Primary.ID == "" { 78 return fmt.Errorf("No IP ID is set") 79 } 80 81 client := testAccProvider.Meta().(*Client).scaleway 82 ip, err := client.GetIP(rs.Primary.ID) 83 84 if err != nil { 85 return err 86 } 87 88 if ip.IP.ID != rs.Primary.ID { 89 return fmt.Errorf("Record not found") 90 } 91 92 return nil 93 } 94 } 95 96 func testAccCheckScalewayIPAttachment(n string, check func(string) bool, msg string) resource.TestCheckFunc { 97 return func(s *terraform.State) error { 98 rs, ok := s.RootModule().Resources[n] 99 100 if !ok { 101 return fmt.Errorf("Not found: %s", n) 102 } 103 104 if rs.Primary.ID == "" { 105 return fmt.Errorf("No IP ID is set") 106 } 107 108 client := testAccProvider.Meta().(*Client).scaleway 109 ip, err := client.GetIP(rs.Primary.ID) 110 111 if err != nil { 112 return err 113 } 114 115 var serverID = "" 116 if ip.IP.Server != nil { 117 serverID = ip.IP.Server.Identifier 118 } 119 if !check(serverID) { 120 return fmt.Errorf("IP check failed: %q", msg) 121 } 122 123 return nil 124 } 125 } 126 127 var testAccCheckScalewayIPConfig = ` 128 resource "scaleway_ip" "base" { 129 } 130 ` 131 132 var testAccCheckScalewayIPAttachConfig = fmt.Sprintf(` 133 resource "scaleway_server" "base" { 134 name = "test" 135 # ubuntu 14.04 136 image = "%s" 137 type = "C1" 138 state = "stopped" 139 } 140 141 resource "scaleway_ip" "base" { 142 server = "${scaleway_server.base.id}" 143 } 144 `, armImageIdentifier)