github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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_bootFromVolume(t *testing.T) {
   257  	var instance servers.Server
   258  	var testAccComputeV2Instance_bootFromVolume = 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_bootFromVolume,
   280  				Check: resource.ComposeTestCheckFunc(
   281  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance),
   282  					testAccCheckComputeV2InstanceBootVolumeAttachment(&instance),
   283  				),
   284  			},
   285  		},
   286  	})
   287  }
   288  
   289  func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error {
   290  	config := testAccProvider.Meta().(*Config)
   291  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
   292  	if err != nil {
   293  		return fmt.Errorf("(testAccCheckComputeV2InstanceDestroy) Error creating OpenStack compute client: %s", err)
   294  	}
   295  
   296  	for _, rs := range s.RootModule().Resources {
   297  		if rs.Type != "openstack_compute_instance_v2" {
   298  			continue
   299  		}
   300  
   301  		_, err := servers.Get(computeClient, rs.Primary.ID).Extract()
   302  		if err == nil {
   303  			return fmt.Errorf("Instance still exists")
   304  		}
   305  	}
   306  
   307  	return nil
   308  }
   309  
   310  func testAccCheckComputeV2InstanceExists(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc {
   311  	return func(s *terraform.State) error {
   312  		rs, ok := s.RootModule().Resources[n]
   313  		if !ok {
   314  			return fmt.Errorf("Not found: %s", n)
   315  		}
   316  
   317  		if rs.Primary.ID == "" {
   318  			return fmt.Errorf("No ID is set")
   319  		}
   320  
   321  		config := testAccProvider.Meta().(*Config)
   322  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   323  		if err != nil {
   324  			return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err)
   325  		}
   326  
   327  		found, err := servers.Get(computeClient, rs.Primary.ID).Extract()
   328  		if err != nil {
   329  			return err
   330  		}
   331  
   332  		if found.ID != rs.Primary.ID {
   333  			return fmt.Errorf("Instance not found")
   334  		}
   335  
   336  		*instance = *found
   337  
   338  		return nil
   339  	}
   340  }
   341  
   342  func testAccCheckComputeV2InstanceMetadata(
   343  	instance *servers.Server, k string, v string) resource.TestCheckFunc {
   344  	return func(s *terraform.State) error {
   345  		if instance.Metadata == nil {
   346  			return fmt.Errorf("No metadata")
   347  		}
   348  
   349  		for key, value := range instance.Metadata {
   350  			if k != key {
   351  				continue
   352  			}
   353  
   354  			if v == value.(string) {
   355  				return nil
   356  			}
   357  
   358  			return fmt.Errorf("Bad value for %s: %s", k, value)
   359  		}
   360  
   361  		return fmt.Errorf("Metadata not found: %s", k)
   362  	}
   363  }
   364  
   365  func testAccCheckComputeV2InstanceVolumeAttachment(
   366  	instance *servers.Server, volume *volumes.Volume) resource.TestCheckFunc {
   367  	return func(s *terraform.State) error {
   368  		var attachments []volumeattach.VolumeAttachment
   369  
   370  		config := testAccProvider.Meta().(*Config)
   371  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   372  		if err != nil {
   373  			return err
   374  		}
   375  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
   376  			actual, err := volumeattach.ExtractVolumeAttachments(page)
   377  			if err != nil {
   378  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   379  			}
   380  
   381  			attachments = actual
   382  			return true, nil
   383  		})
   384  
   385  		for _, attachment := range attachments {
   386  			if attachment.VolumeID == volume.ID {
   387  				return nil
   388  			}
   389  		}
   390  
   391  		return fmt.Errorf("Volume not found: %s", volume.ID)
   392  	}
   393  }
   394  
   395  func testAccCheckComputeV2InstanceVolumesDetached(instance *servers.Server) resource.TestCheckFunc {
   396  	return func(s *terraform.State) error {
   397  		var attachments []volumeattach.VolumeAttachment
   398  
   399  		config := testAccProvider.Meta().(*Config)
   400  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   401  		if err != nil {
   402  			return err
   403  		}
   404  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
   405  			actual, err := volumeattach.ExtractVolumeAttachments(page)
   406  			if err != nil {
   407  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   408  			}
   409  
   410  			attachments = actual
   411  			return true, nil
   412  		})
   413  
   414  		if len(attachments) > 0 {
   415  			return fmt.Errorf("Volumes are still attached.")
   416  		}
   417  
   418  		return nil
   419  	}
   420  }
   421  
   422  func testAccCheckComputeV2InstanceBootVolumeAttachment(
   423  	instance *servers.Server) resource.TestCheckFunc {
   424  	return func(s *terraform.State) error {
   425  		var attachments []volumeattach.VolumeAttachment
   426  
   427  		config := testAccProvider.Meta().(*Config)
   428  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   429  		if err != nil {
   430  			return err
   431  		}
   432  		err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) {
   433  			actual, err := volumeattach.ExtractVolumeAttachments(page)
   434  			if err != nil {
   435  				return false, fmt.Errorf("Unable to lookup attachment: %s", err)
   436  			}
   437  
   438  			attachments = actual
   439  			return true, nil
   440  		})
   441  
   442  		if len(attachments) == 1 {
   443  			return nil
   444  		}
   445  
   446  		return fmt.Errorf("No attached volume found.")
   447  	}
   448  }
   449  
   450  func testAccCheckComputeV2InstanceFloatingIPAttach(
   451  	instance *servers.Server, fip *floatingip.FloatingIP) resource.TestCheckFunc {
   452  	return func(s *terraform.State) error {
   453  		if fip.InstanceID == instance.ID {
   454  			return nil
   455  		}
   456  
   457  		return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.ID, instance.ID)
   458  
   459  	}
   460  }