Skip to content

Commit

Permalink
fix: Addresed PR feedback and added more unit testing
Browse files Browse the repository at this point in the history
Signed-off-by: Leonard Goodell <[email protected]>
  • Loading branch information
Leonard Goodell committed Jul 21, 2022
1 parent ca894e9 commit 15107ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
6 changes: 3 additions & 3 deletions bootstrap/handlers/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func MessagingBootstrapHandler(ctx context.Context, wg *sync.WaitGroup, startupT
// Make sure the MessageBus password is not leaked into the Service Config that can be retrieved via the /config endpoint
messageBusInfo := deepCopy(messageQueue)

messageBusInfo.AuthMode = strings.ToLower(strings.TrimSpace(messageBusInfo.AuthMode))
if len(messageBusInfo.AuthMode) > 0 && messageBusInfo.AuthMode != boostrapMessaging.AuthModeNone {
if len(messageBusInfo.AuthMode) > 0 &&
!strings.EqualFold(strings.TrimSpace(messageBusInfo.AuthMode), boostrapMessaging.AuthModeNone) {
if err := boostrapMessaging.SetOptionsAuthData(&messageBusInfo, lc, dic); err != nil {
lc.Error(err.Error())
lc.Errorf("setting the MessageBus auth options failed: %v", err)
return false
}
}
Expand Down
26 changes: 19 additions & 7 deletions bootstrap/handlers/messaging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-messaging/v2/messaging"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/interfaces/mocks"
Expand Down Expand Up @@ -38,7 +39,7 @@ func TestMain(m *testing.M) {
}

func TestBootstrapHandler(t *testing.T) {
validCreateClient := config.MessageBusInfo{
validCreateClientSecure := config.MessageBusInfo{
Type: messaging.Redis,
Protocol: "redis",
Host: "localhost",
Expand All @@ -48,6 +49,9 @@ func TestBootstrapHandler(t *testing.T) {
SecretName: "redisdb",
}

validCreateClientNonSecure := validCreateClientSecure
validCreateClientNonSecure.AuthMode = boostrapMessaging.AuthModeNone

invalidSecrets := config.MessageBusInfo{
AuthMode: boostrapMessaging.AuthModeCert,
SecretName: "redisdb",
Expand All @@ -65,18 +69,20 @@ func TestBootstrapHandler(t *testing.T) {
tests := []struct {
Name string
MessageQueue config.MessageBusInfo
Secure bool
ExpectedResult bool
ExpectClient bool
}{
{"Valid - creates client", validCreateClient, true, true},
{"Invalid - secrets error", invalidSecrets, false, false},
{"Invalid - can't connect", invalidNoConnect, false, false},
{"Valid secure - creates client", validCreateClientSecure, true, true, true},
{"Valid non-secure - creates client", validCreateClientNonSecure, false, true, true},
{"Invalid - secrets error", invalidSecrets, false, false, false},
{"Invalid - can't connect", invalidNoConnect, true, false, false},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
provider := &mocks.SecretProvider{}
provider.On("GetSecret", test.MessageQueue.SecretName).Return(usernameSecretData, nil)
providerMock := &mocks.SecretProvider{}
providerMock.On("GetSecret", test.MessageQueue.SecretName).Return(usernameSecretData, nil)
configMock := &mocks.Configuration{}
configMock.On("GetBootstrap").Return(config.BootstrapConfiguration{
MessageQueue: test.MessageQueue,
Expand All @@ -87,7 +93,7 @@ func TestBootstrapHandler(t *testing.T) {
return configMock
},
container.SecretProviderName: func(get di.Get) interface{} {
return provider
return providerMock
},
container.MessagingClientName: func(get di.Get) interface{} {
return nil
Expand All @@ -102,6 +108,12 @@ func TestBootstrapHandler(t *testing.T) {
} else {
assert.Nil(t, container.MessagingClientFrom(dic.Get))
}

if test.Secure {
providerMock.AssertCalled(t, "GetSecret", mock.Anything)
} else {
providerMock.AssertNotCalled(t, "GetSecret", mock.Anything)
}
})
}
}

0 comments on commit 15107ca

Please sign in to comment.