summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/rest.rb
blob: fb3790e0c6fddffe60df2c5dca09a833c456afc5 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/indirector/rest'

describe Puppet::Indirector::REST do
    before do
        Puppet::Indirector::Terminus.stubs(:register_terminus_class)
        @model = stub('model')
        @instance = stub('model instance')
        @indirection = stub('indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model)
        Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)

        @rest_class = Class.new(Puppet::Indirector::REST) do
            def self.to_s
                "This::Is::A::Test::Class"
            end
        end

        @searcher = @rest_class.new
    end
    
    describe "when doing a network fetch" do
      it "should escape the provided path"
      it "should look up the appropriate remote server"
      it "should look up the appropriate remote port"
      it "should use the GET http method"
      it "should use the appropriate remote server"
      it "should use the appropriate remote port"
      it "should use the escaped provided path"
      it "should return the results of the GET request"
    end

    describe "when doing a network delete" do
      it "should escape the provided path"
      it "should look up the appropriate remote server"
      it "should look up the appropriate remote port"
      it "should use the delete http method"
      it "should use the appropriate remote server"
      it "should use the appropriate remote port"
      it "should use the escaped provided path"
      it "should return the results of the DELETE request"
    end

    describe "when doing a find" do
      before :each do
        @result = { :foo => 'bar'}.to_yaml
        @searcher.stubs(:network_fetch).returns(@result)  # neuter the network connection
        @model.stubs(:from_yaml).returns(@instance)
      end
      
      it "should look up the model instance over the network" do
        @searcher.expects(:network_fetch).returns(@result)
        @searcher.find('foo')
      end
      
      it "should look up the model instance using the named indirection" do
        @searcher.expects(:network_fetch).with {|path| path =~ %r{^#{@indirection.name.to_s}/} }.returns(@result)
        @searcher.find('foo')
      end
      
      it "should look up the model instance using the provided key" do
        @searcher.expects(:network_fetch).with {|path| path =~ %r{/foo$} }.returns(@result)
        @searcher.find('foo')
      end
      
      it "should deserialize result data to a Model instance" do
        @model.expects(:from_yaml)
        @searcher.find('foo')
      end
      
      it "should return the deserialized Model instance" do
        @searcher.find('foo').should == @instance     
      end
      
      it "should return nil when deserialized model instance is nil" do
        @model.stubs(:from_yaml).returns(nil)
        @searcher.find('foo').should be_nil
      end
      
      it "should generate an error when result data deserializes improperly" do
        @model.stubs(:from_yaml).raises(ArgumentError)
        lambda { @searcher.find('foo') }.should raise_error(ArgumentError)
      end
      
      it "should generate an error when result data specifies an error" do
        @searcher.stubs(:network_fetch).returns(RuntimeError.new("bogus").to_yaml)
        lambda { @searcher.find('foo') }.should raise_error(RuntimeError)        
      end      
    end

    describe "when doing a search" do
      before :each do
        @result = [1, 2].to_yaml
        @searcher.stubs(:network_fetch).returns(@result)
        @model.stubs(:from_yaml).returns(@instance)
      end
      
      it "should look up the model data over the network" do
        @searcher.expects(:network_fetch).returns(@result)
        @searcher.search('foo')
      end
      
      it "should look up the model instance using the named indirection" do
        @searcher.expects(:network_fetch).with {|path| path =~ %r{^#{@indirection.name.to_s}s/} }.returns(@result)
        @searcher.search('foo')
      end
      
      it "should look up the model instance using the provided key" do
        @searcher.expects(:network_fetch).with {|path| path =~ %r{/foo$} }.returns(@result)
        @searcher.search('foo')
      end
      
      it "should deserialize result data into a list of Model instances" do
        @model.expects(:from_yaml).at_least(2)
        @searcher.search('foo')
      end
      
      it "should generate an error when result data deserializes improperly" do
        @model.stubs(:from_yaml).raises(ArgumentError)
        lambda { @searcher.search('foo') }.should raise_error(ArgumentError)        
      end
      
      it "should generate an error when result data specifies an error" do
        @searcher.stubs(:network_fetch).returns(RuntimeError.new("bogus").to_yaml)
        lambda { @searcher.search('foo') }.should raise_error(RuntimeError)        
      end     
    end    
    
    describe "when doing a destroy" do
      before :each do
        @result = true.to_yaml
        @searcher.stubs(:network_delete).returns(@result)  # neuter the network connection
        @model.stubs(:from_yaml).returns(@instance)
      end
      
      it "should look up the model instance over the network" do
        @searcher.expects(:network_delete).returns(@result)
        @searcher.destroy('foo')
      end
      
      it "should look up the model instance using the named indirection" do
        @searcher.expects(:network_delete).with {|path| path =~ %r{^#{@indirection.name.to_s}/} }.returns(@result)
        @searcher.destroy('foo')
      end
      
      it "should look up the model instance using the provided key" do
        @searcher.expects(:network_delete).with {|path| path =~ %r{/foo$} }.returns(@result)
        @searcher.destroy('foo')
      end
      
      it "should deserialize result data" do
        YAML.expects(:load).with(@result)
        @searcher.destroy('foo')
      end
      
      it "should return deserialized result data" do
        @searcher.destroy('foo').should == true
      end
      
      it "should generate an error when result data specifies an error" do
        @searcher.stubs(:network_delete).returns(RuntimeError.new("bogus").to_yaml)
        lambda { @searcher.destroy('foo') }.should raise_error(RuntimeError)        
      end      
    end

    describe "when doing a save" do
      it "should deserialize result data into a boolean"
      it "should generate an error when result data deserializes improperly"
      it "should generate an error when result data specifies an error"      
    end
end