Skip to content

Commit

Permalink
chore: use mocks for providers
Browse files Browse the repository at this point in the history
Fix Mellanox#791

Signed-off-by: Fred Rolland <[email protected]>
  • Loading branch information
rollandf committed Feb 6, 2024
1 parent 8826838 commit df65c50
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 49 deletions.
4 changes: 4 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
inpackage: False
testonly: False
with-expecter: True
keeptree: True
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ $(SKAFFOLD): | $(TOOLSDIR)
$Q curl -fsSL https://storage.googleapis.com/skaffold/releases/latest/skaffold-$(OS)-$(ARCH) -o $(SKAFFOLD)
$Q chmod +x $(SKAFFOLD)

# mockery is used to generate mocks for unit tests.
MOCKERY_PKG := github.com/vektra/mockery/v2
MOCKERY_VER := v2.32.2
MOCKERY_BIN := mockery
MOCKERY_PATH = $(abspath $(TOOLSDIR)/$(MOCKERY_BIN))
MOCKERY = $(abspath $(TOOLSDIR)/$(MOCKERY_BIN))-$(MOCKERY_VER)
$(MOCKERY): | $(TOOLSDIR)
$(call go-install-tool,$(MOCKERY_PKG),$(MOCKERY_BIN),$(MOCKERY_VER))
$Q ln $(MOCKERY) $(MOCKERY_PATH)

# Tests

.PHONY: lint
Expand Down Expand Up @@ -251,6 +261,10 @@ test-coverage: COVERAGE_DIR := $(CURDIR)/test
test-coverage: setup-envtest; $(info running coverage tests...) @ ## Run coverage tests
KUBEBUILDER_ASSETS=`$(SETUP_ENVTEST) use --use-env -p path $(ENVTEST_K8S_VERSION)` $(GO) test -covermode=$(COVERAGE_MODE) -coverpkg=./... -coverprofile=network-operator.cover $(TESTPKGS)

.PHONY: generate-mocks
generate-mocks: | $(MOCKERY) ; $(info running mockery...) @ ## Run mockery
$Q PATH=$(TOOLSDIR):$(PATH) go generate ./...

# Container image
.PHONY: image
image: ; $(info Building Docker image...) @ ## Build container image
Expand Down
92 changes: 92 additions & 0 deletions pkg/nodeinfo/mocks/Provider.go

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

2 changes: 2 additions & 0 deletions pkg/nodeinfo/node_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var MellanoxNICListOptions = []client.ListOption{
client.MatchingLabels{NodeLabelMlnxNIC: "true"}}

// Provider provides Node attributes
//
//go:generate mockery --name Provider
type Provider interface {
// GetNodesAttributes retrieves node attributes for nodes matching the filter criteria
GetNodesAttributes(filters ...Filter) []NodeAttributes
Expand Down
48 changes: 8 additions & 40 deletions pkg/state/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,19 @@ limitations under the License.
package state_test

import (
"github.com/Mellanox/network-operator/pkg/clustertype"
"github.com/Mellanox/network-operator/pkg/nodeinfo"
clustertype_mocks "github.com/Mellanox/network-operator/pkg/clustertype/mocks"
"github.com/Mellanox/network-operator/pkg/state"
"github.com/Mellanox/network-operator/pkg/staticconfig"
staticconfig_mocks "github.com/Mellanox/network-operator/pkg/staticconfig/mocks"
)

type testProvider struct {
isOpenshift bool
cniBinDir string
}

func (tp *testProvider) GetClusterType() clustertype.Type {
if tp.isOpenshift {
return clustertype.Openshift
}
return clustertype.Kubernetes
}

func (tp *testProvider) IsKubernetes() bool {
return !tp.isOpenshift
}

func (tp *testProvider) IsOpenshift() bool {
return tp.isOpenshift
}

func (tp *testProvider) GetStaticConfig() staticconfig.StaticConfig {
return staticconfig.StaticConfig{CniBinDirectory: tp.cniBinDir}
}

func (tp *testProvider) GetNodesAttributes(...nodeinfo.Filter) []nodeinfo.NodeAttributes {
nodeAttr := make(map[nodeinfo.AttributeType]string)
nodeAttr[nodeinfo.AttrTypeCPUArch] = "amd64"
nodeAttr[nodeinfo.AttrTypeOSName] = "ubuntu"
nodeAttr[nodeinfo.AttrTypeOSVer] = "20.04"

return []nodeinfo.NodeAttributes{{Attributes: nodeAttr}}
}

func getTestCatalog() state.InfoCatalog {
catalog := state.NewInfoCatalog()
tp := &testProvider{isOpenshift: false, cniBinDir: ""}
catalog.Add(state.InfoTypeNodeInfo, tp)
catalog.Add(state.InfoTypeStaticConfig, tp)
catalog.Add(state.InfoTypeClusterType, tp)

clusterTypeProvider := clustertype_mocks.Provider{}
clusterTypeProvider.On("IsOpenshift").Return(false)
staticConfigProvider := staticconfig_mocks.Provider{}
staticConfigProvider.On("GetStaticConfig").Return(staticconfig.StaticConfig{CniBinDirectory: ""})
catalog.Add(state.InfoTypeStaticConfig, &staticConfigProvider)
catalog.Add(state.InfoTypeClusterType, &clusterTypeProvider)
return catalog
}
31 changes: 22 additions & 9 deletions pkg/state/state_cni_plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ import (

mellanoxv1alpha1 "github.com/Mellanox/network-operator/api/v1alpha1"

clustertype_mocks "github.com/Mellanox/network-operator/pkg/clustertype/mocks"
"github.com/Mellanox/network-operator/pkg/config"
"github.com/Mellanox/network-operator/pkg/state"
"github.com/Mellanox/network-operator/pkg/staticconfig"
staticconfig_mocks "github.com/Mellanox/network-operator/pkg/staticconfig/mocks"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -96,10 +99,15 @@ var _ = Describe("CNI plugins state", func() {
It("should create Daemonset with user specified CNI bin dir", func() {
By("Sync")
cr := getMinimalNicClusterPolicyWithCNIPlugins()
testCniBinDIr := "/opt/mydir/cni"
catalog = state.NewInfoCatalog()
catalog.Add(state.InfoTypeClusterType, &testProvider{isOpenshift: false})
catalog.Add(state.InfoTypeStaticConfig, &testProvider{cniBinDir: testCniBinDIr})
testCniBinDir := "/opt/mydir/cni"
catalog := state.NewInfoCatalog()
clusterTypeProvider := clustertype_mocks.Provider{}
clusterTypeProvider.On("IsOpenshift").Return(false)
staticConfigProvider := staticconfig_mocks.Provider{}
staticConfigProvider.On("GetStaticConfig").Return(staticconfig.StaticConfig{CniBinDirectory: testCniBinDir})
catalog.Add(state.InfoTypeStaticConfig, &staticConfigProvider)
catalog.Add(state.InfoTypeClusterType, &clusterTypeProvider)

status, err := cniPluginsState.Sync(context.Background(), cr, catalog)
Expect(err).NotTo(HaveOccurred())
Expect(status).To(BeEquivalentTo(state.SyncStateNotReady))
Expand All @@ -108,7 +116,7 @@ var _ = Describe("CNI plugins state", func() {
err = client.Get(context.Background(), types.NamespacedName{Namespace: namespace, Name: "cni-plugins-ds"}, ds)
Expect(err).NotTo(HaveOccurred())
expectedDs := getExpectedMinimalCniPluginDS()
expectedDs.Spec.Template.Spec.Volumes[0].VolumeSource.HostPath.Path = testCniBinDIr
expectedDs.Spec.Template.Spec.Volumes[0].VolumeSource.HostPath.Path = testCniBinDir
By("Verify CNI bin dir")
Expect(ds.Spec).To(BeEquivalentTo(expectedDs.Spec))
})
Expand All @@ -117,8 +125,12 @@ var _ = Describe("CNI plugins state", func() {
By("Sync")
cr := getMinimalNicClusterPolicyWithCNIPlugins()
catalog = state.NewInfoCatalog()
catalog.Add(state.InfoTypeClusterType, &testProvider{isOpenshift: true})
catalog.Add(state.InfoTypeStaticConfig, &testProvider{cniBinDir: ""})
clusterTypeProvider := clustertype_mocks.Provider{}
clusterTypeProvider.On("IsOpenshift").Return(true)
staticConfigProvider := staticconfig_mocks.Provider{}
staticConfigProvider.On("GetStaticConfig").Return(staticconfig.StaticConfig{CniBinDirectory: ""})
catalog.Add(state.InfoTypeStaticConfig, &staticConfigProvider)
catalog.Add(state.InfoTypeClusterType, &clusterTypeProvider)
status, err := cniPluginsState.Sync(context.Background(), cr, catalog)
Expect(err).NotTo(HaveOccurred())
Expect(status).To(BeEquivalentTo(state.SyncStateNotReady))
Expand Down Expand Up @@ -265,8 +277,9 @@ var _ = Describe("CNI plugins state", func() {
It("should fail if clustertype provider not set in catalog", func() {
By("Sync")
catalog := state.NewInfoCatalog()
tp := &testProvider{isOpenshift: false, cniBinDir: ""}
catalog.Add(state.InfoTypeStaticConfig, tp)
staticConfigProvider := staticconfig_mocks.Provider{}
staticConfigProvider.On("GetStaticConfig").Return(staticconfig.StaticConfig{CniBinDirectory: ""})
catalog.Add(state.InfoTypeStaticConfig, &staticConfigProvider)
cr := getMinimalNicClusterPolicyWithCNIPlugins()
status, err := cniPluginsState.Sync(context.Background(), cr, catalog)
Expect(err).To(HaveOccurred())
Expand Down
76 changes: 76 additions & 0 deletions pkg/staticconfig/mocks/Provider.go

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

2 changes: 2 additions & 0 deletions pkg/staticconfig/static_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type StaticConfig struct {
}

// Provider provides static cluster attributes
//
//go:generate mockery --name Provider
type Provider interface {
GetStaticConfig() StaticConfig
}
Expand Down

0 comments on commit df65c50

Please sign in to comment.