Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #348 from secrethub/fix/deterministic-test-2
Browse files Browse the repository at this point in the history
Hotfix: fix test failing 50% of times
  • Loading branch information
SimonBarendse authored Sep 22, 2020
2 parents d5e091a + da83812 commit 7f52f3c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
15 changes: 8 additions & 7 deletions internals/secrethub/env_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ import (
)

type errNameCollision struct {
name string
firstPath string
secondPath string
name string
paths [2]string
}

func (e errNameCollision) Error() string {
return fmt.Sprintf("secrets at path %s and %s map to the same environment variable: %s. Rename one of the secrets or source them in a different way", e.firstPath, e.secondPath, e.name)
return fmt.Sprintf("secrets at path %s and %s map to the same environment variable: %s. Rename one of the secrets or source them in a different way", e.paths[0], e.paths[1], e.name)
}

type environment struct {
Expand Down Expand Up @@ -225,9 +224,11 @@ func (s *secretsDirEnv) env() (map[string]value, error) {
envVarName := s.envVarName(path)
if prevPath, found := paths[envVarName]; found {
return nil, errNameCollision{
name: envVarName,
firstPath: prevPath,
secondPath: path,
name: envVarName,
paths: [2]string{
prevPath,
path,
},
}
}
paths[envVarName] = path
Expand Down
30 changes: 21 additions & 9 deletions internals/secrethub/env_source_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package secrethub

import (
"sort"
"testing"

"github.com/secrethub/secrethub-go/internals/api"
Expand All @@ -18,9 +19,9 @@ func TestSecretsDirEnv(t *testing.T) {
secretUUID2 := uuid.New()

cases := map[string]struct {
newClient newClientFunc
expectedValues []string
err error
newClient newClientFunc
expectedValues []string
expectedCollission *errNameCollision
}{
"success": {
newClient: func() (secrethub.ClientInterface, error) {
Expand Down Expand Up @@ -114,10 +115,12 @@ func TestSecretsDirEnv(t *testing.T) {
},
}, nil
},
err: errNameCollision{
name: "FOO_BAR",
firstPath: "namespace/repo/foo/bar",
secondPath: "namespace/repo/foo_bar",
expectedCollission: &errNameCollision{
name: "FOO_BAR",
paths: [2]string{
"namespace/repo/foo/bar",
"namespace/repo/foo_bar",
},
},
},
}
Expand All @@ -126,8 +129,17 @@ func TestSecretsDirEnv(t *testing.T) {
t.Run(name, func(t *testing.T) {
source := newSecretsDirEnv(tc.newClient, dirPath)
secrets, err := source.env()
if tc.err != nil {
assert.Equal(t, err, tc.err)
if tc.expectedCollission != nil {
collisionErr, ok := err.(errNameCollision)
assert.Equal(t, ok, true)
assert.Equal(t, collisionErr.name, tc.expectedCollission.name)

gotPaths := collisionErr.paths[:]
expectedPaths := tc.expectedCollission.paths[:]
sort.Strings(gotPaths)
sort.Strings(expectedPaths)

assert.Equal(t, gotPaths, expectedPaths)
} else {
assert.OK(t, err)
assert.Equal(t, len(secrets), len(tc.expectedValues))
Expand Down

0 comments on commit 7f52f3c

Please sign in to comment.