github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/openstack/resource_openstack_compute_servergroup_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
    11  	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
    12  )
    13  
    14  func TestAccComputeV2ServerGroup_basic(t *testing.T) {
    15  	var sg servergroups.ServerGroup
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckComputeV2ServerGroupDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccComputeV2ServerGroup_basic,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckComputeV2ServerGroupExists("openstack_compute_servergroup_v2.sg_1", &sg),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccComputeV2ServerGroup_affinity(t *testing.T) {
    33  	var instance servers.Server
    34  	var sg servergroups.ServerGroup
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckComputeV2ServerGroupDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccComputeV2ServerGroup_affinity,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckComputeV2ServerGroupExists("openstack_compute_servergroup_v2.sg_1", &sg),
    45  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
    46  					testAccCheckComputeV2InstanceInServerGroup(&instance, &sg),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func testAccCheckComputeV2ServerGroupDestroy(s *terraform.State) error {
    54  	config := testAccProvider.Meta().(*Config)
    55  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
    56  	if err != nil {
    57  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
    58  	}
    59  
    60  	for _, rs := range s.RootModule().Resources {
    61  		if rs.Type != "openstack_compute_servergroup_v2" {
    62  			continue
    63  		}
    64  
    65  		_, err := servergroups.Get(computeClient, rs.Primary.ID).Extract()
    66  		if err == nil {
    67  			return fmt.Errorf("ServerGroup still exists")
    68  		}
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func testAccCheckComputeV2ServerGroupExists(n string, kp *servergroups.ServerGroup) resource.TestCheckFunc {
    75  	return func(s *terraform.State) error {
    76  		rs, ok := s.RootModule().Resources[n]
    77  		if !ok {
    78  			return fmt.Errorf("Not found: %s", n)
    79  		}
    80  
    81  		if rs.Primary.ID == "" {
    82  			return fmt.Errorf("No ID is set")
    83  		}
    84  
    85  		config := testAccProvider.Meta().(*Config)
    86  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
    87  		if err != nil {
    88  			return fmt.Errorf("Error creating OpenStack compute client: %s", err)
    89  		}
    90  
    91  		found, err := servergroups.Get(computeClient, rs.Primary.ID).Extract()
    92  		if err != nil {
    93  			return err
    94  		}
    95  
    96  		if found.ID != rs.Primary.ID {
    97  			return fmt.Errorf("ServerGroup not found")
    98  		}
    99  
   100  		*kp = *found
   101  
   102  		return nil
   103  	}
   104  }
   105  
   106  func testAccCheckComputeV2InstanceInServerGroup(instance *servers.Server, sg *servergroups.ServerGroup) resource.TestCheckFunc {
   107  	return func(s *terraform.State) error {
   108  		if len(sg.Members) > 0 {
   109  			for _, m := range sg.Members {
   110  				if m == instance.ID {
   111  					return nil
   112  				}
   113  			}
   114  		}
   115  
   116  		return fmt.Errorf("Instance %s is not part of Server Group %s", instance.ID, sg.ID)
   117  	}
   118  }
   119  
   120  const testAccComputeV2ServerGroup_basic = `
   121  resource "openstack_compute_servergroup_v2" "sg_1" {
   122    name = "sg_1"
   123    policies = ["affinity"]
   124  }
   125  `
   126  
   127  const testAccComputeV2ServerGroup_affinity = `
   128  resource "openstack_compute_servergroup_v2" "sg_1" {
   129    name = "sg_1"
   130    policies = ["affinity"]
   131  }
   132  
   133  resource "openstack_compute_instance_v2" "instance_1" {
   134    name = "instance_1"
   135    security_groups = ["default"]
   136    scheduler_hints {
   137      group = "${openstack_compute_servergroup_v2.sg_1.id}"
   138    }
   139  }
   140  `