summaryrefslogtreecommitdiff
path: root/spec/unit/puppet/parser/functions/couchdblookup_spec.rb
blob: 8777d7108bcdb76c8fc2e20a5a1af3087465509d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env rspec

require 'puppet'

describe "the couchdblookup function" do
  before :all do
    Puppet::Parser::Functions.autoloader.loadall
    @datapath = File.dirname(__FILE__) + '/../../../data/'
  end

  before :each do
    @scope = Puppet::Parser::Scope.new
  end

  it "should exist" do
    Puppet::Parser::Functions.function("couchdblookup").should == "function_couchdblookup"
  end

  it "should raise a ParseError unless there is exactly 2 arguments" do
    lambda { @scope.function_couchdblookup([]) }.should       raise_error(Puppet::ParseError)
    lambda { @scope.function_couchdblookup([1]) }.should      raise_error(Puppet::ParseError)
    lambda { @scope.function_couchdblookup([1,2,3]) }.should  raise_error(Puppet::ParseError)
  end

  it "should return the value of a key from a single couchdb document" do
    sample_json = File.open(@datapath + 'one-document.txt')
    OpenURI.stub!(:open_uri).and_return(sample_json)

    result = @scope.function_couchdblookup(["http://fake/uri", "wiki"])
    result.should eq(true)
  end

  it "should raise a ParseError if a key can't be found in a couchdb document" do
    sample_json = File.open(@datapath + 'one-document.txt')
    OpenURI.stub!(:open_uri).and_return(sample_json)

    result = lambda { @scope.function_couchdblookup(["http://fake/uri", "fake-key"]) }
    result.should raise_error(Puppet::ParseError)
  end

  it "should return an array from the values of a couchdb view" do
    sample_json = File.open(@datapath + 'map.txt')
    OpenURI.stub!(:open_uri).and_return(sample_json)

    result = @scope.function_couchdblookup(["http://fake/uri", "value"])
    result.should eq(["foo", "bar"])
  end

  it "should raise a ParseError if a key can't be found in the rows of a couchdb view" do
    sample_json = File.open(@datapath + 'map.txt')
    OpenURI.stub!(:open_uri).and_return(sample_json)

    result = lambda { @scope.function_couchdblookup(["http://fake/uri", "fake-key"]) }
    result.should raise_error(Puppet::ParseError)
  end

  it "should return an array the values from a couchdb reduced view" do
    sample_json = File.open(@datapath + 'map+reduce.txt')
    OpenURI.stub!(:open_uri).and_return(sample_json)

    result = @scope.function_couchdblookup(["http://fake/uri", "value"])
    result.should eq(["foo", "bar", "baz"])
  end

  it "should raise a ParseError if a key can't be found in the rows of a couchdb reduced view" do
    sample_json = File.open(@datapath + 'map+reduce.txt')
    OpenURI.stub!(:open_uri).and_return(sample_json)

    result = lambda { @scope.function_couchdblookup(["http://fake/uri", "fake-key"]) }
    result.should raise_error(Puppet::ParseError)
  end

  it "should raise a ParseError if couchdb can't find the requested document" do
    sample_json = File.open(@datapath + 'missing.txt')
    OpenURI.stub!(:open_uri).and_return(sample_json)

    result = lambda { @scope.function_couchdblookup(["http://fake/uri", "a-key"]) }
    result.should raise_error(Puppet::ParseError)
  end

  it "should raise a ParseError if input in not valid JSON" do
    sample_json = File.open(@datapath + 'proxy-failure.txt')
    OpenURI.stub!(:open_uri).and_return(sample_json)

    result = lambda { @scope.function_couchdblookup(["http://fake/uri", "a-key"]) }
    result.should raise_error(Puppet::ParseError)
  end

end