Please review pull request #153: Add filesystem fact & tests opened by (rodjek)

Description:

Filesystem fact pulled in from https://github.com/kwilczynski/facter-facts/blob/master/filesystems.rb and tests have been added.

Redmine ticket http://projects.puppetlabs.com/issues/9574

  • Opened: Sun Jan 22 06:38:21 UTC 2012
  • Based on: puppetlabs:master (19090296cfe0d08088c27d13710934baaf499a0e)
  • Requested merge: rodjek:ticket/9574 (02c4f2eb434abcd014c78437016dd7b54759795a)

Diff follows:

diff --git a/lib/facter/filesystems.rb b/lib/facter/filesystems.rb
new file mode 100644
index 0000000..49d0f86
--- /dev/null
+++ b/lib/facter/filesystems.rb
@@ -0,0 +1,84 @@
+#
+# filesystems.rb
+#
+# This fact provides an alphabetic list of usable file systems that can
+# be used for block devices like hard drives, media cards and so on ...
+#
+
+require 'thread'
+require 'facter'
+
+if Facter.value(:kernel) == 'Linux'
+  mutex = Mutex.new
+
+  # We store a list of file systems here ...
+  file_systems = []
+
+  # Support for the following might not be of interest ...
+  exclude = %w(fuseblk)
+
+  #
+  # Modern Linux kernels provide "/proc/filesystems" in the following format:
+  #
+  #   nodev   sysfs
+  #   nodev   rootfs
+  #   nodev   bdev
+  #   nodev   proc
+  #   nodev   cgroup
+  #   nodev   cpuset
+  #   nodev   debugfs
+  #   nodev   securityfs
+  #   nodev   sockfs
+  #   nodev   pipefs
+  #   nodev   anon_inodefs
+  #   nodev   tmpfs
+  #   nodev   inotifyfs
+  #   nodev   devpts
+  #           ext3
+  #           ext2
+  #           ext4
+  #   nodev   ramfs
+  #   nodev   hugetlbfs
+  #   nodev   ecryptfs
+  #   nodev   fuse
+  #           fuseblk
+  #   nodev   fusectl
+  #   nodev   mqueue
+  #           xfs
+  #   nodev   binfmt_misc
+  #           vfat
+  #           iso9660
+  #
+  # We skip every "nodev" entry as they cannot really be used for block
+  # devices like hard drives and media cards, and so on ...
+  #
+
+  # Make regular _expression_ form our patterns ...
+  exclude = Regexp.union(*exclude.collect { |i| Regexp.new(i) })
+
+  #
+  # We utilise rely on "cat" for reading values from entries under "/proc".
+  # This is due to some problems with IO#read in Ruby and reading content of
+  # the "proc" file system that was reported more than once in the past ...
+  #
+  Facter::Util::Resolution.exec('cat /proc/filesystems 2> /dev/null').each_line do |line|
+    # Remove bloat ...
+    line.strip!
+
+    # Line of interest should not start with "nodev" ...
+    next if line.empty? or line.match(/^nodev/)
+
+    # We have something, so let us apply our device type filter ...
+    next if line.match(exclude)
+
+    mutex.synchronize { file_systems << line }
+  end
+
+  Facter.add('filesystems') do
+    confine :kernel => :linux
+    setcode { file_systems.sort.join(',') }
+  end
+end
+
+# vim: set ts=2 sw=2 et :
+# encoding: utf-8
diff --git a/spec/fixtures/filesystems/linux b/spec/fixtures/filesystems/linux
new file mode 100644
index 0000000..368f588
--- /dev/null
+++ b/spec/fixtures/filesystems/linux
@@ -0,0 +1,28 @@
+nodev   sysfs
+nodev   rootfs
+nodev   bdev
+nodev   proc
+nodev   cgroup
+nodev   cpuset
+nodev   debugfs
+nodev   securityfs
+nodev   sockfs
+nodev   pipefs
+nodev   anon_inodefs
+nodev   tmpfs
+nodev   inotifyfs
+nodev   devpts
+        ext3
+        ext2
+        ext4
+nodev   ramfs
+nodev   hugetlbfs
+nodev   ecryptfs
+nodev   fuse
+        fuseblk
+nodev   fusectl
+nodev   mqueue
+        xfs
+nodev   binfmt_misc
+        vfat
+        iso9660
diff --git a/spec/unit/filesystem_spec.rb b/spec/unit/filesystem_spec.rb
new file mode 100644
index 0000000..8e77fa2
--- /dev/null
+++ b/spec/unit/filesystem_spec.rb
@@ -0,0 +1,50 @@
+#!/usr/bin/env rspec
+
+require 'spec_helper'
+
+describe 'Filesystem facts' do
+  describe 'on non-Linux OS' do
+    it 'should not exist' do
+      Facter.fact(:kernel).stubs(:value).returns('SunOS')
+      Facter.fact(:filesystems).should == nil
+    end
+  end
+
+  describe 'on Linux' do
+    before :each do
+      Facter.fact(:kernel).stubs(:value).returns('Linux')
+      fixture_data = File.read(fixtures('filesystems', 'linux'))
+      Facter::Util::Resolution.expects(:exec).at_least_once \
+        .with('cat /proc/filesystems 2> /dev/null').returns(fixture_data)
+      Facter.collection.loader.load(:filesystems)
+    end
+
+    after :each do
+      Facter.clear
+    end
+
+    it 'should exist' do
+      Facter.fact(:filesystems).should_not == nil
+    end
+
+    it 'should detect the correct number of filesystems' do
+      Facter.fact(:filesystems).value.split(',').length.should == 6
+    end
+
+    # Check that lines from /proc/filesystems that start with 'nodev' are
+    # skipped
+    it 'should not detect sysfs' do
+      Facter.fact(:filesystems).value.split(',').should_not include('sysfs')
+    end
+
+    # Check that all other lines are counted as valid filesystems
+    it 'should detect ext4' do
+      Facter.fact(:filesystems).value.split(',').should include('ext4')
+    end
+
+    # For some reason the author of the fact doesn't like FUSE...
+    it 'should not detect fuseblk' do
+      Facter.fact(:filesystems).value.split(',').should_not include('fuseblk')
+    end
+  end
+end

    

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to