github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/builtin/providers/openstack/resource_openstack_compute_instance_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/blockstorage/v1/volumes"
    12  	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/floatingip"
    13  	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/volumeattach"
    14  	"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
    15  	"github.com/rackspace/gophercloud/pagination"
    16  )
    17  
    18  func TestAccComputeV2Instance_basic(t *testing.T) {
    19  	var instance servers.Server
    20  	var testAccComputeV2Instance_basic = fmt.Sprintf(`
    21  		resource "openstack_compute_instance_v2" "foo" {
    22  			name = "terraform-test"
    23  			security_groups = ["default"]
    24  			network {
    25  				uuid = "%s"
    26  			}
    27  			metadata {
    28  				foo = "bar"
    29  			}
    30  		}`,
    31  		os.Getenv("OS_NETWORK_ID"))
    32  
    33  	resource.Test(t, resource.TestCase{
    34  		PreCheck:     func() { testAccPreCheck(t) },
    35  		Providers:    testAccProviders,
    36  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    37  		Steps: []resource.TestStep{
    38  			resource.TestStep{
    39  				Config: testAccComputeV2Instance_basic,
    40  				Check: resource.ComposeTestCheckFunc(
    41  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
    42  					testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func TestAccComputeV2Instance_volumeAttach(t *testing.T) {
    50  	var instance servers.Server
    51  	var volume volumes.Volume
    52  
    53  	resource.Test(t, resource.TestCase{
    54  		PreCheck:     func() { testAccPreCheck(t) },
    55  		Providers:    testAccProviders,
    56  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    57  		Steps: []resource.TestStep{
    58  			resource.TestStep{
    59  				Config: testAccComputeV2Instance_volumeAttach,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
    62  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
    63  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccComputeV2Instance_floatingIPAttach(t *testing.T) {
    71  	var instance servers.Server
    72  	var fip floatingip.FloatingIP
    73  	var testAccComputeV2Instance_floatingIPAttach = fmt.Sprintf(`
    74  		resource "openstack_compute_floatingip_v2" "myip" {
    75  		}
    76  
    77  		resource "openstack_compute_instance_v2" "foo" {
    78  			name = "terraform-test"
    79  			security_groups = ["default"]
    80  			floating_ip = "${openstack_compute_floatingip_v2.myip.address}"
    81  
    82  			network {
    83  				uuid = "%s"
    84  			}
    85  		}`,
    86  		os.Getenv("OS_NETWORK_ID"))
    87  
    88  	resource.Test(t, resource.TestCase{
    89  		PreCheck:     func() { testAccPreCheck(t) },
    90  		Providers:    testAccProviders,
    91  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    92  		Steps: []resource.TestStep{
    93  			resource.TestStep{
    94  				Config: testAccComputeV2Instance_floatingIPAttach,
    95  				Check: resource.ComposeTestCheckFunc(
    96  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip),
    97  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
    98  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
    99  				),
   100  			},
   101  		},
   102  	})
   103  }
   104  
   105  func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error {
   106  	config := testAccProvider.Meta().(*Config)
   107  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
   108  	if err != nil {
   109  		return fmt.Errorf("(testAccCheckComputeV2InstanceDestroy) Error creating OpenStack compute client: %s", err)
   110  	}
   111  
   112  	for _, rs := range s.RootModule().Resources {
   113  		if rs.Type != "openstack_compute_instance_v2" {
   114  			continue
   115  		}
   116  
   117  		_, err := servers.Get(computeClient, rs.Primary.ID).Extract()
   118  		if err == nil {
   119  			return fmt.Errorf("Instance still exists")
   120  		}
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  func testAccCheckComputeV2InstanceExists(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc {
   127  	return func(s *terraform.State) error {
   128  		rs, ok := s.RootModule().Resources[n]
   129  		if !ok {
   130  			return fmt.Errorf("Not found: %s", n)
   131  		}
   132  
   133  		if rs.Primary.ID == "" {
   134  			return fmt.Errorf("No ID is set")
   135  		}
   136  
   137  		config := testAccProvider.Meta().(*Config)
   138  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   139  		if err != nil {
   140  			return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err)
   141  		}
   142  
   143  		found, err := servers.Get(computeClient, rs.Primary.ID).Extract()
   144  		if err != nil {
   145  			return err
   146  		}
   147  
   148  		if found.ID != rs.Primary.ID {
   149  			return fmt.Errorf("Instance not found")
   150  		}
   151  
   152  		*instance = *found
   153  
   154  		return nil
   155  	}
   156  }
   157  
   158  func testAccCheckComputeV2InstanceMetadata(
   159  	instance *servers.Server, k string, v string) resource.TestCheckFunc {
   160  	return func(s *terraform.State) error {
   161  		if instance.Metadata == nil {
   162  			return fmt.Errorf("No metadata")
   163  		}
   164  
   165  		for key, value := range instance.Metadata {
   166  			if k != key {
   167  				continue
   168  			}
   169  
   170  			if v == value.(string) {
   171  				return nil
   172  			}
   173  
   174  			return fmt.Errorf("Bad value for %s: %s", k, value)
   175  		}
   176  
   177  		return fmt.Errorf("Metadata not found: %s", k)
   178  	}
   179  }
   180  
   181  func testAccCheckComputeV2InstanceVolumeAttachment(
   182  	instance *servers.Server, volume *volumes.Volume) resource.TestCheckFunc {
   183  	return func(s *terraform.State) error {
   184  		var attachments []volumeattach.VolumeAttachment
   185  
   186  		config := testAccProvider.Meta().(*Config)
   187  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   188  		if err != nil {
   189  			return err
   190  		}
   191  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
   192  			actual, err := volumeattach.ExtractVolumeAttachments(page)
   193  			if err != nil {
   194  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   195  			}
   196  
   197  			attachments = actual
   198  			return true, nil
   199  		})
   200  
   201  		for _, attachment := range attachments {
   202  			if attachment.VolumeID == volume.ID {
   203  				return nil
   204  			}
   205  		}
   206  
   207  		return fmt.Errorf("Volume not found: %s", volume.ID)
   208  	}
   209  }
   210  
   211  func testAccCheckComputeV2InstanceFloatingIPAttach(
   212  	instance *servers.Server, fip *floatingip.FloatingIP) resource.TestCheckFunc {
   213  	return func(s *terraform.State) error {
   214  		if fip.InstanceID == instance.ID {
   215  			return nil
   216  		}
   217  
   218  		return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.ID, instance.ID)
   219  
   220  	}
   221  }
   222  
   223  var testAccComputeV2Instance_volumeAttach = fmt.Sprintf(`
   224    resource "openstack_blockstorage_volume_v1" "myvol" {
   225      name = "myvol"
   226      size = 1
   227    }
   228  
   229    resource "openstack_compute_instance_v2" "foo" {
   230      region = "%s"
   231      name = "terraform-test"
   232      security_groups = ["default"]
   233      volume {
   234        volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
   235      }
   236    }`,
   237  	OS_REGION_NAME)