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