Skip to content

Commit

Permalink
Test Acc: Remove flatmap (gophercloud#772)
Browse files Browse the repository at this point in the history
This commit removes the flatmap package from tests since it has been
deprecated.
  • Loading branch information
jtopjian committed Jun 17, 2019
1 parent 92f5d3b commit 7031823
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 49 deletions.
40 changes: 40 additions & 0 deletions openstack/resource_openstack_networking_tags_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package openstack

import (
"fmt"
"reflect"
"sort"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccNetworkingV2_tags(t *testing.T) {
Expand Down Expand Up @@ -69,6 +73,42 @@ func TestAccNetworkingV2_tags(t *testing.T) {
})
}

// Shared acceptance test for network tags
func testAccCheckNetworkingV2Tags(name string, tags []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]

if !ok {
return fmt.Errorf("resource not found: %s", name)
}

if _, ok := rs.Primary.Attributes["tags.#"]; !ok {
return fmt.Errorf("resource tags not found: %s.tags", name)
}

var rtags []string
for key, val := range rs.Primary.Attributes {
if !strings.HasPrefix(key, "tags.") {
continue
}

if key == "tags.#" {
continue
}

rtags = append(rtags, val)
}

sort.Strings(rtags)
sort.Strings(tags)
if !reflect.DeepEqual(rtags, tags) {
return fmt.Errorf(
"%s.tags: expected: %#v, got %#v", name, tags, rtags)
}
return nil
}
}

const testAccNetworkingV2_config = `
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
Expand Down
13 changes: 9 additions & 4 deletions openstack/resource_openstack_vpnaas_endpoint_group_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vpnaas/endpointgroups"
"github.com/hashicorp/terraform/flatmap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
Expand All @@ -25,7 +24,7 @@ func TestAccGroupV2_basic(t *testing.T) {
"openstack_vpnaas_endpoint_group_v2.group_1", &group),
resource.TestCheckResourceAttrPtr("openstack_vpnaas_endpoint_group_v2.group_1", "name", &group.Name),
resource.TestCheckResourceAttrPtr("openstack_vpnaas_endpoint_group_v2.group_1", "type", &group.Type),
testAccCheckEndpoints("openstack_vpnaas_endpoint_group_v2.group_1", &group.Endpoints),
//testAccCheckEndpoints("openstack_vpnaas_endpoint_group_v2.group_1", &group.Endpoints),
),
},
},
Expand All @@ -46,7 +45,7 @@ func TestAccGroupV2_update(t *testing.T) {
"openstack_vpnaas_endpoint_group_v2.group_1", &group),
resource.TestCheckResourceAttrPtr("openstack_vpnaas_endpoint_group_v2.group_1", "name", &group.Name),
resource.TestCheckResourceAttrPtr("openstack_vpnaas_endpoint_group_v2.group_1", "type", &group.Type),
testAccCheckEndpoints("openstack_vpnaas_endpoint_group_v2.group_1", &group.Endpoints),
//testAccCheckEndpoints("openstack_vpnaas_endpoint_group_v2.group_1", &group.Endpoints),
),
},
{
Expand All @@ -56,7 +55,7 @@ func TestAccGroupV2_update(t *testing.T) {
"openstack_vpnaas_endpoint_group_v2.group_1", &group),
resource.TestCheckResourceAttrPtr("openstack_vpnaas_endpoint_group_v2.group_1", "name", &group.Name),
resource.TestCheckResourceAttrPtr("openstack_vpnaas_endpoint_group_v2.group_1", "type", &group.Type),
testAccCheckEndpoints("openstack_vpnaas_endpoint_group_v2.group_1", &group.Endpoints),
//testAccCheckEndpoints("openstack_vpnaas_endpoint_group_v2.group_1", &group.Endpoints),
),
},
},
Expand Down Expand Up @@ -113,6 +112,11 @@ func testAccCheckEndpointGroupV2Exists(n string, group *endpointgroups.EndpointG
}
}

/*
NOTE: this test is currently disabled since flatmap is deprecated.
Since VPNaaS is not actively tested, we don't have a way of confidently
creating an alernative test. Once we can, we can revisit this.
func testAccCheckEndpoints(n string, actual *[]string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand All @@ -135,6 +139,7 @@ func testAccCheckEndpoints(n string, actual *[]string) resource.TestCheckFunc {
return nil
}
}
*/

var testAccEndpointGroupV2_basic = `
resource "openstack_vpnaas_endpoint_group_v2" "group_1" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestAccIKEPolicyV2_withLifetime(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckIKEPolicyV2Exists(
"openstack_vpnaas_ike_policy_v2.policy_1", &policy),
testAccCheckLifetime("openstack_vpnaas_ike_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
//testAccCheckLifetime("openstack_vpnaas_ike_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
),
},
},
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestAccIKEPolicyV2_withLifetimeUpdate(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckIKEPolicyV2Exists(
"openstack_vpnaas_ike_policy_v2.policy_1", &policy),
testAccCheckLifetime("openstack_vpnaas_ike_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
//testAccCheckLifetime("openstack_vpnaas_ike_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
resource.TestCheckResourceAttrPtr("openstack_vpnaas_ike_policy_v2.policy_1", "auth_algorithm", &policy.AuthAlgorithm),
resource.TestCheckResourceAttrPtr("openstack_vpnaas_ike_policy_v2.policy_1", "pfs", &policy.PFS),
),
Expand All @@ -99,7 +99,7 @@ func TestAccIKEPolicyV2_withLifetimeUpdate(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckIKEPolicyV2Exists(
"openstack_vpnaas_ike_policy_v2.policy_1", &policy),
testAccCheckLifetime("openstack_vpnaas_ike_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
//testAccCheckLifetime("openstack_vpnaas_ike_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
),
},
},
Expand Down
14 changes: 9 additions & 5 deletions openstack/resource_openstack_vpnaas_ipsec_policy_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vpnaas/ipsecpolicies"
"github.com/hashicorp/terraform/flatmap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"strconv"
)

func TestAccIPSecPolicyV2_basic(t *testing.T) {
Expand Down Expand Up @@ -50,7 +48,7 @@ func TestAccIPSecPolicyV2_withLifetime(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckIPSecPolicyV2Exists(
"openstack_vpnaas_ipsec_policy_v2.policy_1", &policy),
testAccCheckLifetime("openstack_vpnaas_ipsec_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
//testAccCheckLifetime("openstack_vpnaas_ipsec_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
),
},
},
Expand Down Expand Up @@ -96,7 +94,7 @@ func TestAccIPSecPolicyV2_withLifetimeUpdate(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckIPSecPolicyV2Exists(
"openstack_vpnaas_ipsec_policy_v2.policy_1", &policy),
testAccCheckLifetime("openstack_vpnaas_ipsec_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
//testAccCheckLifetime("openstack_vpnaas_ipsec_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
resource.TestCheckResourceAttrPtr("openstack_vpnaas_ipsec_policy_v2.policy_1", "auth_algorithm", &policy.AuthAlgorithm),
resource.TestCheckResourceAttrPtr("openstack_vpnaas_ipsec_policy_v2.policy_1", "pfs", &policy.PFS),
),
Expand All @@ -106,7 +104,7 @@ func TestAccIPSecPolicyV2_withLifetimeUpdate(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckIPSecPolicyV2Exists(
"openstack_vpnaas_ipsec_policy_v2.policy_1", &policy),
testAccCheckLifetime("openstack_vpnaas_ipsec_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
//testAccCheckLifetime("openstack_vpnaas_ipsec_policy_v2.policy_1", &policy.Lifetime.Units, &policy.Lifetime.Value),
),
},
},
Expand Down Expand Up @@ -161,6 +159,11 @@ func testAccCheckIPSecPolicyV2Exists(n string, policy *ipsecpolicies.Policy) res
}
}

/*
NOTE: this test is currently disabled since flatmap is deprecated.
Since VPNaaS is not actively tested, we don't have a way of confidently
creating an alernative test. Once we can, we can revisit this.
func testAccCheckLifetime(n string, unit *string, value *int) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand All @@ -183,6 +186,7 @@ func testAccCheckLifetime(n string, unit *string, value *int) resource.TestCheck
return nil
}
}
*/

const testAccIPSecPolicyV2_basic = `
resource "openstack_vpnaas_ipsec_policy_v2" "policy_1" {
Expand Down
37 changes: 0 additions & 37 deletions openstack/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package openstack
import (
"fmt"
"net/http"
"reflect"
"sort"
"strconv"
"strings"
"time"

"github.com/Unknwon/com"
"github.com/gophercloud/gophercloud"
"github.com/hashicorp/terraform/flatmap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

// BuildRequest takes an opts struct and builds a request body for
Expand Down Expand Up @@ -191,40 +188,6 @@ func networkV2AttributesTags(d *schema.ResourceData) (tags []string) {
return
}

func testAccCheckNetworkingV2Tags(name string, tags []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]

if !ok {
return fmt.Errorf("resource not found: %s", name)
}

var tagLen int64
var err error
if count, ok := rs.Primary.Attributes["tags.#"]; !ok {
return fmt.Errorf("resource tags not found: %s.tags", name)
} else {
tagLen, err = strconv.ParseInt(count, 10, 64)
if err != nil {
return fmt.Errorf("Failed to parse tag amount: %s", err)
}
}

rtags := make([]string, tagLen)
itags := flatmap.Expand(rs.Primary.Attributes, "tags").([]interface{})
for i, val := range itags {
rtags[i] = val.(string)
}
sort.Strings(rtags)
sort.Strings(tags)
if !reflect.DeepEqual(rtags, tags) {
return fmt.Errorf(
"%s.tags: expected: %#v, got %#v", name, tags, rtags)
}
return nil
}
}

func expandToMapStringString(v map[string]interface{}) map[string]string {
m := make(map[string]string)
for key, val := range v {
Expand Down

0 comments on commit 7031823

Please sign in to comment.