Skip to content

Commit d3cfb83

Browse files
authored
Testing (crayfishx#60)
* guarding initial spec stuff * added initial testing * added more tests * added travis * removed jruby * added badge
1 parent 815b390 commit d3cfb83

File tree

7 files changed

+121
-21
lines changed

7 files changed

+121
-21
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: ruby
2+
script: "bundle exec rspec --format documentation"
3+
4+
rvm:
5+
- 2.1.9
6+
- 2.4.1

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
gem 'rspec'
3+
gem 'lookup_http'

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
[![Build Status](https://travis-ci.org/crayfishx/hiera-http.svg?branch=master)](https://travis-ci.org/crayfishx/hiera-http)
3+
14
## hiera_http : a HTTP data provider function (backend) for Hiera 5
25

36
### Description

Rakefile

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/puppet/functions/hiera_http.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def lookup_key(key, options, context)
2323
unless key[regex_key_match] == key
2424
context.explain { "Skipping hiera_http backend because key does not match confine_to_keys" }
2525
context.not_found
26+
return
2627
end
2728
end
2829

spec/functions/hiera_http_spec.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require 'spec_helper'
2+
3+
require 'puppet/functions/hiera_http'
4+
5+
describe FakeFunction do
6+
7+
let(:function) { described_class.new }
8+
before(:each) do
9+
@lookuphttp = instance_double("LookupHttp")
10+
@context = instance_double("Puppet::LookupContext")
11+
allow(LookupHttp).to receive(:new).and_return(@lookuphttp)
12+
allow(@context).to receive(:cache_has_key)
13+
allow(@context).to receive(:explain)
14+
allow(@context).to receive(:interpolate)
15+
allow(@context).to receive(:cache)
16+
allow(@context).to receive(:not_found)
17+
allow(@context).to receive(:interpolate).with('/path').and_return('/path')
18+
end
19+
20+
describe "#lookup_key" do
21+
22+
context "Should run" do
23+
let(:options) { {
24+
'uri' => 'http://192.168.0.1:8080/path'
25+
} }
26+
27+
it "should run" do
28+
expect(LookupHttp).to receive(:new).with({ :host => '192.168.0.1', :port => 8080 })
29+
expect(@lookuphttp).to receive(:get_parsed).with('/path').and_return('value')
30+
expect(function.lookup_key('bar', options, @context)).to eq('value')
31+
end
32+
end
33+
34+
context "When using __XXX__ interpolation" do
35+
let(:key) { 'foo::bar::tango' }
36+
37+
it "should interpolate __KEY__ correctly" do
38+
options = { 'uri' => 'http://localhost/path/__KEY__' }
39+
expect(@context).to receive(:interpolate).with('/path/foo::bar::tango').and_return('/path/foo::bar::tango')
40+
expect(@lookuphttp).to receive(:get_parsed).with('/path/foo::bar::tango')
41+
function.lookup_key('foo::bar::tango', options, @context)
42+
end
43+
end
44+
45+
context "When confine_to_keys is set" do
46+
let(:options) { {
47+
'uri' => 'http://localhost/path',
48+
'confine_to_keys' => [ /^tango.*/ ],
49+
} }
50+
51+
before(:each) do
52+
allow(@context).to receive(:interpolate).with('/path').and_return('/path')
53+
end
54+
55+
it "should return not found for non matching keys" do
56+
expect(@context).to receive(:not_found)
57+
expect(@lookuphttp).not_to receive(:get_parsed)
58+
function.lookup_key('bar', options, @context)
59+
end
60+
61+
it "should run if key matches" do
62+
expect(@context).not_to receive(:not_found)
63+
expect(@lookuphttp).to receive(:get_parsed).with('/path').and_return('value')
64+
expect(function.lookup_key('tangodelta', options, @context)).to eq('value')
65+
end
66+
end
67+
68+
context "Output of lookup_http" do
69+
let(:options) { {
70+
'uri' => 'http://localhost/path',
71+
} }
72+
it "should dig if the value is a hash" do
73+
expect(@lookuphttp).to receive(:get_parsed).with('/path').and_return({ 'tango' => 'delta' })
74+
expect(function.lookup_key('tango', options, @context)).to eq('delta')
75+
end
76+
it "should return the whole value if the value is a string" do
77+
expect(@lookuphttp).to receive(:get_parsed).with('/path').and_return('delta')
78+
expect(function.lookup_key('tango', options, @context)).to eq('delta')
79+
end
80+
end
81+
82+
83+
84+
85+
end
86+
87+
88+
end

spec/spec_helper.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
class FakeFunction
4+
def self.dispatch(name, &block)
5+
end
6+
end
7+
8+
9+
module Puppet
10+
module Functions
11+
def self.create_function(name, &block)
12+
FakeFunction.class_eval(&block)
13+
end
14+
end
15+
class DataBinding
16+
class LookupError < RuntimeError
17+
end
18+
end
19+
end
20+

0 commit comments

Comments
 (0)