summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Fournier <marc.fournier@camptocamp.com>2012-01-04 13:17:38 +0100
committerMarc Fournier <marc.fournier@camptocamp.com>2012-01-04 13:17:38 +0100
commitcb96d902a91325998765d9c981eab7ea0da098da (patch)
tree88ce258415400f938383bf6e01ea836df21b53aa
parent9418066cc806ca172c1214fb112c09ac23e41acb (diff)
couchdblookup: 2 more tests
-rw-r--r--lib/puppet/parser/functions/couchdblookup.rb2
-rw-r--r--spec/unit/data/missing.txt1
-rw-r--r--spec/unit/data/proxy-failure.txt1
-rwxr-xr-xspec/unit/puppet/parser/functions/couchdblookup_spec.rb16
4 files changed, 19 insertions, 1 deletions
diff --git a/lib/puppet/parser/functions/couchdblookup.rb b/lib/puppet/parser/functions/couchdblookup.rb
index 8c4bc21..7f920a8 100644
--- a/lib/puppet/parser/functions/couchdblookup.rb
+++ b/lib/puppet/parser/functions/couchdblookup.rb
@@ -15,7 +15,7 @@ module Puppet::Parser::Functions
begin
json = JSON.parse(open(URI.parse(url)).read)
- rescue OpenURI::HTTPError => error
+ rescue OpenURI::HTTPError, JSON::ParserError => error
raise Puppet::ParseError, "couchdblookup(): fetching URL #{url} failed with status #{error.message}"
end
diff --git a/spec/unit/data/missing.txt b/spec/unit/data/missing.txt
new file mode 100644
index 0000000..4e6d97e
--- /dev/null
+++ b/spec/unit/data/missing.txt
@@ -0,0 +1 @@
+{"error":"not_found","reason":"missing"}
diff --git a/spec/unit/data/proxy-failure.txt b/spec/unit/data/proxy-failure.txt
new file mode 100644
index 0000000..f5f1c37
--- /dev/null
+++ b/spec/unit/data/proxy-failure.txt
@@ -0,0 +1 @@
+<html><body><h1>It works!</h1></body></html>
diff --git a/spec/unit/puppet/parser/functions/couchdblookup_spec.rb b/spec/unit/puppet/parser/functions/couchdblookup_spec.rb
index 2af51a9..8777d71 100755
--- a/spec/unit/puppet/parser/functions/couchdblookup_spec.rb
+++ b/spec/unit/puppet/parser/functions/couchdblookup_spec.rb
@@ -70,4 +70,20 @@ describe "the couchdblookup function" do
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