@@ -8,21 +8,106 @@ import (
8
8
"net"
9
9
"os"
10
10
"testing"
11
- "time"
12
11
13
12
"github.com/ansible/receptor/pkg/controlsvc"
14
13
"github.com/ansible/receptor/pkg/controlsvc/mock_controlsvc"
15
14
"github.com/ansible/receptor/pkg/logger"
16
15
"github.com/golang/mock/gomock"
17
16
)
18
17
18
+ func TestConnectionListener (t * testing.T ) {
19
+ ctrl := gomock .NewController (t )
20
+ mockNetceptor := mock_controlsvc .NewMockNetceptorForControlsvc (ctrl )
21
+ mockListener := mock_controlsvc .NewMockListener (ctrl )
22
+ logger := logger .NewReceptorLogger ("" )
23
+
24
+ connectionListenerTestCases := []struct {
25
+ name string
26
+ expectedError bool
27
+ expectedCalls func (context.CancelFunc )
28
+ }{
29
+ {
30
+ name : "return from context error" ,
31
+ expectedError : true ,
32
+ expectedCalls : func (ctx context.CancelFunc ) {},
33
+ },
34
+ {
35
+ name : "error accepting connection" ,
36
+ expectedError : false ,
37
+ expectedCalls : func (ctxCancel context.CancelFunc ) {
38
+ mockListener .EXPECT ().Accept ().DoAndReturn (func () (net.Conn , error ) {
39
+ ctxCancel ()
40
+ return nil , errors .New ("terminated" )
41
+ })
42
+ mockNetceptor .EXPECT ().GetLogger ().Return (logger )
43
+ },
44
+ },
45
+ }
46
+
47
+ for _ , testCase := range connectionListenerTestCases {
48
+ t .Run (testCase .name , func (t * testing.T ) {
49
+ ctx , ctxCancel := context .WithCancel (context .Background ())
50
+ defer ctxCancel ()
51
+
52
+ testCase .expectedCalls (ctxCancel )
53
+ s := controlsvc .New (false , mockNetceptor )
54
+
55
+ if testCase .expectedError {
56
+ ctxCancel ()
57
+ }
58
+
59
+ s .ConnectionListener (ctx , mockListener )
60
+ })
61
+ }
62
+
63
+ }
64
+
65
+ func TestSetupConnection (t * testing.T ) {
66
+ ctrl := gomock .NewController (t )
67
+ mockNetceptor := mock_controlsvc .NewMockNetceptorForControlsvc (ctrl )
68
+ mockConn := mock_controlsvc .NewMockConn (ctrl )
69
+ logger := logger .NewReceptorLogger ("" )
70
+
71
+ setupConnectionTestCases := []struct {
72
+ name string
73
+ expectedError bool
74
+ expectedCalls func ()
75
+ }{
76
+ {
77
+ name : "log error - setting timeout" ,
78
+ expectedError : true ,
79
+ expectedCalls : func () {
80
+ mockConn .EXPECT ().SetDeadline (gomock .Any ()).Return (errors .New ("terminated" ))
81
+ mockNetceptor .EXPECT ().GetLogger ().Return (logger )
82
+ mockConn .EXPECT ().Close ()
83
+ },
84
+ },
85
+ {
86
+ name : "log error - tls handshake" ,
87
+ expectedError : true ,
88
+ expectedCalls : func () {
89
+ mockConn .EXPECT ().SetDeadline (gomock .Any ()).Return (nil )
90
+ mockNetceptor .EXPECT ().GetLogger ().Return (logger )
91
+ mockConn .EXPECT ().Close ().AnyTimes ()
92
+ },
93
+ },
94
+ }
95
+
96
+ for _ , testCase := range setupConnectionTestCases {
97
+ t .Run (testCase .name , func (t * testing.T ) {
98
+ testCase .expectedCalls ()
99
+ s := controlsvc .New (false , mockNetceptor )
100
+ tlsConn := tls .Client (mockConn , & tls.Config {})
101
+ s .SetupConnection (tlsConn )
102
+ })
103
+ }
104
+ }
105
+
19
106
func TestRunControlSvc (t * testing.T ) {
20
107
ctrl := gomock .NewController (t )
21
108
mockNetceptor := mock_controlsvc .NewMockNetceptorForControlsvc (ctrl )
22
109
mockUnix := mock_controlsvc .NewMockUtiler (ctrl )
23
110
mockNet := mock_controlsvc .NewMockNeter (ctrl )
24
- // mockListener := mock_controlsvc.NewMockListener(ctrl)
25
- // logger := logger.NewReceptorLogger("")
26
111
27
112
runControlSvcTestCases := []struct {
28
113
name string
@@ -77,19 +162,6 @@ func TestRunControlSvc(t *testing.T) {
77
162
"tcpListen" : "" ,
78
163
},
79
164
},
80
- // {
81
- // name: "idk",
82
- // expectedError: "",
83
- // expectedCalls: func() {
84
- // mockNet.EXPECT().Listen(gomock.Any(), gomock.Any()).Return(mockListener, nil)
85
- // mockNetceptor.EXPECT().GetLogger().Return(logger)
86
- // },
87
- // listeners: map[string]string{
88
- // "service": "",
89
- // "unixSocket": "",
90
- // "tcpListen": "tcp listener",
91
- // },
92
- // },
93
165
}
94
166
95
167
for _ , testCase := range runControlSvcTestCases {
@@ -102,40 +174,12 @@ func TestRunControlSvc(t *testing.T) {
102
174
err := s .RunControlSvc (context .Background (), testCase .listeners ["service" ], & tls.Config {}, testCase .listeners ["unixSocket" ], os .FileMode (0o600 ), testCase .listeners ["tcpListen" ], & tls.Config {})
103
175
104
176
if err == nil || err .Error () != testCase .expectedError {
105
- t .Errorf ("expected error %s, got %s " , testCase .expectedError , err . Error () )
177
+ t .Errorf ("expected error %s, got %v " , testCase .expectedError , err )
106
178
}
107
179
})
108
180
}
109
181
}
110
182
111
- func TestRunControlSvcOld (t * testing.T ) {
112
- // ctrl := gomock.NewController(t)
113
- // defer ctrl.Finish()
114
-
115
- // mock_netceptor := mock_controlsvc.NewMockNetceptorForControlsvc(ctrl)
116
- // s := controlsvc.New(false, mock_netceptor)
117
- // mock_unix := mock_controlsvc.NewMockUtiler(ctrl)
118
- // s.SetServerUtils(mock_unix)
119
-
120
- // mock_net_listener := mock_controlsvc.NewMockListener(ctrl)
121
- // mock_unix.EXPECT().UnixSocketListen(gomock.Any(), gomock.Any()).Return(mock_net_listener, nil, nil)
122
-
123
- // newCtx, ctxCancel := context.WithTimeout(context.Background(), time.Millisecond*1)
124
- // defer ctxCancel()
125
-
126
- // logger := logger.NewReceptorLogger("test")
127
- // mock_net_listener.EXPECT().Accept().Return(nil, errors.New("blargh"))
128
- // // mock_net_listener.EXPECT().Close()
129
- // mock_netceptor.EXPECT().GetLogger().Return(logger)
130
- // err := s.RunControlSvc(newCtx, "", &tls.Config{}, "unixSocket", os.FileMode(0o600), "", &tls.Config{})
131
- // errorString := "Error accepting connection: blargh"
132
- // fmt.Println(err, errorString)
133
- // if err == nil || err.Error() != errorString {
134
- // t.Errorf("expected error: %+v, got: %+v", errorString, err.Error())
135
- // }
136
-
137
- }
138
-
139
183
func TestSockControlRemoteAddr (t * testing.T ) {
140
184
ctrl := gomock .NewController (t )
141
185
@@ -223,7 +267,7 @@ func TestSockControlBridgeConn(t *testing.T) {
223
267
name : "with message and error" ,
224
268
message : "message" ,
225
269
expectedCalls : func () {
226
- mockCon .EXPECT ().Write (gomock .Any ()).Return (0 , errors .New ("blargh " ))
270
+ mockCon .EXPECT ().Write (gomock .Any ()).Return (0 , errors .New ("terminated " ))
227
271
},
228
272
},
229
273
}
@@ -236,7 +280,7 @@ func TestSockControlBridgeConn(t *testing.T) {
236
280
if testCase .message == "" && err != nil {
237
281
t .Errorf ("should be nil" )
238
282
}
239
- if testCase .message != "" && err .Error () != "blargh " {
283
+ if testCase .message != "" && err .Error () != "terminated " {
240
284
t .Errorf ("stuff %v" , err )
241
285
}
242
286
})
@@ -322,7 +366,7 @@ func TestSockControlWriteToConn(t *testing.T) {
322
366
errorMessage string
323
367
}{
324
368
{
325
- name : "without message and error" ,
369
+ name : "without message and with error" ,
326
370
message : "" ,
327
371
expectedCalls : func () {
328
372
mockCon .EXPECT ().Write (gomock .Any ()).Return (0 , errors .New ("write to conn chan error" ))
@@ -331,7 +375,7 @@ func TestSockControlWriteToConn(t *testing.T) {
331
375
errorMessage : "write to conn chan error" ,
332
376
},
333
377
{
334
- name : "with message and error" ,
378
+ name : "with message and with error" ,
335
379
message : "message" ,
336
380
expectedCalls : func () {
337
381
mockCon .EXPECT ().Write (gomock .Any ()).Return (0 , errors .New ("write to conn write message error" ))
@@ -354,15 +398,11 @@ func TestSockControlWriteToConn(t *testing.T) {
354
398
t .Run (testCase .name , func (t * testing.T ) {
355
399
testCase .expectedCalls ()
356
400
c := make (chan []byte )
357
- go func () {
401
+ go func (c chan [] byte ) {
358
402
c <- []byte {7 }
359
- }( )
360
- if ! testCase . expectedError {
403
+ defer close ( c )
404
+ }( c )
361
405
362
- time .AfterFunc (time .Millisecond * 100 , func () {
363
- close (c )
364
- })
365
- }
366
406
err := sockControl .WriteToConn (testCase .message , c )
367
407
368
408
if testCase .expectedError {
@@ -487,6 +527,14 @@ func TestRunControlSession(t *testing.T) {
487
527
mockCon .EXPECT ().Close ()
488
528
},
489
529
},
530
+ {
531
+ name : "logger warning - could not read in control service" ,
532
+ expectedCalls : func () {
533
+ mockCon .EXPECT ().Write (gomock .Any ()).Return (0 , nil )
534
+ mockCon .EXPECT ().Read (make ([]byte , 1 )).Return (0 , errors .New ("terminated" ))
535
+ mockCon .EXPECT ().Close ()
536
+ },
537
+ },
490
538
}
491
539
492
540
for _ , testCase := range runControlSessionTestCases {
0 commit comments