~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to spec/unit/other/transbucket.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

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
 
 
5
describe Puppet::TransBucket do
 
6
    before do
 
7
        @bucket = Puppet::TransBucket.new
 
8
    end
 
9
 
 
10
    it "should be able to produce a RAL component" do
 
11
        @bucket.name = "luke"
 
12
        @bucket.type = "user"
 
13
 
 
14
        resource = nil
 
15
        proc { resource = @bucket.to_type }.should_not raise_error
 
16
        resource.should be_instance_of(Puppet::Type::Component)
 
17
        resource.title.should == "User[luke]"
 
18
    end
 
19
 
 
20
    it "should accept TransObjects into its children list" do
 
21
        object = Puppet::TransObject.new("luke", "user")
 
22
        proc { @bucket.push(object) }.should_not raise_error
 
23
        @bucket.each do |o|
 
24
            o.should equal(object)
 
25
        end
 
26
    end
 
27
 
 
28
    it "should accept TransBuckets into its children list" do
 
29
        object = Puppet::TransBucket.new()
 
30
        proc { @bucket.push(object) }.should_not raise_error
 
31
        @bucket.each do |o|
 
32
            o.should equal(object)
 
33
        end
 
34
    end
 
35
 
 
36
    it "should refuse to accept any children that are not TransObjects or TransBuckets" do
 
37
        proc { @bucket.push "a test" }.should raise_error
 
38
    end
 
39
 
 
40
    it "should return use 'node' as the type and the provided name as the title if only a type is provided" do
 
41
        @bucket.type = "mystuff"
 
42
        @bucket.to_ref.should == "Node[mystuff]"
 
43
    end
 
44
 
 
45
    it "should return use 'component' as the type and the provided type as the title if only a name is provided" do
 
46
        @bucket.name = "mystuff"
 
47
        @bucket.to_ref.should == "Class[mystuff]"
 
48
    end
 
49
 
 
50
    it "should return nil as its reference when type and name are missing" do
 
51
        @bucket.to_ref.should be_nil
 
52
    end
 
53
 
 
54
    it "should return the title as its reference" do
 
55
        @bucket.name = "luke"
 
56
        @bucket.type = "user"
 
57
        @bucket.to_ref.should == "User[luke]"
 
58
    end
 
59
 
 
60
    it "should canonize resource references when the type is 'component'" do
 
61
        @bucket.name = 'something'
 
62
        @bucket.type = 'foo::bar'
 
63
 
 
64
        @bucket.to_ref.should == "Foo::Bar[something]"
 
65
    end
 
66
end
 
67
 
 
68
describe Puppet::TransBucket, " when generating a catalog" do
 
69
    before do
 
70
        @bottom = Puppet::TransBucket.new
 
71
        @bottom.type = "fake"
 
72
        @bottom.name = "bottom"
 
73
        @bottomobj = Puppet::TransObject.new("bottom", "user")
 
74
        @bottom.push @bottomobj
 
75
 
 
76
        @middle = Puppet::TransBucket.new
 
77
        @middle.type = "fake"
 
78
        @middle.name = "middle"
 
79
        @middleobj = Puppet::TransObject.new("middle", "user")
 
80
        @middle.push(@middleobj)
 
81
        @middle.push(@bottom)
 
82
 
 
83
        @top = Puppet::TransBucket.new
 
84
        @top.type = "fake"
 
85
        @top.name = "top"
 
86
        @topobj = Puppet::TransObject.new("top", "user")
 
87
        @top.push(@topobj)
 
88
        @top.push(@middle)
 
89
 
 
90
        @users = %w{top middle bottom}
 
91
        @fakes = %w{Fake[bottom] Fake[middle] Fake[top]}
 
92
    end
 
93
 
 
94
    after do
 
95
        Puppet::Type.allclear
 
96
    end
 
97
 
 
98
    it "should convert all transportable objects to RAL resources" do
 
99
        @catalog = @top.to_catalog
 
100
        @users.each do |name|
 
101
            @catalog.vertices.find { |r| r.class.name == :user and r.title == name }.should be_instance_of(Puppet::Type.type(:user))
 
102
        end
 
103
    end
 
104
 
 
105
    it "should fail if any transportable resources fail to convert to RAL resources" do
 
106
        @bottomobj.expects(:to_type).raises ArgumentError
 
107
        lambda { @bottom.to_catalog }.should raise_error(ArgumentError)
 
108
    end
 
109
 
 
110
    it "should convert all transportable buckets to RAL components" do
 
111
        @catalog = @top.to_catalog
 
112
        @fakes.each do |name|
 
113
            @catalog.vertices.find { |r| r.class.name == :component and r.title == name }.should be_instance_of(Puppet::Type.type(:component))
 
114
        end
 
115
    end
 
116
 
 
117
    it "should add all resources to the graph's resource table" do
 
118
        @catalog = @top.to_catalog
 
119
        @catalog.resource("fake[top]").should equal(@top)
 
120
    end
 
121
 
 
122
    it "should finalize all resources" do
 
123
        @catalog = @top.to_catalog
 
124
        @catalog.vertices.each do |vertex| vertex.should be_finalized end
 
125
    end
 
126
 
 
127
    it "should only call to_type on each resource once" do
 
128
        # We just raise exceptions here because we're not interested in
 
129
        # what happens with the result, only that the method only
 
130
        # gets called once.
 
131
        resource = @topobj.to_type
 
132
        @topobj.expects(:to_type).once.returns resource
 
133
        @top.to_catalog
 
134
    end
 
135
 
 
136
    it "should set each TransObject's catalog before converting to a RAL resource" do
 
137
        @middleobj.expects(:catalog=).with { |c| c.is_a?(Puppet::Node::Catalog) }
 
138
        @top.to_catalog
 
139
    end
 
140
 
 
141
    it "should set each TransBucket's catalog before converting to a RAL resource" do
 
142
        # each bucket is seen twice in the loop, so we have to handle the case where the config
 
143
        # is set twice
 
144
        @bottom.expects(:catalog=).with { |c| c.is_a?(Puppet::Node::Catalog) }.at_least_once
 
145
        @top.to_catalog
 
146
    end
 
147
end
 
148
 
 
149
describe Puppet::TransBucket, " when serializing" do
 
150
    before do
 
151
        @bucket = Puppet::TransBucket.new(%w{one two})
 
152
        @bucket.name = "one"
 
153
        @bucket.type = "two"
 
154
    end
 
155
 
 
156
    it "should be able to be dumped to yaml" do
 
157
        proc { YAML.dump(@bucket) }.should_not raise_error
 
158
    end
 
159
 
 
160
    it "should dump YAML that produces an equivalent object" do
 
161
        result = YAML.dump(@bucket)
 
162
 
 
163
        newobj = YAML.load(result)
 
164
        newobj.name.should == "one"
 
165
        newobj.type.should == "two"
 
166
        children = []
 
167
        newobj.each do |o|
 
168
            children << o
 
169
        end
 
170
        children.should == %w{one two}
 
171
    end
 
172
end