Skip to content

Commit

Permalink
[review] add test for validateRFC1918Network, fix acc test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidquarles committed Nov 2, 2017
1 parent b34c5f7 commit edd19a7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
1 change: 0 additions & 1 deletion google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,6 @@ func flattenMasterAuthorizedNetworksConfig(c *container.MasterAuthorizedNetworks
"display_name": v.DisplayName,
})
}
// now does this adhere to the schema?
result["cidr_blocks"] = cidrBlocks
}
return []map[string]interface{}{result}
Expand Down
6 changes: 3 additions & 3 deletions google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func TestAccContainerCluster_withMasterAuthorizedNetworksConfig(t *testing.T) {
Config: testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName, []string{"8.8.8.8/32"}),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerCluster("google_container_cluster.with_master_authorized_networks"),
resource.TestCheckNoResourceAttr("google_container_cluster.with_master_authorized_networks",
"master_authorized_networks_config.0.cidr_blocks"),
resource.TestCheckResourceAttr("google_container_cluster.with_master_authorized_networks",
"master_authorized_networks_config.0.cidr_blocks.#", "1"),
),
},
},
Expand Down Expand Up @@ -943,7 +943,7 @@ resource "google_container_cluster" "with_master_authorized_networks" {
master_authorized_networks_config {
%s
}
}
}`, clusterName, cidrBlocks)
}

Expand Down
56 changes: 56 additions & 0 deletions google/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,45 @@ func TestValidateGCPName(t *testing.T) {
}
}

func TestValidateRFC1918Network(t *testing.T) {
x := []RFC1918NetworkTestCase{
// No errors
{TestName: "valid 10.x", CIDR: "10.0.0.0/8", MinPrefix: 0, MaxPrefix: 32},
{TestName: "valid 172.x", CIDR: "172.16.0.0/16", MinPrefix: 0, MaxPrefix: 32},
{TestName: "valid 192.x", CIDR: "192.168.0.0/32", MinPrefix: 0, MaxPrefix: 32},
{TestName: "valid, bounded 10.x CIDR", CIDR: "10.0.0.0/8", MinPrefix: 8, MaxPrefix: 32},
{TestName: "valid, bounded 172.x CIDR", CIDR: "172.16.0.0/16", MinPrefix: 12, MaxPrefix: 32},
{TestName: "valid, bounded 192.x CIDR", CIDR: "192.168.0.0/32", MinPrefix: 16, MaxPrefix: 32},

// With errors
{TestName: "empty CIDR", CIDR: "", MinPrefix: 0, MaxPrefix: 32, ExpectError: true},
{TestName: "missing mask", CIDR: "10.0.0.0", MinPrefix: 0, MaxPrefix: 32, ExpectError: true},
{TestName: "invalid CIDR", CIDR: "10.1.0.0/8", MinPrefix: 0, MaxPrefix: 32, ExpectError: true},
{TestName: "valid 10.x CIDR with lower bound violation", CIDR: "10.0.0.0/8", MinPrefix: 16, MaxPrefix: 32, ExpectError: true},
{TestName: "valid 10.x CIDR with upper bound violation", CIDR: "10.0.0.0/24", MinPrefix: 8, MaxPrefix: 16, ExpectError: true},
{TestName: "valid public CIDR", CIDR: "8.8.8.8/32", MinPrefix: 0, MaxPrefix: 32, ExpectError: true},
}

es := testRFC1918Networks(x)
if len(es) > 0 {
t.Errorf("Failed to validate RFC1918 Networks: %v", es)
}
}

type GCPNameTestCase struct {
TestName string
Value string
ExpectError bool
}

type RFC1918NetworkTestCase struct {
TestName string
CIDR string
MinPrefix int
MaxPrefix int
ExpectError bool
}

func testGCPNames(cases []GCPNameTestCase) []error {
es := make([]error, 0)
for _, c := range cases {
Expand All @@ -55,3 +88,26 @@ func testGCPName(testCase GCPNameTestCase) []error {

return es
}

func testRFC1918Networks(cases []RFC1918NetworkTestCase) []error {
es := make([]error, 0)
for _, c := range cases {
es = append(es, testRFC1918Network(c)...)
}

return es
}

func testRFC1918Network(testCase RFC1918NetworkTestCase) []error {
f := validateRFC1918Network(testCase.MinPrefix, testCase.MaxPrefix)
_, es := f(testCase.CIDR, testCase.TestName)
if testCase.ExpectError {
if len(es) > 0 {
return nil
}
return []error{fmt.Errorf("Didn't see expected error in case \"%s\" with CIDR=\"%s\" MinPrefix=%v MaxPrefix=%v",
testCase.TestName, testCase.CIDR, testCase.MinPrefix, testCase.MaxPrefix)}
}

return es
}

0 comments on commit edd19a7

Please sign in to comment.