Skip to content

Commit

Permalink
DNS package test coverage (3/3) (#635)
Browse files Browse the repository at this point in the history
This is a follow-up PR to #630 (1/3), do not review until #630 is merged.

Thanks to the ntroducing mocks I can test what is going on inside the large functions in the DNS package.
I have written several tests that create/update the DNS record and assert values at runtime (not just asserting the error the function returns).
I managed to achieve 62% coverage. The uncovered are mostly constructor functions, single-line functions that call the assistant package directly (this is more about testing the assistant package).

Signed-off-by: kuritka <[email protected]>
  • Loading branch information
kuritka authored Oct 4, 2021
1 parent bc71033 commit bb58a72
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 56 deletions.
10 changes: 8 additions & 2 deletions controllers/providers/dns/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package dns
import (
"fmt"
"io/ioutil"
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -111,10 +112,15 @@ func TestCreateZoneDelegationOnExternalDNS(t *testing.T) {
m := assistant.NewMockAssistant(ctrl)
p := NewExternalDNS(dnsType, a.Config, m)
m.EXPECT().GslbIngressExposedIPs(a.Gslb).Return(a.TargetIPs, nil).Times(1)
m.EXPECT().SaveDNSEndpoint(a.Config.K8gbNamespace, gomock.Eq(expectedDNSEndpoint)).Return(nil).Times(1)
m.EXPECT().SaveDNSEndpoint(a.Config.K8gbNamespace, gomock.Eq(expectedDNSEndpoint)).Return(nil).Times(1).
Do(func(ns string, ep *externaldns.DNSEndpoint) {
require.True(t, reflect.DeepEqual(ep, expectedDNSEndpoint))
require.Equal(t, ns, a.Config.K8gbNamespace)
})

// act, assert
// act
err := p.CreateZoneDelegationForExternalDNS(a.Gslb)
// assert
assert.NoError(t, err)
}

Expand Down
10 changes: 5 additions & 5 deletions controllers/providers/dns/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
func TestFactoryInfoblox(t *testing.T) {
// arrange
client := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects([]runtime.Object{}...).Build()
customConfig := predefinedConfig
customConfig := defaultConfig
customConfig.EdgeDNSType = depresolver.DNSTypeInfoblox
// act
f, err := NewDNSProviderFactory(client, customConfig)
Expand All @@ -48,7 +48,7 @@ func TestFactoryInfoblox(t *testing.T) {
func TestFactoryNS1(t *testing.T) {
// arrange
client := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects([]runtime.Object{}...).Build()
customConfig := predefinedConfig
customConfig := defaultConfig
customConfig.EdgeDNSType = depresolver.DNSTypeNS1
// act
f, err := NewDNSProviderFactory(client, customConfig)
Expand All @@ -63,7 +63,7 @@ func TestFactoryNS1(t *testing.T) {
func TestFactoryRoute53(t *testing.T) {
// arrange
client := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects([]runtime.Object{}...).Build()
customConfig := predefinedConfig
customConfig := defaultConfig
customConfig.EdgeDNSType = depresolver.DNSTypeRoute53
// act
f, err := NewDNSProviderFactory(client, customConfig)
Expand All @@ -78,7 +78,7 @@ func TestFactoryRoute53(t *testing.T) {
func TestFactoryNoEdgeDNS(t *testing.T) {
// arrange
client := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects([]runtime.Object{}...).Build()
customConfig := predefinedConfig
customConfig := defaultConfig
customConfig.EdgeDNSType = depresolver.DNSTypeNoEdgeDNS
// act
f, err := NewDNSProviderFactory(client, customConfig)
Expand All @@ -91,7 +91,7 @@ func TestFactoryNoEdgeDNS(t *testing.T) {

func TestFactoryNilClient(t *testing.T) {
// arrange
customConfig := predefinedConfig
customConfig := defaultConfig
customConfig.EdgeDNSType = depresolver.DNSTypeNoEdgeDNS
// act
// assert
Expand Down
195 changes: 146 additions & 49 deletions controllers/providers/dns/infoblox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,58 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
package dns

import (
"os"
"testing"

k8gbv1beta1 "github.com/AbsaOSS/k8gb/api/v1beta1"

"github.com/golang/mock/gomock"
ibclient "github.com/infobloxopen/infoblox-go-client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

k8gbv1beta1 "github.com/AbsaOSS/k8gb/api/v1beta1"
"github.com/AbsaOSS/k8gb/controllers/depresolver"
"github.com/AbsaOSS/k8gb/controllers/internal/utils"
"github.com/AbsaOSS/k8gb/controllers/providers/assistant"
)

ibclient "github.com/infobloxopen/infoblox-go-client"
"github.com/stretchr/testify/assert"
const (
ref = "zone_delegated/ZG5zLnpvbmUkLl9kZWZhdWx0LnphLmNvLmFic2EuY2Fhcy5vaG15Z2xiLmdzbGJpYmNsaWVudA:cloud.example.com/default"
namespace = "test-gslb"
gslbName = "test-gslb"
)

var predefinedConfig = depresolver.Config{
ReconcileRequeueSeconds: 30,
ClusterGeoTag: "us-west-1",
ExtClustersGeoTags: []string{"us-east-1"},
EdgeDNSServers: []utils.DNSServer{
{
Host: "8.8.8.8",
Port: 53,
var (
defaultConfig = depresolver.Config{
ReconcileRequeueSeconds: 30,
ClusterGeoTag: "us-west-1",
ExtClustersGeoTags: []string{"us-east-1"},
EdgeDNSServers: []utils.DNSServer{
{
Host: "8.8.8.8",
Port: 53,
},
},
},
EdgeDNSZone: "example.com",
DNSZone: "cloud.example.com",
K8gbNamespace: "k8gb",
Infoblox: depresolver.Infoblox{
Host: "fakeinfoblox.example.com",
Username: "foo",
Password: "blah",
Port: 443,
Version: "0.0.0",
},
}
EdgeDNSZone: "example.com",
DNSZone: "cloud.example.com",
K8gbNamespace: "k8gb",
Infoblox: depresolver.Infoblox{
Host: "fakeinfoblox.example.com",
Username: "foo",
Password: "blah",
Port: 443,
Version: "0.0.0",
},
}

defaultDelegatedZone = ibclient.ZoneDelegated{
Fqdn: defaultConfig.DNSZone,
DelegateTo: []ibclient.NameServer{},
Ref: ref,
}

defaultGslb = new(k8gbv1beta1.Gslb)
ipRange = []string{"10.0.0.1", "10.0.0.2"}
)

func TestCanFilterOutDelegatedZoneEntryAccordingFQDNProvided(t *testing.T) {
// arrange
Expand All @@ -69,7 +86,7 @@ func TestCanFilterOutDelegatedZoneEntryAccordingFQDNProvided(t *testing.T) {
{Address: "10.0.0.2", Name: "gslb-ns-eu-cloud.example.com"},
{Address: "10.0.0.3", Name: "gslb-ns-eu-cloud.example.com"},
}
customConfig := predefinedConfig
customConfig := defaultConfig
customConfig.EdgeDNSZone = "example.com"
customConfig.ExtClustersGeoTags = []string{"za"}
a := assistant.NewGslbAssistant(nil, customConfig.K8gbNamespace, customConfig.EdgeDNSServers)
Expand Down Expand Up @@ -106,7 +123,7 @@ func TestCanSanitizeDelegatedZone(t *testing.T) {
{Address: "10.1.0.2", Name: "gslb-ns-za-cloud.example.com"},
{Address: "10.1.0.3", Name: "gslb-ns-za-cloud.example.com"},
}
customConfig := predefinedConfig
customConfig := defaultConfig
customConfig.EdgeDNSZone = "example.com"
customConfig.ExtClustersGeoTags = []string{"za"}
customConfig.ClusterGeoTag = "eu"
Expand Down Expand Up @@ -142,47 +159,127 @@ func TestSortNameServer(t *testing.T) {
assert.Equal(t, want, delegateTo, "got:\n %q \n\n want:\n %q", delegateTo, want)
}

func TestCreateZoneDelegationForExternalDNS(t *testing.T) {
func TestInfobloxCreateZoneDelegationForExternalDNS(t *testing.T) {
// arrange
const ret = "zone_delegated/ZG5zLnpvbmUkLl9kZWZhdWx0LnphLmNvLmFic2EuY2Fhcy5vaG15Z2xiLmdzbGJpYmNsaWVudA:cloud.example.com/default"
gslb := new(k8gbv1beta1.Gslb)
gslb.Name = "test-gslb"
gslb.Namespace = "test-gslb"
zone := ibclient.ZoneDelegated{
Fqdn: predefinedConfig.DNSZone,
DelegateTo: []ibclient.NameServer{},
Ref: ret,
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
a := assistant.NewMockAssistant(ctrl)
mclient := NewMockInfobloxClient(ctrl)
mcon := NewMockIBConnector(ctrl)
a.EXPECT().GslbIngressExposedIPs(gomock.Any()).Return([]string{"10.0.0.1", "10.0.0.2"}, nil).Times(1)
mcon.EXPECT().CreateObject(gomock.Any()).Return(ret, nil).AnyTimes()
mcon.EXPECT().DeleteObject(gomock.Any()).Return(ret, nil).AnyTimes()
mcon.EXPECT().UpdateObject(gomock.Any(), gomock.Any()).Return(ret, nil).AnyTimes()
mcon.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(2, []ibclient.ZoneDelegated{zone}).Return(nil)
mclient.EXPECT().GetObjectManager().Return(ibclient.NewObjectManager(mcon, "k8gbclient", ""), nil).Times(1)
config := predefinedConfig
provider := NewInfobloxDNS(config, a, mclient)
cl := NewMockInfobloxClient(ctrl)
con := NewMockIBConnector(ctrl)
a.EXPECT().GslbIngressExposedIPs(gomock.Any()).Return(ipRange, nil).Times(1)
con.EXPECT().CreateObject(gomock.Any()).Return(ref, nil).AnyTimes()
con.EXPECT().UpdateObject(gomock.Any(), gomock.Any()).Return(ref, nil).Times(1)
con.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(2, []ibclient.ZoneDelegated{defaultDelegatedZone}).Return(nil)
cl.EXPECT().GetObjectManager().Return(ibclient.NewObjectManager(con, "k8gbclient", ""), nil).Times(1)
config := defaultConfig
provider := NewInfobloxDNS(config, a, cl)

// act
err := provider.CreateZoneDelegationForExternalDNS(gslb)
err := provider.CreateZoneDelegationForExternalDNS(defaultGslb)
// assert
assert.NoError(t, err)
}

func TestInfobloxCreateZoneDelegationForExternalDNSWithSplitBrainEnabled(t *testing.T) {
// arrange
ctrl := gomock.NewController(t)
defer ctrl.Finish()
a := assistant.NewMockAssistant(ctrl)
cl := NewMockInfobloxClient(ctrl)
con := NewMockIBConnector(ctrl)
a.EXPECT().GslbIngressExposedIPs(gomock.Any()).Return(ipRange, nil).Times(1)
a.EXPECT().InspectTXTThreshold(gomock.Any(), gomock.Any()).Do(func(fqdn string, arg1 interface{}) {
require.Equal(t, "test-gslb-heartbeat-us-east-1.example.com", fqdn)
}).Return(nil).Times(1)
con.EXPECT().CreateObject(gomock.Any()).Return(ref, nil).AnyTimes()
con.EXPECT().UpdateObject(gomock.Any(), gomock.Any()).Return(ref, nil).Times(2)
con.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(2, []ibclient.ZoneDelegated{defaultDelegatedZone}).Return(nil)
cl.EXPECT().GetObjectManager().Return(ibclient.NewObjectManager(con, "k8gbclient", ""), nil).Times(1)
con.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(2, []ibclient.RecordTXT{{Ref: ref}}).
Return(nil).Do(func(arg0 *ibclient.RecordTXT, arg1, arg2 interface{}) {
require.Equal(t, "test-gslb-heartbeat-us-west-1.example.com", arg0.Name)
}).AnyTimes()
config := defaultConfig
config.SplitBrainCheck = true
provider := NewInfobloxDNS(config, a, cl)

// act
err := provider.CreateZoneDelegationForExternalDNS(defaultGslb)
// assert
assert.NoError(t, err)
}

func TestInfobloxCreateZoneDelegationForExternalDNSWithSplitBrainEnabledCreatingNewHeartBeatRecord(t *testing.T) {
// arrange
ctrl := gomock.NewController(t)
defer ctrl.Finish()
a := assistant.NewMockAssistant(ctrl)
cl := NewMockInfobloxClient(ctrl)
con := NewMockIBConnector(ctrl)
a.EXPECT().GslbIngressExposedIPs(gomock.Any()).Return(ipRange, nil).Times(1)
a.EXPECT().InspectTXTThreshold(gomock.Any(), gomock.Any()).Return(nil).Times(1)
con.EXPECT().CreateObject(gomock.Any()).Return(ref, nil).AnyTimes()
con.EXPECT().UpdateObject(gomock.Any(), gomock.Any()).Return(ref, nil).Times(1)
con.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(2, []ibclient.ZoneDelegated{defaultDelegatedZone}).Return(nil)
cl.EXPECT().GetObjectManager().Return(ibclient.NewObjectManager(con, "k8gbclient", ""), nil).Times(1)
con.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(2, []ibclient.RecordTXT{}).Return(nil).AnyTimes()
config := defaultConfig
config.SplitBrainCheck = true
provider := NewInfobloxDNS(config, a, cl)

// act
err := provider.CreateZoneDelegationForExternalDNS(defaultGslb)
// assert
assert.NoError(t, err)
}

func TestInfobloxFinalize(t *testing.T) {
// arrange
ctrl := gomock.NewController(t)
defer ctrl.Finish()
a := assistant.NewMockAssistant(ctrl)
cl := NewMockInfobloxClient(ctrl)
con := NewMockIBConnector(ctrl)
con.EXPECT().DeleteObject(gomock.Any()).Return(ref, nil).Do(func(arg0 string) {
require.Equal(t, arg0, ref)
}).AnyTimes()
con.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(2, []ibclient.ZoneDelegated{defaultDelegatedZone}).
Return(nil).Times(1)
con.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(2, []ibclient.RecordTXT{{Ref: ref}}).
Return(nil).Do(func(arg0 *ibclient.RecordTXT, arg1, arg2 interface{}) {
require.Equal(t, "test-gslb-heartbeat-us-west-1.example.com", arg0.Name)
}).Times(1)
cl.EXPECT().GetObjectManager().Return(ibclient.NewObjectManager(con, "k8gbclient", ""), nil).Times(1)
config := defaultConfig
provider := NewInfobloxDNS(config, a, cl)

// act
err := provider.Finalize(defaultGslb)

// assert
assert.NoError(t, err)
}

func TestEmptySort(t *testing.T) {
// arrange
delegateTo := make([]ibclient.NameServer, 0)

// act
sortZones(delegateTo)
// assert
assert.Equal(t, 0, len(delegateTo))
}

func TestNilSort(t *testing.T) {
// arrange
delegateTo := []ibclient.NameServer(nil)

// act
sortZones(delegateTo)
// assert
assert.Nil(t, delegateTo)
}

func TestMain(m *testing.M) {
defaultGslb.Name = gslbName
defaultGslb.Namespace = namespace
os.Exit(m.Run())
}

0 comments on commit bb58a72

Please sign in to comment.