Skip to content

Commit 41fa2da

Browse files
committed
Fix rubocop style issues
1 parent d64647f commit 41fa2da

File tree

9 files changed

+102
-118
lines changed

9 files changed

+102
-118
lines changed

.sync.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Gemfile:
44
':development':
55
- gem: 'puppet-debugger'
66
version: '>= 0.18.0'
7+
- gem: 'bolt'
8+
version: '>= 2.42.0'
79
optional:
810
':development':
911
- gem: 'github_changelog_generator'

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ group :development do
2525
gem "puppet-module-win-default-r#{minor_version}", '~> 1.0', require: false, platforms: [:mswin, :mingw, :x64_mingw]
2626
gem "puppet-module-win-dev-r#{minor_version}", '~> 1.0', require: false, platforms: [:mswin, :mingw, :x64_mingw]
2727
gem "puppet-debugger", '>= 0.18.0', require: false
28+
gem "bolt", '>= 2.42.0', require: false
2829
gem "github_changelog_generator", require: false, git: 'https://github.com/skywinder/github-changelog-generator', ref: '20ee04ba1234e9e83eb2ffb5056e23d641c7a018' if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.2.2')
2930
end
3031
group :system_tests do

lib/puppet/functions/peadm/bolt_version.rb

-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ def bolt_version
55
Bolt::VERSION
66
end
77
end
8-

spec/functions/bolt_version_spec.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4-
require 'bolt'
54

65
describe 'peadm::bolt_version' do
7-
86
it 'should_return_bolt_version' do
9-
is_expected.to run.and_return(Bolt::VERSION)
7+
stub_const('Bolt::VERSION', '2.45.0')
8+
is_expected.to run.and_return('2.45.0')
109
end
11-
1210
end

spec/functions/validate_version_spec.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# frozen_string_literal: true
2+
23
require 'spec_helper'
34

45
describe 'peadm::validate_version' do
56
context 'invalid PE versions' do
67
it 'rejects PE versions that are too new' do
7-
is_expected.to run.with_params('2021.1.0').and_raise_error(Puppet::ParseError, /This\ version\ of\ the/)
8+
is_expected.to run.with_params('2021.1.0').and_raise_error(Puppet::ParseError, %r{This\ version\ of\ the})
89
end
910

1011
it 'rejects PE versions that are too old' do
11-
is_expected.to run.with_params('2018.1.9').and_raise_error(Puppet::ParseError, /This\ version\ of\ the/)
12+
is_expected.to run.with_params('2018.1.9').and_raise_error(Puppet::ParseError, %r{This\ version\ of\ the})
1213
end
1314
end
1415

spec/plans/status_spec.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
# frozen_string_literal: true
2+
# rubocop:disable RSpec/BeforeAfterAll
23

34
require 'spec_helper'
45
# https://github.com/puppetlabs/bolt/blob/master/lib/bolt_spec/plans.rb
56

6-
describe 'peadm::status', if: Gem::Version.new(Puppet.version) >= Gem::Version.new('6.0.0') do
7+
describe 'peadm::status' do
8+
# Include the BoltSpec library functions
9+
include BoltSpec::Plans
10+
11+
# Configure Puppet and Bolt before running any tests
12+
before(:all) do
13+
BoltSpec::Plans.init
14+
end
15+
716
let(:infrastatus) do
817
data = JSON.parse(File.read(File.expand_path(File.join(fixtures, 'infrastatus.json'))))
918
{ 'output' => data }

spec/spec_helper_local.rb

+9-33
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
11
# frozen_string_literal: true
22

3-
require 'puppet'
3+
# Load the BoltSpec library
4+
require 'bolt_spec/plans'
45

5-
# This environment variable can be read by Ruby Bolt tasks to prevent unwanted
6-
# auto-execution, enabling easy unit testing.
7-
ENV["RSPEC_UNIT_TEST_MODE"] ||= "TRUE"
8-
9-
if Gem::Version.new(Puppet.version) >= Gem::Version.new('6.0.0')
10-
begin
11-
require 'bolt_spec/plans'
12-
BoltSpec::Plans.init
13-
14-
# Seems to be needed to make `run_plan` available inside examples:
15-
RSpec.configure { |c| c.include BoltSpec::Plans }
16-
rescue LoadError => e
17-
warn e.message
18-
warn '=== bolt tests will not run; ensure bolt gem is installed (requires Puppet 6+)'
19-
end
20-
end
6+
# Include the BoltSpec library functions
7+
include BoltSpec::Plans
218

22-
# Bolt has some hidden puppet modules inside the bolt codebase that need to be added here
23-
# for proper testing and availablity with other tooling. The code below will
24-
# locate the bolt gem dir and create fixtures for each of the bolt modules.
9+
# Configure Puppet and Bolt for testing
10+
BoltSpec::Plans.init
2511

26-
spec = Gem::Specification.latest_specs.find { |s| s.name.eql?('bolt') }
27-
if spec
28-
bolt_modules = File.join(spec.full_gem_path, 'bolt-modules')
29-
Dir.glob(File.join(bolt_modules, '*')).each do |dir|
30-
mod_name = File.basename(dir)
31-
mod_path = File.expand_path(File.join(__dir__, 'fixtures', 'modules', mod_name))
32-
FileUtils.ln_sf(dir, mod_path) unless File.exist?(mod_path)
33-
end
34-
35-
RSpec.configure do |c|
36-
c.basemodulepath = bolt_modules
37-
end
38-
end
12+
# This environment variable can be read by Ruby Bolt tasks to prevent unwanted
13+
# auto-execution, enabling easy unit testing.
14+
ENV['RSPEC_UNIT_TEST_MODE'] ||= 'TRUE'
+12-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22
require_relative '../../../tasks/puppet_infra_upgrade'
33

4-
describe PEAdm::Task::PuppetInfraUpgrade do
4+
describe PuppetInfraUpgrade do
55
context 'replica' do
66
let(:upgrade) do
77
described_class.new('type' => 'replica',
@@ -10,8 +10,8 @@
1010
end
1111

1212
it 'Returns when the command exits with an expected code' do
13-
status_dbl_0 = double('Status', :exitstatus => 0)
14-
status_dbl_11 = double('Status', :exitstatus => 11)
13+
status_dbl_0 = instance_double(Process::Status, exitstatus: 0)
14+
status_dbl_11 = instance_double(Process::Status, exitstatus: 11)
1515
allow(STDOUT).to receive(:puts)
1616
allow(upgrade).to receive(:wait_until_connected)
1717

@@ -23,7 +23,7 @@
2323
end
2424

2525
it 'Exits non-zero when the command exits with an unexpected code' do
26-
status_dbl_1 = double('Status', :exitstatus => 1)
26+
status_dbl_1 = instance_double('Process::Status', exitstatus: 1)
2727
allow(STDOUT).to receive(:puts)
2828
allow(upgrade).to receive(:wait_until_connected)
2929
allow(Open3).to receive(:capture2e).and_return(['hello world', status_dbl_1])
@@ -33,11 +33,12 @@
3333
end
3434
end
3535

36-
context 'compiler' do
37-
let(:upgrade) do
38-
described_class.new('type' => 'compiler',
39-
'targets' => ['replica.example.com'],
40-
'wait_until_connected_timeout' => 120)
41-
end
42-
end
36+
# TESTS NOT IMPLEMENTED
37+
# context 'compiler' do
38+
# let(:upgrade) do
39+
# described_class.new('type' => 'compiler',
40+
# 'targets' => ['compiler.example.com'],
41+
# 'wait_until_connected_timeout' => 120)
42+
# end
43+
# end
4344
end

tasks/puppet_infra_upgrade.rb

+63-66
Original file line numberDiff line numberDiff line change
@@ -8,93 +8,90 @@
88
require 'timeout'
99
require 'etc'
1010

11-
class PEAdm
12-
class Task
13-
class PuppetInfraUpgrade
14-
def initialize(params)
15-
@type = params['type']
16-
@targets = params['targets']
17-
@timeout = params['wait_until_connected_timeout']
18-
@token_file = params['token_file']
19-
end
11+
# Class to run and execute the `puppet infra upgrade` command as a task.
12+
class PuppetInfraUpgrade
13+
def initialize(params)
14+
@type = params['type']
15+
@targets = params['targets']
16+
@timeout = params['wait_until_connected_timeout']
17+
@token_file = params['token_file']
18+
end
2019

21-
def execute!
22-
exit 0 if @targets.empty?
23-
token_file = @token_file || File.join(Etc.getpwuid.dir, '.puppetlabs', 'token')
20+
def execute!
21+
exit 0 if @targets.empty?
22+
token_file = @token_file || File.join(Etc.getpwuid.dir, '.puppetlabs', 'token')
2423

25-
cmd = ['/opt/puppetlabs/bin/puppet-infrastructure', '--render-as', 'json', 'upgrade']
26-
cmd << '--token-file' << token_file unless @token_file.nil?
27-
cmd << @type << @targets.join(',')
24+
cmd = ['/opt/puppetlabs/bin/puppet-infrastructure', '--render-as', 'json', 'upgrade']
25+
cmd << '--token-file' << token_file unless @token_file.nil?
26+
cmd << @type << @targets.join(',')
2827

29-
wait_until_connected(nodes: @targets, token_file: token_file, timeout: @timeout)
28+
wait_until_connected(nodes: @targets, token_file: token_file, timeout: @timeout)
3029

31-
stdouterr, status = Open3.capture2e(*cmd)
32-
STDOUT.puts stdouterr
30+
stdouterr, status = Open3.capture2e(*cmd)
31+
STDOUT.puts stdouterr
3332

34-
# Exit code 11 indicates PuppetDB sync in progress, just not yet
35-
# finished. We consider that success.
36-
if [0, 11].include?(status.exitstatus)
37-
return
38-
else
39-
exit status.exitstatus
40-
end
41-
end
33+
# Exit code 11 indicates PuppetDB sync in progress, just not yet
34+
# finished. We consider that success.
35+
if [0, 11].include?(status.exitstatus)
36+
nil
37+
else
38+
exit status.exitstatus
39+
end
40+
end
4241

43-
def inventory_uri
44-
@inventory_uri ||= URI.parse('https://localhost:8143/orchestrator/v1/inventory')
45-
end
42+
def inventory_uri
43+
@inventory_uri ||= URI.parse('https://localhost:8143/orchestrator/v1/inventory')
44+
end
4645

47-
def request_object(nodes:, token_file:)
48-
token = File.read(token_file)
49-
body = {
50-
'nodes' => nodes,
51-
}.to_json
46+
def request_object(nodes:, token_file:)
47+
token = File.read(token_file)
48+
body = {
49+
'nodes' => nodes,
50+
}.to_json
5251

53-
request = Net::HTTP::Post.new(inventory_uri.request_uri)
54-
request['Content-Type'] = 'application/json'
55-
request['X-Authentication'] = token
56-
request.body = body
52+
request = Net::HTTP::Post.new(inventory_uri.request_uri)
53+
request['Content-Type'] = 'application/json'
54+
request['X-Authentication'] = token
55+
request.body = body
5756

58-
request
59-
end
57+
request
58+
end
6059

61-
def http_object
62-
http = Net::HTTP.new(inventory_uri.host, inventory_uri.port)
63-
http.use_ssl = true
64-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
60+
def http_object
61+
http = Net::HTTP.new(inventory_uri.host, inventory_uri.port)
62+
http.use_ssl = true
63+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
6564

66-
http
67-
end
65+
http
66+
end
6867

69-
def wait_until_connected(nodes:, token_file:, timeout: 120)
70-
http = http_object
71-
request = request_object(nodes: nodes, token_file: token_file)
72-
inventory = {}
73-
Timeout.timeout(timeout) do
74-
loop do
75-
response = http.request(request)
76-
unless response.is_a? Net::HTTPSuccess
77-
raise "Unexpected result from orchestrator: #{response.class}\n#{response}"
78-
end
79-
inventory = JSON.parse(response.body)
80-
break if inventory['items'].all? { |item| item['connected'] }
81-
sleep(1)
82-
end
68+
def wait_until_connected(nodes:, token_file:, timeout: 120)
69+
http = http_object
70+
request = request_object(nodes: nodes, token_file: token_file)
71+
inventory = {}
72+
Timeout.timeout(timeout) do
73+
loop do
74+
response = http.request(request)
75+
unless response.is_a? Net::HTTPSuccess
76+
raise "Unexpected result from orchestrator: #{response.class}\n#{response}"
8377
end
84-
rescue Timeout::Error
85-
raise 'Timed out waiting for nodes to be connected to orchestrator: ' +
86-
inventory['items'].reject { |item| item['connected'] }
87-
.map { |item| item['name'] }
88-
.to_s
78+
inventory = JSON.parse(response.body)
79+
break if inventory['items'].all? { |item| item['connected'] }
80+
sleep(1)
8981
end
9082
end
83+
rescue Timeout::Error
84+
raise 'Timed out waiting for nodes to be connected to orchestrator: ' +
85+
inventory['items'].reject { |item| item['connected'] }
86+
.map { |item| item['name'] }
87+
.to_s
9188
end
9289
end
9390

9491
# Run the task unless an environment flag has been set, signaling not to. The
9592
# environment flag is used to disable auto-execution and enable Ruby unit
9693
# testing of this task.
9794
unless ENV['RSPEC_UNIT_TEST_MODE']
98-
upgrade = PEAdm::Task::PuppetInfraUpgrade.new(JSON.parse(STDIN.read))
95+
upgrade = PuppetInfraUpgrade.new(JSON.parse(STDIN.read))
9996
upgrade.execute!
10097
end

0 commit comments

Comments
 (0)