Skip to content

Commit 1267136

Browse files
committed
cli/connhelper/ssh: TestParseURL: various improvements
- use designated example domains as example value - swap "expected" and "actual" values in assertions - add doc / name for each test - add test-cases for remote commands - also test the Spec that's produced, not just the args - merge two test-cases that could be combined Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent cf87480 commit 1267136

1 file changed

Lines changed: 76 additions & 17 deletions

File tree

cli/connhelper/ssh/ssh_test.go

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,118 @@ import (
99

1010
func TestParseURL(t *testing.T) {
1111
testCases := []struct {
12+
doc string
1213
url string
14+
remoteCommand []string
1315
expectedArgs []string
1416
expectedError string
17+
expectedSpec Spec
1518
}{
1619
{
17-
url: "ssh://foo",
20+
doc: "bare ssh URL",
21+
url: "ssh://example.com",
1822
expectedArgs: []string{
19-
"--", "foo",
23+
"--", "example.com",
24+
},
25+
expectedSpec: Spec{
26+
Host: "example.com",
2027
},
2128
},
2229
{
23-
url: "ssh://me@foo:10022",
30+
doc: "bare ssh URL and remote command",
31+
url: "ssh://example.com",
32+
remoteCommand: []string{
33+
"docker", "system", "dial-stdio",
34+
},
2435
expectedArgs: []string{
25-
"-l", "me",
26-
"-p", "10022",
27-
"--", "foo",
36+
"--", "example.com",
37+
"docker", "system", "dial-stdio",
38+
},
39+
expectedSpec: Spec{
40+
Host: "example.com",
2841
},
2942
},
3043
{
31-
url: "ssh://me:passw0rd@foo",
32-
expectedError: "plain-text password is not supported",
44+
doc: "ssh URL with path and remote command and flag",
45+
url: "ssh://example.com/var/run/docker.sock",
46+
remoteCommand: []string{
47+
"docker", "--host", "unix:///var/run/docker.sock", "system", "dial-stdio",
48+
},
49+
expectedArgs: []string{
50+
"--", "example.com",
51+
"docker", "--host", "unix:///var/run/docker.sock", "system", "dial-stdio",
52+
},
53+
expectedSpec: Spec{
54+
Host: "example.com",
55+
Path: "/var/run/docker.sock",
56+
},
3357
},
3458
{
35-
url: "ssh://foo/bar",
59+
doc: "ssh URL with username and port",
60+
url: "ssh://me@example.com:10022",
3661
expectedArgs: []string{
37-
"--", "foo",
62+
"-l", "me",
63+
"-p", "10022",
64+
"--", "example.com",
3865
},
66+
expectedSpec: Spec{
67+
User: "me",
68+
Host: "example.com",
69+
Port: "10022",
70+
},
71+
},
72+
{
73+
doc: "ssh URL with username, port, and path",
74+
url: "ssh://me@example.com:10022/var/run/docker.sock",
75+
expectedArgs: []string{
76+
"-l", "me",
77+
"-p", "10022",
78+
"--", "example.com",
79+
},
80+
expectedSpec: Spec{
81+
User: "me",
82+
Host: "example.com",
83+
Port: "10022",
84+
Path: "/var/run/docker.sock",
85+
},
86+
},
87+
{
88+
doc: "invalid URL with password",
89+
url: "ssh://me:passw0rd@example.com",
90+
expectedError: "plain-text password is not supported",
3991
},
4092
{
41-
url: "ssh://foo?bar",
93+
doc: "invalid URL with query parameter",
94+
url: "ssh://example.com?bar",
4295
expectedError: `extra query after the host: "bar"`,
4396
},
4497
{
45-
url: "ssh://foo#bar",
98+
doc: "invalid URL with fragment",
99+
url: "ssh://example.com#bar",
46100
expectedError: `extra fragment after the host: "bar"`,
47101
},
48102
{
103+
doc: "invalid URL without hostname",
49104
url: "ssh://",
50105
expectedError: "no host specified",
51106
},
52107
{
53-
url: "foo://bar",
54-
expectedError: `expected scheme ssh, got "foo"`,
108+
doc: "invalid URL with unsupported scheme",
109+
url: "https://example.com",
110+
expectedError: `expected scheme ssh, got "https"`,
55111
},
56112
}
57113
for _, tc := range testCases {
58-
t.Run(tc.url, func(t *testing.T) {
114+
t.Run(tc.doc, func(t *testing.T) {
59115
sp, err := ParseURL(tc.url)
60116
if tc.expectedError == "" {
61117
assert.NilError(t, err)
62-
assert.Check(t, is.DeepEqual(tc.expectedArgs, sp.Args()))
118+
actualArgs := sp.Args(tc.remoteCommand...)
119+
assert.Check(t, is.DeepEqual(actualArgs, tc.expectedArgs))
120+
assert.Check(t, is.Equal(*sp, tc.expectedSpec))
63121
} else {
64-
assert.ErrorContains(t, err, tc.expectedError)
122+
assert.Check(t, is.Error(err, tc.expectedError))
123+
assert.Check(t, is.Nil(sp))
65124
}
66125
})
67126
}

0 commit comments

Comments
 (0)