Skip to content

Commit

Permalink
Merge pull request #24 from xmidt-org/improve-credentials-tests
Browse files Browse the repository at this point in the history
Improve the coverage for credentials.
  • Loading branch information
schmidtw authored May 17, 2024
2 parents 9c8b2f1 + 104c2aa commit c94f6ff
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 5 deletions.
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)
})
}
}

0 comments on commit c94f6ff

Please sign in to comment.