-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspelunker_test.go
More file actions
148 lines (137 loc) · 3.43 KB
/
Copy pathspelunker_test.go
File metadata and controls
148 lines (137 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package spelunk_test
import (
"context"
"errors"
"testing"
"github.com/detro/spelunk/v2"
"github.com/detro/spelunk/v2/types"
"github.com/detro/spelunk/v2/util"
"github.com/stretchr/testify/require"
)
// mockModifier implements types.SecretModifier for testing.
// It takes the given mod string and appends it as `_<mod>` to the resulting secret.
type mockModifier struct {
typ string
}
func (m *mockModifier) Type() string {
return m.typ
}
func (m *mockModifier) Modify(_ context.Context, secretValue string, mod string) (string, error) {
return secretValue + "_" + mod, nil
}
func TestSpelunker_DigUp(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
opts []spelunk.SpelunkerOption
coordStr string
want string
errMatch error
}{
{
name: "success with single source",
opts: []spelunk.SpelunkerOption{
spelunk.WithSource(func() types.SecretSource {
src := util.NewMockSource("test")
src.Val = "secret-value"
return src
}()),
},
coordStr: "test://loc",
want: "secret-value",
},
{
name: "success with multiple sources",
opts: []spelunk.SpelunkerOption{
spelunk.WithSource(func() types.SecretSource {
src := util.NewMockSource("src1")
src.Val = "val1"
return src
}()),
spelunk.WithSource(func() types.SecretSource {
src := util.NewMockSource("src2")
src.Val = "val2"
return src
}()),
},
coordStr: "src2://loc",
want: "val2",
},
{
name: "modifiers applied in order",
opts: []spelunk.SpelunkerOption{
spelunk.WithSource(func() types.SecretSource {
src := util.NewMockSource("src")
src.Val = "val"
return src
}()),
spelunk.WithModifier(&mockModifier{typ: "mod1"}),
spelunk.WithModifier(&mockModifier{typ: "mod2"}),
},
coordStr: "src://loc?mod1=a&mod2=b&mod1=c",
want: "val_a_b_c",
},
{
name: "trim value by default",
opts: []spelunk.SpelunkerOption{
spelunk.WithSource(func() types.SecretSource {
src := util.NewMockSource("test")
src.Val = " secret \n"
return src
}()),
},
coordStr: "test://loc",
want: "secret",
},
{
name: "disable trim value",
opts: []spelunk.SpelunkerOption{
spelunk.WithSource(func() types.SecretSource {
src := util.NewMockSource("test")
src.Val = " secret \n"
return src
}()),
spelunk.WithoutTrimValue(),
},
coordStr: "test://loc",
want: " secret \n",
},
{
name: "unsupported source type",
opts: []spelunk.SpelunkerOption{},
coordStr: "unknown://loc",
want: "",
errMatch: spelunk.ErrUnsupportedSecretSourceType,
},
{
name: "source returns error",
opts: []spelunk.SpelunkerOption{
spelunk.WithSource(func() types.SecretSource {
src := util.NewMockSource("fail")
src.Err = errors.New("boom")
return src
}()),
},
coordStr: "fail://loc",
want: "",
errMatch: spelunk.ErrFailedToDigUpSecret,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
coord, err := types.NewSecretCoord(tt.coordStr)
require.NoError(t, err)
spelunker := spelunk.NewSpelunker(tt.opts...)
got, err := spelunker.DigUp(ctx, coord)
if tt.errMatch != nil {
require.ErrorIs(t, err, tt.errMatch)
if tt.name == "source returns error" {
require.ErrorContains(t, err, "boom")
}
return
}
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}