Mocks ut faq
How are the mock files for unit tests organized?¶
-
The name of the mock file generated will be the same as the name of the
interfacedefinition. -
Mock files for
interfacesdefined in thego-controller/vendordirectories are located in thego-controller/pkg/testing/mocksdirectory. The directory structure in thego-controller/pkg/testing/mocks/closely mimic the directory structure ofgo-controller/vendor/.e.g; a) The
Cmdinterface defined in thego-controller/vendor/k8s.io/utils/exec.gofile has its mock generated ingo-controller/pkg/testing/mocks/k8s.io/utils/exec/Cmd.gofilee.g; b) The
Linkinterface defined in thego-controller/vendor/github.com/vishvananda/netlink/link.gofile has its mock generated ingo-controller/pkg/testing/mocks/vishvananda/netlink/Link.gofile -
Mock files for
interfacesdefined in the non vendor directories of the project are located in themocksdirectory of the same package as where the interface is defined.e.g; a) The
ExecRunnerinterface defined ingo-controller/pkg/util/ovs.gofile has the its mock generated ingo-controller/pkg/util/mocks/ExecRunner.gofile.e.g; b) The
SriovNetLibOpsinterface defined ingo-controller/pkg/cni/helper_linux.gofile has its mock generated ingo-controller/pkg/cni/mocks/SriovNetLibOps.gofile.
How are the mocks for interfaces to be consumed by unit tests currently generated?¶
-
The vektra/mockery package from https://github.com/vektra/mockery is leveraged to auto generate mocks for defined interfaces.
-
Mocks for interfaces can be generated using vektra/mockery in one of two ways:
-
Using the binaries at https://github.com/vektra/mockery/releases
-
Using the docker image
-
-
Sample commands to generate mocks when using the
binaryinstalled on a linux host.-
Mock for interface
SriovNetLibOpsdefined in thego-controller/pkg/cni/helper_linux.gofile when executing themockerycommand from dirgo-controller/mockery --name SriovnetLibOps --dir pkg/cni/ --output pkg/cni/mocks -
Mock for all interfaces defined in the vendor folder
go-controller/vendor/k8s.io/utils/execwhen executing themockerycommand from dirgo-controller/mockery --all --dir vendor/k8s.io/utils/exec --output pkg/testing/mocks/k8s.io/utils/exec
-
-
Sample command to generate mocks when using the
dockerimage-
Mock for interface
SriovNetLibOpsdefined in thego-controller/pkg/cni/helper_linux.gofile when running thedockercontainer from dirgo-controller/docker run -v $PWD:/src -w /src vektra/mockery --name SriovNetLibOps --dir pkg/cni/ --output pkg/cni/mocks -
Mock for all interfaces defined in the vendor folder
go-controller/vendor/k8s.io/utils/execwhen running thedockercontainer from dirgo-controller/docker run -v $PWD:/src -w /src vektra/mockery --all --dir vendor/k8s.io/utils/exec --output pkg/testing/mocks/k8s.io/utils/exec
-
How to regenerate all existing mocks when interfaces (locally defined or in vendor libraries) are updated?¶
- Execute the ```make mocksgen``` in situations where all existing mocks have to be regenerated.
NOTE: It would take a while(approx 20+ minutes) for all mocks to be regenerated.
Reference links that explain how to use mocks with testify¶
-
https://tutorialedge.net/golang/improving-your-tests-with-testify-go/
-
https://techblog.fexcofts.com/2019/09/23/go-and-test-mocking/
-
https://gowalker.org/github.com/stretchr/testify/mock
-
https://ncona.com/2020/02/using-testify-for-golang-tests/