From 7ca97f4bfcc97aff7b709000919449b5a3b4cd49 Mon Sep 17 00:00:00 2001 From: Joe Topjian Date: Wed, 1 Apr 2015 22:54:09 +0000 Subject: [PATCH] Updating Floating IP acceptance tests --- ...ce_openstack_compute_floatingip_v2_test.go | 41 ++++++++++-- ...openstack_networking_floatingip_v2_test.go | 63 +++++++++++++++++-- 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/builtin/providers/openstack/resource_openstack_compute_floatingip_v2_test.go b/builtin/providers/openstack/resource_openstack_compute_floatingip_v2_test.go index a298a87d1..d6fe43b52 100644 --- a/builtin/providers/openstack/resource_openstack_compute_floatingip_v2_test.go +++ b/builtin/providers/openstack/resource_openstack_compute_floatingip_v2_test.go @@ -2,12 +2,14 @@ package openstack import ( "fmt" + "os" "testing" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/floatingip" + "github.com/rackspace/gophercloud/openstack/compute/v2/servers" ) func TestAccComputeV2FloatingIP_basic(t *testing.T) { @@ -28,6 +30,40 @@ func TestAccComputeV2FloatingIP_basic(t *testing.T) { }) } +func TestAccComputeV2FloatingIP_attach(t *testing.T) { + var instance servers.Server + var fip floatingip.FloatingIP + var testAccComputeV2FloatingIP_attach = fmt.Sprintf(` + resource "openstack_compute_floatingip_v2" "myip" { + } + + resource "openstack_compute_instance_v2" "foo" { + name = "terraform-test" + floating_ip = "${openstack_compute_floatingip_v2.myip.address}" + + network { + uuid = "%s" + } + }`, + os.Getenv("OS_NETWORK_ID")) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeV2FloatingIPDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeV2FloatingIP_attach, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip), + testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), + testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), + ), + }, + }, + }) +} + func testAccCheckComputeV2FloatingIPDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) computeClient, err := config.computeV2Client(OS_REGION_NAME) @@ -83,9 +119,4 @@ func testAccCheckComputeV2FloatingIPExists(t *testing.T, n string, kp *floatingi var testAccComputeV2FloatingIP_basic = ` resource "openstack_compute_floatingip_v2" "foo" { - } - - resource "openstack_compute_instance_v2" "bar" { - name = "terraform-acc-floating-ip-test" - floating_ip = "${openstack_compute_floatingip_v2.foo.address}" }` diff --git a/builtin/providers/openstack/resource_openstack_networking_floatingip_v2_test.go b/builtin/providers/openstack/resource_openstack_networking_floatingip_v2_test.go index 5c8ae38e3..a989f2774 100644 --- a/builtin/providers/openstack/resource_openstack_networking_floatingip_v2_test.go +++ b/builtin/providers/openstack/resource_openstack_networking_floatingip_v2_test.go @@ -2,11 +2,13 @@ package openstack import ( "fmt" + "os" "testing" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" + "github.com/rackspace/gophercloud/openstack/compute/v2/servers" "github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" ) @@ -28,6 +30,40 @@ func TestAccNetworkingV2FloatingIP_basic(t *testing.T) { }) } +func TestAccNetworkingV2FloatingIP_attach(t *testing.T) { + var instance servers.Server + var fip floatingips.FloatingIP + var testAccNetworkV2FloatingIP_attach = fmt.Sprintf(` + resource "openstack_networking_floatingip_v2" "myip" { + } + + resource "openstack_compute_instance_v2" "foo" { + name = "terraform-test" + floating_ip = "${openstack_networking_floatingip_v2.myip.address}" + + network { + uuid = "%s" + } + }`, + os.Getenv("OS_NETWORK_ID")) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckNetworkingV2FloatingIPDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccNetworkV2FloatingIP_attach, + Check: resource.ComposeTestCheckFunc( + testAccCheckNetworkingV2FloatingIPExists(t, "openstack_networking_floatingip_v2.myip", &fip), + testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), + testAccCheckNetworkingV2InstanceFloatingIPAttach(&instance, &fip), + ), + }, + }, + }) +} + func testAccCheckNetworkingV2FloatingIPDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) networkClient, err := config.networkingV2Client(OS_REGION_NAME) @@ -81,11 +117,28 @@ func testAccCheckNetworkingV2FloatingIPExists(t *testing.T, n string, kp *floati } } +func testAccCheckNetworkingV2InstanceFloatingIPAttach( + instance *servers.Server, fip *floatingips.FloatingIP) resource.TestCheckFunc { + + // When Neutron is used, the Instance sometimes does not know its floating IP until some time + // after the attachment happened. This can be anywhere from 2-20 seconds. Because of that delay, + // the test usually completes with failure. + // However, the Fixed IP is known on both sides immediately, so that can be used as a bridge + // to ensure the two are now related. + // I think a better option is to introduce some state changing config in the actual resource. + return func(s *terraform.State) error { + for _, networkAddresses := range instance.Addresses { + for _, element := range networkAddresses.([]interface{}) { + address := element.(map[string]interface{}) + if address["OS-EXT-IPS:type"] == "fixed" && address["addr"] == fip.FixedIP { + return nil + } + } + } + return fmt.Errorf("Floating IP %+v was not attached to instance %+v", fip, instance) + } +} + var testAccNetworkingV2FloatingIP_basic = ` resource "openstack_networking_floatingip_v2" "foo" { - } - - resource "openstack_compute_instance_v2" "bar" { - name = "terraform-acc-floating-ip-test" - floating_ip = "${openstack_networking_floatingip_v2.foo.address}" }`