rspec-puppet-utils 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hieradata/validator.rb +25 -0
- data/lib/hieradata/yaml_validator.rb +40 -0
- data/lib/mock_function.rb +27 -0
- data/lib/rspec-puppet-utils.rb +5 -59
- data/lib/template_harness.rb +33 -0
- data/rspec-puppet-utils.gemspec +2 -2
- data/spec/classes/hieradata/validator_spec.rb +71 -0
- data/spec/classes/hieradata/yaml_validator_spec.rb +72 -0
- data/spec/classes/mock_function_spec.rb +1 -1
- data/spec/classes/template_harness_spec.rb +1 -1
- data/spec/classes/utils_spec.rb +22 -0
- data/spec/fixtures/hieradata/empty/empty.yaml +1 -0
- data/spec/fixtures/hieradata/invalid/invalid.yaml +3 -0
- data/spec/fixtures/hieradata/valid/other.foo +2 -0
- data/spec/fixtures/hieradata/valid/sub/nested.yaml +2 -0
- data/spec/fixtures/hieradata/valid/valid.yaml +2 -0
- metadata +23 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA512:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41a4497de981008000489d01e33176c07417196b7cbcb55479d24c66c2906a91ce39869e431fe0ee11f7b7efb5224f51a2e33dfe463681cf3eb5909efae42ac9
|
4
|
+
data.tar.gz: aebf955a77f6235a8259c89186bdb5544a912c12ec8daa6de71715840c6fa58dbc1c816917289311f862f3a131f2fe82825772da41b74e84b25dcef399fa3806
|
5
5
|
SHA1:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45f31b9cc95b9c2a29aba88d9ddb3dac85b81d28
|
7
|
+
data.tar.gz: 5beeeb27784486ac29730b0bb082a971fc46ad92
|
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
module HieraData
|
3
|
+
|
4
|
+
class Validator
|
5
|
+
|
6
|
+
attr_accessor :data
|
7
|
+
|
8
|
+
def validate?(key, &block)
|
9
|
+
|
10
|
+
raise StandardError, '@data is nil, try load() first' unless @data
|
11
|
+
raise StandardError, '@data is empty' if @data.empty?
|
12
|
+
|
13
|
+
found = false
|
14
|
+
@data.keys.each do |file|
|
15
|
+
if @data[file].has_key? key
|
16
|
+
found = true
|
17
|
+
valid = block.call(@data[file][key])
|
18
|
+
raise StandardError, "Key #{key} is not valid in file #{file}" unless valid
|
19
|
+
end
|
20
|
+
end
|
21
|
+
raise StandardError, "Key #{key} was not found in any files" unless found
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'hieradata/validator'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module HieraData
|
5
|
+
|
6
|
+
class YamlValidator < Validator
|
7
|
+
|
8
|
+
def initialize(directory, extensions = ['yaml', 'yml'])
|
9
|
+
raise ArgumentError, 'extensions should be an Array' unless extensions.is_a? Array
|
10
|
+
@directory = directory
|
11
|
+
@extensions = extensions.map {|ext| ext =~ /\..*/ ? ext : ".#{ext}" }
|
12
|
+
end
|
13
|
+
|
14
|
+
def load(ignore_empty = false)
|
15
|
+
|
16
|
+
files = Dir.glob(File.join(@directory, '**', '*')).reject { |path|
|
17
|
+
File.directory?(path) || !@extensions.include?(File.extname path )
|
18
|
+
}
|
19
|
+
|
20
|
+
@data = {}
|
21
|
+
files.each { |file|
|
22
|
+
|
23
|
+
# Assume all file names are unique i.e. thing.yaml and thing.yml don't both exist
|
24
|
+
file_name = File.basename(file).split('.').first
|
25
|
+
|
26
|
+
begin
|
27
|
+
yaml = File.open(file) { |yf|
|
28
|
+
YAML::load( yf )
|
29
|
+
}
|
30
|
+
rescue ArgumentError => e
|
31
|
+
raise StandardError, "Yaml Syntax error in file #{file}: #{e.message}"
|
32
|
+
end
|
33
|
+
raise StandardError, "Yaml file is empty: #{file}" if !yaml && !ignore_empty
|
34
|
+
|
35
|
+
@data[file_name.to_sym] = yaml
|
36
|
+
}
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rspec-puppet'
|
2
|
+
|
3
|
+
class MockFunction
|
4
|
+
|
5
|
+
attr_accessor :function_type, :has_default_value, :default_value
|
6
|
+
|
7
|
+
def initialize(example_group, name, options = {})
|
8
|
+
opts = options.nil? ? {} : options
|
9
|
+
|
10
|
+
@function_type = opts.has_key?(:type) ? opts[:type] : :rvalue
|
11
|
+
|
12
|
+
opts[:default_value] = nil if @function_type == :statement
|
13
|
+
|
14
|
+
@has_default_value = false
|
15
|
+
if opts.has_key?(:default_value)
|
16
|
+
@has_default_value = true
|
17
|
+
@default_value = opts[:default_value]
|
18
|
+
end
|
19
|
+
|
20
|
+
this = self
|
21
|
+
example_group.before(:each) {
|
22
|
+
Puppet::Parser::Functions.newfunction(name.to_sym, {:type => this.function_type}) { |args| this.call(args) }
|
23
|
+
this.stubs(:call).returns(this.default_value) if this.has_default_value
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/rspec-puppet-utils.rb
CHANGED
@@ -1,64 +1,10 @@
|
|
1
|
-
require 'rspec
|
1
|
+
require 'rspec'
|
2
2
|
require 'mocha'
|
3
|
-
require '
|
3
|
+
require 'lib/mock_function'
|
4
|
+
require 'lib/template_harness'
|
5
|
+
require 'lib/hieradata/validator'
|
6
|
+
require 'lib/hieradata/yaml_validator'
|
4
7
|
|
5
8
|
RSpec.configure do |c|
|
6
9
|
c.mock_with :mocha
|
7
10
|
end
|
8
|
-
|
9
|
-
class MockFunction
|
10
|
-
|
11
|
-
attr_accessor :function_type, :has_default_value, :default_value
|
12
|
-
|
13
|
-
def initialize(example_group, name, options = {})
|
14
|
-
opts = options.nil? ? {} : options
|
15
|
-
|
16
|
-
@function_type = opts.has_key?(:type) ? opts[:type] : :rvalue
|
17
|
-
|
18
|
-
opts[:default_value] = nil if @function_type == :statement
|
19
|
-
|
20
|
-
@has_default_value = false
|
21
|
-
if opts.has_key?(:default_value)
|
22
|
-
@has_default_value = true
|
23
|
-
@default_value = opts[:default_value]
|
24
|
-
end
|
25
|
-
|
26
|
-
this = self
|
27
|
-
example_group.before(:each) {
|
28
|
-
Puppet::Parser::Functions.newfunction(name.to_sym, {:type => this.function_type}) { |args| this.call(args) }
|
29
|
-
this.stubs(:call).returns(this.default_value) if this.has_default_value
|
30
|
-
}
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
class TemplateHarness
|
35
|
-
|
36
|
-
def initialize(template, scope = nil)
|
37
|
-
@template = template
|
38
|
-
@isolator = Isolator.new(scope)
|
39
|
-
end
|
40
|
-
|
41
|
-
def set(name, value)
|
42
|
-
var_name = name.start_with?('@') ? name : "@#{name}"
|
43
|
-
@isolator.instance_variable_set(var_name, value)
|
44
|
-
end
|
45
|
-
|
46
|
-
def run
|
47
|
-
b = @isolator.get_binding
|
48
|
-
template = File.exists?(@template) ? File.new(@template).read : @template
|
49
|
-
ERB.new(template, 0, '-').result b
|
50
|
-
end
|
51
|
-
|
52
|
-
class Isolator
|
53
|
-
# Isolates the binding so that only the defined set
|
54
|
-
# of instance variables are available to erb
|
55
|
-
def initialize scope
|
56
|
-
@scope = scope
|
57
|
-
end
|
58
|
-
def get_binding
|
59
|
-
scope = @scope
|
60
|
-
binding
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
class TemplateHarness
|
4
|
+
|
5
|
+
def initialize(template, scope = nil)
|
6
|
+
@template = template
|
7
|
+
@isolator = Isolator.new(scope)
|
8
|
+
end
|
9
|
+
|
10
|
+
def set(name, value)
|
11
|
+
var_name = name.start_with?('@') ? name : "@#{name}"
|
12
|
+
@isolator.instance_variable_set(var_name, value)
|
13
|
+
end
|
14
|
+
|
15
|
+
def run
|
16
|
+
b = @isolator.get_binding
|
17
|
+
template = File.exists?(@template) ? File.new(@template).read : @template
|
18
|
+
ERB.new(template, 0, '-').result b
|
19
|
+
end
|
20
|
+
|
21
|
+
class Isolator
|
22
|
+
# Isolates the binding so that only the defined set
|
23
|
+
# of instance variables are available to erb
|
24
|
+
def initialize scope
|
25
|
+
@scope = scope
|
26
|
+
end
|
27
|
+
def get_binding
|
28
|
+
scope = @scope
|
29
|
+
binding
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/rspec-puppet-utils.gemspec
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |gem|
|
3
3
|
gem.name = 'rspec-puppet-utils'
|
4
|
-
gem.version = '1.0.
|
5
|
-
gem.description = 'Helper classes for mock functions and
|
4
|
+
gem.version = '1.0.1'
|
5
|
+
gem.description = 'Helper classes for mock/stub functions, templates and hierdata'
|
6
6
|
gem.summary = ''
|
7
7
|
gem.author = 'Tom Poulton'
|
8
8
|
#gem.license = ''
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'lib/hieradata/validator'
|
3
|
+
|
4
|
+
module HieraData
|
5
|
+
class Test < Validator
|
6
|
+
def load
|
7
|
+
@data = {
|
8
|
+
:file1 => {
|
9
|
+
'key' => 'value',
|
10
|
+
:other => 'other value',
|
11
|
+
'missmatch' => 'string'
|
12
|
+
},
|
13
|
+
:file2 => {
|
14
|
+
'hello' => 'world',
|
15
|
+
'missmatch' => ['array']
|
16
|
+
}
|
17
|
+
}
|
18
|
+
end
|
19
|
+
def load_empty
|
20
|
+
@data = {}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe HieraData::Validator do
|
26
|
+
|
27
|
+
validator = HieraData::Test.new
|
28
|
+
validator.load
|
29
|
+
|
30
|
+
it 'should have public data variable' do
|
31
|
+
expect(validator.data).to have_key :file1
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should use block to validate key' do
|
35
|
+
expect {
|
36
|
+
validator.validate?('key') { |v| v == 'value' }
|
37
|
+
}.to_not raise_error
|
38
|
+
|
39
|
+
expect {
|
40
|
+
validator.validate?('key') { |v| v == 'oooops' }
|
41
|
+
}.to raise_error StandardError, /Key key is not valid in file/
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should accept symbol as key' do
|
45
|
+
expect {
|
46
|
+
validator.validate?(:other) { |v| v == 'other value' }
|
47
|
+
}.to_not raise_error
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should validate key in all files' do
|
51
|
+
expect {
|
52
|
+
validator.validate?('missmatch') { |v| v.is_a? String }
|
53
|
+
}.to raise_error StandardError, /Key missmatch is not valid in file file2/
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should raise error if data is nil' do
|
57
|
+
nil_validator = HieraData::Test.new
|
58
|
+
expect {
|
59
|
+
nil_validator.validate?('meh') { }
|
60
|
+
}.to raise_error StandardError, /@data is nil, try load\(\) first/
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should raise error if data is empty' do
|
64
|
+
empty_validator = HieraData::Test.new
|
65
|
+
empty_validator.load_empty
|
66
|
+
expect {
|
67
|
+
empty_validator.validate?('meh') { }
|
68
|
+
}.to raise_error StandardError, /@data is empty/
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'lib/hieradata/yaml_validator'
|
2
|
+
|
3
|
+
describe HieraData::YamlValidator do
|
4
|
+
|
5
|
+
it 'should be of type Validator' do
|
6
|
+
validator = HieraData::YamlValidator.new('spec/fixtures/hieradata/valid')
|
7
|
+
expect(validator).to be_a_kind_of HieraData::Validator
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'with valid yaml' do
|
11
|
+
|
12
|
+
validator = HieraData::YamlValidator.new('spec/fixtures/hieradata/valid')
|
13
|
+
validator.load
|
14
|
+
|
15
|
+
it 'should load yaml files into data' do
|
16
|
+
expect(validator.data.keys.size).to_not be 0
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should load yaml files recursively' do
|
20
|
+
expect(validator.data.keys).to include :nested
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should load yaml data from files' do
|
24
|
+
expect(validator.data[:valid]['string-value']).to eq 'a string'
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'with multiple extensions' do
|
30
|
+
|
31
|
+
validator = HieraData::YamlValidator.new('spec/fixtures/hieradata/valid', ['yaml', 'foo'])
|
32
|
+
validator.load
|
33
|
+
|
34
|
+
it 'should load yml files into data' do
|
35
|
+
expect(validator.data).to have_key :other
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'with extensions as string' do
|
41
|
+
|
42
|
+
it 'should load yml files into data' do
|
43
|
+
expect { HieraData::YamlValidator.new('meh', 'whooops') }.to raise_error ArgumentError, /extensions should be an Array/
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'with invalid yaml' do
|
49
|
+
|
50
|
+
validator = HieraData::YamlValidator.new('spec/fixtures/hieradata/invalid')
|
51
|
+
|
52
|
+
it 'should raise error with syntax error' do
|
53
|
+
expect { validator.load }.to raise_error StandardError, /Yaml Syntax error in file .*\/invalid.yaml/
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'with empty yaml' do
|
59
|
+
|
60
|
+
validator = HieraData::YamlValidator.new('spec/fixtures/hieradata/empty')
|
61
|
+
|
62
|
+
it 'should raise error' do
|
63
|
+
expect { validator.load }.to raise_error StandardError, /Yaml file is empty: .*\/empty.yaml/
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should ignore empty files when flag is set' do
|
67
|
+
expect { validator.load true }.to_not raise_error
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rspec-puppet-utils'
|
3
|
+
|
4
|
+
describe 'rspec-puppet-utils' do
|
5
|
+
|
6
|
+
it 'should require MockFunction' do
|
7
|
+
expect { MockFunction.class }.to_not raise_error
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should require TemplateHarness' do
|
11
|
+
expect { TemplateHarness.class }.to_not raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should require HieraData Validator' do
|
15
|
+
expect { HieraData::Validator.class }.to_not raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should require HieraData YamlValidator' do
|
19
|
+
expect { HieraData::YamlValidator.class }.to_not raise_error
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
---
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-puppet-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Poulton
|
@@ -9,10 +9,10 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2014-03-
|
12
|
+
date: 2014-03-11 00:00:00 Z
|
13
13
|
dependencies: []
|
14
14
|
|
15
|
-
description: Helper classes for mock functions and
|
15
|
+
description: Helper classes for mock/stub functions, templates and hierdata
|
16
16
|
email:
|
17
17
|
executables: []
|
18
18
|
|
@@ -25,10 +25,22 @@ files:
|
|
25
25
|
- Gemfile
|
26
26
|
- README.md
|
27
27
|
- Rakefile
|
28
|
+
- lib/hieradata/validator.rb
|
29
|
+
- lib/hieradata/yaml_validator.rb
|
30
|
+
- lib/mock_function.rb
|
28
31
|
- lib/rspec-puppet-utils.rb
|
32
|
+
- lib/template_harness.rb
|
29
33
|
- rspec-puppet-utils.gemspec
|
34
|
+
- spec/classes/hieradata/validator_spec.rb
|
35
|
+
- spec/classes/hieradata/yaml_validator_spec.rb
|
30
36
|
- spec/classes/mock_function_spec.rb
|
31
37
|
- spec/classes/template_harness_spec.rb
|
38
|
+
- spec/classes/utils_spec.rb
|
39
|
+
- spec/fixtures/hieradata/empty/empty.yaml
|
40
|
+
- spec/fixtures/hieradata/invalid/invalid.yaml
|
41
|
+
- spec/fixtures/hieradata/valid/other.foo
|
42
|
+
- spec/fixtures/hieradata/valid/sub/nested.yaml
|
43
|
+
- spec/fixtures/hieradata/valid/valid.yaml
|
32
44
|
- spec/fixtures/templates/returns_elephant.erb
|
33
45
|
- spec/spec_helper.rb
|
34
46
|
homepage: https://github.com/Accuity/rspec-puppet-utils
|
@@ -58,7 +70,15 @@ signing_key:
|
|
58
70
|
specification_version: 4
|
59
71
|
summary: ""
|
60
72
|
test_files:
|
73
|
+
- spec/classes/hieradata/validator_spec.rb
|
74
|
+
- spec/classes/hieradata/yaml_validator_spec.rb
|
61
75
|
- spec/classes/mock_function_spec.rb
|
62
76
|
- spec/classes/template_harness_spec.rb
|
77
|
+
- spec/classes/utils_spec.rb
|
78
|
+
- spec/fixtures/hieradata/empty/empty.yaml
|
79
|
+
- spec/fixtures/hieradata/invalid/invalid.yaml
|
80
|
+
- spec/fixtures/hieradata/valid/other.foo
|
81
|
+
- spec/fixtures/hieradata/valid/sub/nested.yaml
|
82
|
+
- spec/fixtures/hieradata/valid/valid.yaml
|
63
83
|
- spec/fixtures/templates/returns_elephant.erb
|
64
84
|
- spec/spec_helper.rb
|