Skip to content

Commit

Permalink
Extract upload/download tests into module for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbrictson committed Dec 22, 2023
1 parent ced903d commit e75d894
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 67 deletions.
72 changes: 72 additions & 0 deletions test/functional/backends/netssh_transfer_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'securerandom'

module SSHKit
module Backend
module NetsshTransferTests
def test_upload_and_then_capture_file_contents
actual_file_contents = ""
file_name = File.join("/tmp", SecureRandom.uuid)
File.open file_name, 'w+' do |f|
f.write "Some Content\nWith a newline and trailing spaces \n "
end
Netssh.new(a_host) do
upload!(file_name, file_name)
actual_file_contents = capture(:cat, file_name, strip: false)
end.run
assert_equal "Some Content\nWith a newline and trailing spaces \n ", actual_file_contents
end

def test_upload_within
file_name = SecureRandom.uuid
file_contents = "Some Content"
dir_name = SecureRandom.uuid
actual_file_contents = ""
Netssh.new(a_host) do |_host|
within("/tmp") do
execute :mkdir, "-p", dir_name
within(dir_name) do
upload!(StringIO.new(file_contents), file_name)
end
end
actual_file_contents = capture(:cat, "/tmp/#{dir_name}/#{file_name}", strip: false)
end.run
assert_equal file_contents, actual_file_contents
end

def test_upload_string_io
file_contents = ""
Netssh.new(a_host) do |_host|
file_name = File.join("/tmp", SecureRandom.uuid)
upload!(StringIO.new('example_io'), file_name)
file_contents = download!(file_name)
end.run
assert_equal "example_io", file_contents
end

def test_upload_large_file
size = 25
fills = SecureRandom.random_bytes(1024*1024)
file_name = "/tmp/file-#{size}.txt"
File.open(file_name, 'wb') do |f|
(size).times {f.write(fills) }
end
file_contents = ""
Netssh.new(a_host) do
upload!(file_name, file_name)
file_contents = download!(file_name)
end.run
assert_equal File.open(file_name, 'rb').read, file_contents
end

def test_upload_via_pathname
file_contents = ""
Netssh.new(a_host) do |_host|
file_name = Pathname.new(File.join("/tmp", SecureRandom.uuid))
upload!(StringIO.new('example_io'), file_name)
file_contents = download!(file_name)
end.run
assert_equal "example_io", file_contents
end
end
end
end
71 changes: 4 additions & 67 deletions test/functional/backends/test_netssh.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
require 'helper'
require 'securerandom'
require 'benchmark'
require_relative 'netssh_transfer_tests'

module SSHKit

module Backend

class TestNetssh < FunctionalTest

def setup
super
@output = String.new
SSHKit.config.output_verbosity = :debug
SSHKit.config.output = SSHKit::Formatter::SimpleText.new(@output)
SSHKit::Backend::Netssh.configure do |ssh|
ssh.transfer_method = :scp
end
end

def a_host
Expand Down Expand Up @@ -136,71 +138,6 @@ def test_test_does_not_raise_on_non_zero_exit_status
end.run
end

def test_upload_and_then_capture_file_contents
actual_file_contents = ""
file_name = File.join("/tmp", SecureRandom.uuid)
File.open file_name, 'w+' do |f|
f.write "Some Content\nWith a newline and trailing spaces \n "
end
Netssh.new(a_host) do
upload!(file_name, file_name)
actual_file_contents = capture(:cat, file_name, strip: false)
end.run
assert_equal "Some Content\nWith a newline and trailing spaces \n ", actual_file_contents
end

def test_upload_within
file_name = SecureRandom.uuid
file_contents = "Some Content"
dir_name = SecureRandom.uuid
actual_file_contents = ""
Netssh.new(a_host) do |_host|
within("/tmp") do
execute :mkdir, "-p", dir_name
within(dir_name) do
upload!(StringIO.new(file_contents), file_name)
end
end
actual_file_contents = capture(:cat, "/tmp/#{dir_name}/#{file_name}", strip: false)
end.run
assert_equal file_contents, actual_file_contents
end

def test_upload_string_io
file_contents = ""
Netssh.new(a_host) do |_host|
file_name = File.join("/tmp", SecureRandom.uuid)
upload!(StringIO.new('example_io'), file_name)
file_contents = download!(file_name)
end.run
assert_equal "example_io", file_contents
end

def test_upload_large_file
size = 25
fills = SecureRandom.random_bytes(1024*1024)
file_name = "/tmp/file-#{size}.txt"
File.open(file_name, 'wb') do |f|
(size).times {f.write(fills) }
end
file_contents = ""
Netssh.new(a_host) do
upload!(file_name, file_name)
file_contents = download!(file_name)
end.run
assert_equal File.open(file_name, 'rb').read, file_contents
end

def test_upload_via_pathname
file_contents = ""
Netssh.new(a_host) do |_host|
file_name = Pathname.new(File.join("/tmp", SecureRandom.uuid))
upload!(StringIO.new('example_io'), file_name)
file_contents = download!(file_name)
end.run
assert_equal "example_io", file_contents
end

def test_interaction_handler
captured_command_result = nil
Netssh.new(a_host) do
Expand Down
24 changes: 24 additions & 0 deletions test/functional/backends/test_netssh_scp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'helper'
require_relative 'netssh_transfer_tests'

module SSHKit
module Backend
class TestNetsshScp < FunctionalTest
include NetsshTransferTests

def setup
super
@output = String.new
SSHKit.config.output_verbosity = :debug
SSHKit.config.output = SSHKit::Formatter::SimpleText.new(@output)
SSHKit::Backend::Netssh.configure do |ssh|
ssh.transfer_method = :scp
end
end

def a_host
VagrantWrapper.hosts['one']
end
end
end
end

0 comments on commit e75d894

Please sign in to comment.