1
// Copyright 2012, 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
12
jc "github.com/juju/testing/checkers"
13
gc "gopkg.in/check.v1"
15
coretesting "github.com/juju/juju/testing"
18
var _ = gc.Suite(&SCPSuite{})
20
type SCPSuite struct {
24
var scpTests = []struct {
31
about: "scp from machine 0 to current dir",
32
args: []string{"0:foo", "."},
34
args: "ubuntu@0.public:foo .",
35
hostKeyChecking: "yes",
39
about: "scp from machine 0 to current dir with extra args",
40
args: []string{"0:foo", ".", "-rv", "-o", "SomeOption"},
42
args: "ubuntu@0.public:foo . -rv -o SomeOption",
43
hostKeyChecking: "yes",
47
about: "scp from current dir to machine 0",
48
args: []string{"foo", "0:"},
50
args: "foo ubuntu@0.public:",
51
hostKeyChecking: "yes",
55
about: "scp when no keys available",
56
args: []string{"foo", "1:"},
57
error: `retrieving SSH host keys for "1": keys not found`,
59
about: "scp when no keys available, with --no-host-key-checks",
60
args: []string{"--no-host-key-checks", "foo", "1:"},
62
args: "foo ubuntu@1.public:",
63
hostKeyChecking: "no",
67
about: "scp from current dir to machine 0 with extra args",
68
args: []string{"foo", "0:", "-r", "-v"},
70
args: "foo ubuntu@0.public: -r -v",
71
hostKeyChecking: "yes",
75
about: "scp from machine 0 to unit mysql/0",
76
args: []string{"0:foo", "mysql/0:/foo"},
78
args: "ubuntu@0.public:foo ubuntu@0.public:/foo",
79
hostKeyChecking: "yes",
83
about: "scp from machine 0 to unit mysql/0 and extra args",
84
args: []string{"0:foo", "mysql/0:/foo", "-q"},
86
args: "ubuntu@0.public:foo ubuntu@0.public:/foo -q",
87
hostKeyChecking: "yes",
91
about: "scp from machine 0 to unit mysql/0 and extra args before",
92
args: []string{"-q", "-r", "0:foo", "mysql/0:/foo"},
93
error: "flag provided but not defined: -q",
95
about: "scp two local files to unit mysql/0",
96
args: []string{"file1", "file2", "mysql/0:/foo/"},
98
args: "file1 file2 ubuntu@0.public:/foo/",
99
hostKeyChecking: "yes",
103
about: "scp from machine 0 to unit mysql/0 and multiple extra args",
104
args: []string{"0:foo", "mysql/0:", "-r", "-v", "-q", "-l5"},
106
args: "ubuntu@0.public:foo ubuntu@0.public: -r -v -q -l5",
107
hostKeyChecking: "yes",
111
about: "scp works with IPv6 addresses",
112
args: []string{"2:foo", "bar"},
114
args: `ubuntu@[2001:db8::1]:foo bar`,
115
hostKeyChecking: "yes",
119
about: "scp from machine 0 to unit mysql/0 with proxy",
120
args: []string{"--proxy=true", "0:foo", "mysql/0:/bar"},
122
args: "ubuntu@0.private:foo ubuntu@0.private:/bar",
124
hostKeyChecking: "yes",
128
about: "scp from unit mysql/0 to machine 2 with a --",
129
args: []string{"--", "-r", "-v", "mysql/0:foo", "2:", "-q", "-l5"},
131
args: "-r -v ubuntu@0.public:foo ubuntu@[2001:db8::1]: -q -l5",
132
hostKeyChecking: "yes",
136
about: "scp from unit mysql/0 to current dir as 'sam' user",
137
args: []string{"sam@mysql/0:foo", "."},
139
args: "sam@0.public:foo .",
140
hostKeyChecking: "yes",
144
about: "scp with no such machine",
145
args: []string{"5:foo", "bar"},
146
error: `machine 5 not found`,
148
about: "scp from arbitrary host name to current dir",
149
args: []string{"some.host:foo", "."},
151
args: "some.host:foo .",
155
about: "scp from arbitrary user & host to current dir",
156
args: []string{"someone@some.host:foo", "."},
158
args: "someone@some.host:foo .",
162
about: "scp with arbitrary host name and an entity",
163
args: []string{"some.host:foo", "0:"},
164
error: `can't determine host keys for all targets: consider --no-host-key-checks`,
166
about: "scp with arbitrary host name and an entity, --no-host-key-checks",
167
args: []string{"--no-host-key-checks", "some.host:foo", "0:"},
169
args: "some.host:foo ubuntu@0.public:",
170
hostKeyChecking: "no",
176
func (s *SCPSuite) TestSCPCommand(c *gc.C) {
179
for i, t := range scpTests {
180
c.Logf("test %d: %s -> %s\n", i, t.about, t.args)
182
ctx, err := coretesting.RunCommand(c, newSCPCommand(), t.args...)
184
c.Check(err, gc.ErrorMatches, t.error)
186
c.Assert(err, jc.ErrorIsNil)
187
// we suppress stdout from scp, so get the scp args used
188
// from the "scp.args" file that the fake scp executable
189
// installed by SSHCommonSuite generates.
190
c.Check(coretesting.Stderr(ctx), gc.Equals, "")
191
c.Check(coretesting.Stdout(ctx), gc.Equals, "")
192
actual, err := ioutil.ReadFile(filepath.Join(s.binDir, "scp.args"))
193
c.Assert(err, jc.ErrorIsNil)
194
t.expected.check(c, string(actual))