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