-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#36] SSHKit.user errors because sudo is missing #51
Changes from 4 commits
8d61343
e4a5cee
c79a3fd
e11da3f
1299883
e6ae35b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ defmodule SSHKit.Utils do | |
|
||
def shellescape(value), do: value | ||
|
||
def shellquote(value), do: value | ||
def shellquote(value), do: "'#{value}'" # TODO: Proper quoting | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm glad you included the . That's exactly the reason why I included the "TODO", to make the Credo checks fail until I've added proper quoting on this branch 😁 |
||
|
||
def charlistify(value) when is_list(value) do | ||
Enum.map(value, &charlistify/1) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ defmodule SSHKit.SSHFunctionalTest do | |
test "opens a connection with username and password", %{hosts: [host]} do | ||
options = [port: host.port, user: host.user, password: host.password] | ||
{:ok, conn} = SSH.connect(host.ip, Keyword.merge(@defaults, options)) | ||
{:ok, data, status} = SSH.run(conn, "whoami") | ||
{:ok, data, status} = SSH.run(conn, "id -un") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
assert [stdout: "#{host.user}\n"] == data | ||
assert 0 = status | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,21 +5,15 @@ defmodule SSHKitFunctionalTest do | |
|
||
use SSHKit.FunctionalCase, async: true | ||
|
||
@defaults [silently_accept_hosts: true] | ||
@defaults [silently_accept_hosts: true, timeout: 5000] | ||
|
||
def options(overrides) do | ||
Keyword.merge(@defaults, overrides) | ||
end | ||
|
||
def build_context(host) do | ||
SSHKit.context({ | ||
host.ip, | ||
options(port: host.port, | ||
user: host.user, | ||
password: host.password, | ||
timeout: 5000 | ||
) | ||
}) | ||
overrides = [port: host.port, user: host.user, password: host.password] | ||
SSHKit.context({host.ip, Keyword.merge(@defaults, overrides)}) | ||
end | ||
|
||
defp stdio(output, type) do | ||
|
@@ -33,42 +27,39 @@ defmodule SSHKitFunctionalTest do | |
|
||
@tag boot: 1 | ||
test "connects", %{hosts: [host]} do | ||
[{:ok, output, 0}] = SSHKit.run(build_context(host), "whoami") | ||
|
||
[{:ok, output, 0}] = SSHKit.run(build_context(host), "id -un") | ||
name = String.trim(stdout(output)) | ||
assert name == host.user | ||
end | ||
|
||
@tag boot: 1 | ||
test "run", %{hosts: [host]} do | ||
test "runs commands", %{hosts: [host]} do | ||
context = build_context(host) | ||
|
||
[{:ok, output, status}] = SSHKit.run(context, "pwd") | ||
assert status == 0 | ||
output = stdout(output) | ||
assert output == "/home/me\n" | ||
assert stdout(output) == "/home/me\n" | ||
|
||
[{:ok, output, status}] = SSHKit.run(context, "ls non-existing") | ||
assert status == 1 | ||
output = stderr(output) | ||
assert output =~ "ls: non-existing: No such file or directory" | ||
assert stderr(output) =~ "ls: non-existing: No such file or directory" | ||
|
||
[{:ok, output, status}] = SSHKit.run(context, "does-not-exist") | ||
assert status == 127 | ||
output = stderr(output) | ||
assert output =~ "'does-not-exist': No such file or directory" | ||
assert stderr(output) =~ "'does-not-exist': No such file or directory" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
end | ||
|
||
@tag boot: 1 | ||
test "env", %{hosts: [host]} do | ||
[{:ok, output, status}] = | ||
host | ||
|> build_context | ||
|> build_context() | ||
|> SSHKit.env(%{"PATH" => "$HOME/.rbenv/shims:$PATH", "NODE_ENV" => "production"}) | ||
|> SSHKit.run("env") | ||
|
||
assert status == 0 | ||
output = stdout(output) | ||
|
||
assert status == 0 | ||
assert output =~ "NODE_ENV=production" | ||
assert output =~ ~r/PATH=.*\/\.rbenv\/shims:/ | ||
end | ||
|
@@ -77,14 +68,17 @@ defmodule SSHKitFunctionalTest do | |
test "umask", %{hosts: [host]} do | ||
context = | ||
host | ||
|> build_context | ||
|> build_context() | ||
|> SSHKit.umask("077") | ||
SSHKit.run(context, "mkdir my_dir") | ||
SSHKit.run(context, "touch my_file") | ||
|
||
[{:ok, _, 0}] = SSHKit.run(context, "mkdir my_dir") | ||
[{:ok, _, 0}] = SSHKit.run(context, "touch my_file") | ||
|
||
[{:ok, output, status}] = SSHKit.run(context, "ls -la") | ||
|
||
assert status == 0 | ||
output = stdout(output) | ||
|
||
assert status == 0 | ||
assert output =~ ~r/drwx--S---\s+2\s+me\s+me\s+4096.+my_dir/ | ||
assert output =~ ~r/-rw-------\s+1\s+me\s+me\s+0.+my_file/ | ||
end | ||
|
@@ -97,23 +91,25 @@ defmodule SSHKitFunctionalTest do | |
|> SSHKit.path("/var/log") | ||
|
||
[{:ok, output, status}] = SSHKit.run(context, "pwd") | ||
assert status == 0 | ||
output = stdout(output) | ||
|
||
assert status == 0 | ||
assert output == "/var/log\n" | ||
end | ||
|
||
@tag skip: true # it produces an error: "sudo not found" on stderr | ||
@tag boot: 1 | ||
test "user", %{hosts: [host]} do | ||
adduser(host, "despicable_me") | ||
add_user_to_group(host, host.user, "danger") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does the user need to be in a group? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The group is set up in the Maybe you've got a different idea though? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, nope. maybe renaming the group from |
||
|
||
context = | ||
host | ||
|> build_context | ||
|> build_context() | ||
|> SSHKit.user("despicable_me") | ||
|
||
[{:ok, output, status}] = SSHKit.run(context, "whoami") | ||
[{:ok, output, status}] = SSHKit.run(context, "id -un") | ||
output = stdout(output) | ||
|
||
assert output == "despicable_me\n" | ||
assert status == 0 | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only this match of
sudo/3
is ever used, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Errr, nope. Unless I missed something, the context may hold any combination of
:user
and:group
being present or not present.Example:
Here, context has a user, but no group set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, you're right. i just misread the code yesterday evening. let's act as if i never wrote this comment ;)