github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/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 floating_ip = "${openstack_networking_floatingip_v2.myip.address}" 43 44 network { 45 uuid = "%s" 46 } 47 }`, 48 os.Getenv("OS_NETWORK_ID")) 49 50 resource.Test(t, resource.TestCase{ 51 PreCheck: func() { testAccPreCheck(t) }, 52 Providers: testAccProviders, 53 CheckDestroy: testAccCheckNetworkingV2FloatingIPDestroy, 54 Steps: []resource.TestStep{ 55 resource.TestStep{ 56 Config: testAccNetworkV2FloatingIP_attach, 57 Check: resource.ComposeTestCheckFunc( 58 testAccCheckNetworkingV2FloatingIPExists(t, "openstack_networking_floatingip_v2.myip", &fip), 59 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 60 testAccCheckNetworkingV2InstanceFloatingIPAttach(&instance, &fip), 61 ), 62 }, 63 }, 64 }) 65 } 66 67 func testAccCheckNetworkingV2FloatingIPDestroy(s *terraform.State) error { 68 config := testAccProvider.Meta().(*Config) 69 networkClient, err := config.networkingV2Client(OS_REGION_NAME) 70 if err != nil { 71 return fmt.Errorf("(testAccCheckNetworkingV2FloatingIPDestroy) Error creating OpenStack floating IP: %s", err) 72 } 73 74 for _, rs := range s.RootModule().Resources { 75 if rs.Type != "openstack_networking_floatingip_v2" { 76 continue 77 } 78 79 _, err := floatingips.Get(networkClient, rs.Primary.ID).Extract() 80 if err == nil { 81 return fmt.Errorf("FloatingIP still exists") 82 } 83 } 84 85 return nil 86 } 87 88 func testAccCheckNetworkingV2FloatingIPExists(t *testing.T, n string, kp *floatingips.FloatingIP) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 rs, ok := s.RootModule().Resources[n] 91 if !ok { 92 return fmt.Errorf("Not found: %s", n) 93 } 94 95 if rs.Primary.ID == "" { 96 return fmt.Errorf("No ID is set") 97 } 98 99 config := testAccProvider.Meta().(*Config) 100 networkClient, err := config.networkingV2Client(OS_REGION_NAME) 101 if err != nil { 102 return fmt.Errorf("(testAccCheckNetworkingV2FloatingIPExists) Error creating OpenStack networking client: %s", err) 103 } 104 105 found, err := floatingips.Get(networkClient, rs.Primary.ID).Extract() 106 if err != nil { 107 return err 108 } 109 110 if found.ID != rs.Primary.ID { 111 return fmt.Errorf("FloatingIP not found") 112 } 113 114 *kp = *found 115 116 return nil 117 } 118 } 119 120 func testAccCheckNetworkingV2InstanceFloatingIPAttach( 121 instance *servers.Server, fip *floatingips.FloatingIP) resource.TestCheckFunc { 122 123 // When Neutron is used, the Instance sometimes does not know its floating IP until some time 124 // after the attachment happened. This can be anywhere from 2-20 seconds. Because of that delay, 125 // the test usually completes with failure. 126 // However, the Fixed IP is known on both sides immediately, so that can be used as a bridge 127 // to ensure the two are now related. 128 // I think a better option is to introduce some state changing config in the actual resource. 129 return func(s *terraform.State) error { 130 for _, networkAddresses := range instance.Addresses { 131 for _, element := range networkAddresses.([]interface{}) { 132 address := element.(map[string]interface{}) 133 if address["OS-EXT-IPS:type"] == "fixed" && address["addr"] == fip.FixedIP { 134 return nil 135 } 136 } 137 } 138 return fmt.Errorf("Floating IP %+v was not attached to instance %+v", fip, instance) 139 } 140 } 141 142 var testAccNetworkingV2FloatingIP_basic = ` 143 resource "openstack_networking_floatingip_v2" "foo" { 144 }`