3
require File.dirname(__FILE__) + '/../lib/puppettest'
7
require 'puppet/parser/interpreter'
8
require 'puppet/parser/parser'
9
require 'puppet/network/client'
11
require 'puppettest/parsertesting'
12
require 'puppettest/resourcetesting'
13
require 'puppettest/railstesting'
15
class TestRailsHost < PuppetTest::TestCase
16
confine "Missing ActiveRecord" => Puppet.features.rails?
17
include PuppetTest::ParserTesting
18
include PuppetTest::ResourceTesting
19
include PuppetTest::RailsTesting
23
railsinit if Puppet.features.rails?
27
railsteardown if Puppet.features.rails?
32
assert_nothing_raised {
33
require 'puppet/rails'
39
# First make some objects
43
resources << mkresource(:type => "file",
44
:title => "/tmp/file#{i.to_s}",
45
:params => {:owner => "user#{i}"})
47
# And an exec, so we're checking multiple types
48
resources << mkresource(:type => "exec",
49
:title => "/bin/echo file#{i.to_s}",
50
:params => {:user => "user#{i}"})
53
# Now collect our facts
54
facts = {"hostname" => "myhost", "test1" => "funtest", "ipaddress" => "192.168.0.1"}
56
# Now try storing our crap
58
node = mknode(facts["hostname"])
59
node.parameters = facts
60
assert_nothing_raised {
61
host = Puppet::Rails::Host.store(node, resources)
64
assert(host, "Did not create host")
67
assert_nothing_raised {
68
host = Puppet::Rails::Host.find_by_name(facts["hostname"])
70
assert(host, "Could not find host object")
72
assert(host.resources, "No objects on host")
74
facts.each do |fact, value|
75
assert_equal(value, host.fact(fact)[0].value, "fact %s is wrong" % fact)
77
assert_equal(facts["ipaddress"], host.ip, "IP did not get set")
80
host.resources.each do |resource|
81
assert_equal(host, resource.host)
84
if resource[:title] =~ /file([0-9]+)/
87
raise "Got weird resource %s" % resource.inspect
89
assert(resource[:restype] != "", "Did not get a type from the resource")
90
case resource["restype"]
92
assert_equal("user#{i}", resource.parameter("owner"),
93
"got no owner for %s" % resource.ref)
95
assert_equal("user#{i}", resource.parameter("user"),
96
"got no user for %s" % resource.ref)
98
raise "Unknown type %s" % resource[:restype].inspect
102
assert_equal(8, count, "Did not get enough resources")
104
# Now remove a couple of resources
105
resources.reject! { |r| r.title =~ /file3/ }
107
# Change a few resources
108
resources.find_all { |r| r.title =~ /file2/ }.each do |r|
109
r.send(:set_parameter, "loglevel", "notice")
112
# And add a new resource
113
resources << mkresource(:type => "file",
114
:title => "/tmp/file_added",
115
:params => {:owner => "user_added"})
117
# And change some facts
118
facts["test2"] = "yaytest"
119
facts["test3"] = "funtest"
120
facts["test1"] = "changedfact"
121
facts.delete("ipaddress")
122
node = mknode(facts["hostname"])
123
node.parameters = facts
125
assert_nothing_raised {
126
newhost = Puppet::Rails::Host.store(node, resources)
129
assert_equal(host.id, newhost.id, "Created new host instance)")
131
# Make sure it sets the last_compile time
132
assert_nothing_raised do
133
assert_instance_of(Time, host.last_compile, "did not set last_compile")
136
assert_equal(0, host.fact('ipaddress').size, "removed fact was not deleted")
137
facts.each do |fact, value|
138
assert_equal(value, host.fact(fact)[0].value, "fact %s is wrong" % fact)
141
# And check the changes we made.
142
assert(! host.resources.find(:all).detect { |r| r.title =~ /file3/ },
143
"Removed resources are still present")
145
res = host.resources.find_by_title("/tmp/file_added")
146
assert(res, "New resource was not added")
147
assert_equal("user_added", res.parameter("owner"), "user info was not stored")
149
host.resources.find(:all, :conditions => [ "title like ?", "%file2%"]).each do |r|
150
assert_equal("notice", r.parameter("loglevel"),
151
"loglevel was not added")