github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/rackspace/gophercloud/openstack/compute/v2/extensions/servergroups" 11 "github.com/rackspace/gophercloud/openstack/compute/v2/servers" 12 ) 13 14 func TestAccComputeV2ServerGroup_basic(t *testing.T) { 15 var serverGroup 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(t, "openstack_compute_servergroup_v2.mysg", &serverGroup), 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(t, "openstack_compute_servergroup_v2.mysg", &sg), 45 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.myinstance", &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("(testAccCheckComputeV2ServerGroupDestroy) 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(t *testing.T, 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("(testAccCheckComputeV2ServerGroupExists) 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 var testAccComputeV2ServerGroup_basic = ` 121 resource "openstack_compute_servergroup_v2" "mysg" { 122 name = "mysg" 123 policies = ["affinity"] 124 }` 125 126 var testAccComputeV2ServerGroup_affinity = ` 127 resource "openstack_compute_servergroup_v2" "mysg" { 128 name = "mysg" 129 policies = ["affinity"] 130 } 131 132 resource "openstack_compute_instance_v2" "myinstance" { 133 name = "myinstance" 134 security_groups = ["default"] 135 scheduler_hints { 136 group = "${openstack_compute_servergroup_v2.mysg.id}" 137 } 138 }`