Skip to content

Commit

Permalink
Merge pull request #163 from LMacchi/trusted
Browse files Browse the repository at this point in the history
Add trusted facts support
  • Loading branch information
dylanratcliffe authored Mar 27, 2018
2 parents 47dde98 + 8accd23 commit 9f9c77e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ In the example below we have referred to `centos6a` and `centos7b` in all of our
- **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.
**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 and the trusted facts as `trusted_facts`.

```yaml
before:
Expand Down Expand Up @@ -250,6 +250,26 @@ Once we have our factset all we need to do is copy it into `spec/factsets/` insi

Would map to a node named `server2008r2` in `onceover.yaml`

#### Trusted Facts

You can add trusted facts to the nodesets by creating a new section called trusted:

```
{
"name": "node.puppetlabs.net",
"trusted": {
"pp_role": "agent",
"pp_datacenter": "puppet",
},
"values": {
"aio_agent_build": "1.10.4",
"aio_agent_version": "1.10.4",
"architecture": "x86_64",
```

Notice that the `extensions` part is implied. The first fact in that example translates to `$trusted['extensions']['pp_role']` in Puppet code.

### nodesets

`spec/acceptance/nodesets/onceover-nodes.yml`
Expand Down
12 changes: 8 additions & 4 deletions lib/onceover/controlrepo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ def self.config
end

def self.facts(filter = nil)
@@existing_controlrepo.facts(filter)
@@existing_controlrepo.facts(filter, 'values')
end

def self.trusted_facts(filter = nil)
@@existing_controlrepo.facts(filter, 'trusted')
end

def self.hiera_config_file
Expand Down Expand Up @@ -170,12 +174,12 @@ def classes
classes.flatten
end

def facts(filter = nil)
def facts(filter = nil, key = 'values')
# Returns an array facts hashes
all_facts = []
logger.debug "Reading factsets"
@facts_files.each do |file|
all_facts << read_facts(file)['values']
all_facts << read_facts(file)[key]
end
if filter
# Allow us to pass a hash of facts to filter by
Expand Down Expand Up @@ -579,7 +583,7 @@ def read_facts(facts_file)
begin
result = JSON.parse(file)
rescue JSON::ParserError
raise "Could not parse the JSON file, check that it is valid JSON and that the encoding is correct"
raise "Could not parse the file #{facts_file}, check that it is valid JSON and that the encoding is correct"
end
result
end
Expand Down
4 changes: 4 additions & 0 deletions lib/onceover/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Node
attr_accessor :name
attr_accessor :beaker_node
attr_accessor :fact_set
attr_accessor :trusted_set

def initialize(name)
@name = name
Expand All @@ -19,9 +20,12 @@ def initialize(name)
File.basename(facts_file, '.json') == name
}
@fact_set = Onceover::Controlrepo.facts[facts_file_index]
@trusted_set = Onceover::Controlrepo.trusted_facts[facts_file_index]
rescue TypeError
@fact_set = nil
@trusted_set = nil
end

@@all << self

end
Expand Down
4 changes: 4 additions & 0 deletions templates/test_spec.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ let!(:<%= function %>) { MockFunction.new('<%= function %>') { |f|
context "using fact set <%= node.name %>" do
node_facts = <%= node.fact_set %>
let(:facts) { node_facts }
<% if node.trusted_set -%>
trusted_facts = <%= node.trusted_set %>
let(:trusted_facts) { trusted_facts }
<% end -%>
<% if @before_conditions -%>
before :each do
<% @before_conditions.each do |function| -%>
Expand Down

0 comments on commit 9f9c77e

Please sign in to comment.