Skip to content

Commit 76557a1

Browse files
committed
(gh-26) Extract a base ruby Task class from the check.rb task
This will be shared with the configure.rb task.
1 parent 9abbfb8 commit 76557a1

File tree

6 files changed

+105
-33
lines changed

6 files changed

+105
-33
lines changed

lib/openvox_bootstrap/task.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require 'json'
4+
5+
module OpenvoxBootstrap
6+
# Base class for openvox_bootstrap Ruby tasks.
7+
class Task
8+
# Run the task and print the result as JSON.
9+
def self.run
10+
params = JSON.parse($stdin.read)
11+
raise(ArgumentError, <<~ERR) unless params.is_a?(Hash)
12+
Expected a Hash, got #{params.class}: #{params.inspect}
13+
ERR
14+
15+
params.transform_keys!(&:to_sym)
16+
# Clean out empty params so that task defaults are used.
17+
params.delete_if { |_, v| v.nil? || v == '' }
18+
19+
result = new.task(**params)
20+
puts JSON.pretty_generate(result)
21+
22+
result
23+
end
24+
end
25+
end

spec/lib/contexts.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'json'
4-
53
# Mirrors the output from puppetlabs-facts.
64
module OBRspecFacts
75
UBUNTU_2404 = {
@@ -130,3 +128,24 @@ def mock_facts_task_bash_sh(os)
130128
EOF
131129
end
132130
end
131+
132+
RSpec.shared_context 'task_run_helpers' do
133+
def validate_task_run_for(subject, input:, expected: {}, code: 0)
134+
old_stdin = $stdin
135+
$stdin = StringIO.new(input.to_json)
136+
old_stdout = $stdout
137+
$stdout = StringIO.new
138+
139+
begin
140+
subject.run
141+
rescue SystemExit => e
142+
expect(e.status).to eq(code)
143+
end
144+
145+
output = JSON.parse($stdout.string, symbolize_names: true)
146+
expect(output).to eq(expected)
147+
ensure
148+
$stdin = old_stdin
149+
$stdout = old_stdout
150+
end
151+
end

spec/tasks/check_spec.rb

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4+
require 'lib/contexts'
45
require_relative '../../tasks/check'
56

67
describe 'openvox_bootstrap::check' do
@@ -9,6 +10,8 @@
910
end
1011

1112
describe '.run' do
13+
include_context 'task_run_helpers'
14+
1215
let(:input) do
1316
{
1417
version: nil
@@ -21,23 +24,8 @@
2124
}
2225
end
2326

24-
def validate_task_run(input:, expected: {}, code: 0)
25-
old_stdin = $stdin
26-
$stdin = StringIO.new(input.to_json)
27-
old_stdout = $stdout
28-
$stdout = StringIO.new
29-
30-
begin
31-
OpenvoxBootstrap::Check.run
32-
rescue SystemExit => e
33-
expect(e.status).to eq(code)
34-
end
35-
36-
output = JSON.parse($stdout.string, symbolize_names: true)
37-
expect(output).to eq(expected)
38-
ensure
39-
$stdin = old_stdin
40-
$stdout = old_stdout
27+
def validate_task_run(input:, expected:, code: 0)
28+
validate_task_run_for(OpenvoxBootstrap::Check, input: input, expected: expected, code: code)
4129
end
4230

4331
it 'raises for empty input' do
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'lib/contexts'
5+
require 'openvox_bootstrap/task'
6+
7+
class OpenvoxBootstrap::TaskTester < OpenvoxBootstrap::Task
8+
def task(foo:, baz: 'default')
9+
{
10+
foo: foo,
11+
baz: baz,
12+
}
13+
end
14+
end
15+
16+
describe 'openvox_bootstrap::task' do
17+
describe '.run' do
18+
include_context 'task_run_helpers'
19+
20+
let(:input) { { foo: 'bar', baz: 'dingo' } }
21+
let(:expected_output) do
22+
{
23+
foo: 'bar',
24+
baz: 'dingo',
25+
}
26+
end
27+
let(:tester) { OpenvoxBootstrap::TaskTester }
28+
29+
it 'raises for empty input' do
30+
expect do
31+
validate_task_run_for(tester, input: nil)
32+
end.to raise_error(ArgumentError)
33+
end
34+
35+
it 'returns the task result' do
36+
validate_task_run_for(tester, input: input, expected: expected_output)
37+
end
38+
39+
it 'uses default values for missing params' do
40+
input[:baz] = nil
41+
expected_output[:baz] = 'default'
42+
validate_task_run_for(tester, input: input, expected: expected_output)
43+
end
44+
end
45+
end

tasks/check.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
{
1616
"name": "check.rb",
1717
"input_method": "stdin",
18-
"requirements": ["puppet-agent"]
18+
"requirements": ["puppet-agent"],
19+
"files": [
20+
"openvox_bootstrap/lib/openvox_bootstrap/task.rb",
21+
"openvox_bootstrap/tasks/check.rb"
22+
]
1923
},
2024
{
2125
"name": "check_linux.sh",
2226
"input_method": "environment",
2327
"requirements": ["shell"],
2428
"files": [
29+
"openvox_bootstrap/lib/openvox_bootstrap/task.rb",
2530
"openvox_bootstrap/tasks/check.rb"
2631
]
2732
}

tasks/check.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#! /opt/puppetlabs/puppet/bin/ruby
22
# frozen_string_literal: true
33

4-
require 'json'
4+
require_relative '../lib/openvox_bootstrap/task'
55

66
module OpenvoxBootstrap
7-
class Check
7+
class Check < Task
88
# Get the Puppet version from the installed Puppet library.
99
#
1010
# "require 'puppet/version'" can be fooled by the Ruby environment
@@ -22,17 +22,7 @@ def self.puppet_version
2222

2323
# Run the task and print the result as JSON.
2424
def self.run
25-
params = JSON.parse($stdin.read)
26-
raise(ArgumentError, <<~ERR) unless params.is_a?(Hash)
27-
Expected a Hash, got #{params.class}: #{params.inspect}
28-
ERR
29-
30-
params.transform_keys!(&:to_sym)
31-
# Clean out empty params so that task defaults are used.
32-
params.delete_if { |_, v| v.nil? || v == '' }
33-
34-
result = Check.new.task(**params)
35-
puts JSON.pretty_generate(result)
25+
result = super
3626
result[:valid] ? exit(0) : exit(1)
3727
end
3828

0 commit comments

Comments
 (0)