github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/commands/scp_unix_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build !windows
     5  
     6  package commands
     7  
     8  import (
     9  	"io/ioutil"
    10  	"path/filepath"
    11  
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	coretesting "github.com/juju/juju/testing"
    16  )
    17  
    18  var _ = gc.Suite(&SCPSuite{})
    19  
    20  type SCPSuite struct {
    21  	SSHCommonSuite
    22  }
    23  
    24  var scpTests = []struct {
    25  	about    string
    26  	args     []string
    27  	expected argsSpec
    28  	error    string
    29  }{
    30  	{
    31  		about: "scp from machine 0 to current dir",
    32  		args:  []string{"0:foo", "."},
    33  		expected: argsSpec{
    34  			args:            "ubuntu@0.public:foo .",
    35  			hostKeyChecking: "yes",
    36  			knownHosts:      "0",
    37  		},
    38  	}, {
    39  		about: "scp from machine 0 to current dir with extra args",
    40  		args:  []string{"0:foo", ".", "-rv", "-o", "SomeOption"},
    41  		expected: argsSpec{
    42  			args:            "ubuntu@0.public:foo . -rv -o SomeOption",
    43  			hostKeyChecking: "yes",
    44  			knownHosts:      "0",
    45  		},
    46  	}, {
    47  		about: "scp from current dir to machine 0",
    48  		args:  []string{"foo", "0:"},
    49  		expected: argsSpec{
    50  			args:            "foo ubuntu@0.public:",
    51  			hostKeyChecking: "yes",
    52  			knownHosts:      "0",
    53  		},
    54  	}, {
    55  		about: "scp when no keys available",
    56  		args:  []string{"foo", "1:"},
    57  		error: `retrieving SSH host keys for "1": keys not found`,
    58  	}, {
    59  		about: "scp when no keys available, with --no-host-key-checks",
    60  		args:  []string{"--no-host-key-checks", "foo", "1:"},
    61  		expected: argsSpec{
    62  			args:            "foo ubuntu@1.public:",
    63  			hostKeyChecking: "no",
    64  			knownHosts:      "null",
    65  		},
    66  	}, {
    67  		about: "scp from current dir to machine 0 with extra args",
    68  		args:  []string{"foo", "0:", "-r", "-v"},
    69  		expected: argsSpec{
    70  			args:            "foo ubuntu@0.public: -r -v",
    71  			hostKeyChecking: "yes",
    72  			knownHosts:      "0",
    73  		},
    74  	}, {
    75  		about: "scp from machine 0 to unit mysql/0",
    76  		args:  []string{"0:foo", "mysql/0:/foo"},
    77  		expected: argsSpec{
    78  			args:            "ubuntu@0.public:foo ubuntu@0.public:/foo",
    79  			hostKeyChecking: "yes",
    80  			knownHosts:      "0",
    81  		},
    82  	}, {
    83  		about: "scp from machine 0 to unit mysql/0 and extra args",
    84  		args:  []string{"0:foo", "mysql/0:/foo", "-q"},
    85  		expected: argsSpec{
    86  			args:            "ubuntu@0.public:foo ubuntu@0.public:/foo -q",
    87  			hostKeyChecking: "yes",
    88  			knownHosts:      "0",
    89  		},
    90  	}, {
    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",
    94  	}, {
    95  		about: "scp two local files to unit mysql/0",
    96  		args:  []string{"file1", "file2", "mysql/0:/foo/"},
    97  		expected: argsSpec{
    98  			args:            "file1 file2 ubuntu@0.public:/foo/",
    99  			hostKeyChecking: "yes",
   100  			knownHosts:      "0",
   101  		},
   102  	}, {
   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"},
   105  		expected: argsSpec{
   106  			args:            "ubuntu@0.public:foo ubuntu@0.public: -r -v -q -l5",
   107  			hostKeyChecking: "yes",
   108  			knownHosts:      "0",
   109  		},
   110  	}, {
   111  		about: "scp works with IPv6 addresses",
   112  		args:  []string{"2:foo", "bar"},
   113  		expected: argsSpec{
   114  			args:            `ubuntu@[2001:db8::1]:foo bar`,
   115  			hostKeyChecking: "yes",
   116  			knownHosts:      "2",
   117  		},
   118  	}, {
   119  		about: "scp from machine 0 to unit mysql/0 with proxy",
   120  		args:  []string{"--proxy=true", "0:foo", "mysql/0:/bar"},
   121  		expected: argsSpec{
   122  			args:            "ubuntu@0.private:foo ubuntu@0.private:/bar",
   123  			withProxy:       true,
   124  			hostKeyChecking: "yes",
   125  			knownHosts:      "0",
   126  		},
   127  	}, {
   128  		about: "scp from unit mysql/0 to machine 2 with a --",
   129  		args:  []string{"--", "-r", "-v", "mysql/0:foo", "2:", "-q", "-l5"},
   130  		expected: argsSpec{
   131  			args:            "-r -v ubuntu@0.public:foo ubuntu@[2001:db8::1]: -q -l5",
   132  			hostKeyChecking: "yes",
   133  			knownHosts:      "0,2",
   134  		},
   135  	}, {
   136  		about: "scp from unit mysql/0 to current dir as 'sam' user",
   137  		args:  []string{"sam@mysql/0:foo", "."},
   138  		expected: argsSpec{
   139  			args:            "sam@0.public:foo .",
   140  			hostKeyChecking: "yes",
   141  			knownHosts:      "0",
   142  		},
   143  	}, {
   144  		about: "scp with no such machine",
   145  		args:  []string{"5:foo", "bar"},
   146  		error: `machine 5 not found`,
   147  	}, {
   148  		about: "scp from arbitrary host name to current dir",
   149  		args:  []string{"some.host:foo", "."},
   150  		expected: argsSpec{
   151  			args:            "some.host:foo .",
   152  			hostKeyChecking: "",
   153  		},
   154  	}, {
   155  		about: "scp from arbitrary user & host to current dir",
   156  		args:  []string{"someone@some.host:foo", "."},
   157  		expected: argsSpec{
   158  			args:            "someone@some.host:foo .",
   159  			hostKeyChecking: "",
   160  		},
   161  	}, {
   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`,
   165  	}, {
   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:"},
   168  		expected: argsSpec{
   169  			args:            "some.host:foo ubuntu@0.public:",
   170  			hostKeyChecking: "no",
   171  			knownHosts:      "null",
   172  		},
   173  	},
   174  }
   175  
   176  func (s *SCPSuite) TestSCPCommand(c *gc.C) {
   177  	s.setupModel(c)
   178  
   179  	for i, t := range scpTests {
   180  		c.Logf("test %d: %s -> %s\n", i, t.about, t.args)
   181  
   182  		ctx, err := coretesting.RunCommand(c, newSCPCommand(), t.args...)
   183  		if t.error != "" {
   184  			c.Check(err, gc.ErrorMatches, t.error)
   185  		} else {
   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))
   195  		}
   196  	}
   197  }