summaryrefslogtreecommitdiffstats
path: root/test/rails/host.rb
blob: 582bebcb2fba2fb83a265a1c63af9b7ea7c8d39f (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
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../lib/puppettest'

require 'puppet'
require 'puppet/rails'
require 'puppet/parser/interpreter'
require 'puppet/parser/parser'
require 'puppet/network/client'
require 'puppettest'
require 'puppettest/parsertesting'
require 'puppettest/resourcetesting'
require 'puppettest/railstesting'

class TestRailsHost < PuppetTest::TestCase
    confine "Missing ActiveRecord" => Puppet.features.rails?
    include PuppetTest::ParserTesting
    include PuppetTest::ResourceTesting
    include PuppetTest::RailsTesting

    def setup
        super
        railsinit if Puppet.features.rails?
    end

    def teardown
        railsteardown if Puppet.features.rails?
        super
    end

    def test_includerails
        assert_nothing_raised {
            require 'puppet/rails'
        }
    end

    def test_store
        @scope = mkscope
        # First make some objects
        resources = []
        4.times { |i|
            # Make a file
            resources << mkresource(:type => "file",
                :title => "/tmp/file#{i.to_s}",
                :params => {:owner => "user#{i}"})

            # And an exec, so we're checking multiple types
            resources << mkresource(:type => "exec",
                :title => "/bin/echo file#{i.to_s}",
                :params => {:user => "user#{i}"})
        }

        # Now collect our facts
        facts = {"hostname" => "myhost", "test1" => "funtest", "ipaddress" => "192.168.0.1"}

        # Now try storing our crap
        host = nil
        node = mknode(facts["hostname"])
        node.parameters = facts
        assert_nothing_raised {
            host = Puppet::Rails::Host.store(node, resources)
        }

        assert(host, "Did not create host")

        host = nil
        assert_nothing_raised {
            host = Puppet::Rails::Host.find_by_name(facts["hostname"])
        }
        assert(host, "Could not find host object")

        assert(host.resources, "No objects on host")

        facts.each do |fact, value|
            assert_equal(value, host.fact(fact)[0].value, "fact %s is wrong" % fact)
        end
        assert_equal(facts["ipaddress"], host.ip, "IP did not get set")

        count = 0
        host.resources.each do |resource|
            assert_equal(host, resource.host)
            count += 1
            i = nil
            if resource[:title] =~ /file([0-9]+)/
                i = $1
            else
                raise "Got weird resource %s" % resource.inspect
            end
            assert(resource[:restype] != "", "Did not get a type from the resource")
            case resource["restype"]
            when "File":
                assert_equal("user#{i}", resource.parameter("owner"),
                    "got no owner for %s" % resource.ref)
            when "Exec":
                assert_equal("user#{i}", resource.parameter("user"),
                    "got no user for %s" % resource.ref)
            else
                raise "Unknown type %s" % resource[:restype].inspect
            end
        end

        assert_equal(8, count, "Did not get enough resources")

        # Now remove a couple of resources
        resources.reject! { |r| r.title =~ /file3/ }

        # Change a few resources
        resources.find_all { |r| r.title =~ /file2/ }.each do |r|
            r.send(:set_parameter, "loglevel", "notice")
        end

        # And add a new resource
        resources << mkresource(:type => "file",
            :title => "/tmp/file_added",
            :params => {:owner => "user_added"})

        # And change some facts
        facts["test2"] = "yaytest"
        facts["test3"] = "funtest"
        facts["test1"] = "changedfact"
        facts.delete("ipaddress")
        node = mknode(facts["hostname"])
        node.parameters = facts
        newhost = nil
        assert_nothing_raised {
            newhost = Puppet::Rails::Host.store(node, resources)
        }

        assert_equal(host.id, newhost.id, "Created new host instance)")

        # Make sure it sets the last_compile time
        assert_nothing_raised do
            assert_instance_of(Time, host.last_compile, "did not set last_compile")
        end

        assert_equal(0, host.fact('ipaddress').size, "removed fact was not deleted")
        facts.each do |fact, value|
            assert_equal(value, host.fact(fact)[0].value, "fact %s is wrong" % fact)
        end

        # And check the changes we made.
        assert(! host.resources.find(:all).detect { |r| r.title =~ /file3/ },
            "Removed resources are still present")

        res = host.resources.find_by_title("/tmp/file_added")
        assert(res, "New resource was not added")
        assert_equal("user_added", res.parameter("owner"), "user info was not stored")

        host.resources.find(:all, :conditions => [ "title like ?", "%file2%"]).each do |r|
            assert_equal("notice", r.parameter("loglevel"),
                "loglevel was not added")
        end
    end

    def test_freshness_connect_update
        Puppet::Rails.init
        Puppet[:storeconfigs] = true

        Puppet[:code] = " "
        # this is the default server setup
        master = Puppet::Network::Handler.configuration.new(
            :Local => true
        )

        # Create a host
        Puppet::Rails::Host.new(:name => "test", :ip => "192.168.0.3").save

        assert_nothing_raised("Failed to update last_connect for unknown host") do
            master.version("created",'192.168.0.1')
        end
        
        # Make sure it created the host
        created = Puppet::Rails::Host.find_by_name("created")
        assert(created, "Freshness did not create host")
        assert(created.last_freshcheck,
            "Did not set last_freshcheck on created host")

        # Now check on the existing host
        assert_nothing_raised("Failed to update last_connect for unknown host") do
            master.version("test",'192.168.0.2')
        end

        # Recreate it, so we're not using the cached object.
        host = Puppet::Rails::Host.find_by_name("test")
        
        # Make sure it created the host
        assert(host.last_freshcheck,
            "Did not set last_freshcheck on existing host")
    end
end