-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvalidation_test.go
170 lines (167 loc) · 4.27 KB
/
validation_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
package render
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestValidateAndCanonicalizeRequest(t *testing.T) {
testCases := []struct {
name string
req Request
assertions func(*testing.T, Request, error)
}{
{
name: "no input source specified",
req: Request{},
assertions: func(t *testing.T, _ Request, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "no input source specified")
},
},
{
name: "input source is ambiguous",
req: Request{
RepoURL: "fake-url",
LocalInPath: "/some/path",
},
assertions: func(t *testing.T, _ Request, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "input source is ambiguous")
},
},
{
name: "local input path and git ref incorrectly used together",
req: Request{
LocalInPath: "/some/path",
Ref: "1abcdef2",
},
assertions: func(t *testing.T, _ Request, err error) {
require.Error(t, err)
require.Contains(
t,
err.Error(),
"LocalInPath and Ref are mutually exclusive",
)
},
},
{
name: "output destination is ambiguous",
req: Request{
LocalOutPath: "/some/path",
Stdout: true,
},
assertions: func(t *testing.T, _ Request, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "output destination is ambiguous")
},
},
{
name: "invalid RepoURL",
req: Request{
RepoURL: "fake-url",
},
assertions: func(t *testing.T, _ Request, err error) {
require.Error(t, err)
require.Contains(
t,
err.Error(),
"does not appear to be a valid git repository URL",
)
},
},
{
name: "missing TargetBranch",
req: Request{
RepoURL: "https://github.com/akuity/foobar",
RepoCreds: RepoCredentials{
Password: "foobar",
},
Ref: "1abcdef2",
},
assertions: func(t *testing.T, _ Request, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "TargetBranch is a required field")
},
},
{
name: "invalid TargetBranch",
req: Request{
RepoURL: "https://github.com/akuity/foobar",
RepoCreds: RepoCredentials{
Password: "foobar",
},
Ref: "1abcdef2",
TargetBranch: "env/dev*", // * is an invalid character
},
assertions: func(t *testing.T, _ Request, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "is an invalid branch name")
},
},
{
name: "empty string image",
req: Request{
RepoURL: "https://github.com/akuity/foobar",
RepoCreds: RepoCredentials{
Password: "foobar",
},
Ref: "1abcdef2",
TargetBranch: "env/dev",
Images: []string{""}, // no good
},
assertions: func(t *testing.T, _ Request, err error) {
require.Error(t, err)
require.Contains(
t,
err.Error(),
"Images must not contain any empty strings",
)
},
},
{
name: "LocalInPath does not exist",
req: Request{
LocalInPath: "/some/path/that/does/not/exist",
},
assertions: func(t *testing.T, _ Request, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "does not exist")
},
},
{
name: "LocalOutPath exists",
req: Request{
LocalOutPath: t.TempDir(),
},
assertions: func(t *testing.T, _ Request, err error) {
require.Error(t, err)
require.Contains(t, err.Error(), "already exists; refusing to overwrite")
},
},
{
name: "validation succeeds",
req: Request{
RepoURL: " https://github.com/akuity/foobar ",
RepoCreds: RepoCredentials{
Password: " foobar ",
},
Ref: " 1abcdef2 ",
TargetBranch: " refs/heads/env/dev ",
Images: []string{" akuity/some-image "}, // no good
},
assertions: func(t *testing.T, req Request, err error) {
require.NoError(t, err)
require.Equal(t, "https://github.com/akuity/foobar", req.RepoURL)
require.Equal(t, "foobar", req.RepoCreds.Password)
require.Equal(t, "1abcdef2", req.Ref)
require.Equal(t, "env/dev", req.TargetBranch)
require.Equal(t, []string{"akuity/some-image"}, req.Images)
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
err := testCase.req.canonicalizeAndValidate()
testCase.assertions(t, testCase.req, err)
})
}
}