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