github.com/gwilym/terraform@v0.3.8-0.20151231151641-c7573de75b19/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/secgroups"
    14  	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/volumeattach"
    15  	"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
    16  	"github.com/rackspace/gophercloud/pagination"
    17  )
    18  
    19  func TestAccComputeV2Instance_basic(t *testing.T) {
    20  	var instance servers.Server
    21  	var testAccComputeV2Instance_basic = fmt.Sprintf(`
    22  		resource "openstack_compute_instance_v2" "foo" {
    23  			name = "terraform-test"
    24  			security_groups = ["default"]
    25  			network {
    26  				uuid = "%s"
    27  			}
    28  			metadata {
    29  				foo = "bar"
    30  			}
    31  		}`,
    32  		os.Getenv("OS_NETWORK_ID"))
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccComputeV2Instance_basic,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
    43  					testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func TestAccComputeV2Instance_volumeAttach(t *testing.T) {
    51  	var instance servers.Server
    52  	var volume volumes.Volume
    53  
    54  	var testAccComputeV2Instance_volumeAttach = fmt.Sprintf(`
    55  		resource "openstack_blockstorage_volume_v1" "myvol" {
    56  			name = "myvol"
    57  			size = 1
    58  		}
    59  
    60  		resource "openstack_compute_instance_v2" "foo" {
    61  			name = "terraform-test"
    62  			security_groups = ["default"]
    63  			volume {
    64  				volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
    65  			}
    66  		}`)
    67  
    68  	resource.Test(t, resource.TestCase{
    69  		PreCheck:     func() { testAccPreCheck(t) },
    70  		Providers:    testAccProviders,
    71  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
    72  		Steps: []resource.TestStep{
    73  			resource.TestStep{
    74  				Config: testAccComputeV2Instance_volumeAttach,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
    77  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
    78  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func TestAccComputeV2Instance_volumeAttachPostCreation(t *testing.T) {
    86  	var instance servers.Server
    87  	var volume volumes.Volume
    88  
    89  	var testAccComputeV2Instance_volumeAttachPostCreationInstance = fmt.Sprintf(`
    90  		resource "openstack_compute_instance_v2" "foo" {
    91  			name = "terraform-test"
    92  			security_groups = ["default"]
    93  		}`)
    94  
    95  	var testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume = fmt.Sprintf(`
    96  		resource "openstack_blockstorage_volume_v1" "myvol" {
    97  			name = "myvol"
    98  			size = 1
    99  		}
   100  
   101  		resource "openstack_compute_instance_v2" "foo" {
   102  			name = "terraform-test"
   103  			security_groups = ["default"]
   104  			volume {
   105  				volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
   106  			}
   107  		}`)
   108  
   109  	resource.Test(t, resource.TestCase{
   110  		PreCheck:     func() { testAccPreCheck(t) },
   111  		Providers:    testAccProviders,
   112  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   113  		Steps: []resource.TestStep{
   114  			resource.TestStep{
   115  				Config: testAccComputeV2Instance_volumeAttachPostCreationInstance,
   116  				Check: resource.ComposeTestCheckFunc(
   117  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   118  				),
   119  			},
   120  			resource.TestStep{
   121  				Config: testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume,
   122  				Check: resource.ComposeTestCheckFunc(
   123  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
   124  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   125  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
   126  				),
   127  			},
   128  		},
   129  	})
   130  }
   131  
   132  func TestAccComputeV2Instance_volumeDetachPostCreation(t *testing.T) {
   133  	var instance servers.Server
   134  	var volume volumes.Volume
   135  
   136  	var testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume = fmt.Sprintf(`
   137  		resource "openstack_blockstorage_volume_v1" "myvol" {
   138  			name = "myvol"
   139  			size = 1
   140  		}
   141  
   142  		resource "openstack_compute_instance_v2" "foo" {
   143  			name = "terraform-test"
   144  			security_groups = ["default"]
   145  			volume {
   146  				volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
   147  			}
   148  		}`)
   149  
   150  	var testAccComputeV2Instance_volumeDetachPostCreationInstance = fmt.Sprintf(`
   151  		resource "openstack_compute_instance_v2" "foo" {
   152  			name = "terraform-test"
   153  			security_groups = ["default"]
   154  		}`)
   155  
   156  	resource.Test(t, resource.TestCase{
   157  		PreCheck:     func() { testAccPreCheck(t) },
   158  		Providers:    testAccProviders,
   159  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   160  		Steps: []resource.TestStep{
   161  			resource.TestStep{
   162  				Config: testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume,
   163  				Check: resource.ComposeTestCheckFunc(
   164  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume),
   165  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   166  					testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume),
   167  				),
   168  			},
   169  			resource.TestStep{
   170  				Config: testAccComputeV2Instance_volumeDetachPostCreationInstance,
   171  				Check: resource.ComposeTestCheckFunc(
   172  					testAccCheckBlockStorageV1VolumeDoesNotExist(t, "openstack_blockstorage_volume_v1.myvol", &volume),
   173  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   174  					testAccCheckComputeV2InstanceVolumesDetached(&instance),
   175  				),
   176  			},
   177  		},
   178  	})
   179  }
   180  
   181  func TestAccComputeV2Instance_floatingIPAttach(t *testing.T) {
   182  	var instance servers.Server
   183  	var fip floatingip.FloatingIP
   184  	var testAccComputeV2Instance_floatingIPAttach = fmt.Sprintf(`
   185  		resource "openstack_compute_floatingip_v2" "myip" {
   186  		}
   187  
   188  		resource "openstack_compute_instance_v2" "foo" {
   189  			name = "terraform-test"
   190  			security_groups = ["default"]
   191  			floating_ip = "${openstack_compute_floatingip_v2.myip.address}"
   192  
   193  			network {
   194  				uuid = "%s"
   195  			}
   196  		}`,
   197  		os.Getenv("OS_NETWORK_ID"))
   198  
   199  	resource.Test(t, resource.TestCase{
   200  		PreCheck:     func() { testAccPreCheck(t) },
   201  		Providers:    testAccProviders,
   202  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   203  		Steps: []resource.TestStep{
   204  			resource.TestStep{
   205  				Config: testAccComputeV2Instance_floatingIPAttach,
   206  				Check: resource.ComposeTestCheckFunc(
   207  					testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip),
   208  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   209  					testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip),
   210  				),
   211  			},
   212  		},
   213  	})
   214  }
   215  
   216  func TestAccComputeV2Instance_multi_secgroups(t *testing.T) {
   217  	var instance servers.Server
   218  	var secgroup secgroups.SecurityGroup
   219  	var testAccComputeV2Instance_multi_secgroups = fmt.Sprintf(`
   220  		resource "openstack_compute_secgroup_v2" "foo" {
   221  			name = "terraform-test"
   222  			description = "a security group"
   223  			rule {
   224  				from_port = 22
   225  				to_port = 22
   226  				ip_protocol = "tcp"
   227  				cidr = "0.0.0.0/0"
   228  			}
   229  		}
   230  
   231  		resource "openstack_compute_instance_v2" "foo" {
   232  			name = "terraform-test"
   233  			security_groups = ["default", "${openstack_compute_secgroup_v2.foo.name}"]
   234  			network {
   235  				uuid = "%s"
   236  			}
   237  		}`,
   238  		os.Getenv("OS_NETWORK_ID"))
   239  
   240  	resource.Test(t, resource.TestCase{
   241  		PreCheck:     func() { testAccPreCheck(t) },
   242  		Providers:    testAccProviders,
   243  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   244  		Steps: []resource.TestStep{
   245  			resource.TestStep{
   246  				Config: testAccComputeV2Instance_multi_secgroups,
   247  				Check: resource.ComposeTestCheckFunc(
   248  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.foo", &secgroup),
   249  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   250  				),
   251  			},
   252  		},
   253  	})
   254  }
   255  
   256  func TestAccComputeV2Instance_bootFromVolumeImage(t *testing.T) {
   257  	var instance servers.Server
   258  	var testAccComputeV2Instance_bootFromVolumeImage = fmt.Sprintf(`
   259  		resource "openstack_compute_instance_v2" "foo" {
   260  			name = "terraform-test"
   261  			security_groups = ["default"]
   262  			block_device {
   263  				uuid = "%s"
   264  				source_type = "image"
   265  				volume_size = 5
   266  				boot_index = 0
   267  				destination_type = "volume"
   268  				delete_on_termination = true
   269  			}
   270  		}`,
   271  		os.Getenv("OS_IMAGE_ID"))
   272  
   273  	resource.Test(t, resource.TestCase{
   274  		PreCheck:     func() { testAccPreCheck(t) },
   275  		Providers:    testAccProviders,
   276  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   277  		Steps: []resource.TestStep{
   278  			resource.TestStep{
   279  				Config: testAccComputeV2Instance_bootFromVolumeImage,
   280  				Check: resource.ComposeTestCheckFunc(
   281  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   282  					testAccCheckComputeV2InstanceBootVolumeAttachment(&instance),
   283  				),
   284  			},
   285  		},
   286  	})
   287  }
   288  
   289  func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) {
   290  	var instance servers.Server
   291  	var testAccComputeV2Instance_bootFromVolumeVolume = fmt.Sprintf(`
   292  	  resource "openstack_blockstorage_volume_v1" "foo" {
   293  			name = "terraform-test"
   294  			size = 5
   295  			image_id = "%s"
   296  		}
   297  
   298  		resource "openstack_compute_instance_v2" "foo" {
   299  			name = "terraform-test"
   300  			security_groups = ["default"]
   301  			block_device {
   302  				uuid = "${openstack_blockstorage_volume_v1.foo.id}"
   303  				source_type = "volume"
   304  				volume_size = 5
   305  				boot_index = 0
   306  				destination_type = "volume"
   307  				delete_on_termination = true
   308  			}
   309  		}`,
   310  		os.Getenv("OS_IMAGE_ID"))
   311  
   312  	resource.Test(t, resource.TestCase{
   313  		PreCheck:     func() { testAccPreCheck(t) },
   314  		Providers:    testAccProviders,
   315  		CheckDestroy: testAccCheckComputeV2InstanceDestroy,
   316  		Steps: []resource.TestStep{
   317  			resource.TestStep{
   318  				Config: testAccComputeV2Instance_bootFromVolumeVolume,
   319  				Check: resource.ComposeTestCheckFunc(
   320  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   321  					testAccCheckComputeV2InstanceBootVolumeAttachment(&instance),
   322  				),
   323  			},
   324  		},
   325  	})
   326  }
   327  
   328  func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error {
   329  	config := testAccProvider.Meta().(*Config)
   330  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
   331  	if err != nil {
   332  		return fmt.Errorf("(testAccCheckComputeV2InstanceDestroy) Error creating OpenStack compute client: %s", err)
   333  	}
   334  
   335  	for _, rs := range s.RootModule().Resources {
   336  		if rs.Type != "openstack_compute_instance_v2" {
   337  			continue
   338  		}
   339  
   340  		_, err := servers.Get(computeClient, rs.Primary.ID).Extract()
   341  		if err == nil {
   342  			return fmt.Errorf("Instance still exists")
   343  		}
   344  	}
   345  
   346  	return nil
   347  }
   348  
   349  func testAccCheckComputeV2InstanceExists(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc {
   350  	return func(s *terraform.State) error {
   351  		rs, ok := s.RootModule().Resources[n]
   352  		if !ok {
   353  			return fmt.Errorf("Not found: %s", n)
   354  		}
   355  
   356  		if rs.Primary.ID == "" {
   357  			return fmt.Errorf("No ID is set")
   358  		}
   359  
   360  		config := testAccProvider.Meta().(*Config)
   361  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   362  		if err != nil {
   363  			return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err)
   364  		}
   365  
   366  		found, err := servers.Get(computeClient, rs.Primary.ID).Extract()
   367  		if err != nil {
   368  			return err
   369  		}
   370  
   371  		if found.ID != rs.Primary.ID {
   372  			return fmt.Errorf("Instance not found")
   373  		}
   374  
   375  		*instance = *found
   376  
   377  		return nil
   378  	}
   379  }
   380  
   381  func testAccCheckComputeV2InstanceMetadata(
   382  	instance *servers.Server, k string, v string) resource.TestCheckFunc {
   383  	return func(s *terraform.State) error {
   384  		if instance.Metadata == nil {
   385  			return fmt.Errorf("No metadata")
   386  		}
   387  
   388  		for key, value := range instance.Metadata {
   389  			if k != key {
   390  				continue
   391  			}
   392  
   393  			if v == value.(string) {
   394  				return nil
   395  			}
   396  
   397  			return fmt.Errorf("Bad value for %s: %s", k, value)
   398  		}
   399  
   400  		return fmt.Errorf("Metadata not found: %s", k)
   401  	}
   402  }
   403  
   404  func testAccCheckComputeV2InstanceVolumeAttachment(
   405  	instance *servers.Server, volume *volumes.Volume) resource.TestCheckFunc {
   406  	return func(s *terraform.State) error {
   407  		var attachments []volumeattach.VolumeAttachment
   408  
   409  		config := testAccProvider.Meta().(*Config)
   410  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   411  		if err != nil {
   412  			return err
   413  		}
   414  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
   415  			actual, err := volumeattach.ExtractVolumeAttachments(page)
   416  			if err != nil {
   417  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   418  			}
   419  
   420  			attachments = actual
   421  			return true, nil
   422  		})
   423  
   424  		for _, attachment := range attachments {
   425  			if attachment.VolumeID == volume.ID {
   426  				return nil
   427  			}
   428  		}
   429  
   430  		return fmt.Errorf("Volume not found: %s", volume.ID)
   431  	}
   432  }
   433  
   434  func testAccCheckComputeV2InstanceVolumesDetached(instance *servers.Server) resource.TestCheckFunc {
   435  	return func(s *terraform.State) error {
   436  		var attachments []volumeattach.VolumeAttachment
   437  
   438  		config := testAccProvider.Meta().(*Config)
   439  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   440  		if err != nil {
   441  			return err
   442  		}
   443  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
   444  			actual, err := volumeattach.ExtractVolumeAttachments(page)
   445  			if err != nil {
   446  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   447  			}
   448  
   449  			attachments = actual
   450  			return true, nil
   451  		})
   452  
   453  		if len(attachments) > 0 {
   454  			return fmt.Errorf("Volumes are still attached.")
   455  		}
   456  
   457  		return nil
   458  	}
   459  }
   460  
   461  func testAccCheckComputeV2InstanceBootVolumeAttachment(
   462  	instance *servers.Server) resource.TestCheckFunc {
   463  	return func(s *terraform.State) error {
   464  		var attachments []volumeattach.VolumeAttachment
   465  
   466  		config := testAccProvider.Meta().(*Config)
   467  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   468  		if err != nil {
   469  			return err
   470  		}
   471  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
   472  			actual, err := volumeattach.ExtractVolumeAttachments(page)
   473  			if err != nil {
   474  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   475  			}
   476  
   477  			attachments = actual
   478  			return true, nil
   479  		})
   480  
   481  		if len(attachments) == 1 {
   482  			return nil
   483  		}
   484  
   485  		return fmt.Errorf("No attached volume found.")
   486  	}
   487  }
   488  
   489  func testAccCheckComputeV2InstanceFloatingIPAttach(
   490  	instance *servers.Server, fip *floatingip.FloatingIP) resource.TestCheckFunc {
   491  	return func(s *terraform.State) error {
   492  		if fip.InstanceID == instance.ID {
   493  			return nil
   494  		}
   495  
   496  		return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.ID, instance.ID)
   497  
   498  	}
   499  }