~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to spec/unit/indirector/queue.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require File.dirname(__FILE__) + '/../../spec_helper'
 
4
require 'puppet/indirector/queue'
 
5
 
 
6
class Puppet::Indirector::Queue::TestClient
 
7
end
 
8
 
 
9
class FooExampleData
 
10
    attr_accessor :name
 
11
 
 
12
    def self.pson_create(pson)
 
13
        new(pson['data'].to_sym)
 
14
    end
 
15
 
 
16
    def initialize(name = nil)
 
17
        @name = name if name
 
18
    end
 
19
 
 
20
    def render(format = :pson)
 
21
        to_pson
 
22
    end
 
23
 
 
24
    def to_pson(*args)
 
25
        {:type => self.class.to_s, :data => name}.to_pson(*args)
 
26
    end
 
27
end
 
28
 
 
29
describe Puppet::Indirector::Queue do
 
30
    confine "PSON library is missing; cannot test queueing" => Puppet.features.pson?
 
31
 
 
32
    before :each do
 
33
        @model = mock 'model'
 
34
        @indirection = stub 'indirection', :name => :my_queue, :register_terminus_type => nil, :model => @model
 
35
        Puppet::Indirector::Indirection.stubs(:instance).with(:my_queue).returns(@indirection)
 
36
        @store_class = Class.new(Puppet::Indirector::Queue) do
 
37
            def self.to_s
 
38
                'MyQueue::MyType'
 
39
            end
 
40
        end
 
41
        @store = @store_class.new
 
42
 
 
43
        @subject_class = FooExampleData
 
44
        @subject = @subject_class.new
 
45
        @subject.name = :me
 
46
 
 
47
        Puppet.settings.stubs(:value).returns("bogus setting data")
 
48
        Puppet.settings.stubs(:value).with(:queue_type).returns(:test_client)
 
49
        Puppet::Util::Queue.stubs(:queue_type_to_class).with(:test_client).returns(Puppet::Indirector::Queue::TestClient)
 
50
 
 
51
        @request = stub 'request', :key => :me, :instance => @subject
 
52
    end
 
53
 
 
54
    it "should require PSON" do
 
55
        Puppet.features.expects(:pson?).returns false
 
56
 
 
57
        lambda { @store_class.new }.should raise_error(ArgumentError)
 
58
    end
 
59
 
 
60
    it 'should use the correct client type and queue' do
 
61
        @store.queue.should == :my_queue
 
62
        @store.client.should be_an_instance_of(Puppet::Indirector::Queue::TestClient)
 
63
    end
 
64
 
 
65
    describe "when saving" do
 
66
        it 'should render the instance using pson' do
 
67
            @subject.expects(:render).with(:pson)
 
68
            @store.client.stubs(:send_message)
 
69
            @store.save(@request)
 
70
        end
 
71
 
 
72
        it "should send the rendered message to the appropriate queue on the client" do
 
73
            @subject.expects(:render).returns "mypson"
 
74
 
 
75
            @store.client.expects(:send_message).with(:my_queue, "mypson")
 
76
 
 
77
            @store.save(@request)
 
78
        end
 
79
 
 
80
        it "should catch any exceptions raised" do
 
81
            @store.client.expects(:send_message).raises ArgumentError
 
82
 
 
83
            lambda { @store.save(@request) }.should raise_error(Puppet::Error)
 
84
        end
 
85
    end
 
86
 
 
87
    describe "when subscribing to the queue" do
 
88
        before do
 
89
            @store_class.stubs(:model).returns @model
 
90
        end
 
91
 
 
92
        it "should use the model's Format support to intern the message from pson" do
 
93
            @model.expects(:convert_from).with(:pson, "mymessage")
 
94
 
 
95
            @store_class.client.expects(:subscribe).yields("mymessage")
 
96
            @store_class.subscribe {|o| o }
 
97
        end
 
98
 
 
99
        it "should yield each interned received message" do
 
100
            @model.stubs(:convert_from).returns "something"
 
101
 
 
102
            @subject_two = @subject_class.new
 
103
            @subject_two.name = :too
 
104
 
 
105
            @store_class.client.expects(:subscribe).with(:my_queue).multiple_yields(@subject, @subject_two)
 
106
 
 
107
            received = []
 
108
            @store_class.subscribe do |obj|
 
109
                received.push(obj)
 
110
            end
 
111
 
 
112
            received.should == %w{something something}
 
113
        end
 
114
 
 
115
        it "should log but not propagate errors" do
 
116
            @store_class.client.expects(:subscribe).yields("foo")
 
117
            @store_class.expects(:intern).raises ArgumentError
 
118
            Puppet.expects(:err)
 
119
            @store_class.subscribe {|o| o }
 
120
        end
 
121
    end
 
122
end
 
123