From be4fdf3e7c0a9153ba82b4f9344fbcde00f6fd54 Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Sun, 28 Jul 2024 14:45:33 +0300 Subject: [PATCH 01/13] creating a unit test using mocking for the generate SS function update the function: remove unused vars and update the debug object to use the mock functions on test --- cmd/main.go | 8 +++- commatrix/generate.go | 6 +-- commatrix/generate_test.go | 75 ++++++++++++++++++++++++++++++++++++++ debug/debug.go | 15 +++++++- debug/debug_mock.go | 36 ++++++++++++++++++ go.mod | 3 ++ go.sum | 12 ++++++ ss/ss.go | 16 +++++--- 8 files changed, 158 insertions(+), 13 deletions(-) create mode 100644 commatrix/generate_test.go create mode 100644 debug/debug_mock.go diff --git a/cmd/main.go b/cmd/main.go index 9f8bbe6..0306d3a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" + clientutil "github.com/openshift-kni/commatrix/client" + "github.com/openshift-kni/commatrix/commatrix" ) @@ -33,7 +35,11 @@ func main() { if !ok { panic("must set the KUBECONFIG environment variable") } + cs, err := clientutil.New(kubeconfig) + if err != nil { + panic("must set the KUBECONFIG environment variable") + } var env commatrix.Env switch envStr { case "baremetal": @@ -68,7 +74,7 @@ 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(kubeconfig, customEntriesPath, customEntriesFormat, format, env, deployment, destDir) + ssMat, ssOutTCP, ssOutUDP, err := commatrix.GenerateSS(cs) if err != nil { panic(fmt.Sprintf("Error while generating the ss matrix and ss raws :%v", err)) } diff --git a/commatrix/generate.go b/commatrix/generate.go index a866f8a..2bf4f8f 100644 --- a/commatrix/generate.go +++ b/commatrix/generate.go @@ -18,11 +18,7 @@ import ( "github.com/openshift-kni/commatrix/types" ) -func GenerateSS(kubeconfig, customEntriesPath, customEntriesFormat, format string, env Env, deployment Deployment, destDir string) (ssMat *types.ComMatrix, ssOutTCP, ssOutUDP []byte, err error) { - cs, err := clientutil.New(kubeconfig) - if err != nil { - return nil, nil, nil, err - } +func GenerateSS(cs *clientutil.ClientSet) (ssMat *types.ComMatrix, ssOutTCP, ssOutUDP []byte, err error) { nodesList, err := cs.CoreV1Interface.Nodes().List(context.TODO(), metav1.ListOptions{}) if err != nil { diff --git a/commatrix/generate_test.go b/commatrix/generate_test.go new file mode 100644 index 0000000..4a5b956 --- /dev/null +++ b/commatrix/generate_test.go @@ -0,0 +1,75 @@ +package commatrix + +import ( + "context" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes/fake" + + clientutil "github.com/openshift-kni/commatrix/client" + "github.com/openshift-kni/commatrix/debug" + "github.com/openshift-kni/commatrix/types" +) + +func TestGenerateSS(t *testing.T) { + clientset := fake.NewSimpleClientset() + + mockDebugPod := new(debug.MockDebugPod) + 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)) +LISTEN 0 4096 127.0.0.1:9100 0.0.0.0:* users:(("node_exporter",pid=4147,fd=3))`) + + udpOutput := []byte(`UNCONN 0 0 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=1399,fd=5),("systemd",pid=1,fd=78)) +UNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:(("chronyd",pid=1015,fd=5)) +UNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:(("pluto",pid=2115,fd=21))`) + Output := []byte(`1: /system.slice/containerd.service +2: /system.slice/kubelet.service +3: /system.slice/sshd.service`) + mockDebugPod.On("ExecWithRetry", mock.MatchedBy(func(cmd string) bool { + return strings.HasPrefix(cmd, "cat /proc/") && strings.Contains(cmd, "/cgroup") + }), mock.Anything, mock.Anything).Return( + Output, nil, + ) + + mockDebugPod.On("ExecWithRetry", "ss -anpltH", mock.Anything, mock.Anything).Return( + tcpOutput, nil, + ) + mockDebugPod.On("ExecWithRetry", "ss -anpluH", mock.Anything, mock.Anything).Return( + udpOutput, nil, + ) + mockDebugPod.On("Clean").Return(nil) + mockDebugPod.On("GetNodeName").Return("test-node") + + // Mock the New function + debug.New = func(cs *clientutil.ClientSet, node string, namespace string, image string) (debug.DebugPodInterface, error) { + return mockDebugPod, nil + } + + cs := &clientutil.ClientSet{ + CoreV1Interface: clientset.CoreV1(), + } + testNode := &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-node", + }, + } + + _, _ = clientset.CoreV1().Nodes().Create(context.TODO(), testNode, metav1.CreateOptions{}) + + ssMat, ssOutTCP, ssOutUDP, err := GenerateSS(cs) + expectedSSMat := &types.ComMatrix{ + Matrix: []types.ComDetails{ + {Direction: "Ingress", Protocol: "UDP", Port: 111, Namespace: "", Service: "rpcbind", Pod: "", Container: "", NodeRole: "", Optional: false}, + {Direction: "Ingress", Protocol: "UDP", Port: 500, Namespace: "", Service: "pluto", Pod: "", Container: "", NodeRole: "", Optional: false}, + }} + + assert.NoError(t, err) + assert.Equal(t, expectedSSMat, ssMat, "Expected and actual ssMat values should match") + assert.Equal(t, "node: test-node\nLISTEN 0 4096 127.0.0.1:8797 0.0.0.0:* users:((\"machine-config-\",pid=3534,fd=3)) \nLISTEN 0 4096 127.0.0.1:8798 0.0.0.0:* users:((\"machine-config-\",pid=3534,fd=13)) \nLISTEN 0 4096 127.0.0.1:9100 0.0.0.0:* users:((\"node_exporter\",pid=4147,fd=3))\n", string(ssOutTCP)) + assert.Equal(t, "node: test-node\nUNCONN 0 0 0.0.0.0:111 0.0.0.0:* users:((\"rpcbind\",pid=1399,fd=5),(\"systemd\",pid=1,fd=78))\nUNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:((\"chronyd\",pid=1015,fd=5)) \nUNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:((\"pluto\",pid=2115,fd=21))\n", string(ssOutUDP)) +} diff --git a/debug/debug.go b/debug/debug.go index e668dde..d895d2b 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -17,20 +17,33 @@ import ( "github.com/openshift-kni/commatrix/client" ) +// DebugPodInterface defines the methods of DebugPod that we need to mock +type DebugPodInterface interface { + ExecWithRetry(command string, interval time.Duration, duration time.Duration) ([]byte, error) + Clean() error + GetNodeName() string +} + type DebugPod struct { Name string Namespace string NodeName string } +func (dp *DebugPod) GetNodeName() string { + return dp.NodeName +} + const ( interval = 1 * time.Second timeout = 2 * time.Minute ) +var _ DebugPodInterface = (*DebugPod)(nil) + // 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) (*DebugPod, error) { +var New = func(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/debug_mock.go b/debug/debug_mock.go new file mode 100644 index 0000000..b3e61e2 --- /dev/null +++ b/debug/debug_mock.go @@ -0,0 +1,36 @@ +package debug + +import ( + "time" + + "github.com/openshift-kni/commatrix/client" + "github.com/stretchr/testify/mock" +) + +// MockDebugPod is a mock type for the DebugPodInterface// MockDebugPod is a mock type for the DebugPodInterface +type MockDebugPod struct { + mock.Mock +} + +// ExecWithRetry mocks the ExecWithRetry method +func (m *MockDebugPod) ExecWithRetry(command string, interval time.Duration, duration time.Duration) ([]byte, error) { + args := m.Called(command, interval, duration) + return args.Get(0).([]byte), args.Error(1) +} + +// Clean mocks the Clean method +func (m *MockDebugPod) Clean() error { + args := m.Called() + return args.Error(0) +} + +// GetNodeName mocks the GetNodeName method +func (m *MockDebugPod) GetNodeName() string { + args := m.Called() + return args.String(0) +} + +func (m *MockDebugPod) New(cs *client.ClientSet, node string, namespace string, image string) (DebugPodInterface, error) { + args := m.Called(cs, node, namespace, image) + return args.Get(0).(DebugPodInterface), args.Error(1) +} diff --git a/go.mod b/go.mod index fc9782a..7327c3f 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a + github.com/golang/mock v1.6.0 github.com/openshift/client-go v0.0.0-20240415214935-be70f772f157 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 @@ -16,8 +17,10 @@ require ( ) require ( + github.com/evanphx/json-patch v4.12.0+incompatible // indirect github.com/openshift/api v0.0.0-20240415161129-d7aff303fa1a // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect ) require ( diff --git a/go.sum b/go.sum index 968c25d..9287463 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a h1:RYfmiM0zluBJOiPD github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= @@ -84,6 +86,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -94,6 +97,7 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= @@ -105,11 +109,13 @@ golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfU golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/oauth2 v0.12.0 h1:smVPGxink+n1ZI5pkQa8y6fZT0RW0MgCO5bFpepy4B4= @@ -117,14 +123,19 @@ golang.org/x/oauth2 v0.12.0/go.mod h1:A74bZ3aGXgCY0qaIC9Ahg6Lglin4AMAco8cIv9baba golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -138,6 +149,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/ss/ss.go b/ss/ss.go index 11f9cd1..c7cf7d6 100644 --- a/ss/ss.go +++ b/ss/ss.go @@ -17,13 +17,17 @@ import ( "github.com/openshift-kni/commatrix/types" ) +//type SSGenerator interface { +// CreateSSOutputFromNode(debugPod debug.DebugPodInterface, node *corev1.Node) ([]types.ComDetails, []byte, []byte, error) +//} + const ( localAddrPortFieldIdx = 3 interval = time.Millisecond * 500 duration = time.Second * 5 ) -func CreateSSOutputFromNode(debugPod *debug.DebugPod, node *corev1.Node) (res []types.ComDetails, ssOutTCP, ssOutUDP []byte, err error) { +func CreateSSOutputFromNode(debugPod debug.DebugPodInterface, node *corev1.Node) (res []types.ComDetails, ssOutTCP, ssOutUDP []byte, err error) { ssOutTCP, err = debugPod.ExecWithRetry("ss -anpltH", interval, duration) if err != nil { return nil, nil, nil, err @@ -51,7 +55,7 @@ func splitByLines(bytes []byte) []string { return strings.Split(str, "\n") } -func toComDetails(debugPod *debug.DebugPod, ssOutput []string, protocol string, node *corev1.Node) []types.ComDetails { +func toComDetails(debugPod debug.DebugPodInterface, ssOutput []string, protocol string, node *corev1.Node) []types.ComDetails { res := make([]types.ComDetails, 0) nodeRoles := nodes.GetRole(node) @@ -74,7 +78,7 @@ func toComDetails(debugPod *debug.DebugPod, ssOutput []string, protocol string, } // getContainerName receives an ss entry and gets the name of the container exposing this port. -func getContainerName(debugPod *debug.DebugPod, ssEntry string) (string, error) { +func getContainerName(debugPod debug.DebugPodInterface, ssEntry string) (string, error) { pid, err := extractPID(ssEntry) if err != nil { return "", err @@ -108,7 +112,7 @@ func extractPID(ssEntry string) (string, error) { } // extractContainerID receives a PID of a container, and returns its CRI-O ID. -func extractContainerID(debugPod *debug.DebugPod, pid string) (string, error) { +func extractContainerID(debugPod debug.DebugPodInterface, pid string) (string, error) { cmd := fmt.Sprintf("cat /proc/%s/cgroup", pid) out, err := debugPod.ExecWithRetry(cmd, interval, duration) if err != nil { @@ -119,7 +123,7 @@ func extractContainerID(debugPod *debug.DebugPod, pid string) (string, error) { match := re.FindStringSubmatch(string(out)) if len(match) < 2 { - return "", fmt.Errorf("container ID not found node:%s pid: %s", debugPod.NodeName, pid) + return "", fmt.Errorf("container ID not found node:%s pid: %s", debugPod.GetNodeName(), pid) } containerID := match[1] @@ -127,7 +131,7 @@ func extractContainerID(debugPod *debug.DebugPod, pid string) (string, error) { } // extractContainerName receives CRI-O container ID and returns the container's name. -func extractContainerName(debugPod *debug.DebugPod, containerID string) (string, error) { +func extractContainerName(debugPod debug.DebugPodInterface, containerID string) (string, error) { type ContainerInfo struct { Containers []struct { Labels struct { From 758bd089857079ee76c860659d6cabf1fdec5315 Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Sun, 28 Jul 2024 14:50:37 +0300 Subject: [PATCH 02/13] update linter errors --- cmd/main.go | 2 +- commatrix/generate.go | 1 - debug/debug.go | 1 - debug/debug_mock.go | 7 +++---- ss/ss.go | 4 ---- 5 files changed, 4 insertions(+), 11 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 0306d3a..55626f1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -38,8 +38,8 @@ func main() { cs, err := clientutil.New(kubeconfig) if err != nil { panic("must set the KUBECONFIG environment variable") - } + var env commatrix.Env switch envStr { case "baremetal": diff --git a/commatrix/generate.go b/commatrix/generate.go index 2bf4f8f..6c4d571 100644 --- a/commatrix/generate.go +++ b/commatrix/generate.go @@ -19,7 +19,6 @@ import ( ) func GenerateSS(cs *clientutil.ClientSet) (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 diff --git a/debug/debug.go b/debug/debug.go index d895d2b..ab9b4b9 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -17,7 +17,6 @@ import ( "github.com/openshift-kni/commatrix/client" ) -// DebugPodInterface defines the methods of DebugPod that we need to mock type DebugPodInterface interface { ExecWithRetry(command string, interval time.Duration, duration time.Duration) ([]byte, error) Clean() error diff --git a/debug/debug_mock.go b/debug/debug_mock.go index b3e61e2..019c0bc 100644 --- a/debug/debug_mock.go +++ b/debug/debug_mock.go @@ -7,24 +7,23 @@ import ( "github.com/stretchr/testify/mock" ) -// MockDebugPod is a mock type for the DebugPodInterface// MockDebugPod is a mock type for the DebugPodInterface type MockDebugPod struct { mock.Mock } -// ExecWithRetry mocks the ExecWithRetry method +// ExecWithRetry mocks the ExecWithRetry method. func (m *MockDebugPod) ExecWithRetry(command string, interval time.Duration, duration time.Duration) ([]byte, error) { args := m.Called(command, interval, duration) return args.Get(0).([]byte), args.Error(1) } -// Clean mocks the Clean method +// Clean mocks the Clean method. func (m *MockDebugPod) Clean() error { args := m.Called() return args.Error(0) } -// GetNodeName mocks the GetNodeName method +// GetNodeName mocks the GetNodeName method. func (m *MockDebugPod) GetNodeName() string { args := m.Called() return args.String(0) diff --git a/ss/ss.go b/ss/ss.go index c7cf7d6..dac90c3 100644 --- a/ss/ss.go +++ b/ss/ss.go @@ -17,10 +17,6 @@ import ( "github.com/openshift-kni/commatrix/types" ) -//type SSGenerator interface { -// CreateSSOutputFromNode(debugPod debug.DebugPodInterface, node *corev1.Node) ([]types.ComDetails, []byte, []byte, error) -//} - const ( localAddrPortFieldIdx = 3 interval = time.Millisecond * 500 From ba75b89343f7743314ba9eac1df01073d514f190 Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Tue, 30 Jul 2024 12:11:53 +0300 Subject: [PATCH 03/13] update the generated mock file --- commatrix/generate_test.go | 2 +- debug/debug_mock.go | 97 ++++++++++++++++++++++++++++++-------- go.mod | 1 - go.sum | 11 ----- 4 files changed, 79 insertions(+), 32 deletions(-) diff --git a/commatrix/generate_test.go b/commatrix/generate_test.go index 4a5b956..22cbcba 100644 --- a/commatrix/generate_test.go +++ b/commatrix/generate_test.go @@ -19,7 +19,7 @@ import ( func TestGenerateSS(t *testing.T) { clientset := fake.NewSimpleClientset() - mockDebugPod := new(debug.MockDebugPod) + mockDebugPod := new(debug.MockDebugPodInterface) 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)) LISTEN 0 4096 127.0.0.1:9100 0.0.0.0:* users:(("node_exporter",pid=4147,fd=3))`) diff --git a/debug/debug_mock.go b/debug/debug_mock.go index 019c0bc..fdf0228 100644 --- a/debug/debug_mock.go +++ b/debug/debug_mock.go @@ -1,35 +1,94 @@ +// Code generated by mockery v2.43.2. DO NOT EDIT. + package debug import ( - "time" + time "time" - "github.com/openshift-kni/commatrix/client" - "github.com/stretchr/testify/mock" + mock "github.com/stretchr/testify/mock" ) -type MockDebugPod struct { +// DebugPodInterface is an autogenerated mock type for the DebugPodInterface type +type MockDebugPodInterface struct { mock.Mock } -// ExecWithRetry mocks the ExecWithRetry method. -func (m *MockDebugPod) ExecWithRetry(command string, interval time.Duration, duration time.Duration) ([]byte, error) { - args := m.Called(command, interval, duration) - return args.Get(0).([]byte), args.Error(1) +// Clean provides a mock function with given fields: +func (_m *MockDebugPodInterface) Clean() error { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Clean") + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 } -// Clean mocks the Clean method. -func (m *MockDebugPod) Clean() error { - args := m.Called() - return args.Error(0) +// ExecWithRetry provides a mock function with given fields: command, interval, duration +func (_m *MockDebugPodInterface) ExecWithRetry(command string, interval time.Duration, duration time.Duration) ([]byte, error) { + ret := _m.Called(command, interval, duration) + + if len(ret) == 0 { + panic("no return value specified for ExecWithRetry") + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(string, time.Duration, time.Duration) ([]byte, error)); ok { + return rf(command, interval, duration) + } + if rf, ok := ret.Get(0).(func(string, time.Duration, time.Duration) []byte); ok { + r0 = rf(command, interval, duration) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(string, time.Duration, time.Duration) error); ok { + r1 = rf(command, interval, duration) + } else { + r1 = ret.Error(1) + } + + return r0, r1 } -// GetNodeName mocks the GetNodeName method. -func (m *MockDebugPod) GetNodeName() string { - args := m.Called() - return args.String(0) +// GetNodeName provides a mock function with given fields: +func (_m *MockDebugPodInterface) GetNodeName() string { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetNodeName") + } + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 } -func (m *MockDebugPod) New(cs *client.ClientSet, node string, namespace string, image string) (DebugPodInterface, error) { - args := m.Called(cs, node, namespace, image) - return args.Get(0).(DebugPodInterface), args.Error(1) +// NewDebugPodInterface creates a new instance of DebugPodInterface. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewDebugPodInterface(t interface { + mock.TestingT + Cleanup(func()) +}) *MockDebugPodInterface { + mock := &MockDebugPodInterface{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock } diff --git a/go.mod b/go.mod index 7327c3f..920e0c5 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.21 require ( github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a - github.com/golang/mock v1.6.0 github.com/openshift/client-go v0.0.0-20240415214935-be70f772f157 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 9287463..ba262a4 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,6 @@ github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a h1:RYfmiM0zluBJOiPD github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= -github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= @@ -97,7 +95,6 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= @@ -109,13 +106,11 @@ golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfU golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/oauth2 v0.12.0 h1:smVPGxink+n1ZI5pkQa8y6fZT0RW0MgCO5bFpepy4B4= @@ -123,19 +118,14 @@ golang.org/x/oauth2 v0.12.0/go.mod h1:A74bZ3aGXgCY0qaIC9Ahg6Lglin4AMAco8cIv9baba golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -149,7 +139,6 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= From a4119120df50b84a88a9695b96e7c73f958e5081 Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:06:19 +0300 Subject: [PATCH 04/13] update the code to use the expect function and regenerate the mock file --- commatrix/generate_test.go | 49 +++++++++------ debug/debug_mock.go | 126 ++++++++++++++++--------------------- go.mod | 2 + go.sum | 13 ++++ 4 files changed, 101 insertions(+), 89 deletions(-) diff --git a/commatrix/generate_test.go b/commatrix/generate_test.go index 22cbcba..e9f19fa 100644 --- a/commatrix/generate_test.go +++ b/commatrix/generate_test.go @@ -2,11 +2,14 @@ package commatrix import ( "context" + "fmt" "strings" "testing" + "time" + + gomock "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" @@ -19,7 +22,11 @@ import ( func TestGenerateSS(t *testing.T) { clientset := fake.NewSimpleClientset() - mockDebugPod := new(debug.MockDebugPodInterface) + ctrlTest := gomock.NewController(t) + defer ctrlTest.Finish() + + mockDebugPod := debug.NewMockDebugPodInterface(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)) LISTEN 0 4096 127.0.0.1:9100 0.0.0.0:* users:(("node_exporter",pid=4147,fd=3))`) @@ -28,22 +35,28 @@ LISTEN 0 4096 127.0.0.1:9100 0.0.0.0:* users:(("node_exporter",pid=41 UNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:(("chronyd",pid=1015,fd=5)) UNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:(("pluto",pid=2115,fd=21))`) Output := []byte(`1: /system.slice/containerd.service -2: /system.slice/kubelet.service -3: /system.slice/sshd.service`) - mockDebugPod.On("ExecWithRetry", mock.MatchedBy(func(cmd string) bool { - return strings.HasPrefix(cmd, "cat /proc/") && strings.Contains(cmd, "/cgroup") - }), mock.Anything, mock.Anything).Return( - Output, nil, - ) - - mockDebugPod.On("ExecWithRetry", "ss -anpltH", mock.Anything, mock.Anything).Return( - tcpOutput, nil, - ) - mockDebugPod.On("ExecWithRetry", "ss -anpluH", mock.Anything, mock.Anything).Return( - udpOutput, nil, - ) - mockDebugPod.On("Clean").Return(nil) - mockDebugPod.On("GetNodeName").Return("test-node") + 2: /system.slice/kubelet.service + 3: /system.slice/sshd.service`) + + // Set up the expectations + mockDebugPod.EXPECT().ExecWithRetry(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( + func(cmd string, interval, duration time.Duration) ([]byte, error) { + if strings.HasPrefix(cmd, "cat /proc/") && strings.Contains(cmd, "/cgroup") { + return Output, nil + } + if cmd == "ss -anpltH" { + return tcpOutput, nil + } + if cmd == "ss -anpluH" { + return udpOutput, nil + } + return nil, fmt.Errorf("unknown command") + }, + ).AnyTimes() + + mockDebugPod.EXPECT().Clean().Return(nil).AnyTimes() + + mockDebugPod.EXPECT().GetNodeName().Return("test-node").AnyTimes() // Mock the New function debug.New = func(cs *clientutil.ClientSet, node string, namespace string, image string) (debug.DebugPodInterface, error) { diff --git a/debug/debug_mock.go b/debug/debug_mock.go index fdf0228..113f39c 100644 --- a/debug/debug_mock.go +++ b/debug/debug_mock.go @@ -1,94 +1,78 @@ -// Code generated by mockery v2.43.2. DO NOT EDIT. +// Code generated by MockGen. DO NOT EDIT. +// Source: debug/debug.go +// Package debug is a generated GoMock package. package debug import ( + reflect "reflect" time "time" - mock "github.com/stretchr/testify/mock" + gomock "github.com/golang/mock/gomock" ) -// DebugPodInterface is an autogenerated mock type for the DebugPodInterface type +// MockDebugPodInterface is a mock of DebugPodInterface interface. type MockDebugPodInterface struct { - mock.Mock + ctrl *gomock.Controller + recorder *MockDebugPodInterfaceMockRecorder } -// Clean provides a mock function with given fields: -func (_m *MockDebugPodInterface) Clean() error { - ret := _m.Called() - - if len(ret) == 0 { - panic("no return value specified for Clean") - } - - var r0 error - if rf, ok := ret.Get(0).(func() error); ok { - r0 = rf() - } else { - r0 = ret.Error(0) - } - - return r0 +// MockDebugPodInterfaceMockRecorder is the mock recorder for MockDebugPodInterface. +type MockDebugPodInterfaceMockRecorder struct { + mock *MockDebugPodInterface } -// ExecWithRetry provides a mock function with given fields: command, interval, duration -func (_m *MockDebugPodInterface) ExecWithRetry(command string, interval time.Duration, duration time.Duration) ([]byte, error) { - ret := _m.Called(command, interval, duration) - - if len(ret) == 0 { - panic("no return value specified for ExecWithRetry") - } - - var r0 []byte - var r1 error - if rf, ok := ret.Get(0).(func(string, time.Duration, time.Duration) ([]byte, error)); ok { - return rf(command, interval, duration) - } - if rf, ok := ret.Get(0).(func(string, time.Duration, time.Duration) []byte); ok { - r0 = rf(command, interval, duration) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]byte) - } - } - - if rf, ok := ret.Get(1).(func(string, time.Duration, time.Duration) error); ok { - r1 = rf(command, interval, duration) - } else { - r1 = ret.Error(1) - } - - return r0, r1 +// NewMockDebugPodInterface creates a new mock instance. +func NewMockDebugPodInterface(ctrl *gomock.Controller) *MockDebugPodInterface { + mock := &MockDebugPodInterface{ctrl: ctrl} + mock.recorder = &MockDebugPodInterfaceMockRecorder{mock} + return mock } -// GetNodeName provides a mock function with given fields: -func (_m *MockDebugPodInterface) GetNodeName() string { - ret := _m.Called() +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockDebugPodInterface) EXPECT() *MockDebugPodInterfaceMockRecorder { + return m.recorder +} - if len(ret) == 0 { - panic("no return value specified for GetNodeName") - } +// Clean mocks base method. +func (m *MockDebugPodInterface) Clean() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Clean") + ret0, _ := ret[0].(error) + return ret0 +} - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } +// Clean indicates an expected call of Clean. +func (mr *MockDebugPodInterfaceMockRecorder) Clean() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Clean", reflect.TypeOf((*MockDebugPodInterface)(nil).Clean)) +} - return r0 +// ExecWithRetry mocks base method. +func (m *MockDebugPodInterface) ExecWithRetry(command string, interval, duration time.Duration) ([]byte, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ExecWithRetry", command, interval, duration) + ret0, _ := ret[0].([]byte) + ret1, _ := ret[1].(error) + return ret0, ret1 } -// NewDebugPodInterface creates a new instance of DebugPodInterface. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -// The first argument is typically a *testing.T value. -func NewDebugPodInterface(t interface { - mock.TestingT - Cleanup(func()) -}) *MockDebugPodInterface { - mock := &MockDebugPodInterface{} - mock.Mock.Test(t) +// ExecWithRetry indicates an expected call of ExecWithRetry. +func (mr *MockDebugPodInterfaceMockRecorder) ExecWithRetry(command, interval, duration interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExecWithRetry", reflect.TypeOf((*MockDebugPodInterface)(nil).ExecWithRetry), command, interval, duration) +} - t.Cleanup(func() { mock.AssertExpectations(t) }) +// GetNodeName mocks base method. +func (m *MockDebugPodInterface) GetNodeName() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetNodeName") + ret0, _ := ret[0].(string) + return ret0 +} - return mock +// GetNodeName indicates an expected call of GetNodeName. +func (mr *MockDebugPodInterfaceMockRecorder) GetNodeName() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNodeName", reflect.TypeOf((*MockDebugPodInterface)(nil).GetNodeName)) } diff --git a/go.mod b/go.mod index 920e0c5..8cabdb8 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a + github.com/golang/mock v1.6.0 github.com/openshift/client-go v0.0.0-20240415214935-be70f772f157 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 @@ -44,6 +45,7 @@ require ( github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/spf13/pflag v1.0.5 // indirect + go.uber.org/mock v0.4.0 golang.org/x/net v0.19.0 // indirect golang.org/x/oauth2 v0.12.0 // indirect golang.org/x/sync v0.5.0 diff --git a/go.sum b/go.sum index ba262a4..fd2bd9f 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a h1:RYfmiM0zluBJOiPD github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= @@ -95,6 +97,9 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= +go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= @@ -106,11 +111,13 @@ golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfU golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/oauth2 v0.12.0 h1:smVPGxink+n1ZI5pkQa8y6fZT0RW0MgCO5bFpepy4B4= @@ -118,14 +125,19 @@ golang.org/x/oauth2 v0.12.0/go.mod h1:A74bZ3aGXgCY0qaIC9Ahg6Lglin4AMAco8cIv9baba golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -139,6 +151,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= From 5dafe29e48d3b7b67bdd7ed2044979a198e3189f Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:24:03 +0300 Subject: [PATCH 05/13] move the test to the commatrix_test file --- commatrix/commatrix_test.go | 81 +++++++++++++++++++++++++++++++++- commatrix/generate_test.go | 88 ------------------------------------- 2 files changed, 80 insertions(+), 89 deletions(-) delete mode 100644 commatrix/generate_test.go diff --git a/commatrix/commatrix_test.go b/commatrix/commatrix_test.go index b720e3b..6865c93 100644 --- a/commatrix/commatrix_test.go +++ b/commatrix/commatrix_test.go @@ -1,12 +1,23 @@ package commatrix import ( + "context" "fmt" "path/filepath" + "strings" "testing" + "time" + + gomock "github.com/golang/mock/gomock" - "github.com/openshift-kni/commatrix/types" "github.com/stretchr/testify/assert" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes/fake" + + clientutil "github.com/openshift-kni/commatrix/client" + "github.com/openshift-kni/commatrix/debug" + "github.com/openshift-kni/commatrix/types" ) func TestGetPrintFunction(t *testing.T) { @@ -50,3 +61,71 @@ func TestWriteMatrixToFile(t *testing.T) { assert.NoError(t, err) assert.FileExists(t, filepath.Join(destDir, "test-matrix.json")) } + +func TestGenerateSS(t *testing.T) { + clientset := fake.NewSimpleClientset() + + ctrlTest := gomock.NewController(t) + defer ctrlTest.Finish() + + mockDebugPod := debug.NewMockDebugPodInterface(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)) +LISTEN 0 4096 127.0.0.1:9100 0.0.0.0:* users:(("node_exporter",pid=4147,fd=3))`) + + udpOutput := []byte(`UNCONN 0 0 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=1399,fd=5),("systemd",pid=1,fd=78)) +UNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:(("chronyd",pid=1015,fd=5)) +UNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:(("pluto",pid=2115,fd=21))`) + Output := []byte(`1: /system.slice/containerd.service + 2: /system.slice/kubelet.service + 3: /system.slice/sshd.service`) + + // Set up the expectations + mockDebugPod.EXPECT().ExecWithRetry(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( + func(cmd string, interval, duration time.Duration) ([]byte, error) { + if strings.HasPrefix(cmd, "cat /proc/") && strings.Contains(cmd, "/cgroup") { + return Output, nil + } + if cmd == "ss -anpltH" { + return tcpOutput, nil + } + if cmd == "ss -anpluH" { + return udpOutput, nil + } + return nil, fmt.Errorf("unknown command") + }, + ).AnyTimes() + + mockDebugPod.EXPECT().Clean().Return(nil).AnyTimes() + + mockDebugPod.EXPECT().GetNodeName().Return("test-node").AnyTimes() + + // Mock the New function + debug.New = func(cs *clientutil.ClientSet, node string, namespace string, image string) (debug.DebugPodInterface, error) { + return mockDebugPod, nil + } + + cs := &clientutil.ClientSet{ + CoreV1Interface: clientset.CoreV1(), + } + testNode := &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-node", + }, + } + + _, _ = clientset.CoreV1().Nodes().Create(context.TODO(), testNode, metav1.CreateOptions{}) + + ssMat, ssOutTCP, ssOutUDP, err := GenerateSS(cs) + expectedSSMat := &types.ComMatrix{ + Matrix: []types.ComDetails{ + {Direction: "Ingress", Protocol: "UDP", Port: 111, Namespace: "", Service: "rpcbind", Pod: "", Container: "", NodeRole: "", Optional: false}, + {Direction: "Ingress", Protocol: "UDP", Port: 500, Namespace: "", Service: "pluto", Pod: "", Container: "", NodeRole: "", Optional: false}, + }} + + assert.NoError(t, err) + assert.Equal(t, expectedSSMat, ssMat, "Expected and actual ssMat values should match") + assert.Equal(t, "node: test-node\nLISTEN 0 4096 127.0.0.1:8797 0.0.0.0:* users:((\"machine-config-\",pid=3534,fd=3)) \nLISTEN 0 4096 127.0.0.1:8798 0.0.0.0:* users:((\"machine-config-\",pid=3534,fd=13)) \nLISTEN 0 4096 127.0.0.1:9100 0.0.0.0:* users:((\"node_exporter\",pid=4147,fd=3))\n", string(ssOutTCP)) + assert.Equal(t, "node: test-node\nUNCONN 0 0 0.0.0.0:111 0.0.0.0:* users:((\"rpcbind\",pid=1399,fd=5),(\"systemd\",pid=1,fd=78))\nUNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:((\"chronyd\",pid=1015,fd=5)) \nUNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:((\"pluto\",pid=2115,fd=21))\n", string(ssOutUDP)) +} diff --git a/commatrix/generate_test.go b/commatrix/generate_test.go deleted file mode 100644 index e9f19fa..0000000 --- a/commatrix/generate_test.go +++ /dev/null @@ -1,88 +0,0 @@ -package commatrix - -import ( - "context" - "fmt" - "strings" - "testing" - "time" - - gomock "github.com/golang/mock/gomock" - - "github.com/stretchr/testify/assert" - v1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes/fake" - - clientutil "github.com/openshift-kni/commatrix/client" - "github.com/openshift-kni/commatrix/debug" - "github.com/openshift-kni/commatrix/types" -) - -func TestGenerateSS(t *testing.T) { - clientset := fake.NewSimpleClientset() - - ctrlTest := gomock.NewController(t) - defer ctrlTest.Finish() - - mockDebugPod := debug.NewMockDebugPodInterface(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)) -LISTEN 0 4096 127.0.0.1:9100 0.0.0.0:* users:(("node_exporter",pid=4147,fd=3))`) - - udpOutput := []byte(`UNCONN 0 0 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=1399,fd=5),("systemd",pid=1,fd=78)) -UNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:(("chronyd",pid=1015,fd=5)) -UNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:(("pluto",pid=2115,fd=21))`) - Output := []byte(`1: /system.slice/containerd.service - 2: /system.slice/kubelet.service - 3: /system.slice/sshd.service`) - - // Set up the expectations - mockDebugPod.EXPECT().ExecWithRetry(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( - func(cmd string, interval, duration time.Duration) ([]byte, error) { - if strings.HasPrefix(cmd, "cat /proc/") && strings.Contains(cmd, "/cgroup") { - return Output, nil - } - if cmd == "ss -anpltH" { - return tcpOutput, nil - } - if cmd == "ss -anpluH" { - return udpOutput, nil - } - return nil, fmt.Errorf("unknown command") - }, - ).AnyTimes() - - mockDebugPod.EXPECT().Clean().Return(nil).AnyTimes() - - mockDebugPod.EXPECT().GetNodeName().Return("test-node").AnyTimes() - - // Mock the New function - debug.New = func(cs *clientutil.ClientSet, node string, namespace string, image string) (debug.DebugPodInterface, error) { - return mockDebugPod, nil - } - - cs := &clientutil.ClientSet{ - CoreV1Interface: clientset.CoreV1(), - } - testNode := &v1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-node", - }, - } - - _, _ = clientset.CoreV1().Nodes().Create(context.TODO(), testNode, metav1.CreateOptions{}) - - ssMat, ssOutTCP, ssOutUDP, err := GenerateSS(cs) - expectedSSMat := &types.ComMatrix{ - Matrix: []types.ComDetails{ - {Direction: "Ingress", Protocol: "UDP", Port: 111, Namespace: "", Service: "rpcbind", Pod: "", Container: "", NodeRole: "", Optional: false}, - {Direction: "Ingress", Protocol: "UDP", Port: 500, Namespace: "", Service: "pluto", Pod: "", Container: "", NodeRole: "", Optional: false}, - }} - - assert.NoError(t, err) - assert.Equal(t, expectedSSMat, ssMat, "Expected and actual ssMat values should match") - assert.Equal(t, "node: test-node\nLISTEN 0 4096 127.0.0.1:8797 0.0.0.0:* users:((\"machine-config-\",pid=3534,fd=3)) \nLISTEN 0 4096 127.0.0.1:8798 0.0.0.0:* users:((\"machine-config-\",pid=3534,fd=13)) \nLISTEN 0 4096 127.0.0.1:9100 0.0.0.0:* users:((\"node_exporter\",pid=4147,fd=3))\n", string(ssOutTCP)) - assert.Equal(t, "node: test-node\nUNCONN 0 0 0.0.0.0:111 0.0.0.0:* users:((\"rpcbind\",pid=1399,fd=5),(\"systemd\",pid=1,fd=78))\nUNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:((\"chronyd\",pid=1015,fd=5)) \nUNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:((\"pluto\",pid=2115,fd=21))\n", string(ssOutUDP)) -} From 2580a11ddec0cd97a726282f98cf6b86e5b80070 Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:17:19 +0300 Subject: [PATCH 06/13] update the gomock package and add generate to the makefile --- Makefile | 5 +++-- commatrix/commatrix_test.go | 2 +- debug/debug_mock.go | 9 +++++++-- go.mod | 3 +-- go.sum | 1 - 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 15393cb..fb8733b 100644 --- a/Makefile +++ b/Makefile @@ -10,8 +10,9 @@ EXECUTABLE := commatrix-gen .DEFAULT_GOAL := run build: - go build -o $(EXECUTABLE) $(GO_SRC) - + go build -o $(EXECUTABLE) $(GO_SRC) + mockgen -source=debug/debug.go -destination=debug/debug_mock.go -package=debug + oc: ifeq (, $(shell which oc)) @{ \ diff --git a/commatrix/commatrix_test.go b/commatrix/commatrix_test.go index 6865c93..c0149b9 100644 --- a/commatrix/commatrix_test.go +++ b/commatrix/commatrix_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - gomock "github.com/golang/mock/gomock" + gomock "go.uber.org/mock/gomock" "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" diff --git a/debug/debug_mock.go b/debug/debug_mock.go index 113f39c..89c22c1 100644 --- a/debug/debug_mock.go +++ b/debug/debug_mock.go @@ -1,5 +1,10 @@ // Code generated by MockGen. DO NOT EDIT. // Source: debug/debug.go +// +// Generated by this command: +// +// mockgen -source=debug/debug.go -destination=debug/debug_mock.go -package=debug +// // Package debug is a generated GoMock package. package debug @@ -8,7 +13,7 @@ import ( reflect "reflect" time "time" - gomock "github.com/golang/mock/gomock" + gomock "go.uber.org/mock/gomock" ) // MockDebugPodInterface is a mock of DebugPodInterface interface. @@ -58,7 +63,7 @@ func (m *MockDebugPodInterface) ExecWithRetry(command string, interval, duration } // ExecWithRetry indicates an expected call of ExecWithRetry. -func (mr *MockDebugPodInterfaceMockRecorder) ExecWithRetry(command, interval, duration interface{}) *gomock.Call { +func (mr *MockDebugPodInterfaceMockRecorder) ExecWithRetry(command, interval, duration any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExecWithRetry", reflect.TypeOf((*MockDebugPodInterface)(nil).ExecWithRetry), command, interval, duration) } diff --git a/go.mod b/go.mod index 8cabdb8..bb02534 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/openshift/client-go v0.0.0-20240415214935-be70f772f157 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 + go.uber.org/mock v0.4.0 k8s.io/api v0.29.3 k8s.io/apimachinery v0.29.3 k8s.io/client-go v0.29.3 @@ -20,7 +21,6 @@ require ( github.com/evanphx/json-patch v4.12.0+incompatible // indirect github.com/openshift/api v0.0.0-20240415161129-d7aff303fa1a // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/objx v0.5.0 // indirect ) require ( @@ -45,7 +45,6 @@ require ( github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/spf13/pflag v1.0.5 // indirect - go.uber.org/mock v0.4.0 golang.org/x/net v0.19.0 // indirect golang.org/x/oauth2 v0.12.0 // indirect golang.org/x/sync v0.5.0 diff --git a/go.sum b/go.sum index fd2bd9f..2a6cc7b 100644 --- a/go.sum +++ b/go.sum @@ -86,7 +86,6 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= From 537459bfebe1ee82ede654c9d5721aec0807b75f Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:17:15 +0300 Subject: [PATCH 07/13] add the mock generate to make file --- Makefile | 6 ++++-- debug/debug.go | 4 ++++ debug/debug_mock.go | 12 ++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index fb8733b..618f376 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,10 @@ EXECUTABLE := commatrix-gen build: go build -o $(EXECUTABLE) $(GO_SRC) - mockgen -source=debug/debug.go -destination=debug/debug_mock.go -package=debug - + +mock-generate: + go generate ./... + oc: ifeq (, $(shell which oc)) @{ \ diff --git a/debug/debug.go b/debug/debug.go index ab9b4b9..69d56df 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -1,5 +1,9 @@ +// debug/debug.go + package debug +//go:generate mockgen -destination=debug_mock.go -package=debug . DebugPodInterface + import ( "context" "errors" diff --git a/debug/debug_mock.go b/debug/debug_mock.go index 89c22c1..9ffc39e 100644 --- a/debug/debug_mock.go +++ b/debug/debug_mock.go @@ -1,9 +1,9 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: debug/debug.go +// Source: github.com/openshift-kni/commatrix/debug (interfaces: DebugPodInterface) // // Generated by this command: // -// mockgen -source=debug/debug.go -destination=debug/debug_mock.go -package=debug +// mockgen -destination=debug_mock.go -package=debug . DebugPodInterface // // Package debug is a generated GoMock package. @@ -54,18 +54,18 @@ func (mr *MockDebugPodInterfaceMockRecorder) Clean() *gomock.Call { } // ExecWithRetry mocks base method. -func (m *MockDebugPodInterface) ExecWithRetry(command string, interval, duration time.Duration) ([]byte, error) { +func (m *MockDebugPodInterface) ExecWithRetry(arg0 string, arg1, arg2 time.Duration) ([]byte, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ExecWithRetry", command, interval, duration) + ret := m.ctrl.Call(m, "ExecWithRetry", arg0, arg1, arg2) ret0, _ := ret[0].([]byte) ret1, _ := ret[1].(error) return ret0, ret1 } // ExecWithRetry indicates an expected call of ExecWithRetry. -func (mr *MockDebugPodInterfaceMockRecorder) ExecWithRetry(command, interval, duration any) *gomock.Call { +func (mr *MockDebugPodInterfaceMockRecorder) ExecWithRetry(arg0, arg1, arg2 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExecWithRetry", reflect.TypeOf((*MockDebugPodInterface)(nil).ExecWithRetry), command, interval, duration) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExecWithRetry", reflect.TypeOf((*MockDebugPodInterface)(nil).ExecWithRetry), arg0, arg1, arg2) } // GetNodeName mocks base method. From 8493c654c4ca493a34cab6ee72863e89b98d55e6 Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:22:47 +0300 Subject: [PATCH 08/13] fix linter error --- debug/debug.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/debug/debug.go b/debug/debug.go index 69d56df..571ae74 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -1,5 +1,3 @@ -// debug/debug.go - package debug //go:generate mockgen -destination=debug_mock.go -package=debug . DebugPodInterface From b4b4ad7708dbe41a48c0bcb26106c7a71ca81e09 Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Wed, 31 Jul 2024 17:31:28 +0300 Subject: [PATCH 09/13] few fixes for the new mocking function on debug --- commatrix/commatrix_test.go | 6 ++++-- commatrix/generate.go | 2 +- debug/debug.go | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/commatrix/commatrix_test.go b/commatrix/commatrix_test.go index c0149b9..0586945 100644 --- a/commatrix/commatrix_test.go +++ b/commatrix/commatrix_test.go @@ -15,6 +15,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" + "github.com/openshift-kni/commatrix/client" clientutil "github.com/openshift-kni/commatrix/client" "github.com/openshift-kni/commatrix/debug" "github.com/openshift-kni/commatrix/types" @@ -101,10 +102,11 @@ UNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:(("pluto",pid=2115,fd=21 mockDebugPod.EXPECT().GetNodeName().Return("test-node").AnyTimes() - // Mock the New function - debug.New = func(cs *clientutil.ClientSet, node string, namespace string, image string) (debug.DebugPodInterface, error) { + originalNew := debug.NewDebugPod + debug.NewDebugPod = func(cs *client.ClientSet, node string, namespace string, image string) (debug.DebugPodInterface, error) { return mockDebugPod, nil } + defer func() { debug.NewDebugPod = originalNew }() cs := &clientutil.ClientSet{ CoreV1Interface: clientset.CoreV1(), diff --git a/commatrix/generate.go b/commatrix/generate.go index 6c4d571..baf1c13 100644 --- a/commatrix/generate.go +++ b/commatrix/generate.go @@ -41,7 +41,7 @@ func GenerateSS(cs *clientutil.ClientSet) (ssMat *types.ComMatrix, ssOutTCP, ssO for _, n := range nodesList.Items { node := n g.Go(func() error { - debugPod, err := debug.New(cs, node.Name, consts.DefaultDebugNamespace, consts.DefaultDebugPodImage) + debugPod, err := debug.NewDebugPod(cs, node.Name, consts.DefaultDebugNamespace, consts.DefaultDebugPodImage) if err != nil { return err } diff --git a/debug/debug.go b/debug/debug.go index 571ae74..6b30c66 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -40,11 +40,11 @@ const ( timeout = 2 * time.Minute ) -var _ DebugPodInterface = (*DebugPod)(nil) +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. -var New = func(cs *client.ClientSet, node string, namespace string, image string) (DebugPodInterface, error) { +func 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") } From 55660e12b4e14649a2598b3b4c787cdd4c4b252c Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Wed, 31 Jul 2024 17:33:33 +0300 Subject: [PATCH 10/13] fix the import of clientutil --- commatrix/commatrix_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/commatrix/commatrix_test.go b/commatrix/commatrix_test.go index 0586945..f90d2d1 100644 --- a/commatrix/commatrix_test.go +++ b/commatrix/commatrix_test.go @@ -15,7 +15,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" - "github.com/openshift-kni/commatrix/client" clientutil "github.com/openshift-kni/commatrix/client" "github.com/openshift-kni/commatrix/debug" "github.com/openshift-kni/commatrix/types" @@ -103,7 +102,7 @@ UNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:(("pluto",pid=2115,fd=21 mockDebugPod.EXPECT().GetNodeName().Return("test-node").AnyTimes() originalNew := debug.NewDebugPod - debug.NewDebugPod = func(cs *client.ClientSet, node string, namespace string, image string) (debug.DebugPodInterface, error) { + debug.NewDebugPod = func(cs *clientutil.ClientSet, node string, namespace string, image string) (debug.DebugPodInterface, error) { return mockDebugPod, nil } defer func() { debug.NewDebugPod = originalNew }() From c338734b89ea3a44a6851d1e3e449103756504e7 Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Thu, 1 Aug 2024 17:11:03 +0300 Subject: [PATCH 11/13] update implementation of debugpod --- cmd/main.go | 5 +++- commatrix/commatrix_test.go | 21 ++++++-------- commatrix/generate.go | 10 +++---- debug/debug.go | 11 ++++++-- debug/new_debug_mock.go | 55 +++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 debug/new_debug_mock.go diff --git a/cmd/main.go b/cmd/main.go index 55626f1..97b5b3e 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 f90d2d1..877c35a 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 baf1c13..4c148d8 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 6b30c66..bee0d29 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 0000000..c663a7e --- /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) +} From 7b326800af78b0f40c1f411d30d9a0c06c600911 Mon Sep 17 00:00:00 2001 From: Amal Abu Gosh <88486034+aabughosh@users.noreply.github.com> Date: Sun, 4 Aug 2024 12:26:51 +0300 Subject: [PATCH 12/13] Update v1 on import to be corev1 Co-authored-by: Brandon Palm --- commatrix/commatrix_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commatrix/commatrix_test.go b/commatrix/commatrix_test.go index 877c35a..e658f7e 100644 --- a/commatrix/commatrix_test.go +++ b/commatrix/commatrix_test.go @@ -11,7 +11,7 @@ import ( gomock "go.uber.org/mock/gomock" "github.com/stretchr/testify/assert" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" From 74855c8f7058f702b1edf850bdcd146ed4955538 Mon Sep 17 00:00:00 2001 From: aabughosh <88486034+aabughosh@users.noreply.github.com> Date: Sun, 4 Aug 2024 12:29:55 +0300 Subject: [PATCH 13/13] update corev1 --- commatrix/commatrix_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commatrix/commatrix_test.go b/commatrix/commatrix_test.go index e658f7e..0728d3f 100644 --- a/commatrix/commatrix_test.go +++ b/commatrix/commatrix_test.go @@ -106,7 +106,7 @@ UNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:(("pluto",pid=2115,fd=21 NewmockDebugPod.EXPECT().New(clientset, "test-node", "openshift-commatrix-debug", "quay.io/openshift-release-dev/ocp-release:4.15.12-multi").Return(mockDebugPod, nil) - testNode := &v1.Node{ + testNode := &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ Name: "test-node", },