[Pkg-puppet-devel] [facter] 134/352: (FACT-134) Normalize and validate fact values

Stig Sandbeck Mathisen ssm at debian.org
Sun Apr 6 22:21:39 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 9d2ef21c908fab3fea421322d7ec308fe54e648f
Author: Adrien Thebo <git at somethingsinistral.net>
Date:   Thu Dec 12 14:08:00 2013 -0800

    (FACT-134) Normalize and validate fact values
    
    This commit normalizes fact values to UTF-8 and verifies that the resulting
    encoding is valid.
    
    On Windows, the default string encoding depends on the active code page. When
    adding an environment based fact, e.g. FACTER_one=bar, the value is frozen
    and cannot be encoded in place. So we dup fact values that come from the
    environment.
---
 lib/facter/util/loader.rb         |  2 +-
 lib/facter/util/resolution.rb     |  7 ++++++-
 spec/unit/util/resolution_spec.rb | 23 +++++++++++------------
 3 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/lib/facter/util/loader.rb b/lib/facter/util/loader.rb
index a8fb88d..f08a971 100644
--- a/lib/facter/util/loader.rb
+++ b/lib/facter/util/loader.rb
@@ -141,7 +141,7 @@ class Facter::Util::Loader
 
       Facter.add($1) do
         has_weight 1_000_000
-        setcode { value }
+        setcode { value.dup }
       end
 
       # Short-cut, if we are only looking for one value.
diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb
index 3003e66..dc8708c 100644
--- a/lib/facter/util/resolution.rb
+++ b/lib/facter/util/resolution.rb
@@ -1,5 +1,6 @@
 require 'facter/util/confine'
 require 'facter/util/config'
+require 'facter/util/normalization'
 
 require 'timeout'
 
@@ -440,7 +441,11 @@ class Facter::Util::Resolution
     ms = (finishtime - starttime) * 1000
     Facter.show_time "#{self.name}: #{"%.2f" % ms}ms"
 
-    return nil if result == ""
+    Facter::Util::Normalization.normalize(result)
+
     return result
+  rescue Facter::Util::Normalization::NormalizationError => e
+    Facter.warn "Fact resolution #{self.name} resolved to an invalid value: #{e.message}"
+    nil
   end
 end
diff --git a/spec/unit/util/resolution_spec.rb b/spec/unit/util/resolution_spec.rb
index e0300bf..51767df 100755
--- a/spec/unit/util/resolution_spec.rb
+++ b/spec/unit/util/resolution_spec.rb
@@ -210,10 +210,11 @@ describe Facter::Util::Resolution do
           @resolve.value.should == "yup"
         end
 
-        it "should return nil if the value is an empty string" do
+        it "it validates the resolved value" do
           @resolve.setcode "/bin/foo"
           Facter::Util::Resolution.expects(:exec).once.returns ""
-          @resolve.value.should be_nil
+          Facter::Util::Normalization.expects(:normalize).with ""
+          @resolve.value.should eq ""
         end
       end
 
@@ -229,10 +230,11 @@ describe Facter::Util::Resolution do
           @resolve.value.should == "yup"
         end
 
-        it "should return nil if the value is an empty string" do
+        it "it validates the resolved value" do
           @resolve.setcode "/bin/foo"
           Facter::Util::Resolution.expects(:exec).once.returns ""
-          @resolve.value.should be_nil
+          Facter::Util::Normalization.expects(:normalize).with ""
+          @resolve.value.should eq ""
         end
       end
     end
@@ -249,14 +251,11 @@ describe Facter::Util::Resolution do
         @resolve.value.should == "yayness"
       end
 
-      it "should return nil if the value is an empty string" do
-        @resolve.setcode { "" }
-        @resolve.value.should be_nil
-      end
-
-      it "should return nil if the value is an empty block" do
-        @resolve.setcode { "" }
-        @resolve.value.should be_nil
+      it "it validates the resolved value" do
+        @resolve.setcode "/bin/foo"
+        Facter::Util::Resolution.expects(:exec).once.returns ""
+        Facter::Util::Normalization.expects(:normalize).with ""
+        @resolve.value.should eq ""
       end
 
       it "should use its limit method to determine the timeout, to avoid conflict when a 'timeout' method exists for some other reason" do

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list