-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for controlsvc control types
- Loading branch information
1 parent
7aee413
commit 989ce23
Showing
11 changed files
with
705 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
package controlsvc_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/ansible/receptor/pkg/controlsvc" | ||
"github.com/ansible/receptor/pkg/controlsvc/mock_controlsvc" | ||
"github.com/ansible/receptor/pkg/logger" | ||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
func CheckExpectedError(expectedError bool, errorMessage string, t *testing.T, err error) { | ||
if expectedError { | ||
if errorMessage != err.Error() { | ||
t.Errorf("expected: %s , received: %s", errorMessage, err) | ||
} | ||
} | ||
|
||
if !expectedError { | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
} | ||
|
||
func TestConnectInitFromString(t *testing.T) { | ||
connectCommandType := controlsvc.ConnectCommandType{} | ||
|
||
initFromStringTestCases := []struct { | ||
name string | ||
expectedError bool | ||
errorMessage string | ||
input string | ||
}{ | ||
{ | ||
name: "no connect target", | ||
expectedError: true, | ||
errorMessage: "no connect target", | ||
input: "", | ||
}, | ||
{ | ||
name: "too many parameters", | ||
expectedError: true, | ||
errorMessage: "too many parameters", | ||
input: "one two three four", | ||
}, | ||
{ | ||
name: "three params - pass", | ||
expectedError: false, | ||
errorMessage: "", | ||
input: "one two three", | ||
}, | ||
} | ||
|
||
for _, testCase := range initFromStringTestCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
_, err := connectCommandType.InitFromString(testCase.input) | ||
|
||
CheckExpectedError(testCase.expectedError, testCase.errorMessage, t, err) | ||
}) | ||
} | ||
|
||
} | ||
|
||
func TestConnectInitFromJSON(t *testing.T) { | ||
connectCommandType := controlsvc.ConnectCommandType{} | ||
|
||
initFromJSONTestCases := []struct { | ||
name string | ||
expectedError bool | ||
errorMessage string | ||
input map[string]interface{} | ||
}{ | ||
{ | ||
name: "no connect target node", | ||
expectedError: true, | ||
errorMessage: "no connect target node", | ||
input: map[string]interface{}{}, | ||
}, | ||
{ | ||
name: "connect target node must be string", | ||
expectedError: true, | ||
errorMessage: "connect target node must be string", | ||
input: map[string]interface{}{ | ||
"node": 7, | ||
}, | ||
}, | ||
{ | ||
name: "no connect target service", | ||
expectedError: true, | ||
errorMessage: "no connect target service", | ||
input: map[string]interface{}{ | ||
"node": "node1", | ||
}, | ||
}, | ||
{ | ||
name: "connect target service must be string", | ||
expectedError: true, | ||
errorMessage: "connect target service must be string", | ||
input: map[string]interface{}{ | ||
"node": "node2", | ||
"service": 7, | ||
}, | ||
}, | ||
{ | ||
name: "connect tls name must be string", | ||
expectedError: true, | ||
errorMessage: "connect tls name must be string", | ||
input: map[string]interface{}{ | ||
"node": "node3", | ||
"service": "service1", | ||
"tls": 7, | ||
}, | ||
}, | ||
{ | ||
name: "pass with empty tls config", | ||
expectedError: false, | ||
errorMessage: "connect target service must be string", | ||
input: map[string]interface{}{ | ||
"node": "node4", | ||
"service": "service2", | ||
}, | ||
}, | ||
{ | ||
name: "pass with all targets and tls config", | ||
expectedError: false, | ||
errorMessage: "", | ||
input: map[string]interface{}{ | ||
"node": "node5", | ||
"service": "service3", | ||
"tls": "tls1", | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range initFromJSONTestCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
_, err := connectCommandType.InitFromJSON(testCase.input) | ||
CheckExpectedError(testCase.expectedError, testCase.errorMessage, t, err) | ||
}) | ||
} | ||
|
||
} | ||
|
||
func TestConnectControlFunc(t *testing.T) { | ||
connectCommand := controlsvc.ConnectCommand{} | ||
ctrl := gomock.NewController(t) | ||
mockNetceptor := mock_controlsvc.NewMockNetceptorForControlsvc(ctrl) | ||
mockControlFunc := mock_controlsvc.NewMockControlFuncOperations(ctrl) | ||
logger := logger.NewReceptorLogger("") | ||
|
||
controlFuncTestCases := []struct { | ||
name string | ||
expectedError bool | ||
errorMessage string | ||
expectedCalls func() | ||
}{ | ||
{ | ||
name: "tls config error", | ||
expectedError: true, | ||
errorMessage: "terminated tls", | ||
expectedCalls: func() { | ||
mockNetceptor.EXPECT().GetClientTLSConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("terminated tls")) | ||
}, | ||
}, | ||
{ | ||
name: "dial error", | ||
errorMessage: "terminated dial", | ||
expectedError: true, | ||
expectedCalls: func() { | ||
mockNetceptor.EXPECT().GetClientTLSConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil) | ||
mockNetceptor.EXPECT().Dial(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("terminated dial")) | ||
}, | ||
}, | ||
{ | ||
name: "bridge conn error", | ||
errorMessage: "terminated bridge conn", | ||
expectedError: true, | ||
expectedCalls: func() { | ||
mockNetceptor.EXPECT().GetClientTLSConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil) | ||
mockNetceptor.EXPECT().Dial(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil) | ||
mockControlFunc.EXPECT().BridgeConn(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("terminated bridge conn")) | ||
mockNetceptor.EXPECT().GetLogger().Return(logger) | ||
}, | ||
}, | ||
{ | ||
name: "control func pass", | ||
errorMessage: "", | ||
expectedError: false, | ||
expectedCalls: func() { | ||
mockNetceptor.EXPECT().GetClientTLSConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil) | ||
mockNetceptor.EXPECT().Dial(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil) | ||
mockControlFunc.EXPECT().BridgeConn(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) | ||
mockNetceptor.EXPECT().GetLogger().Return(logger) | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range controlFuncTestCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
testCase.expectedCalls() | ||
_, err := connectCommand.ControlFunc(context.Background(), mockNetceptor, mockControlFunc) | ||
|
||
CheckExpectedError(testCase.expectedError, testCase.errorMessage, t, err) | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.