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
|
package git
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestGetUrlAndResolveConfig(t *testing.T) {
t.Parallel()
type args struct {
remoteURL string
resolvedAddress string
}
tests := []struct {
desc string
args args
expectedURL string
expectedConfigPair []ConfigPair
expectedErrString string
}{
{
desc: "No remote URL provided",
args: args{
remoteURL: "",
resolvedAddress: "192.0.0.1",
},
expectedURL: "",
expectedConfigPair: nil,
expectedErrString: "URL is empty",
},
{
desc: "No resolved address provided",
args: args{
remoteURL: "www.gitlab.com",
resolvedAddress: "",
},
expectedURL: "",
expectedConfigPair: nil,
expectedErrString: "resolved address is empty",
},
{
desc: "resolved address has malformed IP",
args: args{
remoteURL: "www.gitlab.com",
resolvedAddress: "abcd",
},
expectedURL: "",
expectedConfigPair: nil,
expectedErrString: "resolved address has invalid IPv4/IPv6 address",
},
{
desc: "resolved address has malformed IP",
args: args{
remoteURL: "www.gitlab.com",
resolvedAddress: "1922.23.2323.2323",
},
expectedURL: "",
expectedConfigPair: nil,
expectedErrString: "resolved address has invalid IPv4/IPv6 address",
},
{
desc: "bad URL format",
args: args{
remoteURL: "http//foo/bar",
resolvedAddress: "192.168.0.1",
},
expectedURL: "",
expectedConfigPair: nil,
expectedErrString: "invalid protocol/URL encountered: http//foo/bar",
},
{
desc: "valid http format",
args: args{
remoteURL: "http://gitlab.com/gitlab-org/gitaly.git",
resolvedAddress: "192.168.0.1",
},
expectedURL: "http://gitlab.com/gitlab-org/gitaly.git",
expectedConfigPair: []ConfigPair{{Key: "http.curloptResolve", Value: "*:80:192.168.0.1"}},
expectedErrString: "",
},
{
desc: "valid https format",
args: args{
remoteURL: "https://gitlab.com/gitlab-org/gitaly.git",
resolvedAddress: "192.168.0.1",
},
expectedURL: "https://gitlab.com/gitlab-org/gitaly.git",
expectedConfigPair: []ConfigPair{{Key: "http.curloptResolve", Value: "*:443:192.168.0.1"}},
expectedErrString: "",
},
{
desc: "valid https format with port",
args: args{
remoteURL: "https://gitlab.com:1234/gitlab-org/gitaly.git",
resolvedAddress: "192.168.0.1",
},
expectedURL: "https://gitlab.com:1234/gitlab-org/gitaly.git",
expectedConfigPair: []ConfigPair{{Key: "http.curloptResolve", Value: "*:1234:192.168.0.1"}},
expectedErrString: "",
},
{
desc: "valid ssh format",
args: args{
remoteURL: "ssh://user@gitlab.com/gitlab-org/gitaly.git",
resolvedAddress: "192.168.0.1",
},
expectedURL: "ssh://user@192.168.0.1/gitlab-org/gitaly.git",
expectedConfigPair: nil,
expectedErrString: "",
},
{
desc: "valid ssh format without username",
args: args{
remoteURL: "ssh://gitlab.com/gitlab-org/gitaly.git",
resolvedAddress: "192.168.0.1",
},
expectedURL: "ssh://192.168.0.1/gitlab-org/gitaly.git",
expectedConfigPair: nil,
expectedErrString: "",
},
{
desc: "invalid ssh format",
args: args{
remoteURL: "ssh://foo" + string(rune(0x7f)),
resolvedAddress: "192.168.0.1",
},
expectedURL: "",
expectedConfigPair: nil,
expectedErrString: "couldn't parse remoteURL",
},
{
desc: "valid ssh format (scp-like) without prefix",
args: args{
remoteURL: "user@gitlab.com:gitlab-org/gitaly.git",
resolvedAddress: "192.168.0.1",
},
expectedURL: "user@192.168.0.1:gitlab-org/gitaly.git",
expectedConfigPair: nil,
expectedErrString: "",
},
{
desc: "valid ssh format (scp-like) without prefix without user",
args: args{
remoteURL: "gitlab.com:gitlab-org/gitaly.git",
resolvedAddress: "192.168.0.1",
},
expectedURL: "192.168.0.1:gitlab-org/gitaly.git",
expectedConfigPair: nil,
expectedErrString: "",
},
{
desc: "invalid format (shouldn't fallback to other protocol)",
args: args{
remoteURL: "gitlab.com/gitlab-org/gitaly.git",
resolvedAddress: "192.168.0.1",
},
expectedURL: "",
expectedConfigPair: nil,
expectedErrString: "invalid protocol/URL encountered: gitlab.com/gitlab-org/gitaly.git",
},
{
desc: "invalid git (SCP) url provided",
args: args{
remoteURL: "git@gitlab.com/foo/bar",
resolvedAddress: "192.168.0.1",
},
expectedURL: "",
expectedConfigPair: nil,
expectedErrString: "invalid protocol/URL encountered: git@gitlab.com/foo/bar",
},
{
desc: "valid git (SCP) url provided",
args: args{
remoteURL: "git@gitlab.com:gitlab-org/security/gitlab.git",
resolvedAddress: "192.168.0.1",
},
expectedURL: "git@192.168.0.1:gitlab-org/security/gitlab.git",
expectedConfigPair: nil,
expectedErrString: "",
},
{
desc: "valid git url provided",
args: args{
remoteURL: "git://www.gitlab.com/foo/bar",
resolvedAddress: "192.168.0.1",
},
expectedURL: "git://www.gitlab.com/foo/bar",
expectedConfigPair: []ConfigPair{{Key: "http.curloptResolve", Value: "*:9418:192.168.0.1"}},
expectedErrString: "",
},
{
desc: "ssh url with slash before colon",
args: args{
remoteURL: "foo@bar/goo.com:/abc/def",
resolvedAddress: "192.168.0.1",
},
expectedURL: "",
expectedConfigPair: nil,
expectedErrString: "SSH URLs with '/' before colon are unsupported",
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
url, configPair, err := GetURLAndResolveConfig(tc.args.remoteURL, tc.args.resolvedAddress)
if tc.expectedErrString != "" {
require.Error(t, err, tc.expectedErrString)
} else {
require.NoError(t, err)
}
require.Equal(t, tc.expectedURL, url)
require.Equal(t, tc.expectedConfigPair, configPair)
})
}
}
|