github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/openstack/resource_openstack_networking_floatingip_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" 11 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" 12 ) 13 14 func TestAccNetworkingV2FloatingIP_basic(t *testing.T) { 15 var fip floatingips.FloatingIP 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckNetworkingV2FloatingIPDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccNetworkingV2FloatingIP_basic, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_1", &fip), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccNetworkingV2FloatingIP_attach(t *testing.T) { 33 var instance servers.Server 34 var fip floatingips.FloatingIP 35 36 resource.Test(t, resource.TestCase{ 37 PreCheck: func() { testAccPreCheck(t) }, 38 Providers: testAccProviders, 39 CheckDestroy: testAccCheckNetworkingV2FloatingIPDestroy, 40 Steps: []resource.TestStep{ 41 resource.TestStep{ 42 Config: testAccNetworkV2FloatingIP_attach, 43 Check: resource.ComposeTestCheckFunc( 44 testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_1", &fip), 45 testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance), 46 testAccCheckNetworkingV2InstanceFloatingIPAttach(&instance, &fip), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func TestAccNetworkingV2FloatingIP_fixedip_bind(t *testing.T) { 54 var fip floatingips.FloatingIP 55 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 Providers: testAccProviders, 59 CheckDestroy: testAccCheckNetworkingV2FloatingIPDestroy, 60 Steps: []resource.TestStep{ 61 resource.TestStep{ 62 Config: testAccNetworkingV2FloatingIP_fixedip_bind, 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_1", &fip), 65 testAccCheckNetworkingV2FloatingIPBoundToCorrectIP(&fip, "192.168.199.20"), 66 ), 67 }, 68 }, 69 }) 70 } 71 72 func testAccCheckNetworkingV2FloatingIPDestroy(s *terraform.State) error { 73 config := testAccProvider.Meta().(*Config) 74 networkClient, err := config.networkingV2Client(OS_REGION_NAME) 75 if err != nil { 76 return fmt.Errorf("Error creating OpenStack floating IP: %s", err) 77 } 78 79 for _, rs := range s.RootModule().Resources { 80 if rs.Type != "openstack_networking_floatingip_v2" { 81 continue 82 } 83 84 _, err := floatingips.Get(networkClient, rs.Primary.ID).Extract() 85 if err == nil { 86 return fmt.Errorf("FloatingIP still exists") 87 } 88 } 89 90 return nil 91 } 92 93 func testAccCheckNetworkingV2FloatingIPExists(n string, kp *floatingips.FloatingIP) resource.TestCheckFunc { 94 return func(s *terraform.State) error { 95 rs, ok := s.RootModule().Resources[n] 96 if !ok { 97 return fmt.Errorf("Not found: %s", n) 98 } 99 100 if rs.Primary.ID == "" { 101 return fmt.Errorf("No ID is set") 102 } 103 104 config := testAccProvider.Meta().(*Config) 105 networkClient, err := config.networkingV2Client(OS_REGION_NAME) 106 if err != nil { 107 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 108 } 109 110 found, err := floatingips.Get(networkClient, rs.Primary.ID).Extract() 111 if err != nil { 112 return err 113 } 114 115 if found.ID != rs.Primary.ID { 116 return fmt.Errorf("FloatingIP not found") 117 } 118 119 *kp = *found 120 121 return nil 122 } 123 } 124 125 func testAccCheckNetworkingV2FloatingIPBoundToCorrectIP(fip *floatingips.FloatingIP, fixed_ip string) resource.TestCheckFunc { 126 return func(s *terraform.State) error { 127 if fip.FixedIP != fixed_ip { 128 return fmt.Errorf("Floating ip associated with wrong fixed ip") 129 } 130 131 return nil 132 } 133 } 134 135 func testAccCheckNetworkingV2InstanceFloatingIPAttach( 136 instance *servers.Server, fip *floatingips.FloatingIP) resource.TestCheckFunc { 137 138 // When Neutron is used, the Instance sometimes does not know its floating IP until some time 139 // after the attachment happened. This can be anywhere from 2-20 seconds. Because of that delay, 140 // the test usually completes with failure. 141 // However, the Fixed IP is known on both sides immediately, so that can be used as a bridge 142 // to ensure the two are now related. 143 // I think a better option is to introduce some state changing config in the actual resource. 144 return func(s *terraform.State) error { 145 for _, networkAddresses := range instance.Addresses { 146 for _, element := range networkAddresses.([]interface{}) { 147 address := element.(map[string]interface{}) 148 if address["OS-EXT-IPS:type"] == "fixed" && address["addr"] == fip.FixedIP { 149 return nil 150 } 151 } 152 } 153 return fmt.Errorf("Floating IP %+v was not attached to instance %+v", fip, instance) 154 } 155 } 156 157 const testAccNetworkingV2FloatingIP_basic = ` 158 resource "openstack_networking_floatingip_v2" "fip_1" { 159 } 160 ` 161 162 var testAccNetworkV2FloatingIP_attach = fmt.Sprintf(` 163 resource "openstack_networking_floatingip_v2" "fip_1" { 164 } 165 166 resource "openstack_compute_instance_v2" "instance_1" { 167 name = "instance_1" 168 security_groups = ["default"] 169 floating_ip = "${openstack_networking_floatingip_v2.fip_1.address}" 170 171 network { 172 uuid = "%s" 173 } 174 } 175 `, OS_NETWORK_ID) 176 177 var testAccNetworkingV2FloatingIP_fixedip_bind = fmt.Sprintf(` 178 resource "openstack_networking_network_v2" "network_1" { 179 name = "network_1" 180 admin_state_up = "true" 181 } 182 183 resource "openstack_networking_subnet_v2" "subnet_1" { 184 name = "subnet_1" 185 cidr = "192.168.199.0/24" 186 ip_version = 4 187 network_id = "${openstack_networking_network_v2.network_1.id}" 188 } 189 190 resource "openstack_networking_router_interface_v2" "router_interface_1" { 191 router_id = "${openstack_networking_router_v2.router_1.id}" 192 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 193 } 194 195 resource "openstack_networking_router_v2" "router_1" { 196 name = "router_1" 197 external_gateway = "%s" 198 } 199 200 resource "openstack_networking_port_v2" "port_1" { 201 admin_state_up = "true" 202 network_id = "${openstack_networking_subnet_v2.subnet_1.network_id}" 203 204 fixed_ip { 205 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 206 ip_address = "192.168.199.10" 207 } 208 209 fixed_ip { 210 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 211 ip_address = "192.168.199.20" 212 } 213 } 214 215 resource "openstack_networking_floatingip_v2" "fip_1" { 216 pool = "%s" 217 port_id = "${openstack_networking_port_v2.port_1.id}" 218 fixed_ip = "${openstack_networking_port_v2.port_1.fixed_ip.1.ip_address}" 219 } 220 `, OS_EXTGW_ID, OS_POOL_NAME)