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