Small refactor of the CloudStack load balancer tests

This makes the code a little more generic and inline with the other
code. Functionally the same things are tested.
This commit is contained in:
Sander van Harmelen 2015-09-08 11:19:41 +02:00
parent 62f8883df0
commit ce50f35679
1 changed files with 24 additions and 39 deletions

View File

@ -19,7 +19,7 @@ func TestAccCloudStackLoadBalancerRule_basic(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_basic, Config: testAccCloudStackLoadBalancerRule_basic,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"), testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"), "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -35,7 +35,8 @@ func TestAccCloudStackLoadBalancerRule_basic(t *testing.T) {
} }
func TestAccCloudStackLoadBalancerRule_update(t *testing.T) { func TestAccCloudStackLoadBalancerRule_update(t *testing.T) {
id := "" var id string
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
@ -44,15 +45,7 @@ func TestAccCloudStackLoadBalancerRule_update(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_basic, Config: testAccCloudStackLoadBalancerRule_basic,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"), testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", &id),
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
},
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"), "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -67,17 +60,7 @@ func TestAccCloudStackLoadBalancerRule_update(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_update, Config: testAccCloudStackLoadBalancerRule_update,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"), testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", &id),
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
},
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"), "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -101,7 +84,7 @@ func TestAccCloudStackLoadBalancerRule_forcenew(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_basic, Config: testAccCloudStackLoadBalancerRule_basic,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"), testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"), "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -116,7 +99,7 @@ func TestAccCloudStackLoadBalancerRule_forcenew(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_forcenew, Config: testAccCloudStackLoadBalancerRule_forcenew,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"), testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"), "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -140,7 +123,7 @@ func TestAccCloudStackLoadBalancerRule_vpc(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_vpc, Config: testAccCloudStackLoadBalancerRule_vpc,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"), testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"), "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -164,7 +147,7 @@ func TestAccCloudStackLoadBalancerRule_vpc_update(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_vpc, Config: testAccCloudStackLoadBalancerRule_vpc,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"), testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"), "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -179,7 +162,7 @@ func TestAccCloudStackLoadBalancerRule_vpc_update(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccCloudStackLoadBalancerRule_vpc_update, Config: testAccCloudStackLoadBalancerRule_vpc_update,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo"), testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"), "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -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 { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {
@ -205,21 +188,23 @@ func testAccCheckCloudStackLoadBalancerRuleExist(n string) resource.TestCheckFun
return fmt.Errorf("No loadbalancer rule ID is set") return fmt.Errorf("No loadbalancer rule ID is set")
} }
for k, uuid := range rs.Primary.Attributes { if id != nil {
if !strings.Contains(k, "uuid") { if *id != "" && *id != rs.Primary.ID {
continue return fmt.Errorf("Resource ID has changed!")
} }
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) *id = rs.Primary.ID
_, count, err := cs.LoadBalancer.GetLoadBalancerRuleByID(uuid) }
if err != nil { cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
return err _, count, err := cs.LoadBalancer.GetLoadBalancerRuleByID(rs.Primary.ID)
}
if count == 0 { if err != nil {
return fmt.Errorf("Loadbalancer rule for %s not found", k) return err
} }
if count == 0 {
return fmt.Errorf("Loadbalancer rule %s not found", n)
} }
return nil return nil