summaryrefslogtreecommitdiffstats
path: root/spec/unit/other/transbucket.rb
blob: 3904e39fe502c4f8d75c74d161d55c7179e37742 (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

describe Puppet::TransBucket do
    before do
        @bucket = Puppet::TransBucket.new
    end

    it "should be able to produce a RAL component" do
        @bucket.name = "luke"
        @bucket.type = "user"

        resource = nil
        proc { resource = @bucket.to_type }.should_not raise_error
        resource.should be_instance_of(Puppet::Type::Component)
        resource.title.should == "User[luke]"
    end

    it "should accept TransObjects into its children list" do
        object = Puppet::TransObject.new("luke", "user")
        proc { @bucket.push(object) }.should_not raise_error
        @bucket.each do |o|
            o.should equal(object)
        end
    end

    it "should accept TransBuckets into its children list" do
        object = Puppet::TransBucket.new()
        proc { @bucket.push(object) }.should_not raise_error
        @bucket.each do |o|
            o.should equal(object)
        end
    end

    it "should refuse to accept any children that are not TransObjects or TransBuckets" do
        proc { @bucket.push "a test" }.should raise_error
    end

    it "should return use 'node' as the type and the provided name as the title if only a type is provided" do
        @bucket.type = "mystuff"
        @bucket.to_ref.should == "Node[mystuff]"
    end

    it "should return use 'component' as the type and the provided type as the title if only a name is provided" do
        @bucket.name = "mystuff"
        @bucket.to_ref.should == "Class[mystuff]"
    end

    it "should return nil as its reference when type and name are missing" do
        @bucket.to_ref.should be_nil
    end

    it "should return the title as its reference" do
        @bucket.name = "luke"
        @bucket.type = "user"
        @bucket.to_ref.should == "User[luke]"
    end

    it "should canonize resource references when the type is 'component'" do
        @bucket.name = 'something'
        @bucket.type = 'foo::bar'

        @bucket.to_ref.should == "Foo::Bar[something]"
    end
end

describe Puppet::TransBucket, " when generating a catalog" do
    before do
        @bottom = Puppet::TransBucket.new
        @bottom.type = "fake"
        @bottom.name = "bottom"
        @bottomobj = Puppet::TransObject.new("bottom", "user")
        @bottom.push @bottomobj

        @middle = Puppet::TransBucket.new
        @middle.type = "fake"
        @middle.name = "middle"
        @middleobj = Puppet::TransObject.new("middle", "user")
        @middle.push(@middleobj)
        @middle.push(@bottom)

        @top = Puppet::TransBucket.new
        @top.type = "fake"
        @top.name = "top"
        @topobj = Puppet::TransObject.new("top", "user")
        @top.push(@topobj)
        @top.push(@middle)

        @config = @top.to_catalog

        @users = %w{top middle bottom}
        @fakes = %w{Fake[bottom] Fake[middle] Fake[top]}
    end

    it "should convert all transportable objects to RAL resources" do
        @users.each do |name|
            @config.vertices.find { |r| r.class.name == :user and r.title == name }.should be_instance_of(Puppet::Type.type(:user))
        end
    end

    it "should convert all transportable buckets to RAL components" do
        @fakes.each do |name|
            @config.vertices.find { |r| r.class.name == :component and r.title == name }.should be_instance_of(Puppet::Type.type(:component))
        end
    end

    it "should add all resources to the graph's resource table" do
        @config.resource("fake[top]").should equal(@top)
    end

    it "should finalize all resources" do
        @config.vertices.each do |vertex| vertex.should be_finalized end
    end

    it "should only call to_type on each resource once" do
        @topobj.expects(:to_type)
        @bottomobj.expects(:to_type)
        Puppet::Type.allclear
        @top.to_catalog
    end

    it "should set each TransObject's catalog before converting to a RAL resource" do
        @middleobj.expects(:catalog=).with { |c| c.is_a?(Puppet::Node::Catalog) }
        Puppet::Type.allclear
        @top.to_catalog
    end

    it "should set each TransBucket's catalog before converting to a RAL resource" do
        # each bucket is seen twice in the loop, so we have to handle the case where the config
        # is set twice
        @bottom.expects(:catalog=).with { |c| c.is_a?(Puppet::Node::Catalog) }.at_least_once
        Puppet::Type.allclear
        @top.to_catalog
    end

    after do
        Puppet::Type.allclear
    end
end

describe Puppet::TransBucket, " when serializing" do
    before do
        @bucket = Puppet::TransBucket.new(%w{one two})
        @bucket.name = "one"
        @bucket.type = "two"
    end

    it "should be able to be dumped to yaml" do
        proc { YAML.dump(@bucket) }.should_not raise_error
    end

    it "should dump YAML that produces an equivalent object" do
        result = YAML.dump(@bucket)

        newobj = YAML.load(result)
        newobj.name.should == "one"
        newobj.type.should == "two"
        children = []
        newobj.each do |o|
            children << o
        end
        children.should == %w{one two}
    end
end