Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the coverage for credentials. #24

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions cmd/xmidt-agent/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@ type credsOut struct {
WaitUntilFetched time.Duration `name:"wait_until_fetched"`
}

func provideCredentials(in credsIn) (credsOut, error) {
func (in credsIn) Options() ([]credentials.Option, error) {
logger := in.Logger.Named("credentials")

// If the URL is empty, then there is no credentials service to use.
if in.Creds.URL == "" {
return credsOut{}, nil
logger.Warn("no credentials service configured")
return nil, nil
}

client, err := in.Creds.HTTPClient.NewClient()
if err != nil {
return credsOut{}, err
return nil, err
}

logger := in.Logger.Named("credentials")

opts := []credentials.Option{
credentials.URL(in.Creds.URL),
credentials.HTTPClient(client),
Expand Down Expand Up @@ -81,6 +82,14 @@ func provideCredentials(in credsIn) (credsOut, error) {
)
}

return opts, nil
}

func provideCredentials(in credsIn) (credsOut, error) {
opts, err := in.Options()
if err != nil || opts == nil {
return credsOut{}, err
}
creds, err := credentials.New(opts...)
if err != nil {
return credsOut{}, err
Expand Down
105 changes: 105 additions & 0 deletions cmd/xmidt-agent/credentials_test.go
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)
})
}
}
Loading