-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from xmidt-org/improve-credentials-tests
Improve the coverage for credentials.
- Loading branch information
Showing
2 changed files
with
119 additions
and
5 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,105 @@ | ||
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xmidt-org/xmidt-agent/internal/credentials" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zaptest/observer" | ||
) | ||
|
||
func Test_provideCredentials(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
in credsIn | ||
want bool | ||
wantErr bool | ||
checkLog func(assert *assert.Assertions, logs []observer.LoggedEntry) | ||
}{} | ||
for _, tc := range tests { | ||
t.Run(tc.description, func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
core, logs := observer.New(zap.InfoLevel) | ||
|
||
tc.in.Logger = zap.New(core) | ||
|
||
got, err := provideCredentials(tc.in) | ||
|
||
if tc.checkLog != nil { | ||
tc.checkLog(assert, logs.AllUntimed()) | ||
} | ||
|
||
if tc.wantErr { | ||
assert.Error(err) | ||
assert.Nil(got) | ||
return | ||
} | ||
|
||
assert.NoError(err) | ||
if tc.want { | ||
assert.NotNil(got) | ||
} else { | ||
assert.Nil(got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_credsIn_Options(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
in credsIn | ||
want []credentials.Option | ||
checkLog func(assert *assert.Assertions, logs []observer.LoggedEntry) | ||
wantErr bool | ||
}{ | ||
{ | ||
description: "No credentials service configured", | ||
checkLog: func(assert *assert.Assertions, logs []observer.LoggedEntry) { | ||
assert.Len(logs, 1) | ||
assert.Equal(zap.WarnLevel, logs[0].Level) | ||
assert.Equal("no credentials service configured", logs[0].Message) | ||
}, | ||
}, | ||
{ | ||
description: "Mostly empty, but valid", | ||
in: credsIn{ | ||
Creds: XmidtCredentials{ | ||
URL: "http://example.com", | ||
}, | ||
}, | ||
want: []credentials.Option{ | ||
credentials.URL("http://example.com"), | ||
credentials.MacAddress("mac:112233445566"), | ||
}, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.description, func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
core, logs := observer.New(zap.InfoLevel) | ||
tc.in.Logger = zap.New(core) | ||
|
||
got, err := tc.in.Options() | ||
|
||
if tc.checkLog != nil { | ||
tc.checkLog(assert, logs.AllUntimed()) | ||
} | ||
|
||
if tc.wantErr { | ||
assert.Error(err) | ||
assert.Nil(got) | ||
return | ||
} | ||
|
||
assert.NoError(err) | ||
//assert.Equal(tc.want, got) | ||
}) | ||
} | ||
} |