diff options
| author | Luke Kanies <luke@madstop.com> | 2007-09-15 22:17:20 -0600 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2007-09-15 22:17:20 -0600 |
| commit | f17f19dae941b17a56c1fc83ed3a89712b98c427 (patch) | |
| tree | 3615e9be9ed585511bbe0b85737208bbd85f00f0 /test | |
| parent | 3ccf483f77b026dde8a53bd8e9dff6a5fd0f6722 (diff) | |
| download | puppet-f17f19dae941b17a56c1fc83ed3a89712b98c427.tar.gz puppet-f17f19dae941b17a56c1fc83ed3a89712b98c427.tar.xz puppet-f17f19dae941b17a56c1fc83ed3a89712b98c427.zip | |
The whole system now uses Configuration objects instead of
ever converting the Transportable objects into a tree of components
and then converting that into a graph. This is a significant
step, and drastically simplifies the model of how to use a configuration.
The old code might have looked something like this:
file = Puppet::Type.create :path => "/whatever", ...
comp = Puppet::Type.create :name => :whatever
comp.push file
transaction = comp.evaluate
transaction.evaluate
The new code looks like this:
file = Puppet::Type.create :path => "/whatever", ...
config = Puppet::Node::Configuration.new
config.add_resource file
config.apply
I did not really intend to do this much refactoring, but I
found I could not use a Configuration object to do work
without refactoring a lot of the system. The primary problem
was that the Client::Master and the Config classes determined
how the transactions behaved; when I moved to using a Configuration,
this distinction was lost, which meant that configurations were
often needing to create other configurations, which resulted in
a whole lot of infinite recursion (e.g., Config objects that create
directories for Puppet use Configuration objects -- yes, I'm
s/Config/Settings/g soon -- and these Configuration objects would
need to create directories).
Not everything is fixed, but it's very close. I am clearly over
the hump, though, so I wanted to get a commit in.
Diffstat (limited to 'test')
| -rw-r--r-- | test/lib/puppettest/support/assertions.rb | 52 | ||||
| -rwxr-xr-x | test/lib/puppettest/support/resources.rb | 34 | ||||
| -rw-r--r-- | test/lib/puppettest/support/utils.rb | 38 | ||||
| -rwxr-xr-x | test/network/client/master.rb | 52 | ||||
| -rwxr-xr-x | test/other/transactions.rb | 216 |
5 files changed, 156 insertions, 236 deletions
diff --git a/test/lib/puppettest/support/assertions.rb b/test/lib/puppettest/support/assertions.rb index 9369f17e7..7e3e5ca2b 100644 --- a/test/lib/puppettest/support/assertions.rb +++ b/test/lib/puppettest/support/assertions.rb @@ -38,7 +38,7 @@ module PuppetTest run_events(:rollback, trans, events, msg) end - def assert_events(events, *items) + def assert_events(events, *resources) trans = nil comp = nil msg = nil @@ -46,56 +46,26 @@ module PuppetTest unless events.is_a? Array raise Puppet::DevError, "Incorrect call of assert_events" end - if items[-1].is_a? String - msg = items.pop + if resources[-1].is_a? String + msg = resources.pop end - remove_comp = false - # They either passed a comp or a list of items. - if items[0].is_a? Puppet.type(:component) - comp = items.shift - else - comp = newcomp(items[0].title, *items) - remove_comp = true - end - msg ||= comp.title - assert_nothing_raised("Component %s failed" % [msg]) { - trans = comp.evaluate - } + config = resources2config(*resources) + transaction = Puppet::Transaction.new(config) - run_events(:evaluate, trans, events, msg) + run_events(:evaluate, transaction, events, msg) - if remove_comp - Puppet.type(:component).delete(comp) - end - - return trans + return transaction end # A simpler method that just applies what we have. - def assert_apply(*objects) - if objects[0].is_a?(Puppet.type(:component)) - comp = objects.shift - unless objects.empty? - objects.each { |o| comp.push o } - end - else - comp = newcomp(*objects) - end - trans = nil - - assert_nothing_raised("Failed to create transaction") { - trans = comp.evaluate - } + def assert_apply(*resources) + config = resources2config(*resources) events = nil - assert_nothing_raised("Failed to evaluate transaction") { - events = trans.evaluate.collect { |e| e.event } + assert_nothing_raised("Failed to evaluate") { + events = config.apply.events } - trans.cleanup - Puppet.type(:component).delete(comp) events end end - -# $Id$ diff --git a/test/lib/puppettest/support/resources.rb b/test/lib/puppettest/support/resources.rb index 45d89c5fb..18d7caa77 100755 --- a/test/lib/puppettest/support/resources.rb +++ b/test/lib/puppettest/support/resources.rb @@ -4,34 +4,34 @@ # Copyright (c) 2006. All rights reserved. module PuppetTest::Support::Resources - def treefile(name) - Puppet::Type.type(:file).create :path => "/tmp/#{name}", :mode => 0755 + def tree_resource(name) + Puppet::Type.type(:file).create :title => name, :path => "/tmp/#{name}", :mode => 0755 end - def treecomp(name) + def tree_container(name) Puppet::Type::Component.create :name => name, :type => "yay" end - def treenode(name, *children) - comp = treecomp name - children.each do |c| - if c.is_a?(String) - comp.push treefile(c) - else - comp.push c + def treenode(config, name, *resources) + comp = tree_container name + resources.each do |resource| + if resource.is_a?(String) + resource = tree_resource(resource) end + config.add_edge!(comp, resource) + config.add_resource resource unless config.resource(resource.ref) end return comp end def mktree - one = treenode("one", "a", "b") - two = treenode("two", "c", "d") - middle = treenode("middle", "e", "f", two) - top = treenode("top", "g", "h", middle, one) + configuration = Puppet::Node::Configuration.new do |config| + one = treenode(config, "one", "a", "b") + two = treenode(config, "two", "c", "d") + middle = treenode(config, "middle", "e", "f", two) + top = treenode(config, "top", "g", "h", middle, one) + end - return one, two, middle, top + return configuration end end - -# $Id$
\ No newline at end of file diff --git a/test/lib/puppettest/support/utils.rb b/test/lib/puppettest/support/utils.rb index c7d54d5e6..7f4260e31 100644 --- a/test/lib/puppettest/support/utils.rb +++ b/test/lib/puppettest/support/utils.rb @@ -19,6 +19,25 @@ module PuppetTest } end + # Turn a list of resources, or possibly a configuration and some resources, + # into a configuration object. + def resources2config(*resources) + if resources[0].is_a?(Puppet::Node::Configuration) + config = resources.shift + unless resources.empty? + resources.each { |r| config.add_resource r } + end + elsif resources[0].is_a?(Puppet.type(:component)) + raise ArgumentError, "resource2config() no longer accpts components" + comp = resources.shift + comp.delve + else + config = Puppet::Node::Configuration.new + resources.each { |res| config.add_resource res } + end + return config + end + # stop any services that might be hanging around def stopservices if stype = Puppet::Type.type(:service) @@ -127,20 +146,17 @@ module PuppetTest } end - def newcomp(*ary) - name = nil - if ary[0].is_a?(String) - name = ary.shift + def mk_configuration(*resources) + if resources[0].is_a?(String) + name = resources.shift else - name = ary[0].title + name = :testing + end + config = Puppet::Node::Configuration.new :testing do |conf| + resources.each { |resource| conf.add_resource resource } end - comp = Puppet.type(:component).create(:name => name) - ary.each { |item| - comp.push item - } - - return comp + return config end def setme diff --git a/test/network/client/master.rb b/test/network/client/master.rb index a29254d16..2e9ed2752 100755 --- a/test/network/client/master.rb +++ b/test/network/client/master.rb @@ -88,51 +88,6 @@ class TestMasterClient < Test::Unit::TestCase return master, objects end - def test_apply - master, objects = mk_fake_client - - check = Proc.new do |hash| - assert(objects.trans, "transaction was not created") - trans = objects.trans - hash[:yes].each do |m| - assert_equal(1, trans.send(m.to_s + "?"), "did not call #{m} enough times") - end - hash[:no].each do |m| - assert_equal(0, trans.send(m.to_s + "?"), "called #{m} too many times") - end - end - - # First try it with no arguments - assert_nothing_raised do - master.apply - end - check.call :yes => %w{evaluate cleanup addtimes}, :no => %w{report tags ignoreschedules} - assert_equal(0, master.reported, "master sent report with reports disabled") - - - # Now enable reporting and make sure the report method gets called - Puppet[:report] = true - assert_nothing_raised do - master.apply - end - check.call :yes => %w{evaluate cleanup addtimes}, :no => %w{tags ignoreschedules} - assert_equal(1, master.reported, "master did not send report") - - # Now try it with tags enabled - assert_nothing_raised do - master.apply("tags") - end - check.call :yes => %w{evaluate cleanup tags addtimes}, :no => %w{ignoreschedules} - assert_equal(2, master.reported, "master did not send report") - - # and ignoreschedules - assert_nothing_raised do - master.apply("tags", true) - end - check.call :yes => %w{evaluate cleanup tags ignoreschedules addtimes}, :no => %w{} - assert_equal(3, master.reported, "master did not send report") - end - def test_getconfig client = mkclient @@ -167,9 +122,8 @@ class TestMasterClient < Test::Unit::TestCase [:getplugins, :get_actual_config].each do |method| assert($methodsrun.include?(method), "method %s was not run" % method) end - - objects = client.objects - assert(objects.finalized?, "objects were not finalized") + + assert_instance_of(Puppet::Node::Configuration, client.configuration, "Configuration was not created") end def test_disable @@ -233,7 +187,7 @@ class TestMasterClient < Test::Unit::TestCase } end - # This method is supposed + # This method downloads files, and yields each file object if a block is given. def test_download source = tempfile() dest = tempfile() diff --git a/test/other/transactions.rb b/test/other/transactions.rb index bf5f65084..833ead662 100755 --- a/test/other/transactions.rb +++ b/test/other/transactions.rb @@ -7,8 +7,6 @@ require 'puppettest' require 'mocha' require 'puppettest/support/resources' -# $Id$ - class TestTransactions < Test::Unit::TestCase include PuppetTest::FileTesting include PuppetTest::Support::Resources @@ -133,7 +131,7 @@ class TestTransactions < Test::Unit::TestCase inst = type.create :name => "yay" # Create a transaction - trans = Puppet::Transaction.new(newcomp(inst)) + trans = Puppet::Transaction.new(mk_configuration(inst)) # Make sure prefetch works assert_nothing_raised do @@ -255,7 +253,7 @@ class TestTransactions < Test::Unit::TestCase } - component = newcomp("file",file) + component = mk_configuration("file",file) require 'etc' groupname = Etc.getgrgid(File.stat(file.name).gid).name assert_nothing_raised() { @@ -299,7 +297,7 @@ class TestTransactions < Test::Unit::TestCase @@tmpfiles << execfile - component = newcomp("both",file,exec) + component = mk_configuration("both",file,exec) # 'subscribe' expects an array of arrays exec[:subscribe] = [[file.class.name,file.name]] @@ -343,24 +341,30 @@ class TestTransactions < Test::Unit::TestCase file[:group] = @groups[0] assert_apply(file) - fcomp = newcomp("file",file) - ecomp = newcomp("exec",exec) + config = Puppet::Node::Configuration.new + fcomp = Puppet::Type.type(:component).create(:name => "file") + config.add_resource fcomp + config.add_resource file + config.add_edge!(fcomp, file) - component = newcomp("both",fcomp,ecomp) + ecomp = Puppet::Type.type(:component).create(:name => "exec") + config.add_resource ecomp + config.add_resource exec + config.add_edge!(ecomp, exec) # 'subscribe' expects an array of arrays #component[:require] = [[file.class.name,file.name]] ecomp[:subscribe] = fcomp exec[:refreshonly] = true - trans = assert_events([], component) + trans = assert_events([], config) assert_nothing_raised() { file[:group] = @groups[1] file[:mode] = "755" } - trans = assert_events([:file_changed, :file_changed, :triggered], component) + trans = assert_events([:file_changed, :file_changed, :triggered], config) end # Make sure that multiple subscriptions get triggered. @@ -437,11 +441,10 @@ class TestTransactions < Test::Unit::TestCase :subscribe => ["file", file.name] ) - comp = newcomp(file,exec) - comp.finalize + config = mk_configuration(file,exec) # Run it once - assert_apply(comp) + assert_apply(config) assert(FileTest.exists?(fname), "File did not get created") assert(!exec.scheduled?, "Exec is somehow scheduled") @@ -451,7 +454,7 @@ class TestTransactions < Test::Unit::TestCase file[:content] = "some content" - assert_events([:file_changed, :triggered], comp) + assert_events([:file_changed, :triggered], config) assert(FileTest.exists?(fname), "File did not get recreated") # Now remove it, so it can get created again @@ -469,7 +472,7 @@ class TestTransactions < Test::Unit::TestCase assert(! file.insync?(file.retrieve), "Uh, file is in sync?") - assert_events([:file_changed, :triggered], comp) + assert_events([:file_changed, :triggered], config) assert(FileTest.exists?(fname), "File did not get recreated") end @@ -493,11 +496,9 @@ class TestTransactions < Test::Unit::TestCase :ensure => :file ) - comp = newcomp(exec, file1, file2) - - comp.finalize + config = mk_configuration(exec, file1, file2) - assert_apply(comp) + assert_apply(config) assert(! FileTest.exists?(file1[:path]), "File got created even tho its dependency failed") @@ -506,24 +507,20 @@ class TestTransactions < Test::Unit::TestCase end end - def f(n) - Puppet::Type.type(:file)["/tmp/#{n.to_s}"] - end - def test_relationship_graph - one, two, middle, top = mktree + config = mktree + + config.meta_def(:f) do |name| + self.resource("File[%s]" % name) + end - {one => two, "f" => "c", "h" => middle}.each do |source, target| - if source.is_a?(String) - source = f(source) - end - if target.is_a?(String) - target = f(target) - end + {"one" => "two", "File[f]" => "File[c]", "File[h]" => "middle"}.each do |source_ref, target_ref| + source = config.resource(source_ref) or raise "Missing %s" % source_ref + target = config.resource(target_ref) or raise "Missing %s" % target_ref target[:require] = source end - trans = Puppet::Transaction.new(top) + trans = Puppet::Transaction.new(config) graph = nil assert_nothing_raised do @@ -544,13 +541,13 @@ class TestTransactions < Test::Unit::TestCase sorted = graph.topsort.reverse # Now make sure the appropriate edges are there and are in the right order - assert(graph.dependents(f(:f)).include?(f(:c)), + assert(graph.dependents(config.f(:f)).include?(config.f(:c)), "c not marked a dep of f") - assert(sorted.index(f(:c)) < sorted.index(f(:f)), + assert(sorted.index(config.f(:c)) < sorted.index(config.f(:f)), "c is not before f") - one.each do |o| - two.each do |t| + config.resource("one").each do |o| + config.resource("two").each do |t| assert(graph.dependents(o).include?(t), "%s not marked a dep of %s" % [t.ref, o.ref]) assert(sorted.index(t) < sorted.index(o), @@ -558,22 +555,22 @@ class TestTransactions < Test::Unit::TestCase end end - trans.resources.leaves(middle).each do |child| - assert(graph.dependents(f(:h)).include?(child), + trans.configuration.leaves(config.resource("middle")).each do |child| + assert(graph.dependents(config.f(:h)).include?(child), "%s not marked a dep of h" % [child.ref]) - assert(sorted.index(child) < sorted.index(f(:h)), + assert(sorted.index(child) < sorted.index(config.f(:h)), "%s is not before h" % child.ref) end # Lastly, make sure our 'g' vertex made it into the relationship # graph, since it's not involved in any relationships. - assert(graph.vertex?(f(:g)), + assert(graph.vertex?(config.f(:g)), "Lost vertexes with no relations") # Now make the reversal graph and make sure all of the vertices made it into that reverse = graph.reversal %w{a b c d e f g h}.each do |letter| - file = f(letter) + file = config.f(letter) assert(reverse.vertex?(file), "%s did not make it into reversal" % letter) end end @@ -594,15 +591,15 @@ class TestTransactions < Test::Unit::TestCase yay = Puppet::Type.newgenerator :title => "yay" rah = Puppet::Type.newgenerator :title => "rah" - comp = newcomp(yay, rah) - trans = comp.evaluate + config = mk_configuration(yay, rah) + trans = Puppet::Transaction.new(config) assert_nothing_raised do trans.generate end %w{ya ra y r}.each do |name| - assert(trans.resources.vertex?(Puppet::Type.type(:generator)[name]), + assert(trans.configuration.vertex?(Puppet::Type.type(:generator)[name]), "Generated %s was not a vertex" % name) assert($finished.include?(name), "%s was not finished" % name) end @@ -613,7 +610,7 @@ class TestTransactions < Test::Unit::TestCase end %w{ya ra y r}.each do |name| - assert(!trans.resources.vertex?(Puppet::Type.type(:generator)[name]), + assert(!trans.configuration.vertex?(Puppet::Type.type(:generator)[name]), "Generated vertex %s was not removed from graph" % name) assert_nil(Puppet::Type.type(:generator)[name], "Generated vertex %s was not removed from class" % name) @@ -633,8 +630,8 @@ class TestTransactions < Test::Unit::TestCase yay = Puppet::Type.newgenerator :title => "yay" rah = Puppet::Type.newgenerator :title => "rah", :subscribe => yay - comp = newcomp(yay, rah) - trans = comp.evaluate + config = mk_configuration(yay, rah) + trans = Puppet::Transaction.new(config) trans.prepare @@ -702,7 +699,7 @@ class TestTransactions < Test::Unit::TestCase end # Now, start over and make sure that everything gets evaluated. - trans = comp.evaluate + trans = Puppet::Transaction.new(config) $evaluated.clear assert_nothing_raised do trans.evaluate @@ -712,53 +709,51 @@ class TestTransactions < Test::Unit::TestCase "Not all resources were evaluated or not in the right order") end + # Make sure tags on the transaction get copied to the configuration. def test_tags - res = Puppet::Type.newfile :path => tempfile() - comp = newcomp(res) - - # Make sure they default to none - assert_equal([], comp.evaluate.tags) - - # Make sure we get the main tags - Puppet[:tags] = %w{this is some tags} - assert_equal(%w{this is some tags}, comp.evaluate.tags) - - # And make sure they get processed correctly - Puppet[:tags] = ["one", "two,three", "four"] - assert_equal(%w{one two three four}, comp.evaluate.tags) - - # lastly, make sure we can override them - trans = comp.evaluate - trans.tags = ["one", "two,three", "four"] - assert_equal(%w{one two three four}, comp.evaluate.tags) + config = Puppet::Node::Configuration.new + transaction = Puppet::Transaction.new(config) + assert_equal([], transaction.tags, "Tags defaulted to non-empty") + + Puppet[:tags] = "one,two" + transaction = Puppet::Transaction.new(config) + assert_equal(%w{one two}, transaction.tags, "Tags were not copied from the central configuration") + end + + def test_ignore_tags? + config = Puppet::Node::Configuration.new + config.host_config = true + transaction = Puppet::Transaction.new(config) + assert(! transaction.ignore_tags?, "Ignoring tags when applying a host configuration") + + config.host_config = false + transaction = Puppet::Transaction.new(config) + assert(transaction.ignore_tags?, "Not ignoring tags when applying a non-host configuration") end - def test_tagged? - res = Puppet::Type.newfile :path => tempfile() - comp = newcomp(res) - trans = comp.evaluate - - assert(trans.tagged?(res), "tagged? defaulted to false") - - # Now set some tags - trans.tags = %w{some tags} - - # And make sure it's false - assert(! trans.tagged?(res), "matched invalid tags") - - # Set ignoretags and make sure it sticks - trans.ignoretags = true - assert(trans.tagged?(res), "tags were not ignored") - - # Now make sure we actually correctly match tags - res[:tag] = "mytag" - trans.ignoretags = false - trans.tags = %w{notag} - - assert(! trans.tagged?(res), "tags incorrectly matched") - - trans.tags = %w{mytag yaytag} - assert(trans.tagged?(res), "tags should have matched") + def test_missing_tags? + resource = stub 'resource', :tagged? => true + config = Puppet::Node::Configuration.new + + # Mark it as a host config so we don't care which test is first + config.host_config = true + transaction = Puppet::Transaction.new(config) + assert(! transaction.missing_tags?(resource), "Considered a resource to be missing tags when none are set") + + # host configurations pay attention to tags, no one else does. + Puppet[:tags] = "three,four" + config.host_config = false + transaction = Puppet::Transaction.new(config) + assert(! transaction.missing_tags?(resource), "Considered a resource to be missing tags when not running a host configuration") + + # + config.host_config = true + transaction = Puppet::Transaction.new(config) + assert(! transaction.missing_tags?(resource), "Considered a resource to be missing tags when running a host configuration and all tags are present") + + transaction = Puppet::Transaction.new(config) + resource.stubs :tagged? => false + assert(transaction.missing_tags?(resource), "Considered a resource not to be missing tags when running a host configuration and tags are missing") end # Make sure changes generated by eval_generated resources have proxies @@ -772,8 +767,8 @@ class TestTransactions < Test::Unit::TestCase end resource = type.create :name => "test" - comp = newcomp(resource) - trans = comp.evaluate + config = mk_configuration(resource) + trans = Puppet::Transaction.new(config) trans.prepare assert_nothing_raised do @@ -831,7 +826,7 @@ class TestTransactions < Test::Unit::TestCase end # Make a graph with some stuff in it. - graph = Puppet::PGraph.new + graph = Puppet::Node::Configuration.new # Add a non-triggering edge. a = trigger.new(:a) @@ -887,7 +882,8 @@ class TestTransactions < Test::Unit::TestCase def test_graph Puppet.config.use(:main) # Make a graph - graph = Puppet::PGraph.new + graph = Puppet::Node::Configuration.new + graph.host_config = true graph.add_edge!("a", "b") # Create our transaction @@ -908,27 +904,12 @@ class TestTransactions < Test::Unit::TestCase end assert(FileTest.exists?(dotfile), "Did not create graph.") end - - def test_created_graphs - FileUtils.mkdir_p(Puppet[:graphdir]) - file = Puppet::Type.newfile(:path => tempfile, :content => "yay") - exec = Puppet::Type.type(:exec).create(:command => "echo yay", :path => ENV['PATH'], - :require => file) - - Puppet[:graph] = true - assert_apply(file, exec) - - %w{resources relationships expanded_relationships}.each do |name| - file = File.join(Puppet[:graphdir], "%s.dot" % name) - assert(FileTest.exists?(file), "graph for %s was not created" % name) - end - end def test_set_target file = Puppet::Type.newfile(:path => tempfile(), :content => "yay") exec1 = Puppet::Type.type(:exec).create :command => "/bin/echo exec1" exec2 = Puppet::Type.type(:exec).create :command => "/bin/echo exec2" - trans = Puppet::Transaction.new(newcomp(file, exec1, exec2)) + trans = Puppet::Transaction.new(mk_configuration(file, exec1, exec2)) # First try it with an edge that has no callback edge = Puppet::Relationship.new(file, exec1) @@ -975,7 +956,8 @@ class TestTransactions < Test::Unit::TestCase one[:require] = two two[:require] = one - trans = newcomp(one, two).evaluate + config = mk_configuration(one, two) + trans = Puppet::Transaction.new(config) assert_raise(Puppet::Error) do trans.prepare end @@ -1042,8 +1024,8 @@ class TestTransactions < Test::Unit::TestCase rels[dir] = file rels.each do |after, before| - comp = newcomp(before, after) - trans = comp.evaluate + config = mk_configuration(before, after) + trans = Puppet::Transaction.new(config) str = "from %s to %s" % [before, after] assert_nothing_raised("Failed to create graph %s" % str) do @@ -1063,7 +1045,7 @@ class TestTransactions < Test::Unit::TestCase one[:require] = two one[:subscribe] = two - comp = newcomp(one, two) + comp = mk_configuration(one, two) trans = Puppet::Transaction.new(comp) graph = trans.relationship_graph @@ -1188,5 +1170,3 @@ class TestTransactions < Test::Unit::TestCase assert_equal(1, $flushed, "object was flushed in noop") end end - -# $Id$ |
