summaryrefslogtreecommitdiff
path: root/test/tagging/tagging.rb
blob: 434ef6e69a58a7f7fa361b72e524a8f94c4bd4af (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
173
174
175
176
177
178
179
if __FILE__ == $0
    $:.unshift '..'
    $:.unshift '../../lib'
    $puppetbase = "../.."
end

require 'puppet'
require 'puppettest'
require 'test/unit'

class TestTagging < Test::Unit::TestCase
    include TestPuppet

    # Make sure the scopes are getting the right tags
    def test_scopetags
        scope = nil
        assert_nothing_raised {
            scope = Puppet::Parser::Scope.new()
            scope.name = "yayness"
            scope.type = "solaris"
        }

        assert_nothing_raised {
            assert_equal(%w{solaris}, scope.tags, "Incorrect scope tags")
        }
    end

    # Test deeper tags, where a scope gets all of its parent scopes' tags
    def test_deepscopetags
        scope = nil
        assert_nothing_raised {
            scope = Puppet::Parser::Scope.new()
            scope.name = "yayness"
            scope.type = "solaris"
            scope = scope.newscope
            scope.name = "booness"
            scope.type = "apache"
        }

        assert_nothing_raised {
            # Scopes put their own tags first
            assert_equal(%w{apache solaris}, scope.tags, "Incorrect scope tags")
        }
    end

    # Verify that the tags make their way to the objects
    def test_objecttags
        scope = nil
        assert_nothing_raised {
            scope = Puppet::Parser::Scope.new()
            scope.name = "yayness"
            scope.type = "solaris"
        }

        assert_nothing_raised {
            scope.setobject(
                :type => "file",
                :name => "/etc/passwd",
                :arguments => {"owner" => "root"},
                :file => "/yay",
                :line => 1
            )
        }

        objects = nil
        assert_nothing_raised {
            objects = scope.to_trans
        }

        # There's only one object, so shift it out
        object = objects.shift

        assert_nothing_raised {
            assert_equal(%w{solaris}, object.tags,
                "Incorrect tags")
        }
    end

    # Make sure that specifying tags results in only those objects getting
    # run.
    def test_tagspecs
        a = tempfile()
        b = tempfile()

        afile = Puppet.type(:file).create(
            :path => a,
            :ensure => :file
        )
        afile.tag("a")

        bfile = Puppet.type(:file).create(
            :path => b,
            :ensure => :file
        )
        bfile.tag(:b)

        # First, make sure they get created when no spec'ed tags
        assert_events([:file_created,:file_created], afile, bfile)
        assert(FileTest.exists?(a), "A did not get created")
        assert(FileTest.exists?(b), "B did not get created")
        File.unlink(a)
        File.unlink(b)

        # Set the tags to a
        assert_nothing_raised {
            Puppet[:tags] = "a"
        }

        assert_events([:file_created], afile, bfile)
        assert(FileTest.exists?(a), "A did not get created")
        assert(!FileTest.exists?(b), "B got created")
        File.unlink(a)

        # Set the tags to b
        assert_nothing_raised {
            Puppet[:tags] = "b"
        }

        assert_events([:file_created], afile, bfile)
        assert(!FileTest.exists?(a), "A got created")
        assert(FileTest.exists?(b), "B did not get created")
        File.unlink(b)

        # Set the tags to something else
        assert_nothing_raised {
            Puppet[:tags] = "c"
        }

        assert_events([], afile, bfile)
        assert(!FileTest.exists?(a), "A got created")
        assert(!FileTest.exists?(b), "B got created")

        # Now set both tags
        assert_nothing_raised {
            Puppet[:tags] = "b, a"
        }

        assert_events([:file_created, :file_created], afile, bfile)
        assert(FileTest.exists?(a), "A did not get created")
        assert(FileTest.exists?(b), "B did not get created")
        File.unlink(a)

    end

    def test_metaparamtag
        path = tempfile()

        start = %w{some tags}
        tags = %w{a list of tags}

        obj = nil
        assert_nothing_raised do
            obj = Puppet.type(:file).create(
                :path => path,
                :ensure => "file",
                :tag => start
            )
        end


        assert(obj, "Did not make object")

        start.each do |tag|
            assert(obj.tagged?(tag), "Object was not tagged with %s" % tag)
        end

        tags.each do |tag|
            assert_nothing_raised {
                obj[:tag] = tag
            }
        end

        tags.each do |tag|
            assert(obj.tagged?(tag), "Object was not tagged with %s" % tag)
        end
    end
end

# $Id$