diff --git a/cmd/main.go b/cmd/main.go index 55626f10..97b5b3ea 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" + "github.com/openshift-kni/commatrix/debug" + clientutil "github.com/openshift-kni/commatrix/client" "github.com/openshift-kni/commatrix/commatrix" @@ -74,7 +76,8 @@ func main() { panic(fmt.Sprintf("Error while writing the endpoint slice matrix to file :%v", err)) } // generate the ss matrix and ss raws - ssMat, ssOutTCP, ssOutUDP, err := commatrix.GenerateSS(cs) + newDebugPod := &debug.NewDebugPod{} + ssMat, ssOutTCP, ssOutUDP, err := commatrix.GenerateSS(cs, newDebugPod) if err != nil { panic(fmt.Sprintf("Error while generating the ss matrix and ss raws :%v", err)) } diff --git a/commatrix/commatrix_test.go b/commatrix/commatrix_test.go index f90d2d13..877c35a4 100644 --- a/commatrix/commatrix_test.go +++ b/commatrix/commatrix_test.go @@ -63,12 +63,16 @@ func TestWriteMatrixToFile(t *testing.T) { } func TestGenerateSS(t *testing.T) { - clientset := fake.NewSimpleClientset() + fakeClientset := fake.NewSimpleClientset() + clientset := &clientutil.ClientSet{ + CoreV1Interface: fakeClientset.CoreV1(), + } ctrlTest := gomock.NewController(t) defer ctrlTest.Finish() mockDebugPod := debug.NewMockDebugPodInterface(ctrlTest) + NewmockDebugPod := debug.NewMockNewDebugPodInterface(ctrlTest) tcpOutput := []byte(`LISTEN 0 4096 127.0.0.1:8797 0.0.0.0:* users:(("machine-config-",pid=3534,fd=3)) LISTEN 0 4096 127.0.0.1:8798 0.0.0.0:* users:(("machine-config-",pid=3534,fd=13)) @@ -98,27 +102,20 @@ UNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:(("pluto",pid=2115,fd=21 ).AnyTimes() mockDebugPod.EXPECT().Clean().Return(nil).AnyTimes() - mockDebugPod.EXPECT().GetNodeName().Return("test-node").AnyTimes() - originalNew := debug.NewDebugPod - debug.NewDebugPod = func(cs *clientutil.ClientSet, node string, namespace string, image string) (debug.DebugPodInterface, error) { - return mockDebugPod, nil - } - defer func() { debug.NewDebugPod = originalNew }() + NewmockDebugPod.EXPECT().New(clientset, "test-node", "openshift-commatrix-debug", "quay.io/openshift-release-dev/ocp-release:4.15.12-multi").Return(mockDebugPod, nil) - cs := &clientutil.ClientSet{ - CoreV1Interface: clientset.CoreV1(), - } testNode := &v1.Node{ ObjectMeta: metav1.ObjectMeta{ Name: "test-node", }, } + _, _ = clientset.CoreV1Interface.Nodes().Create(context.TODO(), testNode, metav1.CreateOptions{}) - _, _ = clientset.CoreV1().Nodes().Create(context.TODO(), testNode, metav1.CreateOptions{}) + ssMat, ssOutTCP, ssOutUDP, err := GenerateSS(clientset, NewmockDebugPod) - ssMat, ssOutTCP, ssOutUDP, err := GenerateSS(cs) + // Pass the expected ClientSet to GenerateSS expectedSSMat := &types.ComMatrix{ Matrix: []types.ComDetails{ {Direction: "Ingress", Protocol: "UDP", Port: 111, Namespace: "", Service: "rpcbind", Pod: "", Container: "", NodeRole: "", Optional: false}, diff --git a/commatrix/generate.go b/commatrix/generate.go index baf1c13d..4c148d88 100644 --- a/commatrix/generate.go +++ b/commatrix/generate.go @@ -18,7 +18,7 @@ import ( "github.com/openshift-kni/commatrix/types" ) -func GenerateSS(cs *clientutil.ClientSet) (ssMat *types.ComMatrix, ssOutTCP, ssOutUDP []byte, err error) { +func GenerateSS(cs *clientutil.ClientSet, debugPod debug.NewDebugPodInterface) (ssMat *types.ComMatrix, ssOutTCP, ssOutUDP []byte, err error) { nodesList, err := cs.CoreV1Interface.Nodes().List(context.TODO(), metav1.ListOptions{}) if err != nil { return nil, nil, nil, err @@ -41,18 +41,18 @@ func GenerateSS(cs *clientutil.ClientSet) (ssMat *types.ComMatrix, ssOutTCP, ssO for _, n := range nodesList.Items { node := n g.Go(func() error { - debugPod, err := debug.NewDebugPod(cs, node.Name, consts.DefaultDebugNamespace, consts.DefaultDebugPodImage) + newdebugPod, err := debugPod.New(cs, node.Name, consts.DefaultDebugNamespace, consts.DefaultDebugPodImage) if err != nil { return err } defer func() { - err := debugPod.Clean() + err := newdebugPod.Clean() if err != nil { - fmt.Printf("failed cleaning debug pod %s: %v", debugPod, err) + fmt.Printf("failed cleaning debug pod %s: %v", newdebugPod, err) } }() - cds, ssTCP, ssUDP, err := ss.CreateSSOutputFromNode(debugPod, &node) + cds, ssTCP, ssUDP, err := ss.CreateSSOutputFromNode(newdebugPod, &node) if err != nil { return err } diff --git a/debug/debug.go b/debug/debug.go index 6b30c661..bee0d294 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -1,6 +1,7 @@ package debug //go:generate mockgen -destination=debug_mock.go -package=debug . DebugPodInterface +//go:generate mockgen -destination=new_debug_mock.go -package=debug . NewDebugPodInterface import ( "context" @@ -25,6 +26,12 @@ type DebugPodInterface interface { GetNodeName() string } +type NewDebugPodInterface interface { + New(cs *client.ClientSet, node string, namespace string, image string) (DebugPodInterface, error) +} + +type NewDebugPod struct{} + type DebugPod struct { Name string Namespace string @@ -40,11 +47,9 @@ const ( timeout = 2 * time.Minute ) -var NewDebugPod = New - // New creates debug pod on the given node, puts it in infinite sleep, // and returns the DebugPod object. Use the Clean() method to delete it. -func New(cs *client.ClientSet, node string, namespace string, image string) (DebugPodInterface, error) { +func (n *NewDebugPod) New(cs *client.ClientSet, node string, namespace string, image string) (DebugPodInterface, error) { if namespace == "" { return nil, errors.New("failed creating new debug pod: got empty namespace") } diff --git a/debug/new_debug_mock.go b/debug/new_debug_mock.go new file mode 100644 index 00000000..c663a7e6 --- /dev/null +++ b/debug/new_debug_mock.go @@ -0,0 +1,55 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/openshift-kni/commatrix/debug (interfaces: NewDebugPodInterface) +// +// Generated by this command: +// +// mockgen -destination=new_debug_mock.go -package=debug . NewDebugPodInterface +// + +// Package debug is a generated GoMock package. +package debug + +import ( + reflect "reflect" + + client "github.com/openshift-kni/commatrix/client" + gomock "go.uber.org/mock/gomock" +) + +// MockNewDebugPodInterface is a mock of NewDebugPodInterface interface. +type MockNewDebugPodInterface struct { + ctrl *gomock.Controller + recorder *MockNewDebugPodInterfaceMockRecorder +} + +// MockNewDebugPodInterfaceMockRecorder is the mock recorder for MockNewDebugPodInterface. +type MockNewDebugPodInterfaceMockRecorder struct { + mock *MockNewDebugPodInterface +} + +// NewMockNewDebugPodInterface creates a new mock instance. +func NewMockNewDebugPodInterface(ctrl *gomock.Controller) *MockNewDebugPodInterface { + mock := &MockNewDebugPodInterface{ctrl: ctrl} + mock.recorder = &MockNewDebugPodInterfaceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockNewDebugPodInterface) EXPECT() *MockNewDebugPodInterfaceMockRecorder { + return m.recorder +} + +// New mocks base method. +func (m *MockNewDebugPodInterface) New(arg0 *client.ClientSet, arg1, arg2, arg3 string) (DebugPodInterface, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "New", arg0, arg1, arg2, arg3) + ret0, _ := ret[0].(DebugPodInterface) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// New indicates an expected call of New. +func (mr *MockNewDebugPodInterfaceMockRecorder) New(arg0, arg1, arg2, arg3 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "New", reflect.TypeOf((*MockNewDebugPodInterface)(nil).New), arg0, arg1, arg2, arg3) +}