Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kubedns unit tests #2

Draft
wants to merge 2 commits into
base: fork-master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions pkg/model/components/kubedns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2021 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 components

import (
"strconv"
"testing"

kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/pkg/assets"
)

func buildKubeDNSCluster() *kopsapi.Cluster {
return &kopsapi.Cluster{
Spec: kopsapi.ClusterSpec{
CloudProvider: "aws",
KubernetesVersion: "1.20.5",
ServiceClusterIPRange: "10.135.128.0/17",
KubeDNS: &kopsapi.KubeDNSConfig{
NodeLocalDNS: &kopsapi.NodeLocalDNSConfig{},
},
},
}
}

func Test_ForwardToKubeDNS_Is_False_When_NodeLocalDNS_Is_Enabled(t *testing.T) {
c := buildKubeDNSCluster()
c.Spec.KubeDNS.NodeLocalDNS.Enabled = fi.Bool(true)

b := assets.NewAssetBuilder(c, "")

ob := &KubeDnsOptionsBuilder{
&OptionsContext{
AssetBuilder: b,
},
}

err := ob.BuildOptions(&c.Spec)
if err != nil {
t.Fatalf("Error while building KubeDNS options: %v", err)
}

if !fi.BoolValue(c.Spec.KubeDNS.NodeLocalDNS.Enabled) {
t.Fatalf("NodeLocalDNS is not enabled.")
}

if fi.BoolValue(c.Spec.KubeDNS.NodeLocalDNS.ForwardToKubeDNS) {
t.Fatalf("ForwardToKubeDNS is enabled.")
}

}

func Test_ForwardToKubeDNS_Is_Nil_When_NodeLocalDNS_Is_Disabled(t *testing.T) {
c := buildKubeDNSCluster()

b := assets.NewAssetBuilder(c, "")

ob := &KubeDnsOptionsBuilder{
&OptionsContext{
AssetBuilder: b,
},
}

err := ob.BuildOptions(&c.Spec)
if err != nil {
t.Fatalf("Error while building KubeDNS options: %v", err)
}

if fi.BoolValue(c.Spec.KubeDNS.NodeLocalDNS.Enabled) {
t.Fatalf("NodeLocalDNS is enabled.")
}

if c.Spec.KubeDNS.NodeLocalDNS.ForwardToKubeDNS != nil {
t.Fatalf("ForwardToKubeDNS is set to %s", strconv.FormatBool(fi.BoolValue(c.Spec.KubeDNS.NodeLocalDNS.ForwardToKubeDNS)))
}
}