Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ironcladlou committed Jun 23, 2020
1 parent f363d80 commit 97532aa
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 86 deletions.
4 changes: 2 additions & 2 deletions pkg/cmd/render/bootstrap_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func RoutableAddresses(vips []net.IP, af []AddressFilter, rf RouteFilter) ([]net
if err != nil {
return nil, err
}
return getRoutableAddresses(addrMap, routeMap, vips, af, rf)
return routeableAddresses(addrMap, routeMap, vips, af, rf)
}

// AddressesRouting takes a slice of Virtual IPs and returns a slice of configured addresses in the current network namespace that directly route to those vips. You can optionally pass an AddressFilter and/or RouteFilter to further filter down which addresses are considered
func getRoutableAddresses(addrMap addrMap, routeMap routeMap, vips []net.IP, af []AddressFilter, rf RouteFilter) ([]net.IP, error) {
func routeableAddresses(addrMap addrMap, routeMap routeMap, vips []net.IP, af []AddressFilter, rf RouteFilter) ([]net.IP, error) {
matches := make([]net.IP, 0)
for link, addresses := range addrMap {
for _, address := range addresses {
Expand Down
10 changes: 6 additions & 4 deletions pkg/cmd/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,17 @@ func newTemplateData(opts *renderOpts) (*TemplateData, error) {

network, err := getNetwork(opts.networkConfigFile)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get network config: %w", err)
}

infra, err := getInfrastructure(opts.infraConfigFile)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get infrastructure config: %w", err)
}

clusterConfigMap, err := getUnstructured(opts.clusterConfigMapFile)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get cluster configmap: %w", err)
}

for _, network := range network.Spec.ClusterNetwork {
Expand Down Expand Up @@ -304,6 +304,8 @@ func (t *TemplateData) setBootstrapIP(machineCIDR string, ipv6 bool) error {
return nil
}

var getRoutableAddresses = RoutableAddresses

func getBootstrapIP(ipv6 bool, machineCIDR string) (net.IP, error) {
ips, err := ipAddrs()
if err != nil {
Expand All @@ -317,7 +319,7 @@ func getBootstrapIP(ipv6 bool, machineCIDR string) (net.IP, error) {
// TODO: If the machine CIDR isn't required, what's the safe way to proceed?
klog.Warning("No machine CIDR is available; bootstrap IP detection may be unreliable")
}
filtered, err := RoutableAddresses(ips, addrFilters, NonDefaultRoute)
filtered, err := getRoutableAddresses(ips, addrFilters, NonDefaultRoute)
if err != nil {
return nil, err
}
Expand Down
178 changes: 98 additions & 80 deletions pkg/cmd/render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"bytes"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"testing"

configv1 "github.com/openshift/api/config/v1"
"github.com/openshift/cluster-etcd-operator/pkg/cmd/render/options"
)

Expand Down Expand Up @@ -114,16 +114,76 @@ status:
region: us-east-1
type: AWS
`

clusterConfigMap = `
apiVersion: v1
kind: ConfigMap
metadata:
name: cluster-config-v1
namespace: kube-system
data:
install-config: |
apiVersion: v1
baseDomain: gcp.devcluster.openshift.com
compute:
- architecture: amd64
hyperthreading: Enabled
name: worker
platform: {}
replicas: 3
controlPlane:
architecture: amd64
hyperthreading: Enabled
name: master
platform:
gcp:
osDisk:
DiskSizeGB: 128
DiskType: pd-ssd
type: n1-standard-4
zones:
- us-east1-b
- us-east1-c
- us-east1-d
replicas: 3
metadata:
creationTimestamp: null
name: my-cluster
networking:
clusterNetwork:
- cidr: 10.128.0.0/14
hostPrefix: 23
machineCIDR: 10.0.0.0/16
machineNetwork:
- cidr: 10.0.0.0/16
networkType: OpenShiftSDN
serviceNetwork:
- 172.30.0.0/16
platform:
gcp:
projectID: openshift
region: us-east1
publish: External
`
)

type testConfig struct {
t *testing.T
clusterNetworkConfig string
infraConfig string
clusterConfigMap string
want TemplateData
bootstrapIP string
}

func TestMain(m *testing.M) {
// TODO: implement tests for bootstrap IP determination
getRoutableAddresses = func(vips []net.IP, af []AddressFilter, rf RouteFilter) ([]net.IP, error) {
return []net.IP{net.ParseIP("10.0.0.1")}, nil
}
os.Exit(m.Run())
}

func TestRenderIpv4(t *testing.T) {
want := TemplateData{
ManifestConfig: options.ManifestConfig{
Expand All @@ -140,6 +200,7 @@ func TestRenderIpv4(t *testing.T) {
t: t,
clusterNetworkConfig: networkConfigIpv4,
infraConfig: infraConfig,
clusterConfigMap: clusterConfigMap,
want: want,
}

Expand All @@ -166,6 +227,12 @@ func testRender(tc *testConfig) {
}
defer infraConfigFile.Close()

clusterConfigMapFile, err := ioutil.TempFile(dir, "cluster-config.*.yaml")
if err != nil {
tc.t.Fatal(err)
}
defer clusterConfigMapFile.Close()

if err := writeFile(tc.clusterNetworkConfig, clusterConfigFile); err != nil {
tc.t.Fatal(err)
}
Expand All @@ -174,6 +241,10 @@ func testRender(tc *testConfig) {
tc.t.Fatal(err)
}

if err := writeFile(tc.clusterConfigMap, clusterConfigMapFile); err != nil {
tc.t.Fatal(err)
}

generic := options.GenericOptions{
AssetInputDir: dir,
AssetOutputDir: dir,
Expand All @@ -182,11 +253,12 @@ func testRender(tc *testConfig) {
}

render := renderOpts{
generic: generic,
manifest: *options.NewManifestOptions("etcd"),
errOut: errOut,
clusterConfigFile: clusterConfigFile.Name(),
infraConfigFile: infraConfigFile.Name(),
generic: generic,
manifest: *options.NewManifestOptions("etcd"),
errOut: errOut,
networkConfigFile: clusterConfigFile.Name(),
infraConfigFile: infraConfigFile.Name(),
clusterConfigMapFile: clusterConfigMapFile.Name(),
}

if err := render.Run(); err != nil {
Expand All @@ -210,6 +282,7 @@ func TestTemplateDataIpv4(t *testing.T) {
t: t,
clusterNetworkConfig: networkConfigIpv4,
infraConfig: infraConfig,
clusterConfigMap: clusterConfigMap,
want: want,
}
testTemplateData(config)
Expand All @@ -231,6 +304,7 @@ func TestTemplateDataMixed(t *testing.T) {
t: t,
clusterNetworkConfig: networkConfigMixedSwap,
infraConfig: infraConfig,
clusterConfigMap: clusterConfigMap,
want: want,
}
testTemplateData(config)
Expand All @@ -253,6 +327,7 @@ func TestTemplateDataSingleStack(t *testing.T) {
t: t,
clusterNetworkConfig: networkConfigIPv6SingleStack,
infraConfig: infraConfig,
clusterConfigMap: clusterConfigMap,
want: want,
bootstrapIP: "2001:0DB8:C21A",
}
Expand All @@ -279,6 +354,12 @@ func testTemplateData(tc *testConfig) {
}
defer infraConfigFile.Close()

clusterConfigMapFile, err := ioutil.TempFile(dir, "cluster-config.*.yaml")
if err != nil {
tc.t.Fatal(err)
}
defer clusterConfigMapFile.Close()

if err := writeFile(tc.clusterNetworkConfig, clusterConfigFile); err != nil {
tc.t.Fatal(err)
}
Expand All @@ -287,6 +368,10 @@ func testTemplateData(tc *testConfig) {
tc.t.Fatal(err)
}

if err := writeFile(tc.clusterConfigMap, clusterConfigMapFile); err != nil {
tc.t.Fatal(err)
}

generic := options.GenericOptions{
AssetInputDir: dir,
AssetOutputDir: dir,
Expand All @@ -295,12 +380,13 @@ func testTemplateData(tc *testConfig) {
}

render := &renderOpts{
generic: generic,
manifest: *options.NewManifestOptions("etcd"),
errOut: errOut,
clusterConfigFile: clusterConfigFile.Name(),
infraConfigFile: infraConfigFile.Name(),
bootstrapIP: tc.bootstrapIP,
generic: generic,
manifest: *options.NewManifestOptions("etcd"),
errOut: errOut,
networkConfigFile: clusterConfigFile.Name(),
infraConfigFile: infraConfigFile.Name(),
clusterConfigMapFile: clusterConfigMapFile.Name(),
bootstrapIP: tc.bootstrapIP,
}

got, err := newTemplateData(render)
Expand Down Expand Up @@ -337,71 +423,3 @@ func writeFile(input string, w io.Writer) error {
}
return nil
}

func TestTemplateData_setPlatform(t1 *testing.T) {
tests := []struct {
name string
infraSpec string
wantErr bool
wantPlatform configv1.PlatformType
}{
{
name: "test infra config file with AWS",
infraSpec: `apiVersion: config.openshift.io/v1
kind: Infrastructure
metadata:
name: cluster
spec:
cloudConfig:
name: ""
status:
platform: AWS
platformStatus:
aws:
region: us-east-1
type: AWS
`,
wantErr: false,
wantPlatform: configv1.AWSPlatformType,
},
{
name: "test infra config file with empty platform",
infraSpec: `apiVersion: config.openshift.io/v1
kind: Infrastructure
metadata:
name: cluster
spec:
cloudConfig:
name: ""
status:
platform: ""
platformStatus:
type: ""
`,
wantErr: false,
wantPlatform: "",
},
}
for _, tt := range tests {
t1.Run(tt.name, func(t1 *testing.T) {
file, err := ioutil.TempFile("/tmp", "infra-config-file")
if err != nil {
t1.Fatal(err)
}

err = ioutil.WriteFile(file.Name(), []byte(tt.infraSpec), os.ModePerm)
if err != nil {
t1.Fatal(err)
}

t := &TemplateData{}
if err := t.setPlatform(file.Name()); (err != nil) != tt.wantErr {
t1.Errorf("setPlatform() error = %v, wantErr %v", err, tt.wantErr)
}
if t.Platform != string(tt.wantPlatform) {
t1.Errorf("setPlatform() want = %v, got %v", tt.wantPlatform, t.Platform)
}
os.Remove(file.Name())
})
}
}

0 comments on commit 97532aa

Please sign in to comment.