author | Mahlon E. Smith <mahlon@martini.nu> |
Wed, 15 Jul 2020 15:35:06 -0700 | |
changeset 19 | f31d60b04f8a |
parent 11 | ffa70066522c |
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 |
|
19
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
23 |
tmpname = instance.send( :make_remote_filename, "fancy-script.tmpl", "/var/tmp/" ) |
11
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}| ) |
19
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
25 |
|
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
26 |
tmpname = instance.send( :make_remote_filename, "fancy-script.tmpl", '' ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
27 |
expect( tmpname ).to match( %r|fancy-script-[[:xdigit:]]{6}| ) |
11
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
28 |
end |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
29 |
end |
ffa70066522c
Update for Ruby 2.5. Moderize configurability usage, update dependencies.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
30 |
|
4 | 31 |
|
32 |
describe 'subclassed' do |
|
33 |
let( :instance ) { Class.new(described_class).new('queue') } |
|
34 |
let( :payload ) { |
|
35 |
{ 'template' => 'script', 'host' => 'example.com' } |
|
36 |
} |
|
37 |
let( :opts ) { |
|
38 |
opts = described_class::DEFAULT_SSH_OPTIONS |
|
39 |
opts.merge!( |
|
40 |
:port => 22, |
|
41 |
:keys => ['/tmp/sekrit.rsa'] |
|
42 |
) |
|
43 |
opts |
|
44 |
} |
|
45 |
let( :template ) { Inversion::Template.new("Hi there, <?attr name?>!") } |
|
46 |
||
47 |
before( :each ) do |
|
48 |
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
|
49 |
allow( instance ).to receive( :make_remote_filename ).and_return( "/tmp/script_temp" ) |
4 | 50 |
end |
51 |
||
52 |
it "aborts if there is no template in the payload" do |
|
53 |
expect { |
|
54 |
instance.work( {}, {} ) |
|
55 |
}.to raise_exception( ArgumentError, /missing required option 'template'/i ) |
|
56 |
end |
|
57 |
||
58 |
it "aborts if there is no host in the payload" do |
|
59 |
expect { |
|
60 |
instance.work({ 'template' => 'boop' }, {} ) |
|
61 |
}.to raise_exception( ArgumentError, /missing required option 'host'/i ) |
|
62 |
end |
|
63 |
||
64 |
it "adds debugging output if specified in the payload" do |
|
65 |
payload[ 'debug' ] = true |
|
66 |
||
67 |
options = opts.dup |
|
68 |
options.merge!( |
|
69 |
:logger => Loggability[ Net::SSH ], |
|
70 |
:verbose => :debug |
|
71 |
) |
|
72 |
||
73 |
expect( Net::SSH ).to receive( :start ).with( 'example.com', 'symphony', options ) |
|
74 |
instance.work( payload, {} ) |
|
75 |
end |
|
76 |
||
77 |
it "attaches attributes to the scripts from the payload" do |
|
78 |
payload[ 'attributes' ] = { :name => 'Handsome' } |
|
79 |
||
80 |
conn = double( :ssh_connection ) |
|
81 |
expect( instance ).to receive( :upload_script ). |
|
82 |
with( conn, "Hi there, Handsome!", "/tmp/script_temp" ) |
|
83 |
expect( conn ).to receive( :exec! ).with( "/tmp/script_temp" ) |
|
84 |
expect( conn ).to receive( :exec! ).with( "rm /tmp/script_temp" ) |
|
85 |
||
86 |
expect( Net::SSH ).to receive( :start ). |
|
87 |
with( 'example.com', 'symphony', opts ).and_yield( conn ) |
|
88 |
||
89 |
instance.work( payload, {} ) |
|
90 |
end |
|
91 |
||
92 |
it "uploads the file and sets it executable" do |
|
93 |
conn = double( :ssh_connection ) |
|
94 |
sftp = double( :sftp_connection ) |
|
95 |
file = double( :remote_file_obj ) |
|
96 |
fh = double( :remote_filehandle ) |
|
97 |
||
98 |
expect( conn ).to receive( :sftp ).and_return( sftp ) |
|
99 |
expect( sftp ).to receive( :file ).and_return( file ) |
|
100 |
||
101 |
expect( file ).to receive( :open ). |
|
102 |
with( "/tmp/script_temp", "w", 0755 ).and_yield( fh ) |
|
103 |
expect( fh ).to receive( :print ).with( "Hi there, !" ) |
|
104 |
||
105 |
expect( conn ).to receive( :exec! ).with( "/tmp/script_temp" ) |
|
106 |
expect( conn ).to receive( :exec! ).with( "rm /tmp/script_temp" ) |
|
107 |
||
108 |
expect( Net::SSH ).to receive( :start ). |
|
109 |
with( 'example.com', 'symphony', opts ).and_yield( conn ) |
|
110 |
||
111 |
instance.work( payload, {} ) |
|
112 |
end |
|
113 |
||
19
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
114 |
it "can override how it cleans the remote script up" do |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
115 |
payload[ 'delete_cmd' ] = 'del' |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
116 |
|
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
117 |
conn = double( :ssh_connection ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
118 |
expect( instance ).to receive( :upload_script ). |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
119 |
with( conn, "Hi there, !", "/tmp/script_temp" ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
120 |
expect( conn ).to receive( :exec! ).with( "/tmp/script_temp" ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
121 |
expect( conn ).to receive( :exec! ).with( "del /tmp/script_temp" ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
122 |
|
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
123 |
expect( Net::SSH ).to receive( :start ). |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
124 |
with( 'example.com', 'symphony', opts ).and_yield( conn ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
125 |
|
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
126 |
instance.work( payload, {} ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
127 |
end |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
128 |
|
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
129 |
it "can run the script with a specific interpreter" do |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
130 |
payload[ 'run_binary' ] = 'ruby' |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
131 |
|
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
132 |
conn = double( :ssh_connection ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
133 |
expect( instance ).to receive( :upload_script ). |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
134 |
with( conn, "Hi there, !", "/tmp/script_temp" ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
135 |
expect( conn ).to receive( :exec! ).with( "ruby /tmp/script_temp" ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
136 |
expect( conn ).to receive( :exec! ).with( "rm /tmp/script_temp" ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
137 |
|
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
138 |
expect( Net::SSH ).to receive( :start ). |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
139 |
with( 'example.com', 'symphony', opts ).and_yield( conn ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
140 |
|
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
141 |
instance.work( payload, {} ) |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
142 |
end |
f31d60b04f8a
Multiple changes to help support Windows native ssh with sshscript workers.
Mahlon E. Smith <mahlon@martini.nu>
parents:
11
diff
changeset
|
143 |
|
4 | 144 |
it "leaves the remote script in place if asked" do |
145 |
payload[ 'nocleanup' ] = true |
|
146 |
||
147 |
conn = double( :ssh_connection ) |
|
148 |
expect( instance ).to receive( :upload_script ). |
|
149 |
with( conn, "Hi there, !", "/tmp/script_temp" ) |
|
150 |
expect( conn ).to receive( :exec! ).with( "/tmp/script_temp" ) |
|
151 |
expect( conn ).to_not receive( :exec! ).with( "rm /tmp/script_temp" ) |
|
152 |
||
153 |
expect( Net::SSH ).to receive( :start ). |
|
154 |
with( 'example.com', 'symphony', opts ).and_yield( conn ) |
|
155 |
||
156 |
instance.work( payload, {} ) |
|
157 |
end |
|
158 |
||
159 |
it "remembers the output of the remote script" do |
|
160 |
conn = double( :ssh_connection ) |
|
161 |
expect( instance ).to receive( :upload_script ). |
|
162 |
with( conn, "Hi there, !", "/tmp/script_temp" ) |
|
163 |
expect( conn ).to receive( :exec! ).with( "/tmp/script_temp" ).and_return( "Hi there, !" ) |
|
164 |
expect( conn ).to receive( :exec! ).with( "rm /tmp/script_temp" ) |
|
165 |
||
166 |
expect( Net::SSH ).to receive( :start ). |
|
167 |
with( 'example.com', 'symphony', opts ).and_yield( conn ) |
|
168 |
||
169 |
instance.work( payload, {} ) |
|
170 |
output = instance.instance_variable_get( :@output ) |
|
171 |
expect( output ).to eq( 'Hi there, !' ) |
|
172 |
end |
|
173 |
end |
|
174 |
end |
|
175 |