github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/openstack/resource_openstack_networking_floatingip_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 11 "github.com/rackspace/gophercloud/openstack/compute/v2/servers" 12 "github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" 13 ) 14 15 func TestAccNetworkingV2FloatingIP_basic(t *testing.T) { 16 var floatingIP floatingips.FloatingIP 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckNetworkingV2FloatingIPDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccNetworkingV2FloatingIP_basic, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckNetworkingV2FloatingIPExists(t, "openstack_networking_floatingip_v2.foo", &floatingIP), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func TestAccNetworkingV2FloatingIP_attach(t *testing.T) { 34 var instance servers.Server 35 var fip floatingips.FloatingIP 36 var testAccNetworkV2FloatingIP_attach = fmt.Sprintf(` 37 resource "openstack_networking_floatingip_v2" "myip" { 38 } 39 40 resource "openstack_compute_instance_v2" "foo" { 41 name = "terraform-test" 42 security_groups = ["default"] 43 floating_ip = "${openstack_networking_floatingip_v2.myip.address}" 44 45 network { 46 uuid = "%s" 47 } 48 }`, 49 os.Getenv("OS_NETWORK_ID")) 50 51 resource.Test(t, resource.TestCase{ 52 PreCheck: func() { testAccPreCheck(t) }, 53 Providers: testAccProviders, 54 CheckDestroy: testAccCheckNetworkingV2FloatingIPDestroy, 55 Steps: []resource.TestStep{ 56 resource.TestStep{ 57 Config: testAccNetworkV2FloatingIP_attach, 58 Check: resource.ComposeTestCheckFunc( 59 testAccCheckNetworkingV2FloatingIPExists(t, "openstack_networking_floatingip_v2.myip", &fip), 60 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 61 testAccCheckNetworkingV2InstanceFloatingIPAttach(&instance, &fip), 62 ), 63 }, 64 }, 65 }) 66 } 67 68 func testAccCheckNetworkingV2FloatingIPDestroy(s *terraform.State) error { 69 config := testAccProvider.Meta().(*Config) 70 networkClient, err := config.networkingV2Client(OS_REGION_NAME) 71 if err != nil { 72 return fmt.Errorf("(testAccCheckNetworkingV2FloatingIPDestroy) Error creating OpenStack floating IP: %s", err) 73 } 74 75 for _, rs := range s.RootModule().Resources { 76 if rs.Type != "openstack_networking_floatingip_v2" { 77 continue 78 } 79 80 _, err := floatingips.Get(networkClient, rs.Primary.ID).Extract() 81 if err == nil { 82 return fmt.Errorf("FloatingIP still exists") 83 } 84 } 85 86 return nil 87 } 88 89 func testAccCheckNetworkingV2FloatingIPExists(t *testing.T, n string, kp *floatingips.FloatingIP) resource.TestCheckFunc { 90 return func(s *terraform.State) error { 91 rs, ok := s.RootModule().Resources[n] 92 if !ok { 93 return fmt.Errorf("Not found: %s", n) 94 } 95 96 if rs.Primary.ID == "" { 97 return fmt.Errorf("No ID is set") 98 } 99 100 config := testAccProvider.Meta().(*Config) 101 networkClient, err := config.networkingV2Client(OS_REGION_NAME) 102 if err != nil { 103 return fmt.Errorf("(testAccCheckNetworkingV2FloatingIPExists) Error creating OpenStack networking client: %s", err) 104 } 105 106 found, err := floatingips.Get(networkClient, rs.Primary.ID).Extract() 107 if err != nil { 108 return err 109 } 110 111 if found.ID != rs.Primary.ID { 112 return fmt.Errorf("FloatingIP not found") 113 } 114 115 *kp = *found 116 117 return nil 118 } 119 } 120 121 func testAccCheckNetworkingV2InstanceFloatingIPAttach( 122 instance *servers.Server, fip *floatingips.FloatingIP) resource.TestCheckFunc { 123 124 // When Neutron is used, the Instance sometimes does not know its floating IP until some time 125 // after the attachment happened. This can be anywhere from 2-20 seconds. Because of that delay, 126 // the test usually completes with failure. 127 // However, the Fixed IP is known on both sides immediately, so that can be used as a bridge 128 // to ensure the two are now related. 129 // I think a better option is to introduce some state changing config in the actual resource. 130 return func(s *terraform.State) error { 131 for _, networkAddresses := range instance.Addresses { 132 for _, element := range networkAddresses.([]interface{}) { 133 address := element.(map[string]interface{}) 134 if address["OS-EXT-IPS:type"] == "fixed" && address["addr"] == fip.FixedIP { 135 return nil 136 } 137 } 138 } 139 return fmt.Errorf("Floating IP %+v was not attached to instance %+v", fip, instance) 140 } 141 } 142 143 var testAccNetworkingV2FloatingIP_basic = ` 144 resource "openstack_networking_floatingip_v2" "foo" { 145 }`