github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/openstack/resource_openstack_compute_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/extensions/floatingips"
    12  	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
    13  )
    14  
    15  func TestAccComputeV2FloatingIP_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: testAccCheckComputeV2FloatingIPDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccComputeV2FloatingIP_basic,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.foo", &floatingIP),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccComputeV2FloatingIP_attach(t *testing.T) {
    34  	var instance servers.Server
    35  	var fip floatingips.FloatingIP
    36  	var testAccComputeV2FloatingIP_attach = fmt.Sprintf(`
    37      resource "openstack_compute_floatingip_v2" "myip" {
    38      }
    39  
    40      resource "openstack_compute_instance_v2" "foo" {
    41        name = "terraform-test"
    42        security_groups = ["default"]
    43        floating_ip = "${openstack_compute_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: testAccCheckComputeV2FloatingIPDestroy,
    55  		Steps: []resource.TestStep{
    56  			resource.TestStep{
    57  				Config: testAccComputeV2FloatingIP_attach,
    58  				Check: resource.ComposeTestCheckFunc(
    59  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip),
    60  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
    61  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func testAccCheckComputeV2FloatingIPDestroy(s *terraform.State) error {
    69  	config := testAccProvider.Meta().(*Config)
    70  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
    71  	if err != nil {
    72  		return fmt.Errorf("(testAccCheckComputeV2FloatingIPDestroy) Error creating OpenStack compute client: %s", err)
    73  	}
    74  
    75  	for _, rs := range s.RootModule().Resources {
    76  		if rs.Type != "openstack_compute_floatingip_v2" {
    77  			continue
    78  		}
    79  
    80  		_, err := floatingips.Get(computeClient, 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 testAccCheckComputeV2FloatingIPExists(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  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   102  		if err != nil {
   103  			return fmt.Errorf("(testAccCheckComputeV2FloatingIPExists) Error creating OpenStack compute client: %s", err)
   104  		}
   105  
   106  		found, err := floatingips.Get(computeClient, 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  var testAccComputeV2FloatingIP_basic = `
   122    resource "openstack_compute_floatingip_v2" "foo" {
   123    }`