github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudstack/resource_cloudstack_affinity_group_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 TestAccCloudStackAffinityGroup_basic(t *testing.T) {
    13  	var affinityGroup cloudstack.AffinityGroup
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackAffinityGroupDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackAffinityGroup,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackAffinityGroupExists("cloudstack_affinity_group.foo", &affinityGroup),
    24  					testAccCheckCloudStackAffinityGroupAttributes(&affinityGroup),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckCloudStackAffinityGroupExists(
    32  	n string, affinityGroup *cloudstack.AffinityGroup) resource.TestCheckFunc {
    33  	return func(s *terraform.State) error {
    34  		rs, ok := s.RootModule().Resources[n]
    35  		if !ok {
    36  			return fmt.Errorf("Not found: %s", n)
    37  		}
    38  
    39  		if rs.Primary.ID == "" {
    40  			return fmt.Errorf("No affinity group ID is set")
    41  		}
    42  
    43  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    44  		ag, _, err := cs.AffinityGroup.GetAffinityGroupByID(rs.Primary.ID)
    45  
    46  		if err != nil {
    47  			return err
    48  		}
    49  
    50  		if ag.Id != rs.Primary.ID {
    51  			return fmt.Errorf("Affinity group not found")
    52  		}
    53  
    54  		*affinityGroup = *ag
    55  
    56  		return nil
    57  	}
    58  }
    59  
    60  func testAccCheckCloudStackAffinityGroupAttributes(
    61  	affinityGroup *cloudstack.AffinityGroup) resource.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  
    64  		if affinityGroup.Name != "terraform-affinity-group" {
    65  			return fmt.Errorf("Bad name: %s", affinityGroup.Name)
    66  		}
    67  
    68  		if affinityGroup.Description != "terraform-affinity-group" {
    69  			return fmt.Errorf("Bad description: %s", affinityGroup.Description)
    70  		}
    71  
    72  		if affinityGroup.Type != "host anti-affinity" {
    73  			return fmt.Errorf("Bad type: %s", affinityGroup.Type)
    74  		}
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  func testAccCheckCloudStackAffinityGroupDestroy(s *terraform.State) error {
    81  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    82  
    83  	for _, rs := range s.RootModule().Resources {
    84  		if rs.Type != "cloudstack_affinity_group" {
    85  			continue
    86  		}
    87  
    88  		if rs.Primary.ID == "" {
    89  			return fmt.Errorf("No affinity group ID is set")
    90  		}
    91  
    92  		_, _, err := cs.AffinityGroup.GetAffinityGroupByID(rs.Primary.ID)
    93  		if err == nil {
    94  			return fmt.Errorf("Affinity group %s still exists", rs.Primary.ID)
    95  		}
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  var testAccCloudStackAffinityGroup = fmt.Sprintf(`
   102  resource "cloudstack_affinity_group" "foo" {
   103  	name = "terraform-affinity-group"
   104  	type = "host anti-affinity"
   105  }`)