[Pkg-puppet-devel] [facter] 302/352: (FACT-356) Return nil when no vlans are present

Stig Sandbeck Mathisen ssm at debian.org
Sun Apr 6 22:21:55 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 c60f11e6a917d5cf12f3fa8b5293e33d7c0de023
Author: Adrien Thebo <git at somethingsinistral.net>
Date:   Mon Mar 3 10:12:16 2014 -0800

    (FACT-356) Return nil when no vlans are present
---
 lib/facter/util/vlans.rb     | 21 +++++++++------------
 spec/unit/util/vlans_spec.rb | 33 +++++++++++++++++++++++++++++----
 2 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/lib/facter/util/vlans.rb b/lib/facter/util/vlans.rb
index 0a48ec8..e42a28f 100644
--- a/lib/facter/util/vlans.rb
+++ b/lib/facter/util/vlans.rb
@@ -2,23 +2,20 @@
 #
 module Facter::Util::Vlans
   def self.get_vlan_config
-    output = ""
-      if File.exists?('/proc/net/vlan/config') and File.readable?('/proc/net/vlan/config')
-        output = File.open('/proc/net/vlan/config').read
-      end
-    output
+    if File.exist?('/proc/net/vlan/config') and File.readable?('/proc/net/vlan/config')
+      File.read('/proc/net/vlan/config')
+    end
   end
 
   def self.get_vlans
-    vlans = Array.new
-    if self.get_vlan_config
-      self.get_vlan_config.each_line do |line|
-        if line =~ /^([0-9A-Za-z]+)\.([0-9]+) /
-          vlans.insert(-1, $~[2]) if $~[2]
+    if (config = self.get_vlan_config)
+      vlans = []
+      config.each_line do |line|
+        if (match = line.match(/^([0-9A-Za-z]+)\.([0-9]+) /))
+          vlans << match[2] if match[2]
         end
       end
+      vlans.join(',')
     end
-
-    vlans.join(',')
   end
 end
diff --git a/spec/unit/util/vlans_spec.rb b/spec/unit/util/vlans_spec.rb
index 70de170..caed3a6 100755
--- a/spec/unit/util/vlans_spec.rb
+++ b/spec/unit/util/vlans_spec.rb
@@ -4,9 +4,34 @@ require 'spec_helper'
 require 'facter/util/vlans'
 
 describe Facter::Util::Vlans do
-  it "should return a list of vlans on Linux" do
-    linux_vlanconfig = my_fixture_read("linux_vlan_config")
-    Facter::Util::Vlans.stubs(:get_vlan_config).returns(linux_vlanconfig)
-    Facter::Util::Vlans.get_vlans().should == %{400,300,200,100}
+  let(:vlan_file) { "/proc/net/vlan/config" }
+
+  describe "reading the vlan configuration" do
+    it "uses the contents of /proc/net/vlan/config" do
+      File.expects(:exist?).with(vlan_file).returns true
+      File.expects(:readable?).with(vlan_file).returns true
+      File.expects(:read).with(vlan_file).returns "vlan contents here"
+
+      expect(Facter::Util::Vlans.get_vlan_config).to eq "vlan contents here"
+    end
+
+    it "returns nil when /proc/net/vlan/config is absent" do
+      File.expects(:exist?).with(vlan_file).returns false
+      expect(Facter::Util::Vlans.get_vlan_config).to be_nil
+    end
+  end
+
+  describe "parsing the vlan configuration" do
+    let(:vlan_content) { my_fixture_read("linux_vlan_config") }
+
+    it "returns a list of vlans on Linux when vlans are configured" do
+      Facter::Util::Vlans.stubs(:get_vlan_config).returns(vlan_content)
+      expect(Facter::Util::Vlans.get_vlans()).to eq %{400,300,200,100}
+    end
+
+    it "returns nil when no vlans are configured" do
+      Facter::Util::Vlans.stubs(:get_vlan_config).returns(nil)
+      expect(Facter::Util::Vlans.get_vlans()).to be_nil
+    end
   end
 end

-- 
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