Skip to content

Commit

Permalink
Merge pull request #155 from LMacchi/before
Browse files Browse the repository at this point in the history
Add before and after :each blocks
  • Loading branch information
dylanratcliffe authored Feb 27, 2018
2 parents 1a45f54 + a5d1a45 commit 6de7eeb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,20 @@ Why an array of hashes? Well, that is so that we can refer to the same node or n

In the example below we have referred to `centos6a` and `centos7b` in all of our tests as they are in `all_nodes`, `non_windows_servers` and `centos_severs`. However we have *left the more specific references to last*. This is because entries in the test_matrix will override entries above them if applicable. Meaning that we are still only testing each class on the two Centos servers once (Because the gem does de-duplication before running the tests), but also making sure we run `roles::frontend_webserver` twice before checking for idempotency.

**functions** In this section we can add functions that we want to mock when running spec tests. Each function takes the following agruments:
**functions** In this section we can add functions that we want to mock when running spec tests. Each function takes the following arguments:
- **type** *statement or rvalue*
- **returns** *Optional: A value to return*

**before and after conditions** We can set `before` and `after` blocks before each spec test. These are usually used when the functions to stub are conditional: stub functionx if the OS is windows, stub functiony if the fact java_installed is true. The facts are available through the `node_facts` hash.

```yaml
before:
- "Puppet::Util::Platform.stubs(:'windows?').returns(node_facts['kernel'] == 'windows')"

after:
- "puts 'Test finished running'"
```
**opts** The `opts` section overrides defaults for the `Onceover::Controlrepo` class' `opts` hash.

```yaml
Expand Down
22 changes: 13 additions & 9 deletions lib/onceover/testconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class TestConfig
attr_accessor :filter_classes
attr_accessor :filter_nodes
attr_accessor :mock_functions
attr_accessor :before_conditions
attr_accessor :after_conditions
attr_accessor :skip_r10k
attr_accessor :strict_variables

Expand All @@ -35,15 +37,17 @@ def initialize(file, opts = {})
raise "Could not parse #{file}, check that it is valid YAML and that the encoding is correct"
end

@classes = []
@nodes = []
@node_groups = []
@class_groups = []
@spec_tests = []
@acceptance_tests = []
@opts = opts
@mock_functions = config['functions']
@strict_variables = opts[:strict_variables] ? 'yes' : 'no'
@classes = []
@nodes = []
@node_groups = []
@class_groups = []
@spec_tests = []
@acceptance_tests = []
@opts = opts
@mock_functions = config['functions']
@before_conditions = config['before']
@after_conditions = config['after']
@strict_variables = opts[:strict_variables] ? 'yes' : 'no'

# Initialise all of the classes and nodes
config['classes'].each { |clarse| Onceover::Class.new(clarse) } unless config['classes'] == nil
Expand Down
17 changes: 16 additions & 1 deletion templates/test_spec.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,22 @@ let!(:<%= function %>) { MockFunction.new('<%= function %>') { |f|
<% end -%>
<% test.nodes.each do |node| -%>
context "using fact set <%= node.name %>" do
let(:facts) { <%= node.fact_set %> }
node_facts = <%= node.fact_set %>
let(:facts) { node_facts }
<% if @before_conditions -%>
before :each do
<% @before_conditions.each do |function| -%>
<%= function %>
<% end -%>
end
<% end -%>
<% if @after_conditions -%>
after :each do
<% @after_conditions.each do |function| -%>
<%= function %>
<% end -%>
end
<% end -%>
<% if pre_condition -%>
let(:pre_condition) {
pp = <%= '<<' %>-END
Expand Down

0 comments on commit 6de7eeb

Please sign in to comment.