spec/symphony/tasks/ssh_spec.rb
author Mahlon E. Smith <mahlon@martini.nu>
Thu, 09 Jul 2020 15:05:21 -0700
changeset 17 5db18679edcf
parent 4 3972315383b3
permissions -rw-r--r--
Multiple changes. - Migrate to gem.deps.rb from .gems - Bump dependency versions - Clear environment before connecting to remote server - Allow option for environment modifications - Place commands in args, rather than via the IO pipe. (won't spawn remote shell)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
require_relative '../../helpers'
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
require 'symphony/tasks/ssh'
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
context Symphony::Task::SSH do
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
	let( :ssh ) { (Pathname( __FILE__ ).dirname.parent.parent + 'fake_ssh').realpath }
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
	before( :each ) do
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
		described_class.configure(
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
			path: ssh.to_s,
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
			key:  '/tmp/sekrit.rsa',
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
			user: 'symphony'
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
		)
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
	end
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
	it_should_behave_like "an object with Configurability"
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
	describe 'subclassed' do
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
		let( :instance ) { Class.new(described_class).new('queue') }
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
		let( :payload ) {
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
			{ 'command' => 'woohoo', 'host' => 'example.com' }
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
		}
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
		it "aborts if there is no command in the payload" do
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
			expect {
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
				instance.work( {}, {} )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
			}.to raise_exception( ArgumentError, /missing required option 'command'/i )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
		end
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
		it "aborts if there is no host in the payload" do
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
			expect {
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
				instance.work({ 'command' => 'boop' }, {} )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
			}.to raise_exception( ArgumentError, /missing required option 'host'/i )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
		end
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
		it "builds the proper command line" do
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
			pipe = double( :fake_pipes ).as_null_object
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
			allow( IO ).to receive( :pipe ).and_return([ pipe, pipe ])
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
			args = [
17
5db18679edcf Multiple changes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    41
				'-p', '22', '-i', '/tmp/sekrit.rsa', '-l', 'symphony', 'example.com', 'woohoo'
4
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
			]
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
			expect( Process ).to receive( :spawn ).with(
17
5db18679edcf Multiple changes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    45
				*[ {}, ssh.to_s, described_class.opts, args ].flatten,
5db18679edcf Multiple changes.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    46
				out: pipe, in: pipe, close_others: true, unsetenv_others: true
4
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
			).and_return( 12 )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
			expect( Process ).to receive( :waitpid2 ).with( 12 ).and_return([ 12, 1 ])
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
			code = instance.work( payload, {} )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
			expect( code ).to eq( 1 )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
		end
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
		it "execs and captures output" do
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
			code = instance.work( payload, {} )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
			expect( code ).to eq( 0 )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
			output = instance.instance_variable_get( :@output )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
			expect( output ).to eq( 'Hi there!' )
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
		end
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
	end
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
end
3972315383b3 Small cleanups, add tests.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64