From 0356f4595746ecc26b263138ab687259506c24e8 Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Sun, 24 Sep 2017 13:06:57 -0400 Subject: [PATCH] Add unit test for model helper functions --- pkg/apis/kops/model/BUILD.bazel | 9 +- pkg/apis/kops/model/utils_test.go | 170 ++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 pkg/apis/kops/model/utils_test.go diff --git a/pkg/apis/kops/model/BUILD.bazel b/pkg/apis/kops/model/BUILD.bazel index 3f2f0893b5ba7..61b0400e4ce4b 100644 --- a/pkg/apis/kops/model/BUILD.bazel +++ b/pkg/apis/kops/model/BUILD.bazel @@ -1,4 +1,4 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", @@ -9,3 +9,10 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library", ], ) + +go_test( + name = "go_default_test", + srcs = ["utils_test.go"], + library = ":go_default_library", + deps = ["//pkg/apis/kops:go_default_library"], +) diff --git a/pkg/apis/kops/model/utils_test.go b/pkg/apis/kops/model/utils_test.go new file mode 100644 index 0000000000000..1b4deb0812156 --- /dev/null +++ b/pkg/apis/kops/model/utils_test.go @@ -0,0 +1,170 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package model + +import ( + "k8s.io/kops/pkg/apis/kops" + "reflect" + "testing" +) + +// Test_FindSubnet tests FindSubnet +func Test_FindSubnet(t *testing.T) { + cluster := &kops.Cluster{ + Spec: kops.ClusterSpec{ + Subnets: []kops.ClusterSubnetSpec{ + {Name: "a"}, + {Name: "b"}, + }, + }, + } + + grid := []struct { + cluster *kops.Cluster + subnet string + expectFound bool + }{ + { + cluster: cluster, + subnet: "a", + expectFound: true, + }, + { + cluster: cluster, + subnet: "a", + expectFound: true, + }, + { + cluster: cluster, + subnet: "ab", + expectFound: false, + }, + } + for _, g := range grid { + actual := FindSubnet(g.cluster, g.subnet) + if g.expectFound { + if actual == nil { + t.Errorf("did not find %q", g.subnet) + continue + } + if actual.Name != g.subnet { + t.Errorf("found but had wrong name: %q vs %q", g.subnet, actual.Name) + } + } else { + if actual != nil { + t.Errorf("unexpectedly found %q", g.subnet) + continue + } + } + } +} + +// Test_FindZonesForInstanceGroup tests FindZonesForInstanceGroup +func Test_FindZonesForInstanceGroup(t *testing.T) { + cluster := &kops.Cluster{ + Spec: kops.ClusterSpec{ + Subnets: []kops.ClusterSubnetSpec{ + {Name: "zonea", Zone: "zonea"}, + {Name: "zoneb", Zone: "zoneb"}, + }, + }, + } + + grid := []struct { + cluster *kops.Cluster + ig *kops.InstanceGroup + expected []string + expectError bool + }{ + { + cluster: cluster, + ig: &kops.InstanceGroup{ + Spec: kops.InstanceGroupSpec{ + Subnets: []string{"zonea"}, + }, + }, + expected: []string{"zonea"}, + }, + { + cluster: cluster, + ig: &kops.InstanceGroup{ + Spec: kops.InstanceGroupSpec{ + Subnets: []string{"zonea", "zoneb"}, + }, + }, + expected: []string{"zonea", "zoneb"}, + }, + { + cluster: cluster, + ig: &kops.InstanceGroup{ + Spec: kops.InstanceGroupSpec{ + Subnets: []string{"zoneb", "zonea"}, + }, + }, + // Order is not preserved (they are in fact sorted) + expected: []string{"zonea", "zoneb"}, + }, + { + cluster: cluster, + ig: &kops.InstanceGroup{ + Spec: kops.InstanceGroupSpec{ + Subnets: []string{"zoneb", "nope"}, + }, + }, + expectError: true, + }, + { + cluster: cluster, + ig: &kops.InstanceGroup{ + Spec: kops.InstanceGroupSpec{ + Zones: []string{"directa", "directb"}, + }, + }, + expected: []string{"directa", "directb"}, + }, + { + cluster: cluster, + ig: &kops.InstanceGroup{ + Spec: kops.InstanceGroupSpec{ + Zones: []string{"directa", "directb"}, + Subnets: []string{"zonea", "zoneb"}, + }, + }, + // Not sure if we actually should merge here - should the IG zones actually act as a restriction on the Subnet zones? + // For now, this isn't likely to come up, so we test the current behaviour + expected: []string{"directa", "directb", "zonea", "zoneb"}, + }, + } + for i, g := range grid { + actual, err := FindZonesForInstanceGroup(g.cluster, g.ig) + if err != nil { + if g.expectError { + continue + } + t.Errorf("unexpected error for %d: %v", i, err) + continue + } + if g.expectError { + t.Errorf("expected error for %d, result was %v", i, actual) + continue + } + if !reflect.DeepEqual(actual, g.expected) { + t.Errorf("unexpected result for %d: actual=%v, expected=%v", i, actual, g.expected) + continue + } + } +}