-
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
12 changed files
with
218 additions
and
27 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,16 @@ | ||
require 'kafo/answer_file/v1' | ||
|
||
module Kafo | ||
module AnswerFile | ||
|
||
def self.load_answers(filename, version) | ||
case version | ||
when 1 | ||
AnswerFile::V1.new(filename) | ||
else | ||
raise InvalidAnswerFileVersion | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'yaml' | ||
|
||
module Kafo | ||
module AnswerFile | ||
class Base | ||
|
||
attr_reader :answers, :filename | ||
|
||
def initialize(filename) | ||
@filename = filename | ||
@answers = YAML.load_file(@filename) | ||
validate | ||
end | ||
|
||
def save(data, config_header) | ||
FileUtils.touch @filename | ||
File.chmod 0600, @filename | ||
File.open(@filename, 'w') { |f| f.write(config_header + format(YAML.dump(data))) } | ||
end | ||
|
||
def puppet_classes | ||
raise NoMethodError | ||
end | ||
|
||
def class_enabled?(puppet_class) | ||
raise NoMethodError | ||
end | ||
|
||
def parameters_for_class(puppet_class) | ||
raise NoMethodError | ||
end | ||
|
||
def validate | ||
raise NoMethodError | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'kafo/answer_file/base' | ||
|
||
module Kafo | ||
module AnswerFile | ||
class V1 < Base | ||
|
||
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 | ||
|
||
def validate | ||
invalid = @answers.reject do |puppet_class, value| | ||
value.is_a?(Hash) || [true, false].include?(value) | ||
end | ||
|
||
unless invalid.empty? | ||
fail InvalidAnswerFile, "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
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 '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.load_answers(answer_file_path, 1) } | ||
|
||
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' } | ||
|
||
it 'exits with invalid_answer_file' do | ||
_(Proc.new { Kafo::AnswerFile.load_answers(answer_file_path, 1) } ).must_raise Kafo::InvalidAnswerFile | ||
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