-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a class to represent the answer file
Refactors code into an AnswerFile class for handling the loading of and details of the parts of the answer file. This abstraction will allow easier introduction of newer versions of the answer file.
- Loading branch information
Showing
6 changed files
with
162 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'yaml' | ||
|
||
module Kafo | ||
class AnswerFile | ||
|
||
attr_reader :answers, :filename, :version | ||
|
||
def initialize(answer_filename, version: 1, exit_handler: KafoConfigure, logger: KafoConfigure.logger) | ||
@filename = answer_filename | ||
@version = version.nil? ? 1 : version | ||
@exit_handler = exit_handler | ||
@logger = logger | ||
|
||
begin | ||
@answers = YAML.load_file(@filename) | ||
rescue Errno::ENOENT | ||
@exit_handler.exit(:no_answer_file) do | ||
@logger.error "No answer file found at #{@filename}" | ||
end | ||
end | ||
|
||
validate | ||
end | ||
|
||
def filename | ||
@filename | ||
end | ||
|
||
def puppet_classes | ||
@answers.keys.sort | ||
end | ||
|
||
def parameters_for_class(puppet_class) | ||
if @version == 1 | ||
params = @answers[puppet_class] | ||
params.is_a?(Hash) ? params : {} | ||
end | ||
end | ||
|
||
def class_enabled?(puppet_class) | ||
if @version == 1 | ||
value = @answers[puppet_class.is_a?(String) ? puppet_class : puppet_class.identifier] | ||
!!value || value.is_a?(Hash) | ||
end | ||
end | ||
|
||
private | ||
|
||
def validate | ||
if @version == 1 | ||
validate_version_1 | ||
end | ||
end | ||
|
||
def validate_version_1 | ||
invalid = @answers.reject do |puppet_class, value| | ||
value.is_a?(Hash) || [true, false].include?(value) | ||
end | ||
|
||
unless invalid.empty? | ||
@exit_handler.exit(:invalid_values) do | ||
@logger.error("Answer file at #{@filename} has invalid values for #{invalid.keys.join(', ')}. Please ensure they are either a hash or true/false.") | ||
end | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
class_a: true | ||
class_b: | ||
key: value | ||
class_c: {} | ||
class_d: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
class_a: 'true' | ||
class_b: | ||
class_c: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'test_helper' | ||
|
||
describe 'Kafo::AnswerFile' do | ||
let(:dummy_logger) { DummyLogger.new } | ||
|
||
describe 'answer file version 1' do | ||
describe 'valid answer file' do | ||
let(:answer_file_path) { 'test/fixtures/answer_files/v1/basic-answers.yaml' } | ||
let(:answer_file) { Kafo::AnswerFile.new(answer_file_path) } | ||
|
||
it 'returns the sorted puppet classes' do | ||
_(answer_file.puppet_classes).must_equal(['class_a', 'class_b', 'class_c', 'class_d']) | ||
end | ||
|
||
it 'returns the parameters for a class' do | ||
_(answer_file.parameters_for_class('class_b')).must_equal({'key' => 'value'}) | ||
end | ||
|
||
it 'returns true for a class with a hash' do | ||
_(answer_file.class_enabled?('class_c')).must_equal(true) | ||
end | ||
|
||
it 'returns true for a class set to true' do | ||
_(answer_file.class_enabled?('class_a')).must_equal(true) | ||
end | ||
|
||
it 'returns false for a class set to false' do | ||
_(answer_file.class_enabled?('class_d')).must_equal(false) | ||
end | ||
end | ||
|
||
describe 'invalid answer file' do | ||
let(:answer_file_path) { 'test/fixtures/answer_files/v1/invalid-answers.yaml' } | ||
|
||
before do | ||
Kafo::KafoConfigure.logger = dummy_logger | ||
end | ||
|
||
it 'exits with invalid_answer_file' do | ||
must_exit_with_code(21) { Kafo::AnswerFile.new(answer_file_path) } | ||
|
||
dummy_logger.rewind | ||
_(dummy_logger.error.read).must_match(%r{Answer file at test/fixtures/answer_files/v1/invalid-answers.yaml has invalid values for class_a, class_b, class_c. Please ensure they are either a hash or true/false.\n}) | ||
end | ||
end | ||
end | ||
end |