-
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
7 changed files
with
118 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'yaml' | ||
|
||
module Kafo | ||
class AnswerFile | ||
|
||
attr_reader :answers, :filename, :version | ||
|
||
def initialize(answer_filename, version: 1) | ||
@filename = answer_filename | ||
@version = version.nil? ? 1 : version | ||
|
||
begin | ||
@answers = YAML.load_file(@filename) | ||
rescue Errno::ENOENT | ||
KafoConfigure.exit(:no_answer_filename) do | ||
puts "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) | ||
params = @answers[puppet_class] | ||
params.is_a?(Hash) ? params : {} | ||
end | ||
|
||
def class_enabled?(puppet_class) | ||
value = @answers[puppet_class.is_a?(String) ? puppet_class : puppet_class.identifier] | ||
!!value || value.is_a?(Hash) | ||
end | ||
|
||
private | ||
|
||
def validate | ||
invalid = @answers.reject do |puppet_class, value| | ||
value.is_a?(Hash) || [true, false].include?(value) | ||
end | ||
|
||
unless invalid.empty? | ||
KafoConfigure.exit(:invalid_answer_file) do | ||
KafoConfigure.logger.error("Answer file at #{@filename} has invalid values for #{invalid}. 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
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,38 @@ | ||
require 'test_helper' | ||
|
||
describe 'Kafo::AnswerFile' do | ||
|
||
describe 'valid answer file' do | ||
let(:answer_file_path) { 'test/fixtures/answer_files/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/invalid-answers.yaml' } | ||
|
||
it 'exits with invalid_answer_file' do | ||
must_exit_with_code(31) { Kafo::AnswerFile.new(answer_file_path) } | ||
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