Skip to content

Commit

Permalink
Mock DNS package, extend testing (#630)
Browse files Browse the repository at this point in the history
decided to expand the tests for the DNS package (the current coverage is only 20%).

Consequence:
- I had to split the creation of ObjectManager and infoblox provider
- FakeInfoblox client is no longer needed - we have mocks (cleaner design separates test code from execution code)
- I don't need to keep the Overrides.FakeInfoblox switch in the config - I'll remove it in the next PR
- We can test more - in the next PR I will extend the DNS package testing
- I moved functions `checkZoneDelegated`, `filterOutDelegateTo` from `ib-client.go` to `infoblox-client.go`.
  IBX provider constructor accepts ObjectManager interface now so I had to extend factory and fix several tests.

To test the change, I created one test `TestCreateZoneDelegationForExternalDNS` and generated the necessary mocks.

We currently have around 40% coverage but as said, will add new tests in following PR.

Signed-off-by: kuritka <[email protected]>
  • Loading branch information
kuritka authored Oct 2, 2021
1 parent 3fe5396 commit 8e9d3cb
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 144 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ mocks:
go install github.com/golang/mock/[email protected]
mockgen -source=controllers/providers/assistant/assistant.go -destination=controllers/providers/assistant/assistant_mock.go -package=assistant
mockgen -source=controllers/providers/dns/dns.go -destination=controllers/providers/dns/dns_mock.go -package=dns
mockgen -source=controllers/providers/dns/infoblox-client.go -destination=controllers/providers/dns/infoblox-client_mock.go -package=dns
mockgen -destination=controllers/providers/dns/infoblox-connection_mock.go -package=dns github.com/infobloxopen/infoblox-go-client IBConnector
$(call golic)

# remove clusters and redeploy
Expand Down
1 change: 0 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion controllers/providers/dns/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func (f *ProviderFactory) Provider() Provider {
case depresolver.DNSTypeRoute53:
return NewExternalDNS(externalDNSTypeRoute53, f.config, a)
case depresolver.DNSTypeInfoblox:
return NewInfobloxDNS(f.config, a)
ibx := NewInfobloxClient(f.config)
return NewInfobloxDNS(f.config, a, ibx)
}
return NewEmptyDNS(f.config, a)
}
62 changes: 62 additions & 0 deletions controllers/providers/dns/infoblox-client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2021 The k8gb Contributors.
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.
Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/
package dns

import (
"strconv"

"github.com/AbsaOSS/k8gb/controllers/depresolver"
ibclient "github.com/infobloxopen/infoblox-go-client"
)

type InfobloxClient interface {
GetObjectManager() (objMgr *ibclient.ObjectManager, err error)
}

type Client struct {
objMgr *ibclient.ObjectManager
config depresolver.Config
}

func NewInfobloxClient(config depresolver.Config) *Client {
return &Client{
config: config,
}
}

func (c *Client) GetObjectManager() (objMgr *ibclient.ObjectManager, err error) {
hostConfig := ibclient.HostConfig{
Host: c.config.Infoblox.Host,
Version: c.config.Infoblox.Version,
Port: strconv.Itoa(c.config.Infoblox.Port),
Username: c.config.Infoblox.Username,
Password: c.config.Infoblox.Password,
}
transportConfig := ibclient.NewTransportConfig("false", c.config.Infoblox.HTTPRequestTimeout, c.config.Infoblox.HTTPPoolConnections)
requestBuilder := &ibclient.WapiRequestBuilder{}
requestor := &ibclient.WapiHttpRequestor{}
conn, err := ibclient.NewConnector(hostConfig, transportConfig, requestBuilder, requestor)
if err != nil {
return
}
defer func() {
err = conn.Logout()
}()
c.objMgr = ibclient.NewObjectManager(conn, "k8gbclient", "")
return
}
67 changes: 67 additions & 0 deletions controllers/providers/dns/infoblox-client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions controllers/providers/dns/infoblox-connection_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 0 additions & 54 deletions controllers/providers/dns/infoblox-fake.go

This file was deleted.

Loading

0 comments on commit 8e9d3cb

Please sign in to comment.