Merge lp:~axwalk/juju-core/lp1298115-utils-ssh-testing into lp:~go-bot/juju-core/trunk

Proposed by Andrew Wilkins
Status: Merged
Approved by: Andrew Wilkins
Approved revision: no longer in the source branch.
Merged at revision: 2497
Proposed branch: lp:~axwalk/juju-core/lp1298115-utils-ssh-testing
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 121 lines (+13/-45)
1 file modified
utils/ssh/ssh_openssh.go (+13/-45)
To merge this branch: bzr merge lp:~axwalk/juju-core/lp1298115-utils-ssh-testing
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+212977@code.launchpad.net

Commit message

utils/ssh: revert to using slices for command args

Reverts part of https://codereview.appspot.com/66340045/
which changed OpenSSH-based client args from using a slice
to a map.

Fixes lp:1298115

https://codereview.appspot.com/81180043/

Description of the change

utils/ssh: revert to using slices for command args

Reverts part of https://codereview.appspot.com/66340045/
which changed OpenSSH-based client args from using a slice
to a map.

Fixes lp:1298115

https://codereview.appspot.com/81180043/

To post a comment you must log in.
Revision history for this message
Andrew Wilkins (axwalk) wrote :
Download full text (4.8 KiB)

Reviewers: mp+212977_code.launchpad.net,

Message:
Please take a look.

Description:
utils/ssh: revert to using slices for command args

Reverts part of https://codereview.appspot.com/66340045/
which changed OpenSSH-based client args from using a slice
to a map.

Fixes lp:1298115

https://code.launchpad.net/~axwalk/juju-core/lp1298115-utils-ssh-testing/+merge/212977

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/81180043/

Affected files (+15, -45 lines):
   A [revision details]
   M utils/ssh/ssh_openssh.go

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: tarmac-20140326230643-olixj5ukxzhgt4cv
+New revision: <email address hidden>

Index: utils/ssh/ssh_openssh.go
=== modified file 'utils/ssh/ssh_openssh.go'
--- utils/ssh/ssh_openssh.go 2014-03-19 03:18:41 +0000
+++ utils/ssh/ssh_openssh.go 2014-03-27 02:42:08 +0000
@@ -14,7 +14,7 @@
   "launchpad.net/juju-core/utils"
  )

-var opensshCommonOptions = map[string][]string{"-o":
[]string{"StrictHostKeyChecking no"}}
+var opensshCommonOptions = []string{"-o", "StrictHostKeyChecking no"}

  // default identities will not be attempted if
  // -i is specified and they are not explcitly
@@ -63,22 +63,19 @@
   return &c, nil
  }

-func opensshOptions(options *Options, commandKind opensshCommandKind)
map[string][]string {
- args := make(map[string][]string)
- for k, v := range opensshCommonOptions {
- args[k] = v
- }
+func opensshOptions(options *Options, commandKind opensshCommandKind)
[]string {
+ args := append([]string{}, opensshCommonOptions...)
   if options == nil {
    options = &Options{}
   }
   if len(options.proxyCommand) > 0 {
- args["-o"] =
append(args["-o"], "ProxyCommand "+utils.CommandString(options.proxyCommand...))
+ args =
append(args, "-o", "ProxyCommand "+utils.CommandString(options.proxyCommand...))
   }
   if !options.passwordAuthAllowed {
- args["-o"] = append(args["-o"], "PasswordAuthentication no")
+ args = append(args, "-o", "PasswordAuthentication no")
   }
   if options.allocatePTY {
- args["-t"] = []string{}
+ args = append(args, "-t", "-t") // twice to force
   }
   identities := append([]string{}, options.identities...)
   if pk := PrivateKeyFiles(); len(pk) > 0 {
@@ -100,54 +97,29 @@
    }
   }
   for _, identity := range identities {
- args["-i"] = append(args["-i"], identity)
+ args = append(args, "-i", identity)
   }
   if options.port != 0 {
    port := fmt.Sprint(options.port)
    if commandKind == scpKind {
     // scp uses -P instead of -p (-p means preserve).
- args["-P"] = []string{port}
+ args = append(args, "-P", port)
    } else {
- args["-p"] = []string{port}
+ args = append(args, "-p", port)
    }
   }
   return args
  }

-func expandArgs(args map[string][]string, quote bool) []string {
- var list []string
- for opt, vals := range args {
- if len(vals) == 0 {
- list = append(list, opt)
- if opt == "-t" {
- // In order to force a PTY to be allocated, we need to
- // pass -t twice.
- ...

Read more...

Revision history for this message
Dave Cheney (dave-cheney) wrote :

LGTM.

I'm a bit sad because options really shouldn't be position dependent. It
feels as if the code is being forced to conform to a specific output
just so we can write the test.

That said, without what you have done testing the output would have been
really difficult as the line would have to be parsed and split into a
set of atoms, taking care to worry about quoting and flags that have or
don't have options.

All in all, this is the right choice.

https://codereview.appspot.com/81180043/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'utils/ssh/ssh_openssh.go'
2--- utils/ssh/ssh_openssh.go 2014-03-19 03:18:41 +0000
3+++ utils/ssh/ssh_openssh.go 2014-03-27 02:55:06 +0000
4@@ -14,7 +14,7 @@
5 "launchpad.net/juju-core/utils"
6 )
7
8-var opensshCommonOptions = map[string][]string{"-o": []string{"StrictHostKeyChecking no"}}
9+var opensshCommonOptions = []string{"-o", "StrictHostKeyChecking no"}
10
11 // default identities will not be attempted if
12 // -i is specified and they are not explcitly
13@@ -63,22 +63,19 @@
14 return &c, nil
15 }
16
17-func opensshOptions(options *Options, commandKind opensshCommandKind) map[string][]string {
18- args := make(map[string][]string)
19- for k, v := range opensshCommonOptions {
20- args[k] = v
21- }
22+func opensshOptions(options *Options, commandKind opensshCommandKind) []string {
23+ args := append([]string{}, opensshCommonOptions...)
24 if options == nil {
25 options = &Options{}
26 }
27 if len(options.proxyCommand) > 0 {
28- args["-o"] = append(args["-o"], "ProxyCommand "+utils.CommandString(options.proxyCommand...))
29+ args = append(args, "-o", "ProxyCommand "+utils.CommandString(options.proxyCommand...))
30 }
31 if !options.passwordAuthAllowed {
32- args["-o"] = append(args["-o"], "PasswordAuthentication no")
33+ args = append(args, "-o", "PasswordAuthentication no")
34 }
35 if options.allocatePTY {
36- args["-t"] = []string{}
37+ args = append(args, "-t", "-t") // twice to force
38 }
39 identities := append([]string{}, options.identities...)
40 if pk := PrivateKeyFiles(); len(pk) > 0 {
41@@ -100,54 +97,29 @@
42 }
43 }
44 for _, identity := range identities {
45- args["-i"] = append(args["-i"], identity)
46+ args = append(args, "-i", identity)
47 }
48 if options.port != 0 {
49 port := fmt.Sprint(options.port)
50 if commandKind == scpKind {
51 // scp uses -P instead of -p (-p means preserve).
52- args["-P"] = []string{port}
53+ args = append(args, "-P", port)
54 } else {
55- args["-p"] = []string{port}
56+ args = append(args, "-p", port)
57 }
58 }
59 return args
60 }
61
62-func expandArgs(args map[string][]string, quote bool) []string {
63- var list []string
64- for opt, vals := range args {
65- if len(vals) == 0 {
66- list = append(list, opt)
67- if opt == "-t" {
68- // In order to force a PTY to be allocated, we need to
69- // pass -t twice.
70- list = append(list, opt)
71- }
72- }
73- for _, val := range vals {
74- list = append(list, opt)
75- if quote {
76- val = fmt.Sprintf("%q", val)
77- }
78- list = append(list, val)
79- }
80- }
81- return list
82-}
83-
84 // Command implements Client.Command.
85 func (c *OpenSSHClient) Command(host string, command []string, options *Options) *Cmd {
86- opts := opensshOptions(options, sshKind)
87- args := expandArgs(opts, false)
88+ args := opensshOptions(options, sshKind)
89 args = append(args, host)
90 if len(command) > 0 {
91 args = append(args, command...)
92 }
93 bin, args := sshpassWrap("ssh", args)
94- optsList := strings.Join(expandArgs(opts, true), " ")
95- fullCommand := strings.Join(command, " ")
96- logger.Debugf("running: %s %s %q '%s'", bin, optsList, host, fullCommand)
97+ logger.Debugf("running: %s %s", bin, utils.CommandString(args...))
98 return &Cmd{impl: &opensshCmd{exec.Command(bin, args...)}}
99 }
100
101@@ -158,18 +130,14 @@
102 options = *userOptions
103 options.allocatePTY = false // doesn't make sense for scp
104 }
105- opts := opensshOptions(&options, scpKind)
106- args := expandArgs(opts, false)
107+ args := opensshOptions(&options, scpKind)
108 args = append(args, extraArgs...)
109 args = append(args, targets...)
110 bin, args := sshpassWrap("scp", args)
111 cmd := exec.Command(bin, args...)
112 var stderr bytes.Buffer
113 cmd.Stderr = &stderr
114- allOpts := append(expandArgs(opts, true), extraArgs...)
115- optsList := strings.Join(allOpts, " ")
116- targetList := `"` + strings.Join(targets, `" "`) + `"`
117- logger.Debugf("running: %s %s %s", bin, optsList, targetList)
118+ logger.Debugf("running: %s %s", bin, utils.CommandString(args...))
119 if err := cmd.Run(); err != nil {
120 stderr := strings.TrimSpace(stderr.String())
121 if len(stderr) > 0 {

Subscribers

People subscribed via source and target branches

to status/vote changes: