forked from getporter/porter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials_test.go
233 lines (202 loc) · 5.79 KB
/
credentials_test.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package porter
import (
"testing"
cnabprovider "github.com/deislabs/porter/pkg/cnab/provider"
printer "github.com/deislabs/porter/pkg/printer"
"github.com/deislabs/duffle/pkg/bundle"
"github.com/stretchr/testify/require"
)
type TestCNABProvider struct {
}
func (t *TestCNABProvider) LoadBundle(bundleFile string, insecure bool) (*bundle.Bundle, error) {
b := &bundle.Bundle{
Name: "testbundle",
Credentials: map[string]bundle.Location{
"name": bundle.Location{
EnvironmentVariable: "BLAH",
},
},
}
return b, nil
}
func (t *TestCNABProvider) Install(arguments cnabprovider.InstallArguments) error {
return nil
}
func (t *TestCNABProvider) Upgrade(arguments cnabprovider.UpgradeArguments) error {
return nil
}
func (t *TestCNABProvider) Uninstall(arguments cnabprovider.UninstallArguments) error {
return nil
}
func TestGenerateNoName(t *testing.T) {
p := NewTestPorter(t)
p.CNAB = &TestCNABProvider{}
opts := CredentialOptions{
Silent: true,
}
err := p.GenerateCredentials(opts)
require.NoError(t, err, "no error should have existed")
path, err := p.Porter.Config.GetCredentialPath("testbundle")
require.NoError(t, err, "couldn't get credential path")
_, err = p.Porter.Context.FileSystem.Stat(path)
require.NoError(t, err, "expected the file %s to exist", path)
}
func TestGenerateNameProvided(t *testing.T) {
p := NewTestPorter(t)
p.CNAB = &TestCNABProvider{}
opts := CredentialOptions{
Silent: true,
}
opts.Name = "kool-kred"
err := p.GenerateCredentials(opts)
require.NoError(t, err, "no error should have existed")
path, err := p.Porter.Config.GetCredentialPath("kool-kred")
require.NoError(t, err, "couldn't get credential path")
_, err = p.Porter.Context.FileSystem.Stat(path)
require.NoError(t, err, "expected the file %s to exist", path)
}
func TestGenerateBadNameProvided(t *testing.T) {
p := NewTestPorter(t)
p.CNAB = &TestCNABProvider{}
opts := CredentialOptions{
Silent: true,
}
opts.Name = "this.isabadname"
err := p.GenerateCredentials(opts)
require.Error(t, err, "name is invalid, we should have had an error")
path, err := p.Porter.Config.GetCredentialPath("this.isabadname")
require.NoError(t, err, "couldn't get credential path")
_, err = p.Porter.Context.FileSystem.Stat(path)
require.Error(t, err, "expected the file %s to not exist", path)
}
type CredentialsListTest struct {
name string
format printer.Format
wantContains []string
errorMsg string
}
func TestCredentialsList_None(t *testing.T) {
testcases := []CredentialsListTest{
{
name: "invalid format",
format: "wingdings",
wantContains: []string{},
errorMsg: "invalid format: wingdings",
},
{
name: "json",
format: printer.FormatJson,
wantContains: []string{"[]\n"},
errorMsg: "",
},
{
name: "yaml",
format: printer.FormatYaml,
wantContains: []string{"[]\n\n"},
errorMsg: "",
},
{
name: "table",
format: printer.FormatTable,
wantContains: []string{"NAME MODIFIED\n"},
errorMsg: "",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
p := NewTestPorter(t)
p.CNAB = &TestCNABProvider{}
listOpts := printer.PrintOptions{
Format: tc.format,
}
err := p.ListCredentials(listOpts)
if tc.errorMsg != "" {
require.Equal(t, err.Error(), tc.errorMsg)
} else {
require.NoError(t, err, "no error should have existed")
}
gotOutput := p.TestConfig.TestContext.GetOutput()
for _, contains := range tc.wantContains {
require.Contains(t, gotOutput, contains)
}
})
}
}
func TestCredentialsList(t *testing.T) {
testcases := []CredentialsListTest{
{
name: "json",
format: printer.FormatJson,
wantContains: []string{`"Name": "kool-kreds"`},
errorMsg: "",
},
{
name: "yaml",
format: printer.FormatYaml,
wantContains: []string{`- name: kool-kreds`},
errorMsg: "",
},
{
name: "table",
format: printer.FormatTable,
wantContains: []string{`NAME MODIFIED
kool-kreds now`},
errorMsg: "",
},
{
name: "error",
format: printer.FormatTable,
wantContains: []string{},
errorMsg: "",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
p := NewTestPorter(t)
p.CNAB = &TestCNABProvider{}
credsDir, err := p.TestConfig.GetCredentialsDir()
require.NoError(t, err, "no error should have existed")
p.TestConfig.TestContext.AddTestDirectory("testdata/test-creds", credsDir)
listOpts := printer.PrintOptions{
Format: tc.format,
}
err = p.ListCredentials(listOpts)
require.NoError(t, err, "no error should have existed")
gotOutput := p.TestConfig.TestContext.GetOutput()
for _, contains := range tc.wantContains {
require.Contains(t, gotOutput, contains)
}
})
}
}
func TestCredentialsList_BadCred(t *testing.T) {
testcases := []CredentialsListTest{
{
name: "unmarshal error",
format: printer.FormatTable,
wantContains: []string{
"unable to unmarshal credential set from file bad-creds.yaml: yaml: unmarshal errors",
`NAME MODIFIED
good-creds now`},
errorMsg: "",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
p := NewTestPorter(t)
p.CNAB = &TestCNABProvider{}
credsDir, err := p.TestConfig.GetCredentialsDir()
require.NoError(t, err, "no error should have existed")
p.TestConfig.TestContext.AddTestDirectory("testdata/good-and-bad-test-creds", credsDir)
listOpts := printer.PrintOptions{
Format: tc.format,
}
err = p.ListCredentials(listOpts)
require.NoError(t, err, "no error should have existed")
gotOutput := p.TestConfig.TestContext.GetOutput()
for _, contains := range tc.wantContains {
require.Contains(t, gotOutput, contains)
}
})
}
}