github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/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",
    28  						"user_data",
    29  						"0cf3dcdc356ec8369494cb3991985ecd5296cdd5"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccCloudStackInstance_update(t *testing.T) {
    37  	var instance cloudstack.VirtualMachine
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccCheckCloudStackInstanceDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccCloudStackInstance_basic,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckCloudStackInstanceExists(
    48  						"cloudstack_instance.foobar", &instance),
    49  					testAccCheckCloudStackInstanceAttributes(&instance),
    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 testAccCheckCloudStackInstanceExists(
    91  	n string, instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
    92  	return func(s *terraform.State) error {
    93  		rs, ok := s.RootModule().Resources[n]
    94  		if !ok {
    95  			return fmt.Errorf("Not found: %s", n)
    96  		}
    97  
    98  		if rs.Primary.ID == "" {
    99  			return fmt.Errorf("No instance ID is set")
   100  		}
   101  
   102  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   103  		vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(rs.Primary.ID)
   104  
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		if vm.Id != rs.Primary.ID {
   110  			return fmt.Errorf("Instance not found")
   111  		}
   112  
   113  		*instance = *vm
   114  
   115  		return nil
   116  	}
   117  }
   118  
   119  func testAccCheckCloudStackInstanceAttributes(
   120  	instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
   121  	return func(s *terraform.State) error {
   122  
   123  		if instance.Name != "terraform-test" {
   124  			return fmt.Errorf("Bad name: %s", instance.Name)
   125  		}
   126  
   127  		if instance.Displayname != "terraform" {
   128  			return fmt.Errorf("Bad display name: %s", instance.Displayname)
   129  		}
   130  
   131  		if instance.Serviceofferingname != CLOUDSTACK_SERVICE_OFFERING_1 {
   132  			return fmt.Errorf("Bad service offering: %s", instance.Serviceofferingname)
   133  		}
   134  
   135  		if instance.Templatename != CLOUDSTACK_TEMPLATE {
   136  			return fmt.Errorf("Bad template: %s", instance.Templatename)
   137  		}
   138  
   139  		if instance.Nic[0].Networkname != CLOUDSTACK_NETWORK_1 {
   140  			return fmt.Errorf("Bad network: %s", instance.Nic[0].Networkname)
   141  		}
   142  
   143  		return nil
   144  	}
   145  }
   146  
   147  func testAccCheckCloudStackInstanceRenamedAndResized(
   148  	instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
   149  	return func(s *terraform.State) error {
   150  
   151  		if instance.Displayname != "terraform-updated" {
   152  			return fmt.Errorf("Bad display name: %s", instance.Displayname)
   153  		}
   154  
   155  		if instance.Serviceofferingname != CLOUDSTACK_SERVICE_OFFERING_2 {
   156  			return fmt.Errorf("Bad service offering: %s", instance.Serviceofferingname)
   157  		}
   158  
   159  		return nil
   160  	}
   161  }
   162  
   163  func testAccCheckCloudStackInstanceDestroy(s *terraform.State) error {
   164  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   165  
   166  	for _, rs := range s.RootModule().Resources {
   167  		if rs.Type != "cloudstack_instance" {
   168  			continue
   169  		}
   170  
   171  		if rs.Primary.ID == "" {
   172  			return fmt.Errorf("No instance ID is set")
   173  		}
   174  
   175  		p := cs.VirtualMachine.NewDestroyVirtualMachineParams(rs.Primary.ID)
   176  		err, _ := cs.VirtualMachine.DestroyVirtualMachine(p)
   177  
   178  		if err != nil {
   179  			return fmt.Errorf(
   180  				"Error deleting instance (%s): %s",
   181  				rs.Primary.ID, err)
   182  		}
   183  	}
   184  
   185  	return nil
   186  }
   187  
   188  var testAccCloudStackInstance_basic = fmt.Sprintf(`
   189  resource "cloudstack_instance" "foobar" {
   190    name = "terraform-test"
   191    display_name = "terraform"
   192    service_offering= "%s"
   193    network = "%s"
   194    template = "%s"
   195    zone = "%s"
   196    user_data = "foobar\nfoo\nbar"
   197    expunge = true
   198  }`,
   199  	CLOUDSTACK_SERVICE_OFFERING_1,
   200  	CLOUDSTACK_NETWORK_1,
   201  	CLOUDSTACK_TEMPLATE,
   202  	CLOUDSTACK_ZONE)
   203  
   204  var testAccCloudStackInstance_renameAndResize = fmt.Sprintf(`
   205  resource "cloudstack_instance" "foobar" {
   206    name = "terraform-test"
   207    display_name = "terraform-updated"
   208    service_offering= "%s"
   209    network = "%s"
   210    template = "%s"
   211    zone = "%s"
   212    user_data = "foobar\nfoo\nbar"
   213    expunge = true
   214  }`,
   215  	CLOUDSTACK_SERVICE_OFFERING_2,
   216  	CLOUDSTACK_NETWORK_1,
   217  	CLOUDSTACK_TEMPLATE,
   218  	CLOUDSTACK_ZONE)
   219  
   220  var testAccCloudStackInstance_fixedIP = fmt.Sprintf(`
   221  resource "cloudstack_instance" "foobar" {
   222    name = "terraform-test"
   223    display_name = "terraform"
   224    service_offering= "%s"
   225    network = "%s"
   226    ipaddress = "%s"
   227    template = "%s"
   228    zone = "%s"
   229    expunge = true
   230  }`,
   231  	CLOUDSTACK_SERVICE_OFFERING_1,
   232  	CLOUDSTACK_NETWORK_1,
   233  	CLOUDSTACK_NETWORK_1_IPADDRESS,
   234  	CLOUDSTACK_TEMPLATE,
   235  	CLOUDSTACK_ZONE)