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

« back to all changes in this revision

Viewing changes to test/util/autoload.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/util/autoload'
7
 
require 'puppettest'
8
 
 
9
 
class TestAutoload < Test::Unit::TestCase
10
 
        include PuppetTest
11
 
    @things = []
12
 
    def self.newthing(name)
13
 
        @things << name
14
 
    end
15
 
 
16
 
    def self.thing?(name)
17
 
        @things.include? name
18
 
    end
19
 
 
20
 
    def self.clear
21
 
        @things.clear
22
 
    end
23
 
 
24
 
    def mkfile(name, path)
25
 
        # Now create a file to load
26
 
        File.open(path, "w") do |f|
27
 
            f.puts %{
28
 
TestAutoload.newthing(:#{name.to_s})
29
 
            }
30
 
        end
31
 
    end
32
 
 
33
 
    def mk_loader(name)
34
 
        dir = tempfile()
35
 
        $: << dir
36
 
        cleanup do
37
 
            $:.delete(dir)
38
 
        end
39
 
 
40
 
        Dir.mkdir(dir)
41
 
 
42
 
        rbdir = File.join(dir, name.to_s)
43
 
 
44
 
        Dir.mkdir(rbdir)
45
 
 
46
 
        loader = nil
47
 
        assert_nothing_raised {
48
 
            loader = Puppet::Util::Autoload.new(self.class, name)
49
 
        }
50
 
        return rbdir, loader
51
 
    end
52
 
 
53
 
    def test_load
54
 
        dir, loader = mk_loader(:yayness)
55
 
 
56
 
        assert_equal(loader.object_id, Puppet::Util::Autoload[self.class].object_id,
57
 
                    "Did not retrieve loader object by class")
58
 
 
59
 
        # Make sure we don't fail on missing files
60
 
        assert_nothing_raised {
61
 
            assert_equal(false, loader.load(:mything),
62
 
                        "got incorrect return on failed load")
63
 
        }
64
 
 
65
 
        # Now create a couple of files for testing
66
 
        path = File.join(dir, "mything.rb")
67
 
        mkfile(:mything, path)
68
 
        opath = File.join(dir, "othing.rb")
69
 
        mkfile(:othing, opath)
70
 
 
71
 
        # Now try to actually load it.
72
 
        assert_nothing_raised {
73
 
            assert_equal(true, loader.load(:mything),
74
 
                        "got incorrect return on load")
75
 
        }
76
 
 
77
 
        assert(loader.loaded?(:mything), "Not considered loaded")
78
 
 
79
 
        assert(self.class.thing?(:mything),
80
 
                "Did not get loaded thing")
81
 
 
82
 
        self.class.clear
83
 
 
84
 
        [:mything, :othing].each do |thing|
85
 
            loader.load(thing)
86
 
            assert(loader.loaded?(thing), "#{thing.to_s} not considered loaded")
87
 
            assert(loader.loaded?("%s.rb" % thing), "#{thing.to_s} not considered loaded with .rb")
88
 
            assert(Puppet::Util::Autoload.loaded?("yayness/%s" % thing), "%s not considered loaded by the main class" % thing)
89
 
            assert(Puppet::Util::Autoload.loaded?("yayness/%s.rb" % thing), "%s not considered loaded by the main class with .rb" % thing)
90
 
 
91
 
            assert(self.class.thing?(thing),
92
 
                    "Did not get loaded #{thing.to_s}")
93
 
        end
94
 
    end
95
 
 
96
 
    # Make sure that autoload dynamically modifies $: with the libdir as
97
 
    # appropriate.
98
 
    def test_searchpath
99
 
        dir = Puppet[:libdir]
100
 
 
101
 
        loader = Puppet::Util::Autoload.new(self, "testing")
102
 
 
103
 
        assert(loader.send(:searchpath).include?(dir), "searchpath does not include the libdir")
104
 
    end
105
 
 
106
 
    # This tests #1027, which was caused by using the unqualified
107
 
    # path for requires, which was initially done so that the kernel
108
 
    # would keep track of which files got loaded.
109
 
    def test_require_uses_full_path
110
 
        loadname = "testing"
111
 
        loader = Puppet::Util::Autoload.new(self.class, loadname)
112
 
 
113
 
        basedir = "/some/dir"
114
 
        dir = File.join(basedir, loadname)
115
 
        loader.expects(:eachdir).yields(dir)
116
 
 
117
 
        subname = "instance"
118
 
 
119
 
        file = File.join(dir, subname) + ".rb"
120
 
 
121
 
        Dir.expects(:glob).with("#{dir}/*.rb").returns(file)
122
 
 
123
 
        Kernel.expects(:require).with(file)
124
 
        loader.loadall
125
 
    end
126
 
 
127
 
    def test_searchpath_includes_plugin_dirs
128
 
        moddir = "/what/ever"
129
 
        libdir = "/other/dir"
130
 
        Puppet.settings.stubs(:value).with(:modulepath).returns(moddir)
131
 
        Puppet.settings.stubs(:value).with(:libdir).returns(libdir)
132
 
 
133
 
        loadname = "testing"
134
 
        loader = Puppet::Util::Autoload.new(self.class, loadname)
135
 
 
136
 
        # Currently, include both plugins and libs.
137
 
        paths = %w{plugins lib}.inject({}) { |hash, d| hash[d] = File.join(moddir, "testing", d); FileTest.stubs(:directory?).with(hash[d]).returns(true); hash  }
138
 
        Dir.expects(:glob).with("#{moddir}/*/{plugins,lib}").returns(paths.values)
139
 
 
140
 
        searchpath = loader.searchpath
141
 
        paths.each do |dir, path|
142
 
            assert(searchpath.include?(path), "search path did not include path for %s" % dir)
143
 
        end
144
 
    end
145
 
end