author | Mahlon E. Smith <mahlon@martini.nu> |
Mon, 02 Sep 2019 08:41:08 -0700 | |
changeset 13 | 8e29e051b417 |
parent 11 | ffa70066522c |
child 19 | f31d60b04f8a |
permissions | -rw-r--r-- |
4 | 1 |
|
2 |
require_relative '../../helpers' |
|
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
3 |
require 'symphony/tasks/ssh' |
4 | 4 |
require 'symphony/tasks/sshscript' |
5 |
||
6 |
context Symphony::Task::SSHScript do |
|
7 |
||
8 |
before( :each ) do |
|
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
9 |
Symphony::Task::SSH.configure( |
4 | 10 |
key: '/tmp/sekrit.rsa', |
11 |
user: 'symphony' |
|
12 |
) |
|
13 |
end |
|
14 |
||
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
15 |
|
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
16 |
describe 'utility' do |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
17 |
|
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
18 |
it "can generate an appropriate tempfile name" do |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
19 |
instance = Class.new( described_class ).new( 'queue' ) |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
20 |
tmpname = instance.send( :make_remote_filename, "fancy-script.tmpl" ) |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
21 |
expect( tmpname ).to match( %r|^/tmp/fancy-script-[[:xdigit:]]{6}| ) |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
22 |
|
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
23 |
tmpname = instance.send( :make_remote_filename, "fancy-script.tmpl", "/var/tmp" ) |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
24 |
expect( tmpname ).to match( %r|/var/tmp/fancy-script-[[:xdigit:]]{6}| ) |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
25 |
end |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
26 |
end |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
27 |
|
4 | 28 |
|
29 |
describe 'subclassed' do |
|
30 |
let( :instance ) { Class.new(described_class).new('queue') } |
|
31 |
let( :payload ) { |
|
32 |
{ 'template' => 'script', 'host' => 'example.com' } |
|
33 |
} |
|
34 |
let( :opts ) { |
|
35 |
opts = described_class::DEFAULT_SSH_OPTIONS |
|
36 |
opts.merge!( |
|
37 |
:port => 22, |
|
38 |
:keys => ['/tmp/sekrit.rsa'] |
|
39 |
) |
|
40 |
opts |
|
41 |
} |
|
42 |
let( :template ) { Inversion::Template.new("Hi there, <?attr name?>!") } |
|
43 |
||
44 |
before( :each ) do |
|
45 |
allow( Inversion::Template ).to receive( :load ).and_return( template ) |
|
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
46 |
allow( instance ).to receive( :make_remote_filename ).and_return( "/tmp/script_temp" ) |
4 | 47 |
end |
48 |
||
49 |
it "aborts if there is no template in the payload" do |
|
50 |
expect { |
|
51 |
instance.work( {}, {} ) |
|
52 |
}.to raise_exception( ArgumentError, /missing required option 'template'/i ) |
|
53 |
end |
|
54 |
||
55 |
it "aborts if there is no host in the payload" do |
|
56 |
expect { |
|
57 |
instance.work({ 'template' => 'boop' }, {} ) |
|
58 |
}.to raise_exception( ArgumentError, /missing required option 'host'/i ) |
|
59 |
end |
|
60 |
||
61 |
it "adds debugging output if specified in the payload" do |
|
62 |
payload[ 'debug' ] = true |
|
63 |
||
64 |
options = opts.dup |
|
65 |
options.merge!( |
|
66 |
:logger => Loggability[ Net::SSH ], |
|
67 |
:verbose => :debug |
|
68 |
) |
|
69 |
||
70 |
expect( Net::SSH ).to receive( :start ).with( 'example.com', 'symphony', options ) |
|
71 |
instance.work( payload, {} ) |
|
72 |
end |
|
73 |
||
74 |
it "attaches attributes to the scripts from the payload" do |
|
75 |
payload[ 'attributes' ] = { :name => 'Handsome' } |
|
76 |
||
77 |
conn = double( :ssh_connection ) |
|
78 |
expect( instance ).to receive( :upload_script ). |
|
79 |
with( conn, "Hi there, Handsome!", "/tmp/script_temp" ) |
|
80 |
expect( conn ).to receive( :exec! ).with( "/tmp/script_temp" ) |
|
81 |
expect( conn ).to receive( :exec! ).with( "rm /tmp/script_temp" ) |
|
82 |
||
83 |
expect( Net::SSH ).to receive( :start ). |
|
84 |
with( 'example.com', 'symphony', opts ).and_yield( conn ) |
|
85 |
||
86 |
instance.work( payload, {} ) |
|
87 |
end |
|
88 |
||
89 |
it "uploads the file and sets it executable" do |
|
90 |
conn = double( :ssh_connection ) |
|
91 |
sftp = double( :sftp_connection ) |
|
92 |
file = double( :remote_file_obj ) |
|
93 |
fh = double( :remote_filehandle ) |
|
94 |
||
95 |
expect( conn ).to receive( :sftp ).and_return( sftp ) |
|
96 |
expect( sftp ).to receive( :file ).and_return( file ) |
|
97 |
||
98 |
expect( file ).to receive( :open ). |
|
99 |
with( "/tmp/script_temp", "w", 0755 ).and_yield( fh ) |
|
100 |
expect( fh ).to receive( :print ).with( "Hi there, !" ) |
|
101 |
||
102 |
expect( conn ).to receive( :exec! ).with( "/tmp/script_temp" ) |
|
103 |
expect( conn ).to receive( :exec! ).with( "rm /tmp/script_temp" ) |
|
104 |
||
105 |
expect( Net::SSH ).to receive( :start ). |
|
106 |
with( 'example.com', 'symphony', opts ).and_yield( conn ) |
|
107 |
||
108 |
instance.work( payload, {} ) |
|
109 |
end |
|
110 |
||
111 |
it "leaves the remote script in place if asked" do |
|
112 |
payload[ 'nocleanup' ] = true |
|
113 |
||
114 |
conn = double( :ssh_connection ) |
|
115 |
expect( instance ).to receive( :upload_script ). |
|
116 |
with( conn, "Hi there, !", "/tmp/script_temp" ) |
|
117 |
expect( conn ).to receive( :exec! ).with( "/tmp/script_temp" ) |
|
118 |
expect( conn ).to_not receive( :exec! ).with( "rm /tmp/script_temp" ) |
|
119 |
||
120 |
expect( Net::SSH ).to receive( :start ). |
|
121 |
with( 'example.com', 'symphony', opts ).and_yield( conn ) |
|
122 |
||
123 |
instance.work( payload, {} ) |
|
124 |
end |
|
125 |
||
126 |
it "remembers the output of the remote script" do |
|
127 |
conn = double( :ssh_connection ) |
|
128 |
expect( instance ).to receive( :upload_script ). |
|
129 |
with( conn, "Hi there, !", "/tmp/script_temp" ) |
|
130 |
expect( conn ).to receive( :exec! ).with( "/tmp/script_temp" ).and_return( "Hi there, !" ) |
|
131 |
expect( conn ).to receive( :exec! ).with( "rm /tmp/script_temp" ) |
|
132 |
||
133 |
expect( Net::SSH ).to receive( :start ). |
|
134 |
with( 'example.com', 'symphony', opts ).and_yield( conn ) |
|
135 |
||
136 |
instance.work( payload, {} ) |
|
137 |
output = instance.instance_variable_get( :@output ) |
|
138 |
expect( output ).to eq( 'Hi there, !' ) |
|
139 |
end |
|
140 |
end |
|
141 |
end |
|
142 |