diff options
| author | Luke Kanies <luke@madstop.com> | 2007-11-08 11:28:34 -0600 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2007-11-08 11:28:34 -0600 |
| commit | f465e7e96d62f9b18bdebd51319582d5b2ffa332 (patch) | |
| tree | c33a94d5430d42a702735554c2110c149eae349d /spec | |
| parent | 65858356cb3170e04200a6d8204f0978223e2c61 (diff) | |
| parent | 1ffcce079fd7a677ebdc22453a9d10675fb4ed4d (diff) | |
Merge branch 'rest'
Diffstat (limited to 'spec')
| -rwxr-xr-x | spec/unit/node/configuration.rb | 27 | ||||
| -rwxr-xr-x | spec/unit/other/pgraph.rb | 48 | ||||
| -rwxr-xr-x | spec/unit/simple_graph.rb | 225 |
3 files changed, 255 insertions, 45 deletions
diff --git a/spec/unit/node/configuration.rb b/spec/unit/node/configuration.rb index 5f807b9b6..e9dc6b85d 100755 --- a/spec/unit/node/configuration.rb +++ b/spec/unit/node/configuration.rb @@ -312,6 +312,17 @@ describe Puppet::Node::Configuration, " when functioning as a resource container @config.resource(@two.ref).should equal(@two) end + it "should set itself as the resource's configuration if it is not a relationship graph" do + @one.expects(:configuration=).with(@config) + @config.add_resource @one + end + + it "should not set itself as the resource's configuration if it is a relationship graph" do + @one.expects(:configuration=).never + @config.is_relationship_graph = true + @config.add_resource @one + end + it "should make all vertices available by resource reference" do @config.add_resource(@one) @config.resource(@one.ref).should equal(@one) @@ -516,6 +527,10 @@ describe Puppet::Node::Configuration, " when creating a relationship graph" do @relationships = @config.relationship_graph end + it "should fail when trying to create a relationship graph for a relationship graph" do + proc { @relationships.relationship_graph }.should raise_error(Puppet::DevError) + end + it "should be able to create a relationship graph" do @relationships.should be_instance_of(Puppet::Node::Configuration) end @@ -578,6 +593,18 @@ describe Puppet::Node::Configuration, " when creating a relationship graph" do @config.resource("File[/yay]").should equal(resource) end + it "should add implicit resources to the relationship graph if there is one" do + args = {:name => "/yay", :ensure => :file} + resource = stub 'file', :ref => "File[/yay]", :configuration= => @config + resource.expects(:implicit=).with(true) + Puppet::Type.type(:file).expects(:create).with(args).returns(resource) + # build the graph + relgraph = @config.relationship_graph + + @config.create_implicit_resource :file, args + relgraph.resource("File[/yay]").should equal(resource) + end + it "should remove resources created mid-transaction" do args = {:name => "/yay", :ensure => :file} resource = stub 'file', :ref => "File[/yay]", :configuration= => @config diff --git a/spec/unit/other/pgraph.rb b/spec/unit/other/pgraph.rb index 19809ac1e..9446a8371 100755 --- a/spec/unit/other/pgraph.rb +++ b/spec/unit/other/pgraph.rb @@ -122,7 +122,7 @@ describe Puppet::PGraph, " when splicing the relationship graph" do def dependency_graph @depgraph = Puppet::PGraph.new @contgraph.vertices.each do |v| - @depgraph.add_vertex(v) + @depgraph.add_vertex!(v) end # We have to specify a relationship to our empty container, else it @@ -204,54 +204,12 @@ describe Puppet::PGraph, " when splicing the relationship graph" do splice @three.each do |child| edge = @depgraph.edge_class.new("c", child) - @depgraph.should be_edge(edge) - @depgraph[edge].should == {:callback => :refresh} + @depgraph.should be_edge(edge.source, edge.target) + @depgraph.edge_label(edge.source, edge.target).should == {:callback => :refresh} end end end -# Labels in this graph are used for managing relationships, -# including callbacks, so they're quite important. -describe Puppet::PGraph, " when managing labels" do - before do - @graph = Puppet::PGraph.new - @label = {:callback => :yay} - end - - it "should return nil for edges with no label" do - @graph.add_edge!(:a, :b) - @graph.edge_label(:a, :b).should be_nil - end - - it "should just return empty label hashes" do - @graph.add_edge!(:a, :b, {}) - @graph.edge_label(:a, :b).should == {} - end - - it "should consider empty label hashes to be nil when copying" do - @graph.add_edge!(:a, :b) - @graph.copy_label(:a, :b, {}) - @graph.edge_label(:a, :b).should be_nil - end - - it "should return label hashes" do - @graph.add_edge!(:a, :b, @label) - @graph.edge_label(:a, :b).should == @label - end - - it "should replace nil labels with real labels" do - @graph.add_edge!(:a, :b) - @graph.copy_label(:a, :b, @label) - @graph.edge_label(:a, :b).should == @label - end - - it "should not replace labels with nil labels" do - @graph.add_edge!(:a, :b, @label) - @graph.copy_label(:a, :b, {}) - @graph.edge_label(:a, :b).should == @label - end -end - describe Puppet::PGraph, " when sorting the graph" do before do @graph = Puppet::PGraph.new diff --git a/spec/unit/simple_graph.rb b/spec/unit/simple_graph.rb new file mode 100755 index 000000000..1be29c0dc --- /dev/null +++ b/spec/unit/simple_graph.rb @@ -0,0 +1,225 @@ +#!/usr/bin/env ruby +# +# Created by Luke Kanies on 2007-11-1. +# Copyright (c) 2006. All rights reserved. + +require File.dirname(__FILE__) + '/../spec_helper' +require 'puppet/simple_graph' + +describe Puppet::SimpleGraph do + it "should return the number of its vertices as its length" do + @graph = Puppet::SimpleGraph.new + @graph.add_vertex!("one") + @graph.add_vertex!("two") + @graph.size.should == 2 + end + + it "should consider itself a directed graph" do + Puppet::SimpleGraph.new.directed?.should be_true + end + + it "should provide a method for reversing the graph" do + @graph = Puppet::SimpleGraph.new + @graph.add_edge!(:one, :two) + @graph.reversal.edge?(:two, :one).should be_true + end +end + +describe Puppet::SimpleGraph, " when managing vertices" do + before do + @graph = Puppet::SimpleGraph.new + end + + it "should provide a method to add a vertex" do + @graph.add_vertex!(:test) + @graph.vertex?(:test).should be_true + end + + it "should ignore already-present vertices when asked to add a vertex" do + @graph.add_vertex!(:test) + proc { @graph.add_vertex!(:test) }.should_not raise_error + end + + it "should return true when asked if a vertex is present" do + @graph.add_vertex!(:test) + @graph.vertex?(:test).should be_true + end + + it "should return false when asked if a non-vertex is present" do + @graph.vertex?(:test).should be_false + end + + it "should return all set vertices when asked" do + @graph.add_vertex!(:one) + @graph.add_vertex!(:two) + @graph.vertices.should == [:one, :two] + end + + it "should remove a given vertex when asked" do + @graph.add_vertex!(:one) + @graph.remove_vertex!(:one) + @graph.vertex?(:one).should be_false + end + + it "should do nothing when a non-vertex is asked to be removed" do + proc { @graph.remove_vertex!(:one) }.should_not raise_error + end +end + +describe Puppet::SimpleGraph, " when managing edges" do + before do + @graph = Puppet::SimpleGraph.new + end + + it "should provide a method to test whether a given vertex pair is an edge" do + @graph.should respond_to(:edge?) + end + + it "should provide a method to add an edge as an instance of the edge class" do + edge = Puppet::Relationship.new(:one, :two) + @graph.add_edge!(edge) + @graph.edge?(:one, :two).should be_true + end + + it "should provide a method to add an edge by specifying the two vertices" do + @graph.add_edge!(:one, :two) + @graph.edge?(:one, :two).should be_true + end + + it "should provide a method to add an edge by specifying the two vertices and a label" do + @graph.add_edge!(:one, :two, :stuff => :awesome) + @graph.edge?(:one, :two).should be_true + end + + it "should provide a method for retrieving an edge label" do + edge = Puppet::Relationship.new(:one, :two, :stuff => :awesome) + @graph.add_edge!(edge) + @graph.edge_label(:one, :two).should == {:stuff => :awesome} + end + + it "should provide a method for retrieving an edge" do + edge = Puppet::Relationship.new(:one, :two) + @graph.add_edge!(edge) + @graph.edge(:one, :two).should equal(edge) + end + + it "should add the edge source as a vertex if it is not already" do + edge = Puppet::Relationship.new(:one, :two) + @graph.add_edge!(edge) + @graph.vertex?(:one).should be_true + end + + it "should add the edge target as a vertex if it is not already" do + edge = Puppet::Relationship.new(:one, :two) + @graph.add_edge!(edge) + @graph.vertex?(:two).should be_true + end + + it "should return all edges as edge instances when asked" do + one = Puppet::Relationship.new(:one, :two) + two = Puppet::Relationship.new(:two, :three) + @graph.add_edge!(one) + @graph.add_edge!(two) + edges = @graph.edges + edges.length.should == 2 + edges.should include(one) + edges.should include(two) + end + + it "should remove an edge when asked" do + edge = Puppet::Relationship.new(:one, :two) + @graph.add_edge!(edge) + @graph.remove_edge!(edge) + @graph.edge?(edge.source, edge.target).should be_false + end + + it "should remove all related edges when a vertex is removed" do + one = Puppet::Relationship.new(:one, :two) + two = Puppet::Relationship.new(:two, :three) + @graph.add_edge!(one) + @graph.add_edge!(two) + @graph.remove_vertex!(:two) + @graph.edge?(:one, :two).should be_false + @graph.edge?(:two, :three).should be_false + @graph.edges.length.should == 0 + end +end + +describe Puppet::SimpleGraph, " when finding adjacent vertices" do + before do + @graph = Puppet::SimpleGraph.new + @one_two = Puppet::Relationship.new(:one, :two) + @two_three = Puppet::Relationship.new(:two, :three) + @one_three = Puppet::Relationship.new(:one, :three) + @graph.add_edge!(@one_two) + @graph.add_edge!(@one_three) + @graph.add_edge!(@two_three) + end + + it "should return adjacent vertices" do + adj = @graph.adjacent(:one) + adj.should be_include(:three) + adj.should be_include(:two) + end + + it "should default to finding :out vertices" do + @graph.adjacent(:two).should == [:three] + end + + it "should support selecting :in vertices" do + @graph.adjacent(:two, :direction => :in).should == [:one] + end + + it "should default to returning the matching vertices as an array of vertices" do + @graph.adjacent(:two).should == [:three] + end + + it "should support returning an array of matching edges" do + @graph.adjacent(:two, :type => :edges).should == [@two_three] + end +end + +describe Puppet::SimpleGraph, " when clearing" do + before do + @graph = Puppet::SimpleGraph.new + one = Puppet::Relationship.new(:one, :two) + two = Puppet::Relationship.new(:two, :three) + @graph.add_edge!(one) + @graph.add_edge!(two) + + @graph.clear + end + + it "should remove all vertices" do + @graph.vertices.should be_empty + end + + it "should remove all edges" do + @graph.edges.should be_empty + end +end + +describe Puppet::SimpleGraph, " when reversing graphs" do + before do + @graph = Puppet::SimpleGraph.new + end + + it "should provide a method for reversing the graph" do + @graph.add_edge!(:one, :two) + @graph.reversal.edge?(:two, :one).should be_true + end + + it "should add all vertices to the reversed graph" do + end + + it "should retain labels on edges" do + @graph.add_edge!(:one, :two, :stuff => :awesome) + edge = @graph.reversal.edge(:two, :one) + edge.label.should == {:stuff => :awesome} + end +end + +describe Puppet::SimpleGraph, " when making DOT files" do + # LAK:FIXME yay + it "should have tests" +end |
