github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 TestAccNetworkingV2FloatingIP_timeout(t *testing.T) {
    73  	var fip floatingips.FloatingIP
    74  
    75  	resource.Test(t, resource.TestCase{
    76  		PreCheck:     func() { testAccPreCheck(t) },
    77  		Providers:    testAccProviders,
    78  		CheckDestroy: testAccCheckNetworkingV2FloatingIPDestroy,
    79  		Steps: []resource.TestStep{
    80  			resource.TestStep{
    81  				Config: testAccNetworkingV2FloatingIP_timeout,
    82  				Check: resource.ComposeTestCheckFunc(
    83  					testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_1", &fip),
    84  				),
    85  			},
    86  		},
    87  	})
    88  }
    89  
    90  func testAccCheckNetworkingV2FloatingIPDestroy(s *terraform.State) error {
    91  	config := testAccProvider.Meta().(*Config)
    92  	networkClient, err := config.networkingV2Client(OS_REGION_NAME)
    93  	if err != nil {
    94  		return fmt.Errorf("Error creating OpenStack floating IP: %s", err)
    95  	}
    96  
    97  	for _, rs := range s.RootModule().Resources {
    98  		if rs.Type != "openstack_networking_floatingip_v2" {
    99  			continue
   100  		}
   101  
   102  		_, err := floatingips.Get(networkClient, rs.Primary.ID).Extract()
   103  		if err == nil {
   104  			return fmt.Errorf("FloatingIP still exists")
   105  		}
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  func testAccCheckNetworkingV2FloatingIPExists(n string, kp *floatingips.FloatingIP) resource.TestCheckFunc {
   112  	return func(s *terraform.State) error {
   113  		rs, ok := s.RootModule().Resources[n]
   114  		if !ok {
   115  			return fmt.Errorf("Not found: %s", n)
   116  		}
   117  
   118  		if rs.Primary.ID == "" {
   119  			return fmt.Errorf("No ID is set")
   120  		}
   121  
   122  		config := testAccProvider.Meta().(*Config)
   123  		networkClient, err := config.networkingV2Client(OS_REGION_NAME)
   124  		if err != nil {
   125  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
   126  		}
   127  
   128  		found, err := floatingips.Get(networkClient, rs.Primary.ID).Extract()
   129  		if err != nil {
   130  			return err
   131  		}
   132  
   133  		if found.ID != rs.Primary.ID {
   134  			return fmt.Errorf("FloatingIP not found")
   135  		}
   136  
   137  		*kp = *found
   138  
   139  		return nil
   140  	}
   141  }
   142  
   143  func testAccCheckNetworkingV2FloatingIPBoundToCorrectIP(fip *floatingips.FloatingIP, fixed_ip string) resource.TestCheckFunc {
   144  	return func(s *terraform.State) error {
   145  		if fip.FixedIP != fixed_ip {
   146  			return fmt.Errorf("Floating ip associated with wrong fixed ip")
   147  		}
   148  
   149  		return nil
   150  	}
   151  }
   152  
   153  func testAccCheckNetworkingV2InstanceFloatingIPAttach(
   154  	instance *servers.Server, fip *floatingips.FloatingIP) resource.TestCheckFunc {
   155  
   156  	// When Neutron is used, the Instance sometimes does not know its floating IP until some time
   157  	// after the attachment happened. This can be anywhere from 2-20 seconds. Because of that delay,
   158  	// the test usually completes with failure.
   159  	// However, the Fixed IP is known on both sides immediately, so that can be used as a bridge
   160  	// to ensure the two are now related.
   161  	// I think a better option is to introduce some state changing config in the actual resource.
   162  	return func(s *terraform.State) error {
   163  		for _, networkAddresses := range instance.Addresses {
   164  			for _, element := range networkAddresses.([]interface{}) {
   165  				address := element.(map[string]interface{})
   166  				if address["OS-EXT-IPS:type"] == "fixed" && address["addr"] == fip.FixedIP {
   167  					return nil
   168  				}
   169  			}
   170  		}
   171  		return fmt.Errorf("Floating IP %+v was not attached to instance %+v", fip, instance)
   172  	}
   173  }
   174  
   175  const testAccNetworkingV2FloatingIP_basic = `
   176  resource "openstack_networking_floatingip_v2" "fip_1" {
   177  }
   178  `
   179  
   180  var testAccNetworkV2FloatingIP_attach = fmt.Sprintf(`
   181  resource "openstack_networking_floatingip_v2" "fip_1" {
   182  }
   183  
   184  resource "openstack_compute_instance_v2" "instance_1" {
   185    name = "instance_1"
   186    security_groups = ["default"]
   187    floating_ip = "${openstack_networking_floatingip_v2.fip_1.address}"
   188  
   189    network {
   190      uuid = "%s"
   191    }
   192  }
   193  `, OS_NETWORK_ID)
   194  
   195  var testAccNetworkingV2FloatingIP_fixedip_bind = fmt.Sprintf(`
   196  resource "openstack_networking_network_v2" "network_1" {
   197    name = "network_1"
   198    admin_state_up = "true"
   199  }
   200  
   201  resource "openstack_networking_subnet_v2" "subnet_1" {
   202    name = "subnet_1"
   203    cidr = "192.168.199.0/24"
   204    ip_version = 4
   205    network_id = "${openstack_networking_network_v2.network_1.id}"
   206  }
   207  
   208  resource "openstack_networking_router_interface_v2" "router_interface_1" {
   209    router_id = "${openstack_networking_router_v2.router_1.id}"
   210    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   211  }
   212  
   213  resource "openstack_networking_router_v2" "router_1" {
   214    name = "router_1"
   215    external_gateway = "%s"
   216  }
   217  
   218  resource "openstack_networking_port_v2" "port_1" {
   219    admin_state_up = "true"
   220    network_id = "${openstack_networking_subnet_v2.subnet_1.network_id}"
   221  
   222    fixed_ip {
   223      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   224      ip_address = "192.168.199.10"
   225    }
   226  
   227    fixed_ip {
   228      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   229      ip_address = "192.168.199.20"
   230    }
   231  }
   232  
   233  resource "openstack_networking_floatingip_v2" "fip_1" {
   234    pool = "%s"
   235    port_id = "${openstack_networking_port_v2.port_1.id}"
   236    fixed_ip = "${openstack_networking_port_v2.port_1.fixed_ip.1.ip_address}"
   237  }
   238  `, OS_EXTGW_ID, OS_POOL_NAME)
   239  
   240  const testAccNetworkingV2FloatingIP_timeout = `
   241  resource "openstack_networking_floatingip_v2" "fip_1" {
   242    timeouts {
   243      create = "5m"
   244      delete = "5m"
   245    }
   246  }
   247  `