Skip to content

Commit

Permalink
Add test for controlsvc.go
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronH88 committed Jul 25, 2024
1 parent 06fd6b1 commit 5c80870
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pkg/controlsvc/controlsvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func TestRunControlSvc(t *testing.T) {
mockNetceptor := mock_controlsvc.NewMockNetceptorForControlsvc(ctrl)
mockUnix := mock_controlsvc.NewMockUtiler(ctrl)
mockNet := mock_controlsvc.NewMockNeter(ctrl)
mockTLS := mock_controlsvc.NewMockTlser(ctrl)
mockListener := mock_controlsvc.NewMockListener(ctrl)

logger := logger.NewReceptorLogger("")

runControlSvcTestCases := []struct {
name string
Expand Down Expand Up @@ -158,6 +162,21 @@ func TestRunControlSvc(t *testing.T) {
"tcpListen": "",
},
},
{
name: "tcp listener set",
expectedError: "",
expectedCalls: func() {
mockNet.EXPECT().Listen(gomock.Any(), gomock.Any()).Return(mockListener, nil)
mockTLS.EXPECT().NewListener(gomock.Any(), gomock.Any()).Return(mockListener)
mockNetceptor.EXPECT().GetLogger().Return(logger).AnyTimes()
mockListener.EXPECT().Accept().Return(nil, errors.New("normal close")).AnyTimes()
},
listeners: map[string]string{
"service": "",
"unixSocket": "",
"tcpListen": "tcp:1",
},
},
}

for _, testCase := range runControlSvcTestCases {
Expand All @@ -166,10 +185,11 @@ func TestRunControlSvc(t *testing.T) {
s := controlsvc.New(false, mockNetceptor)
s.SetServerUtils(mockUnix)
s.SetServerNet(mockNet)
s.SetServerTLS(mockTLS)

err := s.RunControlSvc(context.Background(), testCase.listeners["service"], &tls.Config{}, testCase.listeners["unixSocket"], os.FileMode(0o600), testCase.listeners["tcpListen"], &tls.Config{})

if err.Error() != testCase.expectedError {
if err != nil && err.Error() != testCase.expectedError {
t.Errorf("expected error %s, got %v", testCase.expectedError, err)
}
})
Expand Down

0 comments on commit 5c80870

Please sign in to comment.