~nvalcarcel/ubuntu/lucid/puppet/fix-546677

« back to all changes in this revision

Viewing changes to test/rails/host.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env ruby
2
 
 
3
 
require File.dirname(__FILE__) + '/../lib/puppettest'
4
 
 
5
 
require 'puppet'
6
 
require 'puppet/rails'
7
 
require 'puppet/parser/interpreter'
8
 
require 'puppet/parser/parser'
9
 
require 'puppet/network/client'
10
 
require 'puppettest'
11
 
require 'puppettest/parsertesting'
12
 
require 'puppettest/resourcetesting'
13
 
require 'puppettest/railstesting'
14
 
 
15
 
class TestRailsHost < PuppetTest::TestCase
16
 
    confine "Missing ActiveRecord" => Puppet.features.rails?
17
 
    include PuppetTest::ParserTesting
18
 
    include PuppetTest::ResourceTesting
19
 
    include PuppetTest::RailsTesting
20
 
 
21
 
    def setup
22
 
        super
23
 
        railsinit if Puppet.features.rails?
24
 
    end
25
 
 
26
 
    def teardown
27
 
        railsteardown if Puppet.features.rails?
28
 
        super
29
 
    end
30
 
 
31
 
    def test_includerails
32
 
        assert_nothing_raised {
33
 
            require 'puppet/rails'
34
 
        }
35
 
    end
36
 
 
37
 
    def test_store
38
 
        @scope = mkscope
39
 
        # First make some objects
40
 
        resources = []
41
 
        4.times { |i|
42
 
            # Make a file
43
 
            resources << mkresource(:type => "file",
44
 
                :title => "/tmp/file#{i.to_s}",
45
 
                :params => {:owner => "user#{i}"})
46
 
 
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}"})
51
 
        }
52
 
 
53
 
        # Now collect our facts
54
 
        facts = {"hostname" => "myhost", "test1" => "funtest", "ipaddress" => "192.168.0.1"}
55
 
 
56
 
        # Now try storing our crap
57
 
        host = nil
58
 
        node = mknode(facts["hostname"])
59
 
        node.parameters = facts
60
 
        assert_nothing_raised {
61
 
            host = Puppet::Rails::Host.store(node, resources)
62
 
        }
63
 
 
64
 
        assert(host, "Did not create host")
65
 
 
66
 
        host = nil
67
 
        assert_nothing_raised {
68
 
            host = Puppet::Rails::Host.find_by_name(facts["hostname"])
69
 
        }
70
 
        assert(host, "Could not find host object")
71
 
 
72
 
        assert(host.resources, "No objects on host")
73
 
 
74
 
        facts.each do |fact, value|
75
 
            assert_equal(value, host.fact(fact)[0].value, "fact %s is wrong" % fact)
76
 
        end
77
 
        assert_equal(facts["ipaddress"], host.ip, "IP did not get set")
78
 
 
79
 
        count = 0
80
 
        host.resources.each do |resource|
81
 
            assert_equal(host, resource.host)
82
 
            count += 1
83
 
            i = nil
84
 
            if resource[:title] =~ /file([0-9]+)/
85
 
                i = $1
86
 
            else
87
 
                raise "Got weird resource %s" % resource.inspect
88
 
            end
89
 
            assert(resource[:restype] != "", "Did not get a type from the resource")
90
 
            case resource["restype"]
91
 
            when "File":
92
 
                assert_equal("user#{i}", resource.parameter("owner"),
93
 
                    "got no owner for %s" % resource.ref)
94
 
            when "Exec":
95
 
                assert_equal("user#{i}", resource.parameter("user"),
96
 
                    "got no user for %s" % resource.ref)
97
 
            else
98
 
                raise "Unknown type %s" % resource[:restype].inspect
99
 
            end
100
 
        end
101
 
 
102
 
        assert_equal(8, count, "Did not get enough resources")
103
 
 
104
 
        # Now remove a couple of resources
105
 
        resources.reject! { |r| r.title =~ /file3/ }
106
 
 
107
 
        # Change a few resources
108
 
        resources.find_all { |r| r.title =~ /file2/ }.each do |r|
109
 
            r.send(:set_parameter, "loglevel", "notice")
110
 
        end
111
 
 
112
 
        # And add a new resource
113
 
        resources << mkresource(:type => "file",
114
 
            :title => "/tmp/file_added",
115
 
            :params => {:owner => "user_added"})
116
 
 
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
124
 
        newhost = nil
125
 
        assert_nothing_raised {
126
 
            newhost = Puppet::Rails::Host.store(node, resources)
127
 
        }
128
 
 
129
 
        assert_equal(host.id, newhost.id, "Created new host instance)")
130
 
 
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")
134
 
        end
135
 
 
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)
139
 
        end
140
 
 
141
 
        # And check the changes we made.
142
 
        assert(! host.resources.find(:all).detect { |r| r.title =~ /file3/ },
143
 
            "Removed resources are still present")
144
 
 
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")
148
 
 
149
 
        host.resources.find(:all, :conditions => [ "title like ?", "%file2%"]).each do |r|
150
 
            assert_equal("notice", r.parameter("loglevel"),
151
 
                "loglevel was not added")
152
 
        end
153
 
    end
154
 
end