Skip to content

Commit

Permalink
Merge pull request hashicorp#3186 from svanharmelen/f-tweak-cloudstac…
Browse files Browse the repository at this point in the history
…k-test

provider/cloudstack: small refactor of the CloudStack load balancer tests
  • Loading branch information
Sander van Harmelen committed Sep 8, 2015
2 parents 62f8883 + ce50f35 commit b2718ea
Showing 1 changed file with 24 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccCloudStackLoadBalancerRule_basic(t *testing.T) {
resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"),
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
resource.TestCheckResourceAttr(
Expand All @@ -35,7 +35,8 @@ func TestAccCloudStackLoadBalancerRule_basic(t *testing.T) {
}

func TestAccCloudStackLoadBalancerRule_update(t *testing.T) {
id := ""
var id string

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -44,15 +45,7 @@ func TestAccCloudStackLoadBalancerRule_update(t *testing.T) {
resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"),
func(s *terraform.State) error {
rs, ok := s.RootModule().Resources["cloudstack_loadbalancer_rule.foo"]
if !ok {
return fmt.Errorf("Not found: cloudstack_loadbalancer_rule.foo")
}
id = rs.Primary.ID
return nil
},
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", &id),
resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
resource.TestCheckResourceAttr(
Expand All @@ -67,17 +60,7 @@ func TestAccCloudStackLoadBalancerRule_update(t *testing.T) {
resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"),
func(s *terraform.State) error {
rs, ok := s.RootModule().Resources["cloudstack_loadbalancer_rule.foo"]
if !ok {
return fmt.Errorf("Not found: cloudstack_loadbalancer_rule.foo")
}
if id != rs.Primary.ID {
return fmt.Errorf("Resource has changed!")
}
return nil
},
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", &id),
resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"),
resource.TestCheckResourceAttr(
Expand All @@ -101,7 +84,7 @@ func TestAccCloudStackLoadBalancerRule_forcenew(t *testing.T) {
resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"),
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
resource.TestCheckResourceAttr(
Expand All @@ -116,7 +99,7 @@ func TestAccCloudStackLoadBalancerRule_forcenew(t *testing.T) {
resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_forcenew,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"),
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"),
resource.TestCheckResourceAttr(
Expand All @@ -140,7 +123,7 @@ func TestAccCloudStackLoadBalancerRule_vpc(t *testing.T) {
resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_vpc,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"),
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
resource.TestCheckResourceAttr(
Expand All @@ -164,7 +147,7 @@ func TestAccCloudStackLoadBalancerRule_vpc_update(t *testing.T) {
resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_vpc,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"),
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
resource.TestCheckResourceAttr(
Expand All @@ -179,7 +162,7 @@ func TestAccCloudStackLoadBalancerRule_vpc_update(t *testing.T) {
resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_vpc_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"),
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"),
resource.TestCheckResourceAttr(
Expand All @@ -194,7 +177,7 @@ func TestAccCloudStackLoadBalancerRule_vpc_update(t *testing.T) {
})
}

func testAccCheckCloudStackLoadBalancerRuleExist(n string) resource.TestCheckFunc {
func testAccCheckCloudStackLoadBalancerRuleExist(n string, id *string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -205,21 +188,23 @@ func testAccCheckCloudStackLoadBalancerRuleExist(n string) resource.TestCheckFun
return fmt.Errorf("No loadbalancer rule ID is set")
}

for k, uuid := range rs.Primary.Attributes {
if !strings.Contains(k, "uuid") {
continue
if id != nil {
if *id != "" && *id != rs.Primary.ID {
return fmt.Errorf("Resource ID has changed!")
}

cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
_, count, err := cs.LoadBalancer.GetLoadBalancerRuleByID(uuid)
*id = rs.Primary.ID
}

if err != nil {
return err
}
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
_, count, err := cs.LoadBalancer.GetLoadBalancerRuleByID(rs.Primary.ID)

if count == 0 {
return fmt.Errorf("Loadbalancer rule for %s not found", k)
}
if err != nil {
return err
}

if count == 0 {
return fmt.Errorf("Loadbalancer rule %s not found", n)
}

return nil
Expand Down

0 comments on commit b2718ea

Please sign in to comment.