[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, master, updated. debian/0.24.6-1-356-g5718585

James Turnbull james at lovedthanlost.net
Fri Jan 23 14:21:17 UTC 2009


The following commit has been merged in the master branch:
commit 765db307f875a52b7dec386b0af90f8e5b4590bb
Author: Luke Kanies <luke at madstop.com>
Date:   Mon Oct 6 19:03:40 2008 -0500

    Adding partial spec tests for Puppet::Util::Metric.
    
    Signed-off-by: Luke Kanies <luke at madstop.com>

diff --git a/lib/puppet/util/metric.rb b/lib/puppet/util/metric.rb
index ca23aa8..722277e 100644
--- a/lib/puppet/util/metric.rb
+++ b/lib/puppet/util/metric.rb
@@ -5,6 +5,8 @@ require 'puppet'
 class Puppet::Util::Metric
     
     # Load the library as a feature, so we can test its presence.
+    # It's only used by this class, so there's no reason to move it
+    # to the main feature list.
     Puppet.features.add :rrd, :libs => 'RRDtool'
 
     attr_accessor :type, :name, :value, :label
@@ -93,11 +95,7 @@ class Puppet::Util::Metric
     def initialize(name,label = nil)
         @name = name.to_s
 
-        if label
-            @label = label
-        else
-            @label = name.to_s.capitalize.gsub("_", " ")
-        end
+        @label = label || labelize(name)
 
         @values = []
     end
@@ -107,9 +105,7 @@ class Puppet::Util::Metric
     end
 
     def newvalue(name,value,label = nil)
-        unless label
-            label = name.to_s.capitalize.gsub("_", " ")
-        end
+        label ||= labelize(name)
         @values.push [name,label,value]
     end
 
@@ -145,7 +141,16 @@ class Puppet::Util::Metric
     def values
         @values.sort { |a, b| a[1] <=> b[1] }
     end
+
+    private
+    
+    # Convert a name into a label.
+    def labelize(name)
+        name.to_s.capitalize.gsub("_", " ")
+    end
 end
 
+# This is necessary because we changed the class path in early 2007,
+# and reports directly yaml-dump these metrics, so both client and server
+# have to agree on the class name.
 Puppet::Metric = Puppet::Util::Metric
-
diff --git a/spec/unit/util/metric.rb b/spec/unit/util/metric.rb
new file mode 100755
index 0000000..7aa0448
--- /dev/null
+++ b/spec/unit/util/metric.rb
@@ -0,0 +1,86 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+require 'puppet/util/metric'
+
+describe Puppet::Util::Metric do
+    before do
+        @metric = Puppet::Util::Metric.new("foo")
+    end
+
+    it "should be aliased to Puppet::Metric" do
+        Puppet::Util::Metric.should equal(Puppet::Metric)
+    end
+
+    [:type, :name, :value, :label, :basedir].each do |name|
+        it "should have a #{name} attribute" do
+            @metric.should respond_to(name)
+            @metric.should respond_to(name.to_s + "=")
+        end
+    end
+
+    it "should default to the :rrdir as the basedir "do
+        Puppet.settings.expects(:value).with(:rrddir).returns "myrrd"
+        @metric.basedir.should == "myrrd"
+    end
+
+    it "should use any provided basedir" do
+        @metric.basedir = "foo"
+        @metric.basedir.should == "foo"
+    end
+
+    it "should require a name at initialization" do
+        lambda { Puppet::Util::Metric.new }.should raise_error(ArgumentError)
+    end
+
+    it "should always convert its name to a string" do
+        Puppet::Util::Metric.new(:foo).name.should == "foo"
+    end
+
+    it "should support a label" do
+        Puppet::Util::Metric.new("foo", "mylabel").label.should == "mylabel"
+    end
+
+    it "should autogenerate a label if none is provided" do
+        Puppet::Util::Metric.new("foo_bar").label.should == "Foo bar"
+    end
+
+    it "should have a method for adding values" do
+        @metric.should respond_to(:newvalue)
+    end
+
+    it "should have a method for returning values" do
+        @metric.should respond_to(:values)
+    end
+
+    it "should require a name and value for its values" do
+        lambda { @metric.newvalue }.should raise_error(ArgumentError)
+    end
+
+    it "should support a label for values" do
+        @metric.newvalue(:foo, 10, "label")
+        @metric.values[0][1].should == "label"
+    end
+
+    it "should autogenerate value labels if none is provided" do
+        @metric.newvalue("foo_bar", 10)
+        @metric.values[0][1].should == "Foo bar"
+    end
+
+    it "should return its values sorted by label" do
+        @metric.newvalue(:foo, 10, "b")
+        @metric.newvalue(:bar, 10, "a")
+
+        @metric.values.should == [[:bar, "a", 10], [:foo, "b", 10]]
+    end
+
+    # LAK: I'm not taking the time to develop these tests right now.
+    # I expect they should actually be extracted into a separate class
+    # anyway.
+    it "should be able to graph metrics using RRDTool"
+
+    it "should be able to create a new RRDTool database"
+
+    it "should be able to store metrics into an RRDTool database"
+end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list