Skip to content

Commit 8e11c8d

Browse files
committed
fix: unit tests adjusted
Add AZKV tests add cfgmanager test
1 parent 0a7a1a8 commit 8e11c8d

File tree

9 files changed

+258
-41
lines changed

9 files changed

+258
-41
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
OWNER := dnitsch
33
NAME := configmanager
4-
GIT_TAG := "1.1.0"
5-
VERSION := "v1.1.0"
4+
GIT_TAG := "1.2.0"
5+
VERSION := "v1.2.0"
66
# VERSION := "$(shell git describe --tags --abbrev=0)"
77
REVISION := $(shell git rev-parse --short HEAD)
88

@@ -48,4 +48,4 @@ tag:
4848
git tag "v$(GIT_TAG)"
4949
git push origin "v$(GIT_TAG)"
5050

51-
tagbuildrelease: tag cross-build release
51+
tagbuildrelease: tag cross-build release

configmanager.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,29 @@ type ConfigManager struct{}
1919
// Retrieve gets a rawMap from a set implementation
2020
// will be empty if no matches found
2121
func (c *ConfigManager) Retrieve(tokens []string, config generator.GenVarsConfig) (generator.ParsedMap, error) {
22-
gv := generator.NewGenerator()
23-
gv.WithConfig(&config)
22+
gv := generator.NewGenerator().WithConfig(&config)
23+
return retrieve(tokens, gv)
24+
}
25+
26+
func retrieve(tokens []string, gv generator.Generatoriface) (generator.ParsedMap, error) {
2427
return gv.Generate(tokens)
2528
}
2629

2730
// RetrieveWithInputReplaced parses given input against all possible token strings
2831
// using regex to grab a list of found tokens in the given string
2932
func (c *ConfigManager) RetrieveWithInputReplaced(input string, config generator.GenVarsConfig) (string, error) {
33+
gv := generator.NewGenerator().WithConfig(&config)
34+
return retrieveWithInputReplaced(input, gv)
35+
}
36+
37+
func retrieveWithInputReplaced(input string, gv generator.Generatoriface) (string, error) {
3038
tokens := []string{}
3139
for k := range generator.VarPrefix {
3240
matches := regexp.MustCompile(`(?s)`+regexp.QuoteMeta(k)+`.([^\"]+)`).FindAllString(input, -1)
3341
tokens = append(tokens, matches...)
3442
}
3543

36-
cnf := generator.GenVarsConfig{}
37-
m, err := c.Retrieve(tokens, cnf)
44+
m, err := retrieve(tokens, gv)
3845

3946
if err != nil {
4047
return "", err

configmanager_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package configmanager
2+
3+
import (
4+
"testing"
5+
6+
"github.com/dnitsch/configmanager/internal/testutils"
7+
"github.com/dnitsch/configmanager/pkg/generator"
8+
)
9+
10+
type mockGenVars struct{}
11+
12+
var (
13+
testKey = "FOO#/test"
14+
testVal = "val1"
15+
)
16+
17+
func (m *mockGenVars) Generate(tokens []string) (generator.ParsedMap, error) {
18+
pm := generator.ParsedMap{}
19+
pm[testKey] = testVal
20+
return pm, nil
21+
}
22+
23+
func (m *mockGenVars) ConvertToExportVar() {
24+
}
25+
26+
func (m *mockGenVars) FlushToFile() (string, error) {
27+
return "", nil
28+
}
29+
30+
func Test_retrieve(t *testing.T) {
31+
tests := []struct {
32+
name string
33+
tokens []string
34+
genvar generator.Generatoriface
35+
expectKey string
36+
expectVal string
37+
}{
38+
{
39+
name: "standard",
40+
tokens: []string{"FOO#/test"},
41+
genvar: &mockGenVars{},
42+
expectKey: testKey,
43+
expectVal: testVal,
44+
},
45+
}
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
pm, err := retrieve(tt.tokens, tt.genvar)
49+
if err != nil {
50+
t.Errorf(testutils.TestPhrase, nil, err)
51+
}
52+
for k, v := range pm {
53+
if k != tt.expectKey {
54+
t.Errorf(testutils.TestPhrase, tt.expectKey, k)
55+
}
56+
if v != tt.expectVal {
57+
t.Errorf(testutils.TestPhrase, tt.expectVal, k)
58+
}
59+
}
60+
})
61+
}
62+
}

pkg/generator/generator.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ const (
1919
//
2020
// secretMgrPrefix = "AWSSECRETS"
2121
// paramStorePrefix = "AWSPARAMSTR"
22-
SecretMgrPrefix = "AWSSECRETS"
23-
ParamStorePrefix = "AWSPARAMSTR"
24-
AzKeyVaultPrefix = "AZKEYVAULT"
22+
SecretMgrPrefix = "AWSSECRETS"
23+
ParamStorePrefix = "AWSPARAMSTR"
24+
AzKeyVaultSecretsPrefix = "AZKVSECRET"
2525
)
2626

2727
var (
@@ -154,8 +154,8 @@ func (c *GenVars) retrieveSpecific(prefix, in string) (string, error) {
154154
c.setImplementation(paramStr)
155155
c.setToken(in)
156156
return c.getTokenValue()
157-
case AzKeyVaultPrefix:
158-
azKv, err := NewKvStoreWithToken(c.ctx, in, c.config.tokenSeparator, c.config.keySeparator)
157+
case AzKeyVaultSecretsPrefix:
158+
azKv, err := NewKvScrtStoreWithToken(c.ctx, in, c.config.tokenSeparator, c.config.keySeparator)
159159
if err != nil {
160160
return "", err
161161
}
@@ -262,7 +262,7 @@ func (c *GenVars) stripPrefix(in, prefix string) string {
262262
return stripPrefix(in, prefix, c.config.tokenSeparator, c.config.keySeparator)
263263
}
264264

265-
// stripPrefix
265+
// stripPrefix
266266
func stripPrefix(in, prefix, tokenSeparator, keySeparator string) string {
267267
t := in
268268
b := regexp.MustCompile(`[|].*`).ReplaceAll([]byte(t), []byte(""))

pkg/generator/generator_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package generator
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/dnitsch/configmanager/internal/testutils"
@@ -182,22 +183,22 @@ func Test_ConvertToExportVars(t *testing.T) {
182183
tests := []struct {
183184
name string
184185
rawMap ParsedMap
185-
expect []string
186+
expect string
186187
}{
187188
{
188189
name: "number included",
189190
rawMap: ParsedMap{"foo": "BAR", "num": 123},
190-
expect: []string{`export FOO='BAR'`, `export NUM=123`},
191+
expect: `export FOO='BAR'`,
191192
},
192193
{
193194
name: "strings only",
194195
rawMap: ParsedMap{"foo": "BAR", "num": "a123"},
195-
expect: []string{`export FOO='BAR'`, `export NUM='a123'`},
196+
expect: `export FOO='BAR'`,
196197
},
197198
{
198199
name: "numbers only",
199200
rawMap: ParsedMap{"foo": 123, "num": 456},
200-
expect: []string{`export FOO=123`, `export NUM=456`},
201+
expect: `export FOO=123`,
201202
},
202203
}
203204
for _, tt := range tests {
@@ -210,14 +211,13 @@ func Test_ConvertToExportVars(t *testing.T) {
210211
if got == nil {
211212
t.Errorf(testutils.TestPhrase, "not nil", got)
212213
}
213-
if len(tt.expect) != len(got) {
214-
t.Errorf(testutils.TestPhrase, len(tt.expect), len(got))
214+
if 2 != len(got) {
215+
t.Errorf(testutils.TestPhrase, 2, len(got))
215216

216217
}
217-
for k, v := range got {
218-
if v != tt.expect[k] {
219-
t.Errorf(testutils.TestPhrase, tt.expect[k], got[k])
220-
}
218+
st := strings.Join(got, "\n")
219+
if !strings.Contains(st, tt.expect) {
220+
t.Errorf(testutils.TestPhrase, tt.expect, st)
221221
}
222222
})
223223
}

pkg/generator/keyvault.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type kvApi interface {
2121
GetSecret(ctx context.Context, name string, version string, options *azsecrets.GetSecretOptions) (azsecrets.GetSecretResponse, error)
2222
}
2323

24-
type KvStore struct {
24+
type KvScrtStore struct {
2525
svc kvApi
2626
token string
2727
}
@@ -32,18 +32,18 @@ type azVaultHelper struct {
3232
token string
3333
}
3434

35-
// NewKvStore returns a KvStore
35+
// NewKvScrtStore returns a KvStore
3636
// requires `AZURE_SUBSCRIPTION_ID` environment variable to be present to successfuly work
37-
func NewKvStore(ctx context.Context) (*KvStore, error) {
38-
return &KvStore{}, nil
37+
func NewKvScrtStore(ctx context.Context) (*KvScrtStore, error) {
38+
return &KvScrtStore{}, nil
3939
}
4040

4141
// NewKvStore returns a KvStore
4242
// requires `AZURE_SUBSCRIPTION_ID` environment variable to be present to successfuly work
43-
func NewKvStoreWithToken(ctx context.Context, token, tokenSeparator, keySeparator string) (*KvStore, error) {
43+
func NewKvScrtStoreWithToken(ctx context.Context, token, tokenSeparator, keySeparator string) (*KvScrtStore, error) {
4444

4545
//
46-
conf := azSplitToken(stripPrefix(token, AzKeyVaultPrefix, tokenSeparator, keySeparator))
46+
conf := azSplitToken(stripPrefix(token, AzKeyVaultSecretsPrefix, tokenSeparator, keySeparator))
4747

4848
cred, err := azidentity.NewDefaultAzureCredential(nil)
4949
if err != nil {
@@ -53,20 +53,20 @@ func NewKvStoreWithToken(ctx context.Context, token, tokenSeparator, keySeparato
5353

5454
c := azsecrets.NewClient(conf.vaultUri, cred, nil)
5555

56-
return &KvStore{
56+
return &KvScrtStore{
5757
svc: c,
5858
token: conf.token,
5959
}, nil
6060
}
6161

62-
func (paramStr *KvStore) setToken(token string) {
62+
func (paramStr *KvScrtStore) setToken(token string) {
6363
paramStr.token = token
6464
}
6565

66-
func (implmt *KvStore) setValue(val string) {
66+
func (implmt *KvScrtStore) setValue(val string) {
6767
}
6868

69-
func (imp *KvStore) getTokenValue(v *GenVars) (string, error) {
69+
func (imp *KvScrtStore) getTokenValue(v *GenVars) (string, error) {
7070
log.Infof("%s", "Concrete implementation AzKeyVault Secret")
7171
log.Infof("AzKeyVault Token: %s", imp.token)
7272

pkg/generator/keyvault_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package generator
22

33
import (
4+
"context"
5+
"fmt"
6+
"strings"
47
"testing"
58

9+
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets"
610
"github.com/dnitsch/configmanager/internal/testutils"
711
)
812

@@ -36,6 +40,14 @@ func Test_azSplitToken(t *testing.T) {
3640
token: "some/json/test",
3741
},
3842
},
43+
{
44+
name: "with_initial_slash_multislash_secretname",
45+
token: "test-vault//some/json/test",
46+
expect: azVaultHelper{
47+
vaultUri: "https://test-vault.vault.azure.net",
48+
token: "/some/json/test",
49+
},
50+
},
3951
}
4052
for _, tt := range tests {
4153
t.Run(tt.name, func(t *testing.T) {
@@ -49,3 +61,106 @@ func Test_azSplitToken(t *testing.T) {
4961
})
5062
}
5163
}
64+
65+
var (
66+
tazkvsuccessObj map[string]string = map[string]string{fmt.Sprintf("%s#/token/1", AzKeyVaultSecretsPrefix): tsuccessParam}
67+
)
68+
69+
type mockAzKvSecretApi func(ctx context.Context, name string, version string, options *azsecrets.GetSecretOptions) (azsecrets.GetSecretResponse, error)
70+
71+
func (m mockAzKvSecretApi) GetSecret(ctx context.Context, name string, version string, options *azsecrets.GetSecretOptions) (azsecrets.GetSecretResponse, error) {
72+
return m(ctx, name, version, options)
73+
}
74+
75+
func Test_GetAzKeyVaultSecretVarHappy(t *testing.T) {
76+
tests := []struct {
77+
name string
78+
token string
79+
value string
80+
mockClient func(t *testing.T) kvApi
81+
genVars *GenVars
82+
}{
83+
{
84+
name: "successVal",
85+
token: "AZKVSECRET#/test-vault//token/1",
86+
value: tsuccessParam,
87+
mockClient: func(t *testing.T) kvApi {
88+
return mockAzKvSecretApi(func(ctx context.Context, name string, version string, options *azsecrets.GetSecretOptions) (azsecrets.GetSecretResponse, error) {
89+
t.Helper()
90+
if name == "" {
91+
t.Errorf("expect name to not be nil")
92+
}
93+
if name != "/token/1" {
94+
t.Errorf(testutils.TestPhrase, "/token/1", name)
95+
}
96+
97+
if strings.Contains(name, "#") {
98+
t.Errorf("incorrectly stripped token separator")
99+
}
100+
101+
if strings.Contains(name, AzKeyVaultSecretsPrefix) {
102+
t.Errorf("incorrectly stripped prefix")
103+
}
104+
105+
if version != "" {
106+
t.Fatal("expect version to be \"\" an empty string ")
107+
}
108+
109+
resp := azsecrets.GetSecretResponse{}
110+
resp.Value = &tsuccessParam
111+
return resp, nil
112+
})
113+
},
114+
genVars: &GenVars{},
115+
},
116+
{
117+
name: "successVal with keyseparator",
118+
token: "AZKVSECRET#/test-vault/token/1|somekey",
119+
value: tsuccessParam,
120+
mockClient: func(t *testing.T) kvApi {
121+
return mockAzKvSecretApi(func(ctx context.Context, name string, version string, options *azsecrets.GetSecretOptions) (azsecrets.GetSecretResponse, error) {
122+
t.Helper()
123+
if name == "" {
124+
t.Error("expect name to not be nil")
125+
}
126+
if name != "token/1" {
127+
t.Errorf(testutils.TestPhrase, "token/1", name)
128+
}
129+
if strings.Contains(name, "#") {
130+
t.Errorf("incorrectly stripped token separator")
131+
}
132+
133+
if strings.Contains(name, AzKeyVaultSecretsPrefix) {
134+
t.Errorf("incorrectly stripped prefix")
135+
}
136+
137+
if version != "" {
138+
t.Fatal("expect version to be \"\" an empty string ")
139+
}
140+
resp := azsecrets.GetSecretResponse{}
141+
resp.Value = &tsuccessParam
142+
return resp, nil
143+
})
144+
},
145+
genVars: &GenVars{},
146+
},
147+
}
148+
for _, tt := range tests {
149+
t.Run(tt.name, func(t *testing.T) {
150+
tt.genVars.config = GenVarsConfig{tokenSeparator: tokenSeparator}
151+
kvStr, err := NewKvScrtStoreWithToken(context.TODO(), tt.token, "#", "|")
152+
if err != nil {
153+
t.Errorf("failed to init azkvstore")
154+
}
155+
kvStr.svc = tt.mockClient(t)
156+
tt.genVars.setImplementation(kvStr)
157+
want, err := tt.genVars.getTokenValue()
158+
if err != nil {
159+
t.Errorf("%v", err)
160+
}
161+
if want != tt.value {
162+
t.Errorf(testutils.TestPhrase, want, tt.value)
163+
}
164+
})
165+
}
166+
}

0 commit comments

Comments
 (0)