github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_instance_test.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func TestAccCloudStackInstance_basic(t *testing.T) {
    13  	var instance cloudstack.VirtualMachine
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackInstanceDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackInstance_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackInstanceExists(
    24  						"cloudstack_instance.foobar", &instance),
    25  					testAccCheckCloudStackInstanceAttributes(&instance),
    26  					resource.TestCheckResourceAttr(
    27  						"cloudstack_instance.foobar", "user_data", "0cf3dcdc356ec8369494cb3991985ecd5296cdd5"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccCloudStackInstance_update(t *testing.T) {
    35  	var instance cloudstack.VirtualMachine
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckCloudStackInstanceDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccCloudStackInstance_basic,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckCloudStackInstanceExists(
    46  						"cloudstack_instance.foobar", &instance),
    47  					testAccCheckCloudStackInstanceAttributes(&instance),
    48  					resource.TestCheckResourceAttr(
    49  						"cloudstack_instance.foobar", "user_data", "0cf3dcdc356ec8369494cb3991985ecd5296cdd5"),
    50  				),
    51  			},
    52  
    53  			resource.TestStep{
    54  				Config: testAccCloudStackInstance_renameAndResize,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckCloudStackInstanceExists(
    57  						"cloudstack_instance.foobar", &instance),
    58  					testAccCheckCloudStackInstanceRenamedAndResized(&instance),
    59  					resource.TestCheckResourceAttr(
    60  						"cloudstack_instance.foobar", "display_name", "terraform-updated"),
    61  					resource.TestCheckResourceAttr(
    62  						"cloudstack_instance.foobar", "service_offering", CLOUDSTACK_SERVICE_OFFERING_2),
    63  				),
    64  			},
    65  		},
    66  	})
    67  }
    68  
    69  func TestAccCloudStackInstance_fixedIP(t *testing.T) {
    70  	var instance cloudstack.VirtualMachine
    71  
    72  	resource.Test(t, resource.TestCase{
    73  		PreCheck:     func() { testAccPreCheck(t) },
    74  		Providers:    testAccProviders,
    75  		CheckDestroy: testAccCheckCloudStackInstanceDestroy,
    76  		Steps: []resource.TestStep{
    77  			resource.TestStep{
    78  				Config: testAccCloudStackInstance_fixedIP,
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testAccCheckCloudStackInstanceExists(
    81  						"cloudstack_instance.foobar", &instance),
    82  					resource.TestCheckResourceAttr(
    83  						"cloudstack_instance.foobar", "ipaddress", CLOUDSTACK_NETWORK_1_IPADDRESS),
    84  				),
    85  			},
    86  		},
    87  	})
    88  }
    89  
    90  func TestAccCloudStackInstance_keyPair(t *testing.T) {
    91  	var instance cloudstack.VirtualMachine
    92  
    93  	resource.Test(t, resource.TestCase{
    94  		PreCheck:     func() { testAccPreCheck(t) },
    95  		Providers:    testAccProviders,
    96  		CheckDestroy: testAccCheckCloudStackInstanceDestroy,
    97  		Steps: []resource.TestStep{
    98  			resource.TestStep{
    99  				Config: testAccCloudStackInstance_keyPair,
   100  				Check: resource.ComposeTestCheckFunc(
   101  					testAccCheckCloudStackInstanceExists(
   102  						"cloudstack_instance.foobar", &instance),
   103  					resource.TestCheckResourceAttr(
   104  						"cloudstack_instance.foobar", "keypair", "terraform-test-keypair"),
   105  				),
   106  			},
   107  		},
   108  	})
   109  }
   110  
   111  func TestAccCloudStackInstance_project(t *testing.T) {
   112  	var instance cloudstack.VirtualMachine
   113  
   114  	resource.Test(t, resource.TestCase{
   115  		PreCheck:     func() { testAccPreCheck(t) },
   116  		Providers:    testAccProviders,
   117  		CheckDestroy: testAccCheckCloudStackInstanceDestroy,
   118  		Steps: []resource.TestStep{
   119  			resource.TestStep{
   120  				Config: testAccCloudStackInstance_project,
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testAccCheckCloudStackInstanceExists(
   123  						"cloudstack_instance.foobar", &instance),
   124  					resource.TestCheckResourceAttr(
   125  						"cloudstack_instance.foobar", "project", CLOUDSTACK_PROJECT_NAME),
   126  				),
   127  			},
   128  		},
   129  	})
   130  }
   131  
   132  func testAccCheckCloudStackInstanceExists(
   133  	n string, instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
   134  	return func(s *terraform.State) error {
   135  		rs, ok := s.RootModule().Resources[n]
   136  		if !ok {
   137  			return fmt.Errorf("Not found: %s", n)
   138  		}
   139  
   140  		if rs.Primary.ID == "" {
   141  			return fmt.Errorf("No instance ID is set")
   142  		}
   143  
   144  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   145  		vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(rs.Primary.ID)
   146  
   147  		if err != nil {
   148  			return err
   149  		}
   150  
   151  		if vm.Id != rs.Primary.ID {
   152  			return fmt.Errorf("Instance not found")
   153  		}
   154  
   155  		*instance = *vm
   156  
   157  		return nil
   158  	}
   159  }
   160  
   161  func testAccCheckCloudStackInstanceAttributes(
   162  	instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
   163  	return func(s *terraform.State) error {
   164  
   165  		if instance.Name != "terraform-test" {
   166  			return fmt.Errorf("Bad name: %s", instance.Name)
   167  		}
   168  
   169  		if instance.Displayname != "terraform" {
   170  			return fmt.Errorf("Bad display name: %s", instance.Displayname)
   171  		}
   172  
   173  		if instance.Serviceofferingname != CLOUDSTACK_SERVICE_OFFERING_1 {
   174  			return fmt.Errorf("Bad service offering: %s", instance.Serviceofferingname)
   175  		}
   176  
   177  		if instance.Templatename != CLOUDSTACK_TEMPLATE {
   178  			return fmt.Errorf("Bad template: %s", instance.Templatename)
   179  		}
   180  
   181  		if instance.Nic[0].Networkname != CLOUDSTACK_NETWORK_1 {
   182  			return fmt.Errorf("Bad network: %s", instance.Nic[0].Networkname)
   183  		}
   184  
   185  		return nil
   186  	}
   187  }
   188  
   189  func testAccCheckCloudStackInstanceRenamedAndResized(
   190  	instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
   191  	return func(s *terraform.State) error {
   192  
   193  		if instance.Displayname != "terraform-updated" {
   194  			return fmt.Errorf("Bad display name: %s", instance.Displayname)
   195  		}
   196  
   197  		if instance.Serviceofferingname != CLOUDSTACK_SERVICE_OFFERING_2 {
   198  			return fmt.Errorf("Bad service offering: %s", instance.Serviceofferingname)
   199  		}
   200  
   201  		return nil
   202  	}
   203  }
   204  
   205  func testAccCheckCloudStackInstanceDestroy(s *terraform.State) error {
   206  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   207  
   208  	for _, rs := range s.RootModule().Resources {
   209  		if rs.Type != "cloudstack_instance" {
   210  			continue
   211  		}
   212  
   213  		if rs.Primary.ID == "" {
   214  			return fmt.Errorf("No instance ID is set")
   215  		}
   216  
   217  		_, _, err := cs.VirtualMachine.GetVirtualMachineByID(rs.Primary.ID)
   218  		if err == nil {
   219  			return fmt.Errorf("Virtual Machine %s still exists", rs.Primary.ID)
   220  		}
   221  	}
   222  
   223  	return nil
   224  }
   225  
   226  var testAccCloudStackInstance_basic = fmt.Sprintf(`
   227  resource "cloudstack_instance" "foobar" {
   228    name = "terraform-test"
   229    display_name = "terraform"
   230    service_offering= "%s"
   231    network = "%s"
   232    template = "%s"
   233    zone = "%s"
   234    user_data = "foobar\nfoo\nbar"
   235    expunge = true
   236  }`,
   237  	CLOUDSTACK_SERVICE_OFFERING_1,
   238  	CLOUDSTACK_NETWORK_1,
   239  	CLOUDSTACK_TEMPLATE,
   240  	CLOUDSTACK_ZONE)
   241  
   242  var testAccCloudStackInstance_renameAndResize = fmt.Sprintf(`
   243  resource "cloudstack_instance" "foobar" {
   244    name = "terraform-test"
   245    display_name = "terraform-updated"
   246    service_offering= "%s"
   247    network = "%s"
   248    template = "%s"
   249    zone = "%s"
   250    user_data = "foobar\nfoo\nbar"
   251    expunge = true
   252  }`,
   253  	CLOUDSTACK_SERVICE_OFFERING_2,
   254  	CLOUDSTACK_NETWORK_1,
   255  	CLOUDSTACK_TEMPLATE,
   256  	CLOUDSTACK_ZONE)
   257  
   258  var testAccCloudStackInstance_fixedIP = fmt.Sprintf(`
   259  resource "cloudstack_instance" "foobar" {
   260    name = "terraform-test"
   261    display_name = "terraform"
   262    service_offering= "%s"
   263    network = "%s"
   264    ipaddress = "%s"
   265    template = "%s"
   266    zone = "%s"
   267    expunge = true
   268  }`,
   269  	CLOUDSTACK_SERVICE_OFFERING_1,
   270  	CLOUDSTACK_NETWORK_1,
   271  	CLOUDSTACK_NETWORK_1_IPADDRESS,
   272  	CLOUDSTACK_TEMPLATE,
   273  	CLOUDSTACK_ZONE)
   274  
   275  var testAccCloudStackInstance_keyPair = fmt.Sprintf(`
   276  resource "cloudstack_ssh_keypair" "foo" {
   277    name = "terraform-test-keypair"
   278  }
   279  
   280  resource "cloudstack_instance" "foobar" {
   281    name = "terraform-test"
   282    display_name = "terraform"
   283    service_offering= "%s"
   284    network = "%s"
   285    ipaddress = "%s"
   286    template = "%s"
   287    zone = "%s"
   288  	keypair = "${cloudstack_ssh_keypair.foo.name}"
   289    expunge = true
   290  }`,
   291  	CLOUDSTACK_SERVICE_OFFERING_1,
   292  	CLOUDSTACK_NETWORK_1,
   293  	CLOUDSTACK_NETWORK_1_IPADDRESS,
   294  	CLOUDSTACK_TEMPLATE,
   295  	CLOUDSTACK_ZONE)
   296  
   297  var testAccCloudStackInstance_project = fmt.Sprintf(`
   298  resource "cloudstack_instance" "foobar" {
   299    name = "terraform-test"
   300    display_name = "terraform"
   301    service_offering= "%s"
   302  	network = "%s"
   303    template = "%s"
   304  	project = "%s"
   305    zone = "%s"
   306    expunge = true
   307  }`,
   308  	CLOUDSTACK_SERVICE_OFFERING_1,
   309  	CLOUDSTACK_PROJECT_NETWORK,
   310  	CLOUDSTACK_TEMPLATE,
   311  	CLOUDSTACK_PROJECT_NAME,
   312  	CLOUDSTACK_ZONE)