rspec-puppet-utils 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e9e1b2ce2d09e5a50fff68ae9cc3f1e7e639ec18
4
- data.tar.gz: c8184a93084596ce109c344c438107a4914f4cfc
5
2
  SHA512:
6
- metadata.gz: e51385d1bdf52f5ade7292fc13cf3b58f27008db4db08ff9c4fb056af3d091042054b3b75737530f26bfbe7ed27d3c98149fa84137823ddb9635ae0ad01c4a77
7
- data.tar.gz: 7aa2fe95f16d78ea0382451c6b9de02c3fb2286193f445d74a4c7df308da7d0d7bca6dfd935d334ecadbd8a2448202a0ad024dc9b80974aa3a82bfcf4250b712
3
+ data.tar.gz: 3e303ac68dc2710bf81b485b78fb9fe2060aa1e362e28bbeecc2c6c7b3d060b82a82cb5c117ef0861ee2a7ab9e6a5b93f9b6d49ae76472849d1f85d8ea335f57
4
+ metadata.gz: cf4ed17265b26a8e22cd38c093f7029e47e60ede625acebcef3cc921421a98409335e7503e73a21973e220afbdf8caf9579550f58df4cdf85349c877daea5f73
5
+ SHA1:
6
+ data.tar.gz: 9de61be3f57bd1a2e660b27afa93fbe3434274a5
7
+ metadata.gz: f68d290d274904354046b1871b430cbae5e846e4
data/Gemfile CHANGED
@@ -1,8 +1,8 @@
1
1
  source 'https://rubygems.org/'
2
2
 
3
- gem 'puppet'
3
+ gem 'puppet', '~> 3'
4
4
  gem 'mocha'
5
- gem 'rspec'
5
+ gem 'rspec', '3.1.0'
6
6
  gem 'rspec-puppet'
7
7
  gem 'puppetlabs_spec_helper'
8
8
  gem 'thor'
data/README.md CHANGED
@@ -65,6 +65,64 @@ Clearing the cache ensures tests aren't coupled and order dependent. The downsid
65
65
  - You always stub the `execute` method as that gets called internally
66
66
  - The `execute` method takes a set of arguments instead of an array of arguments
67
67
 
68
+ ### MockResource (experimental feature)
69
+
70
+ I've created a rough version for now just to help myself out, if people find it useful or find bugs, let me know
71
+
72
+ #####Usage:
73
+
74
+ To stop your tests dissapearing down a rabbit hole, you can use the rspec-puppet `let(:pre_condition) { ... }` feature to create mock versions of resources that your puppet class depends on. For example:
75
+
76
+ ```puppet
77
+ class my_module::my_class {
78
+
79
+ include foo::bar
80
+
81
+ $useful_var = $foo::bar::baz
82
+
83
+ external_module::complex_type { 'complex thing':
84
+ param_one => 'one',
85
+ param_two => 'two',
86
+ required_param => 'important value',
87
+ }
88
+
89
+ <actual stuff you want to test>
90
+ }
91
+ ```
92
+
93
+ In the tests for `my_class`, you don't want to use the actual `foo::bar` and `external_module::complex_type` resources because it could be a lot of complex setup code, it can be difficult to test multiple scenarios, and you are by extension testing these other classes (which should have tests of their own)
94
+
95
+ You can therefore mock these resources by creating fakes that have the same "interface", but empty bodies:
96
+ ```ruby
97
+ let(:pre_condition) { [
98
+ "class foo::bar { $baz = 'a value' }",
99
+ "define external_module::complex_type ( $param_one = 'default', $param_two = undef, $required_param ) {}",
100
+ ] }
101
+ ```
102
+
103
+ This can get quite complex if there are multiple parameters and/or internal variables. `MockResource` is designed to make it easier to mock out these dependencies
104
+ ```ruby
105
+ mock_bar = MockResource.new 'foo::bar', {
106
+ :vars => { :baz => some_var_you_want_to_use_later }
107
+ }
108
+
109
+ mock_complex_type = MockResource.new 'external_module::complex_type', {
110
+ :type => :define,
111
+ :params => {
112
+ :param_one => 'default',
113
+ :param_two => :undef,
114
+ :required_param => nil,
115
+ }
116
+ }
117
+
118
+ let(:pre_condition) { [
119
+ mock_bar.render,
120
+ mock_complex_thing.render,
121
+ ] }
122
+ ```
123
+
124
+ Hopefully you spend less time debugging syntax errors in your test strings, and more time writing useful code
125
+
68
126
  ### TemplateHarness
69
127
 
70
128
  If your templates have some logic in them that you want to test, you'd ideally like to get hold of the generated template so you can inspect it programmatically rather than just using a regex. In this case use `TemplateHarness`
@@ -0,0 +1,43 @@
1
+ module RSpecPuppetUtils
2
+
3
+ # MockResource is an experimental feature, API may change!
4
+ # Use at your own risk... although feedback would be helpful :)
5
+ class MockResource
6
+
7
+ def initialize(name, resource_definition = {})
8
+ @name = name
9
+ @resource_definition = resource_definition
10
+ end
11
+
12
+ def render
13
+ type = @resource_definition[:type] || :class
14
+ vars = join_vars @resource_definition[:vars], "\n"
15
+
16
+ if @resource_definition[:params].nil?
17
+ param_section = ''
18
+ else
19
+ params = join_vars @resource_definition[:params], ', '
20
+ param_section = "( #{params} )"
21
+ end
22
+
23
+ "#{type} #{@name} #{param_section} { #{vars} }"
24
+ end
25
+
26
+ def join_vars(vars, join_string)
27
+ return '' unless vars
28
+ parsed_vars = []
29
+ vars.each { |key, val|
30
+ param = "$#{key}"
31
+ value = normalise_value val
32
+ val ? parsed_vars.push("#{param} = #{value}") : parsed_vars.push(param)
33
+ }
34
+ parsed_vars.join join_string
35
+ end
36
+
37
+ def normalise_value(value)
38
+ # If string, wrap with quotes
39
+ value.is_a?(String) ? "'#{value}'" : value
40
+ end
41
+
42
+ end
43
+ end
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |gem|
3
3
  gem.name = 'rspec-puppet-utils'
4
- gem.version = '2.1.0'
4
+ gem.version = '2.2.0'
5
5
  gem.description = 'Helper classes for mock/stub functions, templates and hierdata'
6
6
  gem.summary = ''
7
7
  gem.author = 'Tom Poulton'
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+ require 'lib/mock_resource'
3
+
4
+ include RSpecPuppetUtils
5
+
6
+ describe MockResource do
7
+
8
+ let(:resource_name) { 'my_mock_resource' }
9
+ let(:resource_definition) {
10
+ {
11
+ :params => {
12
+ :string_param => 'hello',
13
+ :undef_param => :undef,
14
+ :required_param => nil,
15
+ },
16
+ :vars => {
17
+ :my_var_one => 'uno',
18
+ :my_var_two => 2,
19
+ }
20
+ }
21
+ }
22
+
23
+ describe 'render' do
24
+
25
+ subject(:mock_resource) { MockResource.new(resource_name, resource_definition).render }
26
+
27
+ it 'returns the rendered resource' do
28
+ expect(mock_resource).to match /class .* \{/
29
+ end
30
+
31
+ it 'adds the name to the resource' do
32
+ expect(mock_resource).to match /class #{resource_name}/
33
+ end
34
+
35
+ it 'renders string params in quotes' do
36
+ expect(mock_resource).to match /\(.*\$string_param = 'hello'.*\)/
37
+ end
38
+
39
+ it 'renders undef params without quotes' do
40
+ expect(mock_resource).to match /\(.*\$undef_param = undef.*\)/
41
+ end
42
+
43
+ it 'renders required params without an assigned value' do
44
+ expect(mock_resource).to match /\(.*\$required_param(,.*|\s*\))/
45
+ end
46
+
47
+ it 'renders string variables with quotes' do
48
+ expect(mock_resource).to match /\{.*\$my_var_one = 'uno'.*\}/m
49
+ end
50
+
51
+ it 'renders numerical variables without quotes' do
52
+ expect(mock_resource).to match /\{.*\$my_var_two = 2.*\}/m
53
+ end
54
+
55
+ context 'when no params are provided' do
56
+
57
+ before :each do
58
+ resource_definition.delete :params
59
+ end
60
+
61
+ it 'renders no parenthesis' do
62
+ expect(mock_resource).to match /#{resource_name}\s+\{.*\}/m
63
+ end
64
+
65
+ end
66
+
67
+ context 'when type is :define' do
68
+
69
+ before :each do
70
+ resource_definition[:type] = :define
71
+ end
72
+
73
+ it 'renders a defined type' do
74
+ expect(mock_resource).to match /define #{resource_name}/
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end
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: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Poulton
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2015-07-28 00:00:00 Z
12
+ date: 2015-10-28 00:00:00 Z
13
13
  dependencies: []
14
14
 
15
15
  description: Helper classes for mock/stub functions, templates and hierdata
@@ -28,12 +28,14 @@ files:
28
28
  - lib/hieradata/validator.rb
29
29
  - lib/hieradata/yaml_validator.rb
30
30
  - lib/mock_function.rb
31
+ - lib/mock_resource.rb
31
32
  - lib/rspec-puppet-utils.rb
32
33
  - lib/template_harness.rb
33
34
  - rspec-puppet-utils.gemspec
34
35
  - spec/classes/hieradata/validator_spec.rb
35
36
  - spec/classes/hieradata/yaml_validator_spec.rb
36
37
  - spec/classes/mock_function_spec.rb
38
+ - spec/classes/mock_resource_spec.rb
37
39
  - spec/classes/template_harness_spec.rb
38
40
  - spec/classes/utils_spec.rb
39
41
  - spec/fixtures/hieradata/empty/empty.yaml
@@ -77,6 +79,7 @@ test_files:
77
79
  - spec/classes/hieradata/validator_spec.rb
78
80
  - spec/classes/hieradata/yaml_validator_spec.rb
79
81
  - spec/classes/mock_function_spec.rb
82
+ - spec/classes/mock_resource_spec.rb
80
83
  - spec/classes/template_harness_spec.rb
81
84
  - spec/classes/utils_spec.rb
82
85
  - spec/fixtures/hieradata/empty/empty.yaml