forked from OpenVoxProject/openbolt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_spec.rb
More file actions
225 lines (191 loc) · 6.89 KB
/
local_spec.rb
File metadata and controls
225 lines (191 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# frozen_string_literal: true
require 'spec_helper'
require 'bolt/inventory'
require 'bolt/transport/local'
require 'bolt/util'
require 'bolt_spec/files'
require 'bolt_spec/integration'
require 'bolt_spec/pal'
require 'bolt_spec/project'
require 'bolt_spec/transport'
require 'shared_examples/transport'
describe Bolt::Transport::Local do
include BoltSpec::PAL
include BoltSpec::Transport
let(:transport) { :local }
let(:host_and_port) { 'localhost' }
let(:safe_name) { host_and_port }
let(:user) { 'runner' }
let(:password) { 'runner' }
let(:os_context) { Bolt::Util.windows? ? windows_context : posix_context }
let(:transport_config) { {} }
let(:config) { make_config({ local: transport_config }) }
let(:plugins) { make_plugins(config) }
let(:inventory) { Bolt::Inventory::Inventory.new({}, config.transport, config.transports, plugins) }
let(:target) { make_target }
def make_target
inventory.get_target(host_and_port)
end
it 'is always connected' do
expect(runner.connected?(target)).to eq(true)
end
include_examples 'transport api'
it "can run a command with pipes" do
command, expected = os_context[:pipe_command]
result = runner.run_command(target, command, catch_errors: true)
expect(result.value['exit_code']).to eq(0)
expect(result.value['stdout']).to match(expected)
end
context 'running as another user', sudo: true do
include_examples 'with sudo'
context "overriding with '_run_as'" do
let(:transport_config) do
{
'sudo-password' => password,
'run-as' => 'root'
}
end
it "can override run_as for command via an option" do
expect(runner.run_command(target, 'whoami', run_as: user)['stdout']).to eq("#{user}\n")
end
it "can override run_as for script via an option" do
contents = "#!/bin/sh\nwhoami"
with_tempfile_containing('script test', contents) do |file|
expect(runner.run_script(target, file.path, [], run_as: user)['stdout']).to eq("#{user}\n")
end
end
it "can override run_as for task via an option" do
contents = "#!/bin/sh\nwhoami"
with_task_containing('tasks_test', contents, 'environment') do |task|
expect(runner.run_task(target, task, {}, run_as: user).message).to eq("#{user}\n")
end
end
it "can override run_as for file upload via an option" do
contents = "upload file test as root content"
dest = '/tmp/root-file-upload-test'
with_tempfile_containing('tasks test upload as root', contents) do |file|
expect(runner.upload(target, file.path, dest, run_as: user).message).to match(/Uploaded/)
expect(runner.run_command(target, "cat #{dest}", run_as: user)['stdout']).to eq(contents)
expect(runner.run_command(target, "stat -c %U #{dest}", run_as: user)['stdout'].chomp).to eq(user)
expect(runner.run_command(target, "stat -c %G #{dest}", run_as: user)['stdout'].chomp).to eq('runner')
end
runner.run_command(target, "rm #{dest}", sudoable: true, run_as: user)
end
end
context "as user with no password" do
let(:transport_config) do
{
'run-as' => 'root'
}
end
it "returns a failed result when a temporary directory is created" do
contents = "#!/bin/sh\nwhoami"
with_tempfile_containing('script test', contents) do |file|
expect {
runner.run_script(target, file.path, [])
}.to raise_error(Bolt::Node::EscalateError,
"Sudo password for user #{user} was not provided for #{host_and_port}")
end
end
end
end
context 'with large input and output' do
let(:file_size) { 1011 }
let(:str) { (0...1024).map { rand(65..90).chr }.join }
let(:arguments) { { 'input' => str * file_size } }
let(:ruby_task) do
<<~TASK
#!/usr/bin/env ruby
input = STDIN.read
STDERR.print input
STDOUT.print input
TASK
end
it "runs with large input and captures large output" do
with_task_containing('big_kahuna', ruby_task, 'stdin', '.rb') do |task|
result = runner.run_task(target, task, arguments).value
expect(result['input'].bytesize).to eq(file_size * 1024)
# Ensure the strings are the same
expect(result['input'][-1024..-1]).to eq(str)
end
end
context 'with run-as', sudo: true do
let(:transport_config) do
{
'sudo-password' => password,
'run-as' => 'root'
}
end
it "runs with large input and output" do
with_task_containing('big_kahuna', ruby_task, 'stdin', '.rb') do |task|
result = runner.run_task(target, task, arguments).value
expect(result['input'].bytesize).to eq(file_size * 1024)
expect(result['input'][-1024..-1]).to eq(str)
end
end
end
context 'with slow input' do
let(:file_size) { 10 }
let(:ruby_task) do
<<~TASK
#!/usr/bin/env ruby
while true
begin
input = STDIN.readpartial(1024)
sleep(0.2)
STDERR.print input
STDOUT.print input
rescue EOFError
break
end
end
TASK
end
it "runs with large input and captures large output" do
with_task_containing('slow_and_steady', ruby_task, 'stdin', '.rb') do |task|
result = runner.run_task(target, task, arguments).value
expect(result['input'].bytesize).to eq(file_size * 1024)
# Ensure the strings are the same
expect(result['input'][-1024..-1]).to eq(str)
end
end
end
end
context 'file extensions', windows: true do
include BoltSpec::Files
include BoltSpec::Integration
include BoltSpec::Project
let(:config) { { 'modulepath' => fixtures_path('modules') } }
around(:each) do |example|
in_project(config: config, inventory: inventory) do |project|
@project = project
example.run
end
end
context 'with an unspecified extension' do
let(:inventory) { {} }
it 'errors' do
results = run_cli_json(%w[task run sample::python -t localhost], project: @project)
result = results['items'][0]
expect(result['status']).to eq('failure')
expect(result.dig('value', '_error', 'msg')).to match(/File extension .py is not enabled/)
end
end
context 'with a specified extension' do
let(:inventory) do
{
'config' => {
'local' => {
'extensions' => ['.py']
}
}
}
end
it 'runs the task' do
results = run_cli_json(%w[task run sample::python -t localhost], project: @project)
result = results['items'][0]
expect(result['status']).to eq('success')
end
end
end
end