diff options
author | Mingxiang Xue <mingxiangxue@gmail.com> | 2020-03-11 18:14:07 +0800 |
---|---|---|
committer | Mingxiang Xue <mingxiangxue@gmail.com> | 2020-03-11 18:14:07 +0800 |
commit | aea5223eea51c2b60e6373dc8175d49f7520f4e6 (patch) | |
tree | aad7178eb8741d3df23d6ef9dda4f10e89e3e477 | |
parent | 990695c4892d8f51a7ce29d4bfc987a8f2738a0e (diff) | |
download | net-ssh-aea5223eea51c2b60e6373dc8175d49f7520f4e6.tar.gz |
Add integration test for set_env option
Signed-off-by: Mingxiang Xue <mingxiangxue@gmail.com>
-rw-r--r-- | .rubocop_todo.yml | 3 | ||||
-rw-r--r-- | lib/net/ssh/config.rb | 2 | ||||
-rw-r--r-- | lib/net/ssh/connection/channel.rb | 3 | ||||
-rw-r--r-- | test/integration/common.rb | 1 | ||||
-rw-r--r-- | test/integration/playbook.yml | 6 | ||||
-rw-r--r-- | test/integration/test_channel.rb | 40 | ||||
-rw-r--r-- | test/integration/test_forward.rb | 12 |
7 files changed, 43 insertions, 24 deletions
diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index bc4efd8..a1b7067 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -274,6 +274,8 @@ Metrics/ClassLength: # Offense count: 38 Metrics/CyclomaticComplexity: Max: 27 + Exclude: + - 'lib/net/ssh/config.rb' # Offense count: 211 # Configuration parameters: CountComments, ExcludedMethods. @@ -299,6 +301,7 @@ Naming/AccessorMethodName: Exclude: - 'lib/net/ssh/authentication/methods/password.rb' - 'lib/net/ssh/authentication/pageant.rb' + - 'lib/net/ssh/connection/channel.rb' - 'lib/net/ssh/connection/session.rb' - 'lib/net/ssh/transport/kex/abstract5656.rb' - 'lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb' diff --git a/lib/net/ssh/config.rb b/lib/net/ssh/config.rb index fdc2e09..a40262e 100644 --- a/lib/net/ssh/config.rb +++ b/lib/net/ssh/config.rb @@ -216,7 +216,6 @@ module Net userknownhostsfile: :user_known_hosts_file, checkhostip: :check_host_ip }.freeze - # rubocop:disable Metrics/CyclomaticComplexity def translate_config_key(hash, key, value, settings) case key when :ciphers @@ -285,7 +284,6 @@ module Net hash[TRANSLATE_CONFIG_KEY_RENAME_MAP[key]] = value end end - # rubocop:enable Metrics/CyclomaticComplexity def setup_proxy(type, value) case type diff --git a/lib/net/ssh/connection/channel.rb b/lib/net/ssh/connection/channel.rb index e65e5c1..7bfee72 100644 --- a/lib/net/ssh/connection/channel.rb +++ b/lib/net/ssh/connection/channel.rb @@ -682,12 +682,9 @@ module Net # Set a +Hash+ of environment variables in the remote process' environment. # # channel.set_remote_env foo: 'bar', baz: 'whale' - # - # rubocop:disable Naming/AccessorMethodName def set_remote_env(env) env.each { |key, value| self.env(key, value) } end - # rubocop:enable Naming/AccessorMethodName end end diff --git a/test/integration/common.rb b/test/integration/common.rb index ef99b04..6295ada 100644 --- a/test/integration/common.rb +++ b/test/integration/common.rb @@ -94,6 +94,7 @@ module IntegrationTestHelpers # @yield [pid, port] def start_sshd_7_or_later(port = '2200', config: nil) + pid = nil if config with_lines_as_tempfile(config) do |path| pid = spawn('sudo', '/opt/net-ssh-openssh/sbin/sshd', '-D', '-f', path, '-p', port) diff --git a/test/integration/playbook.yml b/test/integration/playbook.yml index e2268c6..dd9d078 100644 --- a/test/integration/playbook.yml +++ b/test/integration/playbook.yml @@ -41,14 +41,14 @@ - lineinfile: dest=/etc/sudoers.d/net_ssh_1 mode=0440 state=present create=yes line='net_ssh_2 ALL=(ALL) NOPASSWD:ALL' regexp=net_ssh_2 - unarchive: - src: https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-7.4p1.tar.gz + src: https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-7.9p1.tar.gz dest: /tmp remote_src: True validate_certs: False - - name: building and installing openssh 7.4 (used in forward test) + - name: building and installing openssh 7.9 (used in forward test) command: sh -c "./configure --prefix=/opt/net-ssh-openssh && make && sudo make install" args: - chdir: /tmp/openssh-7.4p1/ + chdir: /tmp/openssh-7.9p1/ - name: drop installed openssh etc/ in favor of symlink file: state: absent diff --git a/test/integration/test_channel.rb b/test/integration/test_channel.rb index 2b424b4..d07e0a1 100644 --- a/test/integration/test_channel.rb +++ b/test/integration/test_channel.rb @@ -51,11 +51,11 @@ class TestChannel < NetSSHTest proxy = Net::SSH::Proxy::Command.new("/bin/nc localhost 22") res = nil Net::SSH.start(*ssh_start_params(proxy: proxy)) do |ssh| - chanell_success_handler = lambda do + channel_success_handler = lambda do sleep(0.1) system("killall /bin/nc") end - channel = ssh_exec(ssh, "echo Begin ; sleep 100 ; echo End", chanell_success_handler) do |ch, _type, data| + channel = ssh_exec(ssh, "echo Begin ; sleep 100 ; echo End", channel_success_handler) do |ch, _type, data| ch[:result] ||= "" ch[:result] << data end @@ -72,11 +72,11 @@ class TestChannel < NetSSHTest proxy = Net::SSH::Proxy::Command.new("/bin/nc localhost 22") res = nil Net::SSH.start(*ssh_start_params(proxy: proxy)) do |ssh| - chanell_success_handler = lambda do + channel_success_handler = lambda do sleep(0.1) system("killall /bin/nc") end - channel = ssh_exec(ssh, "echo Hello!", chanell_success_handler) do |ch, _type, data| + channel = ssh_exec(ssh, "echo Hello!", channel_success_handler) do |ch, _type, data| ch[:result] ||= "" ch[:result] << data end @@ -104,4 +104,36 @@ class TestChannel < NetSSHTest end end end + + def test_channel_should_set_environment_variables_on_remote + setup_ssh_env do + start_sshd_7_or_later(config: 'AcceptEnv *') do |_pid, port| + Timeout.timeout(4) do + begin + # We have our own sshd, give it a chance to come up before + # listening. + proxy = Net::SSH::Proxy::Command.new("/bin/nc localhost #{port}") + res = nil + Net::SSH.start(*ssh_start_params(port: port, proxy: proxy, set_env: { foo: 'bar', baz: 'whale will' })) do |ssh| + channel_success_handler = lambda do + sleep(0.1) + system("killall /bin/nc") + end + channel = ssh_exec(ssh, "echo $foo; echo $baz", channel_success_handler) do |ch, _type, data| + ch[:result] ||= "" + ch[:result] << data + end + channel.wait + res = channel[:result] + assert_equal(res, "bar\nwhale will\n") + end + assert_equal(res, "bar\nwhale will\n") + rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Net::SSH::Proxy::ConnectError + sleep 0.25 + retry + end + end + end + end + end end diff --git a/test/integration/test_forward.rb b/test/integration/test_forward.rb index 9cd8e64..3ff76cd 100644 --- a/test/integration/test_forward.rb +++ b/test/integration/test_forward.rb @@ -24,18 +24,6 @@ require 'tempfile' class ForwardTestBase < NetSSHTest include IntegrationTestHelpers - # @yield [pid, port] - def start_sshd_7_or_later(port = '2200') - pid = spawn('sudo', '/opt/net-ssh-openssh/sbin/sshd', '-D', '-p', port) - yield pid, port - ensure - # Our pid is sudo, -9 (KILL) on sudo will not clean up its children - # properly, so we just have to hope that -15 (TERM) will manage to bring - # down sshd. - system('sudo', 'kill', '-15', pid.to_s) - Process.wait(pid) - end - def localhost 'localhost' end |