Skip to content

Commit c029473

Browse files
author
Vivek Singh
committed
Add verify! method & exact match for sudo errors.
Signed-off-by: Vivek Singh <[email protected]>
1 parent f884192 commit c029473

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

lib/train/extras/command_wrapper.rb

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,27 @@ def verify
5656
res = @backend.run_command(run("echo"))
5757
return nil if res.exit_status == 0
5858

59-
rawerr = res.stdout + " " + res.stderr
60-
msg, reason =
61-
case rawerr
62-
when /Sorry, try again/
63-
["Wrong sudo password.", :bad_sudo_password]
64-
when /sudo: no tty present and no askpass program specified/
65-
["Sudo requires a password, please configure it.", :sudo_password_required]
66-
when /sudo: command not found/
67-
["Can't find sudo command. Please either install and "\
68-
"configure it on the target or deactivate sudo.", :sudo_command_not_found]
69-
when /sudo: sorry, you must have a tty to run sudo/
70-
["Sudo requires a TTY. Please see the README on how to configure "\
71-
"sudo to allow for non-interactive usage.", :sudo_no_tty]
72-
else
73-
rawerr
74-
end
59+
rawerr = "#{res.stdout} #{res.stderr}".strip
60+
61+
case rawerr
62+
when "Sorry, try again"
63+
["Wrong sudo password.", :bad_sudo_password]
64+
when "sudo: no tty present and no askpass program specified"
65+
["Sudo requires a password, please configure it.", :sudo_password_required]
66+
when "sudo: command not found"
67+
["Can't find sudo command. Please either install and "\
68+
"configure it on the target or deactivate sudo.", :sudo_command_not_found]
69+
when "sudo: sorry, you must have a tty to run sudo"
70+
["Sudo requires a TTY. Please see the README on how to configure "\
71+
"sudo to allow for non-interactive usage.", :sudo_no_tty]
72+
else
73+
[rawerr, nil]
74+
end
75+
end
76+
77+
def verify!
78+
msg, reason = verify
79+
return nil unless msg
7580

7681
raise Train::UserError.new("Sudo failed: #{msg}", reason)
7782
end
@@ -168,7 +173,7 @@ def self.load(transport, options)
168173
return nil unless LinuxCommand.active?(options)
169174

170175
res = LinuxCommand.new(transport, options)
171-
res.verify
176+
res.verify!
172177

173178
res
174179
elsif transport.platform.windows?

test/unit/extras/command_wrapper_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,35 +121,35 @@ def mock_connect_result(stderr, exit_status)
121121
it "error message for bad sudo password" do
122122
backend.stubs(:run_command).returns(mock_connect_result("Sorry, try again", 1))
123123
lc = cls.new(backend, { sudo: true })
124-
err = _ { lc.verify }.must_raise Train::UserError
124+
err = _ { lc.verify! }.must_raise Train::UserError
125125
_(err.message).must_match(/Sudo failed: Wrong sudo password./)
126126
end
127127

128128
it "error message for sudo password required" do
129129
backend.stubs(:run_command).returns(mock_connect_result("sudo: no tty present and no askpass program specified", 1))
130130
lc = cls.new(backend, { sudo: true })
131-
err = _ { lc.verify }.must_raise Train::UserError
131+
err = _ { lc.verify! }.must_raise Train::UserError
132132
_(err.message).must_match(/Sudo requires a password, please configure it./)
133133
end
134134

135135
it "error message for sudo: command not found" do
136136
backend.stubs(:run_command).returns(mock_connect_result("sudo: command not found", 1))
137137
lc = cls.new(backend, { sudo: true })
138-
err = _ { lc.verify }.must_raise Train::UserError
138+
err = _ { lc.verify! }.must_raise Train::UserError
139139
_(err.message).must_match(/Can't find sudo command. Please either install and configure it on the target or deactivate sudo./)
140140
end
141141

142142
it "error message for requires tty" do
143143
backend.stubs(:run_command).returns(mock_connect_result("sudo: sorry, you must have a tty to run sudo", 1))
144144
lc = cls.new(backend, { sudo: true })
145-
err = _ { lc.verify }.must_raise Train::UserError
145+
err = _ { lc.verify! }.must_raise Train::UserError
146146
_(err.message).must_match(/Sudo failed: Sudo requires a TTY. Please see the README/)
147147
end
148148

149149
it "error message for other sudo related errors" do
150150
backend.stubs(:run_command).returns(mock_connect_result("Other sudo related error", 1))
151151
lc = cls.new(backend, { sudo: true })
152-
err = _ { lc.verify }.must_raise Train::UserError
152+
err = _ { lc.verify! }.must_raise Train::UserError
153153
_(err.message).must_match(/Other sudo related error/)
154154
end
155155
end

0 commit comments

Comments
 (0)