github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/vcd/resource_vcd_vapp_test.go (about)

     1  package vcd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/hmrc/vmware-govcd"
    11  )
    12  
    13  func TestAccVcdVApp_PowerOff(t *testing.T) {
    14  	var vapp govcd.VApp
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckVcdVAppDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: fmt.Sprintf(testAccCheckVcdVApp_basic, os.Getenv("VCD_EDGE_GATWEWAY")),
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckVcdVAppExists("vcd_vapp.foobar", &vapp),
    25  					testAccCheckVcdVAppAttributes(&vapp),
    26  					resource.TestCheckResourceAttr(
    27  						"vcd_vapp.foobar", "name", "foobar"),
    28  					resource.TestCheckResourceAttr(
    29  						"vcd_vapp.foobar", "ip", "10.10.102.160"),
    30  					resource.TestCheckResourceAttr(
    31  						"vcd_vapp.foobar", "power_on", "true"),
    32  				),
    33  			},
    34  			resource.TestStep{
    35  				Config: fmt.Sprintf(testAccCheckVcdVApp_powerOff, os.Getenv("VCD_EDGE_GATWEWAY")),
    36  				Check: resource.ComposeTestCheckFunc(
    37  					testAccCheckVcdVAppExists("vcd_vapp.foobar", &vapp),
    38  					testAccCheckVcdVAppAttributes_off(&vapp),
    39  					resource.TestCheckResourceAttr(
    40  						"vcd_vapp.foobar", "name", "foobar"),
    41  					resource.TestCheckResourceAttr(
    42  						"vcd_vapp.foobar", "ip", "10.10.102.160"),
    43  					resource.TestCheckResourceAttr(
    44  						"vcd_vapp.foobar", "power_on", "false"),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccCheckVcdVAppExists(n string, vapp *govcd.VApp) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		rs, ok := s.RootModule().Resources[n]
    54  		if !ok {
    55  			return fmt.Errorf("Not found: %s", n)
    56  		}
    57  
    58  		if rs.Primary.ID == "" {
    59  			return fmt.Errorf("No VAPP ID is set")
    60  		}
    61  
    62  		conn := testAccProvider.Meta().(*VCDClient)
    63  
    64  		resp, err := conn.OrgVdc.FindVAppByName(rs.Primary.ID)
    65  		if err != nil {
    66  			return err
    67  		}
    68  
    69  		*vapp = resp
    70  
    71  		return nil
    72  	}
    73  }
    74  
    75  func testAccCheckVcdVAppDestroy(s *terraform.State) error {
    76  	conn := testAccProvider.Meta().(*VCDClient)
    77  
    78  	for _, rs := range s.RootModule().Resources {
    79  		if rs.Type != "vcd_vapp" {
    80  			continue
    81  		}
    82  
    83  		_, err := conn.OrgVdc.FindVAppByName(rs.Primary.ID)
    84  
    85  		if err == nil {
    86  			return fmt.Errorf("VPCs still exist")
    87  		}
    88  
    89  		return nil
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  func testAccCheckVcdVAppAttributes(vapp *govcd.VApp) resource.TestCheckFunc {
    96  	return func(s *terraform.State) error {
    97  
    98  		if vapp.VApp.Name != "foobar" {
    99  			return fmt.Errorf("Bad name: %s", vapp.VApp.Name)
   100  		}
   101  
   102  		if vapp.VApp.Name != vapp.VApp.Children.VM[0].Name {
   103  			return fmt.Errorf("VApp and VM names do not match. %s != %s",
   104  				vapp.VApp.Name, vapp.VApp.Children.VM[0].Name)
   105  		}
   106  
   107  		status, _ := vapp.GetStatus()
   108  		if status != "POWERED_ON" {
   109  			return fmt.Errorf("VApp is not powered on")
   110  		}
   111  
   112  		return nil
   113  	}
   114  }
   115  
   116  func testAccCheckVcdVAppAttributes_off(vapp *govcd.VApp) resource.TestCheckFunc {
   117  	return func(s *terraform.State) error {
   118  
   119  		if vapp.VApp.Name != "foobar" {
   120  			return fmt.Errorf("Bad name: %s", vapp.VApp.Name)
   121  		}
   122  
   123  		if vapp.VApp.Name != vapp.VApp.Children.VM[0].Name {
   124  			return fmt.Errorf("VApp and VM names do not match. %s != %s",
   125  				vapp.VApp.Name, vapp.VApp.Children.VM[0].Name)
   126  		}
   127  
   128  		status, _ := vapp.GetStatus()
   129  		if status != "POWERED_OFF" {
   130  			return fmt.Errorf("VApp is still powered on")
   131  		}
   132  
   133  		return nil
   134  	}
   135  }
   136  
   137  const testAccCheckVcdVApp_basic = `
   138  resource "vcd_network" "foonet" {
   139  	name = "foonet"
   140  	edge_gateway = "%s"
   141  	gateway = "10.10.102.1"
   142  	static_ip_pool {
   143  		start_address = "10.10.102.2"
   144  		end_address = "10.10.102.254"
   145  	}
   146  }
   147  
   148  resource "vcd_vapp" "foobar" {
   149    name = "foobar"
   150    template_name = "base-centos-7.0-x86_64_v-0.1_b-74"
   151    catalog_name = "NubesLab"
   152    network_name = "${vcd_network.foonet.name}"
   153    memory = 1024
   154  	cpus = 1
   155  	ip = "10.10.102.160"
   156  }
   157  `
   158  
   159  const testAccCheckVcdVApp_powerOff = `
   160  resource "vcd_network" "foonet" {
   161  	name = "foonet"
   162  	edge_gateway = "%s"
   163  	gateway = "10.10.102.1"
   164  	static_ip_pool {
   165  		start_address = "10.10.102.2"
   166  		end_address = "10.10.102.254"
   167  	}
   168  }
   169  
   170  resource "vcd_vapp" "foobar" {
   171    name = "foobar"
   172    template_name = "base-centos-7.0-x86_64_v-0.1_b-74"
   173    catalog_name = "NubesLab"
   174    network_name = "${vcd_network.foonet.name}"
   175    memory = 1024
   176  	cpus = 1
   177  	ip = "10.10.102.160"
   178  	power_on = false
   179  }
   180  `