Please review pull request #669: 2 patches to the zfs/zpool providers... opened by (mfournier)

Description:

...which address #9416 and #13938.

  • Opened: Fri Apr 13 08:21:41 UTC 2012
  • Based on: puppetlabs:2.7.x (849a882d0914e305547d566471f1027102bbfba5)
  • Requested merge: mfournier:2-zfs-patches (b95a997850b89bb60d35a6b127140b25b2bbe3c3)

Diff follows:

diff --git a/lib/puppet/provider/zfs/solaris.rb b/lib/puppet/provider/zfs/solaris.rb
deleted file mode 100644
index b783f9e..0000000
--- a/lib/puppet/provider/zfs/solaris.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-Puppet::Type.type(:zfs).provide(:solaris) do
-  desc "Provider for Solaris zfs."
-
-  commands :zfs => "/usr/sbin/zfs"
-  defaultfor :operatingsystem => :solaris
-
-  def add_properties
-    properties = []
-    Puppet::Type.type(:zfs).validproperties.each do |property|
-      next if property == :ensure
-      if value = @resource[property] and value != ""
-        properties << "-o" << "#{property}=#{value}"
-      end
-    end
-    properties
-  end
-
-  def create
-    zfs *([:create] + add_properties + [@resource[:name]])
-  end
-
-  def destroy
-    zfs(:destroy, @resource[:name])
-  end
-
-  def exists?
-    if zfs(:list).split("\n").detect { |line| line.split("\s")[0] == @resource[:name] }
-      true
-    else
-      false
-    end
-  end
-
-  [:aclinherit, :aclmode, :atime, :canmount, :checksum, :compression, :copies, :devices, :exec, :logbias, :mountpoint, :nbmand, :primarycache, :quota, :readonly, :recordsize, :refquota, :refreservation, :reservation, :secondarycache, :setuid, :shareiscsi, :sharenfs, :sharesmb, :snapdir, :version, :volsize, :vscan, :xattr, :zoned, :vscan].each do |field|
-    define_method(field) do
-      zfs(:get, "-H", "-o", "value", field, @resource[:name]).strip
-    end
-
-    define_method(field.to_s + "=") do |should|
-      zfs(:set, "#{field}=#{should}", @resource[:name])
-    end
-  end
-
-end
-
diff --git a/lib/puppet/provider/zfs/zfs.rb b/lib/puppet/provider/zfs/zfs.rb
new file mode 100644
index 0000000..63e7e3d
--- /dev/null
+++ b/lib/puppet/provider/zfs/zfs.rb
@@ -0,0 +1,54 @@
+Puppet::Type.type(:zfs).provide(:zfs) do
+  desc "Provider for zfs."
+
+  zfspath = case Facter.value(:operatingsystem)
+            when /^(Solaris|SunOS|Darwin)/i
+              '/usr/sbin'
+            else
+              '/sbin'
+            end
+
+  commands :zfs => "#{zfspath}/zfs"
+
+  defaultfor :kernel => :linux
+  defaultfor :operatingsystem => [:solaris, :sunos, :darwin, :freebsd, :netbsd, :"gnu/kfreebsd"]
+
+  def add_properties
+    properties = []
+    Puppet::Type.type(:zfs).validproperties.each do |property|
+      next if property == :ensure
+      if value = @resource[property] and value != ""
+        properties << "-o" << "#{property}=#{value}"
+      end
+    end
+    properties
+  end
+
+  def create
+    zfs *([:create] + add_properties + [@resource[:name]])
+  end
+
+  def destroy
+    zfs(:destroy, @resource[:name])
+  end
+
+  def exists?
+    if zfs(:list).split("\n").detect { |line| line.split("\s")[0] == @resource[:name] }
+      true
+    else
+      false
+    end
+  end
+
+  [:aclinherit, :aclmode, :atime, :canmount, :checksum, :compression, :copies, :dedup, :devices, :exec, :logbias, :mountpoint, :nbmand, :primarycache, :quota, :readonly, :recordsize, :refquota, :refreservation, :reservation, :secondarycache, :setuid, :shareiscsi, :sharenfs, :sharesmb, :snapdir, :version, :volsize, :vscan, :xattr, :zoned].each do |field|
+    define_method(field) do
+      zfs(:get, "-H", "-o", "value", field, @resource[:name]).strip
+    end
+
+    define_method(field.to_s + "=") do |should|
+      zfs(:set, "#{field}=#{should}", @resource[:name])
+    end
+  end
+
+end
+
diff --git a/lib/puppet/provider/zpool/solaris.rb b/lib/puppet/provider/zpool/solaris.rb
deleted file mode 100644
index 758ea61..0000000
--- a/lib/puppet/provider/zpool/solaris.rb
+++ /dev/null
@@ -1,116 +0,0 @@
-Puppet::Type.type(:zpool).provide(:solaris) do
-  desc "Provider for Solaris zpool."
-
-  commands :zpool => "/usr/sbin/zpool"
-  defaultfor :operatingsystem => :solaris
-
-  def process_zpool_data(pool_array)
-    if pool_array == []
-      return Hash.new(:absent)
-    end
-    #get the name and get rid of it
-    pool = Hash.new
-    pool[:pool] = pool_array[0]
-    pool_array.shift
-
-    tmp = []
-
-    #order matters here :(
-    pool_array.reverse.each do |value|
-      sym = nil
-      case value
-      when "spares";
-        sym = :spare
-      when "logs";
-        sym = :log
-      when /^mirror|^raidz1|^raidz2/;
-        sym = value =~ /^mirror/ ? :mirror : :raidz
-        pool[:raid_parity] = "raidz2" if value =~ /^raidz2/
-      else
-        tmp << value
-        sym = :disk if value == pool_array.first
-      end
-
-      if sym
-        pool[sym] = pool[sym] ? pool[sym].unshift(tmp.reverse.join(' ')) : [tmp.reverse.join(' ')]
-        tmp.clear
-      end
-    end
-
-    pool
-  end
-
-  def get_pool_data
-    #this is all voodoo dependent on the output from zpool
-    zpool_data = %x{ zpool status #{@resource[:pool]}}.split("\n").select { |line| line.index("\t") == 0 }.collect { |l| l.strip.split("\s")[0] }
-    zpool_data.shift
-    zpool_data
-  end
-
-  def current_pool
-    @current_pool = process_zpool_data(get_pool_data) unless (defined?(@current_pool) and @current_pool)
-    @current_pool
-  end
-
-  def flush
-    @current_pool= nil
-  end
-
-  #Adds log and spare
-  def build_named(name)
-    if prop = @resource[name.intern]
-      [name] + prop.collect { |p| p.split(' ') }.flatten
-    else
-      []
-    end
-  end
-
-  #query for parity and set the right string
-  def raidzarity
-    @resource[:raid_parity] ? @resource[:raid_parity] : "raidz1"
-  end
-
-  #handle mirror or raid
-  def handle_multi_arrays(prefix, array)
-    array.collect{ |a| [prefix] +  a.split(' ') }.flatten
-  end
-
-  #builds up the vdevs for create command
-  def build_vdevs
-    if disk = @resource[:disk]
-      disk.collect { |d| d.split(' ') }.flatten
-    elsif mirror = @resource[:mirror]
-      handle_multi_arrays("mirror", mirror)
-    elsif raidz = @resource[:raidz]
-      handle_multi_arrays(raidzarity, raidz)
-    end
-  end
-
-  def create
-    zpool(*([:create, @resource[:pool]] + build_vdevs + build_named("spare") + build_named("log")))
-  end
-
-  def delete
-    zpool :destroy, @resource[:pool]
-  end
-
-  def exists?
-    if current_pool[:pool] == :absent
-      false
-    else
-      true
-    end
-  end
-
-  [:disk, :mirror, :raidz, :log, :spare].each do |field|
-    define_method(field) do
-      current_pool[field]
-    end
-
-    define_method(field.to_s + "=") do |should|
-      Puppet.warning "NO CHANGES BEING MADE: zpool #{field} does not match, should be '#{should}' currently is '#{current_pool[field]}'"
-    end
-  end
-
-end
-
diff --git a/lib/puppet/provider/zpool/zpool.rb b/lib/puppet/provider/zpool/zpool.rb
new file mode 100644
index 0000000..af31a5f
--- /dev/null
+++ b/lib/puppet/provider/zpool/zpool.rb
@@ -0,0 +1,126 @@
+Puppet::Type.type(:zpool).provide(:zpool) do
+  desc "Provider for zpool."
+
+  zpoolpath = case Facter.value(:operatingsystem)
+              when /^(Solaris|SunOS|Darwin)/i
+                '/usr/sbin'
+              else
+                '/sbin'
+              end
+
+  commands :zpool => "#{zpoolpath}/zpool"
+
+  defaultfor :kernel => :linux
+  defaultfor :operatingsystem => [:solaris, :sunos, :darwin, :freebsd, :netbsd, :"gnu/kfreebsd"]
+
+
+  def process_zpool_data(pool_array)
+    if pool_array == []
+      return Hash.new(:absent)
+    end
+    #get the name and get rid of it
+    pool = Hash.new
+    pool[:pool] = pool_array[0]
+    pool_array.shift
+
+    tmp = []
+
+    #order matters here :(
+    pool_array.reverse.each do |value|
+      sym = nil
+      case value
+      when "spares";
+        sym = :spare
+      when "logs";
+        sym = :log
+      when /^mirror|^raidz1|^raidz2/;
+        sym = value =~ /^mirror/ ? :mirror : :raidz
+        pool[:raid_parity] = "raidz2" if value =~ /^raidz2/
+      else
+        tmp << value
+        sym = :disk if value == pool_array.first
+      end
+
+      if sym
+        pool[sym] = pool[sym] ? pool[sym].unshift(tmp.reverse.join(' ')) : [tmp.reverse.join(' ')]
+        tmp.clear
+      end
+    end
+
+    pool
+  end
+
+  def get_pool_data
+    #this is all voodoo dependent on the output from zpool
+    zpool_data = %x{ zpool status #{@resource[:pool]}}.split("\n").select { |line| line.index("\t") == 0 }.collect { |l| l.strip.split("\s")[0] }
+    zpool_data.shift
+    zpool_data
+  end
+
+  def current_pool
+    @current_pool = process_zpool_data(get_pool_data) unless (defined?(@current_pool) and @current_pool)
+    @current_pool
+  end
+
+  def flush
+    @current_pool= nil
+  end
+
+  #Adds log and spare
+  def build_named(name)
+    if prop = @resource[name.intern]
+      [name] + prop.collect { |p| p.split(' ') }.flatten
+    else
+      []
+    end
+  end
+
+  #query for parity and set the right string
+  def raidzarity
+    @resource[:raid_parity] ? @resource[:raid_parity] : "raidz1"
+  end
+
+  #handle mirror or raid
+  def handle_multi_arrays(prefix, array)
+    array.collect{ |a| [prefix] +  a.split(' ') }.flatten
+  end
+
+  #builds up the vdevs for create command
+  def build_vdevs
+    if disk = @resource[:disk]
+      disk.collect { |d| d.split(' ') }.flatten
+    elsif mirror = @resource[:mirror]
+      handle_multi_arrays("mirror", mirror)
+    elsif raidz = @resource[:raidz]
+      handle_multi_arrays(raidzarity, raidz)
+    end
+  end
+
+  def create
+    zpool(*([:create, @resource[:pool]] + build_vdevs + build_named("spare") + build_named("log")))
+  end
+
+  def delete
+    zpool :destroy, @resource[:pool]
+  end
+
+  def exists?
+    if current_pool[:pool] == :absent
+      false
+    else
+      true
+    end
+  end
+
+  [:disk, :mirror, :raidz, :log, :spare].each do |field|
+    define_method(field) do
+      current_pool[field]
+    end
+
+    define_method(field.to_s + "=") do |should|
+      Puppet.warning "NO CHANGES BEING MADE: zpool #{field} does not match, should be '#{should}' currently is '#{current_pool[field]}'"
+    end
+  end
+
+end
+
diff --git a/lib/puppet/type/zfs.rb b/lib/puppet/type/zfs.rb
index 0826631..f4faa38 100644
--- a/lib/puppet/type/zfs.rb
+++ b/lib/puppet/type/zfs.rb
@@ -40,6 +40,10 @@ module Puppet
       desc "The copies property. Valid values are `1`, `2`, `3`."
     end
 
+    newproperty(:dedup) do
+      desc "The dedup property. Valid values are `on`, `off`."
+    end
+
     newproperty(:devices) do
       desc "The devices property. Valid values are `on`, `off`."
     end
diff --git a/spec/unit/provider/zfs/solaris_spec.rb b/spec/unit/provider/zfs/solaris_spec.rb
deleted file mode 100755
index 8a0cd23..0000000
--- a/spec/unit/provider/zfs/solaris_spec.rb
+++ /dev/null
@@ -1,99 +0,0 @@
-#!/usr/bin/env rspec
-require 'spec_helper'
-
-provider_class = Puppet::Type.type(:zfs).provider(:solaris)
-
-describe provider_class do
-  before do
-    @resource = stub("resource", :name => "myzfs")
-    @resource.stubs(:[]).with(:name).returns "myzfs"
-    @resource.stubs(:[]).returns "shouldvalue"
-    @provider = provider_class.new(@resource)
-  end
-
-  it "should have a create method" do
-    @provider.should respond_to(:create)
-  end
-
-  it "should have a destroy method" do
-    @provider.should respond_to(:destroy)
-  end
-
-  it "should have an exists? method" do
-    @provider.should respond_to(:exists?)
-  end
-
-  describe "when calling add_properties" do
-    it "should add -o and the key=value for each properties with a value" do
-      @resource.stubs(:[]).with(:quota).returns ""
-      @resource.stubs(:[]).with(:refquota).returns ""
-      @resource.stubs(:[]).with(:mountpoint).returns "/foo"
-      properties = @provider.add_properties
-      properties.include?("-o").should == true
-      properties.include?("mountpoint=/foo").should == true
-      properties.detect { |a| a.include?("quota") }.should == nil
-    end
-  end
-
-  describe "when calling create" do
-    it "should call add_properties" do
-      @provider.stubs(:zfs)
-      @provider.expects(:add_properties).returns([])
-      @provider.create
-    end
-
-    it "should call zfs with create, properties and this zfs" do
-      @provider.stubs(:add_properties).returns(%w{a b})
-      @provider.expects(:zfs).with(:create, "a", "b", @resource[:name])
-      @provider.create
-    end
-  end
-
-  describe "when calling destroy" do
-    it "should call zfs with :destroy and this zfs" do
-      @provider.expects(:zfs).with(:destroy, @resource[:name])
-      @provider.destroy
-    end
-  end
-
-  describe "when calling exist?" do
-    it "should call zfs with :list" do
-      #return stuff because we have to slice and dice it
-      @provider.expects(:zfs).with(:list).returns("NAME USED AVAIL REFER MOUNTPOINT\nmyzfs 100K 27.4M /myzfs")
-      @provider.exists?
-    end
-
-    it "should return true if returned values match the name" do
-      @provider.stubs(:zfs).with(:list).returns("NAME USED AVAIL REFER MOUNTPOINT\n#{@resource[:name]} 100K 27.4M /myzfs")
-      @provider.exists?.should == true
-    end
-
-    it "should return false if returned values don't match the name" do
-      @provider.stubs(:zfs).with(:list).returns("no soup for you")
-      @provider.exists?.should == false
-    end
-
-  end
-
-  [:mountpoint, :recordsize, :aclmode, :aclinherit, :primarycache, :secondarycache, :compression, :copies, :quota, :reservation, :sharenfs, :snapdir].each do |prop|
-    describe "when getting the #{prop} value" do
-      it "should call zfs with :get, #{prop} and this zfs" do
-        @provider.expects(:zfs).with(:get, "-H", "-o", "value", prop, @resource[:name]).returns("value\n")
-        @provider.send(prop)
-      end
-
-      it "should get the third value of the second line from the output" do
-        @provider.stubs(:zfs).with(:get, "-H", "-o", "value", prop, @resource[:name]).returns("value\n")
-        @provider.send(prop).should == "value"
-      end
-    end
-
-    describe "when setting the #{prop} value" do
-      it "should call zfs with :set, #{prop}=value and this zfs" do
-        @provider.expects(:zfs).with(:set, "#{prop}=value", @resource[:name])
-        @provider.send("#{prop}=".intern, "value")
-      end
-    end
-  end
-
-end
diff --git a/spec/unit/provider/zfs/zfs_spec.rb b/spec/unit/provider/zfs/zfs_spec.rb
new file mode 100755
index 0000000..df62a98
--- /dev/null
+++ b/spec/unit/provider/zfs/zfs_spec.rb
@@ -0,0 +1,99 @@
+#!/usr/bin/env rspec
+require 'spec_helper'
+
+provider_class = Puppet::Type.type(:zfs).provider(:zfs)
+
+describe provider_class do
+  before do
+    @resource = stub("resource", :name => "myzfs")
+    @resource.stubs(:[]).with(:name).returns "myzfs"
+    @resource.stubs(:[]).returns "shouldvalue"
+    @provider = provider_class.new(@resource)
+  end
+
+  it "should have a create method" do
+    @provider.should respond_to(:create)
+  end
+
+  it "should have a destroy method" do
+    @provider.should respond_to(:destroy)
+  end
+
+  it "should have an exists? method" do
+    @provider.should respond_to(:exists?)
+  end
+
+  describe "when calling add_properties" do
+    it "should add -o and the key=value for each properties with a value" do
+      @resource.stubs(:[]).with(:quota).returns ""
+      @resource.stubs(:[]).with(:refquota).returns ""
+      @resource.stubs(:[]).with(:mountpoint).returns "/foo"
+      properties = @provider.add_properties
+      properties.include?("-o").should == true
+      properties.include?("mountpoint=/foo").should == true
+      properties.detect { |a| a.include?("quota") }.should == nil
+    end
+  end
+
+  describe "when calling create" do
+    it "should call add_properties" do
+      @provider.stubs(:zfs)
+      @provider.expects(:add_properties).returns([])
+      @provider.create
+    end
+
+    it "should call zfs with create, properties and this zfs" do
+      @provider.stubs(:add_properties).returns(%w{a b})
+      @provider.expects(:zfs).with(:create, "a", "b", @resource[:name])
+      @provider.create
+    end
+  end
+
+  describe "when calling destroy" do
+    it "should call zfs with :destroy and this zfs" do
+      @provider.expects(:zfs).with(:destroy, @resource[:name])
+      @provider.destroy
+    end
+  end
+
+  describe "when calling exist?" do
+    it "should call zfs with :list" do
+      #return stuff because we have to slice and dice it
+      @provider.expects(:zfs).with(:list).returns("NAME USED AVAIL REFER MOUNTPOINT\nmyzfs 100K 27.4M /myzfs")
+      @provider.exists?
+    end
+
+    it "should return true if returned values match the name" do
+      @provider.stubs(:zfs).with(:list).returns("NAME USED AVAIL REFER MOUNTPOINT\n#{@resource[:name]} 100K 27.4M /myzfs")
+      @provider.exists?.should == true
+    end
+
+    it "should return false if returned values don't match the name" do
+      @provider.stubs(:zfs).with(:list).returns("no soup for you")
+      @provider.exists?.should == false
+    end
+
+  end
+
+  [:aclinherit, :aclmode, :atime, :canmount, :checksum, :compression, :copies, :dedup, :devices, :exec, :logbias, :mountpoint, :nbmand, :primarycache, :quota, :readonly, :recordsize, :refquota, :refreservation, :reservation, :secondarycache, :setuid, :shareiscsi, :sharenfs, :sharesmb, :snapdir, :version, :volsize, :vscan, :xattr, :zoned].each do |prop|
+    describe "when getting the #{prop} value" do
+      it "should call zfs with :get, #{prop} and this zfs" do
+        @provider.expects(:zfs).with(:get, "-H", "-o", "value", prop, @resource[:name]).returns("value\n")
+        @provider.send(prop)
+      end
+
+      it "should get the third value of the second line from the output" do
+        @provider.stubs(:zfs).with(:get, "-H", "-o", "value", prop, @resource[:name]).returns("value\n")
+        @provider.send(prop).should == "value"
+      end
+    end
+
+    describe "when setting the #{prop} value" do
+      it "should call zfs with :set, #{prop}=value and this zfs" do
+        @provider.expects(:zfs).with(:set, "#{prop}=value", @resource[:name])
+        @provider.send("#{prop}=".intern, "value")
+      end
+    end
+  end
+
+end
diff --git a/spec/unit/provider/zpool/solaris_spec.rb b/spec/unit/provider/zpool/solaris_spec.rb
deleted file mode 100755
index 39ee9c9..0000000
--- a/spec/unit/provider/zpool/solaris_spec.rb
+++ /dev/null
@@ -1,208 +0,0 @@
-#!/usr/bin/env rspec
-require 'spec_helper'
-
-provider_class = Puppet::Type.type(:zpool).provider(:solaris)
-
-describe provider_class do
-  before do
-    @resource = stub("resource", :name => "mypool")
-    @resource.stubs(:[]).returns "shouldvalue"
-    @provider = provider_class.new(@resource)
-  end
-
-  describe "when getting the instance" do
-    it "should call process_zpool_data with the result of get_pool_data only once" do
-      @provider.stubs(:get_pool_data).returns(["foo", "disk"])
-      @provider.expects(:process_zpool_data).with(["foo", "disk"]).returns("stuff").once
-      @provider.current_pool
-      @provider.current_pool
-    end
-  end
-
-  describe "when calling flush" do
-    it "should need to reload the pool" do
-      @provider.stubs(:get_pool_data)
-      @provider.expects(:process_zpool_data).returns("stuff").times(2)
-      @provider.current_pool
-      @provider.flush
-      @provider.current_pool
-    end
-  end
-
-  describe "when procesing zpool data" do
-    before do
-      @zpool_data = ["foo", "disk"]
-    end
-
-    describe "when there is no data" do
-      it "should return a hash with ensure=>:absent" do
-        @provider.process_zpool_data([])[:ensure].should == :absent
-      end
-    end
-
-    describe "when there is a spare" do
-      it "should add the spare disk to the hash" do
-        @zpool_data += ["spares", "spare_disk"]
-        @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk"]
-      end
-    end
-
-    describe "when there are two spares" do
-      it "should add the spare disk to the hash as a single string" do
-        @zpool_data += ["spares", "spare_disk", "spare_disk2"]
-        @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk spare_disk2"]
-      end
-    end
-
-    describe "when there is a log" do
-      it "should add the log disk to the hash" do
-        @zpool_data += ["logs", "log_disk"]
-        @provider.process_zpool_data(@zpool_data)[:log].should == ["log_disk"]
-      end
-    end
-
-    describe "when there are two logs" do
-      it "should add the log disks to the hash as a single string" do
-        @zpool_data += ["spares", "spare_disk", "spare_disk2"]
-        @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk spare_disk2"]
-      end
-    end
-
-    describe "when the vdev is a single mirror" do
-      it "should call create_multi_array with mirror" do
-        @zpool_data = ["mirrorpool", "mirror", "disk1", "disk2"]
-        @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2"]
-      end
-    end
-
-    describe "when the vdev is a single mirror on solaris 10u9 or later" do
-      it "should call create_multi_array with mirror" do
-        @zpool_data = ["mirrorpool", "mirror-0", "disk1", "disk2"]
-        @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2"]
-      end
-    end
-
-    describe "when the vdev is a double mirror" do
-      it "should call create_multi_array with mirror" do
-        @zpool_data = ["mirrorpool", "mirror", "disk1", "disk2", "mirror", "disk3", "disk4"]
-        @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2", "disk3 disk4"]
-      end
-    end
-
-    describe "when the vdev is a double mirror on solaris 10u9 or later" do
-      it "should call create_multi_array with mirror" do
-        @zpool_data = ["mirrorpool", "mirror-0", "disk1", "disk2", "mirror-1", "disk3", "disk4"]
-        @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2", "disk3 disk4"]
-      end
-    end
-
-    describe "when the vdev is a raidz1" do
-      it "should call create_multi_array with raidz1" do
-        @zpool_data = ["mirrorpool", "raidz1", "disk1", "disk2"]
-        @provider.process_zpool_data(@zpool_data)[:raidz].should == ["disk1 disk2"]
-      end
-    end
-
-    describe "when the vdev is a raidz1 on solaris 10u9 or later" do
-      it "should call create_multi_array with raidz1" do
-        @zpool_data = ["mirrorpool", "raidz1-0", "disk1", "disk2"]
-        @provider.process_zpool_data(@zpool_data)[:raidz].should == ["disk1 disk2"]
-      end
-    end
-
-    describe "when the vdev is a raidz2" do
-      it "should call create_multi_array with raidz2 and set the raid_parity" do
-        @zpool_data = ["mirrorpool", "raidz2", "disk1", "disk2"]
-        pool = @provider.process_zpool_data(@zpool_data)
-        pool[:raidz].should == ["disk1 disk2"]
-        pool[:raid_parity].should == "raidz2"
-      end
-    end
-
-    describe "when the vdev is a raidz2 on solaris 10u9 or later" do
-      it "should call create_multi_array with raidz2 and set the raid_parity" do
-        @zpool_data = ["mirrorpool", "raidz2-0", "disk1", "disk2"]
-        pool = @provider.process_zpool_data(@zpool_data)
-        pool[:raidz].should == ["disk1 disk2"]
-        pool[:raid_parity].should == "raidz2"
-      end
-    end
-  end
-
-  describe "when calling the getters and setters" do
-    [:disk, :mirror, :raidz, :log, :spare].each do |field|
-      describe "when calling #{field}" do
-        it "should get the #{field} value from the current_pool hash" do
-          pool_hash = mock "pool hash"
-          pool_hash.expects(:[]).with(field)
-          @provider.stubs(:current_pool).returns(pool_hash)
-          @provider.send(field)
-        end
-      end
-
-      describe "when setting the #{field}" do
-        it "should warn the #{field} values were not in sync" do
-          Puppet.expects(:warning).with("NO CHANGES BEING MADE: zpool #{field} does not match, should be 'shouldvalue' currently is 'currentvalue'")
-          @provider.stubs(:current_pool).returns(Hash.new("currentvalue"))
-          @provider.send((field.to_s + "=").intern, "shouldvalue")
-        end
-      end
-    end
-  end
-
-  describe "when calling create", :'fails_on_ruby_1.9.2' => true do
-    before do
-      @resource.stubs(:[]).with(:pool).returns("mypool")
-      @provider.stubs(:zpool)
-    end
-
-
-    it "should call build_vdevs" do
-      @provider.expects(:build_vdevs).returns([])
-      @provider.create
-    end
-
-    it "should call build_named with 'spares' and 'log" do
-      @provider.expects(:build_named).with("spare").returns([])
-      @provider.expects(:build_named).with("log").returns([])
-      @provider.create
-    end
-
-    it "should call zpool with arguments from build_vdevs and build_named" do
-      @provider.expects(:zpool).with(:create, 'mypool', 'shouldvalue', 'spare', 'shouldvalue', 'log', 'shouldvalue')
-      @provider.create
-    end
-  end
-
-  describe "when calling delete" do
-    it "should call zpool with destroy and the pool name" do
-      @resource.stubs(:[]).with(:pool).returns("poolname")
-      @provider.expects(:zpool).with(:destroy, "poolname")
-      @provider.delete
-    end
-  end
-
-  describe "when calling exists?" do
-    before do
-      @current_pool = Hash.new(:absent)
-      @provider.stubs(:get_pool_data).returns([])
-      @provider.stubs(:process_zpool_data).returns(@current_pool)
-    end
-
-    it "should get the current pool" do
-      @provider.expects(:process_zpool_data).returns(@current_pool)
-      @provider.exists?
-    end
-
-    it "should return false if the current_pool is absent" do
-      #the before sets it up
-      @provider.exists?.should == false
-    end
-
-    it "should return true if the current_pool has values" do
-      @current_pool[:pool] = "mypool"
-      @provider.exists?.should == true
-    end
-  end
-
-end
diff --git a/spec/unit/provider/zpool/zpool_spec.rb b/spec/unit/provider/zpool/zpool_spec.rb
new file mode 100755
index 0000000..4049bcc
--- /dev/null
+++ b/spec/unit/provider/zpool/zpool_spec.rb
@@ -0,0 +1,208 @@
+#!/usr/bin/env rspec
+require 'spec_helper'
+
+provider_class = Puppet::Type.type(:zpool).provider(:zpool)
+
+describe provider_class do
+  before do
+    @resource = stub("resource", :name => "mypool")
+    @resource.stubs(:[]).returns "shouldvalue"
+    @provider = provider_class.new(@resource)
+  end
+
+  describe "when getting the instance" do
+    it "should call process_zpool_data with the result of get_pool_data only once" do
+      @provider.stubs(:get_pool_data).returns(["foo", "disk"])
+      @provider.expects(:process_zpool_data).with(["foo", "disk"]).returns("stuff").once
+      @provider.current_pool
+      @provider.current_pool
+    end
+  end
+
+  describe "when calling flush" do
+    it "should need to reload the pool" do
+      @provider.stubs(:get_pool_data)
+      @provider.expects(:process_zpool_data).returns("stuff").times(2)
+      @provider.current_pool
+      @provider.flush
+      @provider.current_pool
+    end
+  end
+
+  describe "when procesing zpool data" do
+    before do
+      @zpool_data = ["foo", "disk"]
+    end
+
+    describe "when there is no data" do
+      it "should return a hash with ensure=>:absent" do
+        @provider.process_zpool_data([])[:ensure].should == :absent
+      end
+    end
+
+    describe "when there is a spare" do
+      it "should add the spare disk to the hash" do
+        @zpool_data += ["spares", "spare_disk"]
+        @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk"]
+      end
+    end
+
+    describe "when there are two spares" do
+      it "should add the spare disk to the hash as a single string" do
+        @zpool_data += ["spares", "spare_disk", "spare_disk2"]
+        @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk spare_disk2"]
+      end
+    end
+
+    describe "when there is a log" do
+      it "should add the log disk to the hash" do
+        @zpool_data += ["logs", "log_disk"]
+        @provider.process_zpool_data(@zpool_data)[:log].should == ["log_disk"]
+      end
+    end
+
+    describe "when there are two logs" do
+      it "should add the log disks to the hash as a single string" do
+        @zpool_data += ["spares", "spare_disk", "spare_disk2"]
+        @provider.process_zpool_data(@zpool_data)[:spare].should == ["spare_disk spare_disk2"]
+      end
+    end
+
+    describe "when the vdev is a single mirror" do
+      it "should call create_multi_array with mirror" do
+        @zpool_data = ["mirrorpool", "mirror", "disk1", "disk2"]
+        @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2"]
+      end
+    end
+
+    describe "when the vdev is a single mirror on solaris 10u9 or later" do
+      it "should call create_multi_array with mirror" do
+        @zpool_data = ["mirrorpool", "mirror-0", "disk1", "disk2"]
+        @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2"]
+      end
+    end
+
+    describe "when the vdev is a double mirror" do
+      it "should call create_multi_array with mirror" do
+        @zpool_data = ["mirrorpool", "mirror", "disk1", "disk2", "mirror", "disk3", "disk4"]
+        @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2", "disk3 disk4"]
+      end
+    end
+
+    describe "when the vdev is a double mirror on solaris 10u9 or later" do
+      it "should call create_multi_array with mirror" do
+        @zpool_data = ["mirrorpool", "mirror-0", "disk1", "disk2", "mirror-1", "disk3", "disk4"]
+        @provider.process_zpool_data(@zpool_data)[:mirror].should == ["disk1 disk2", "disk3 disk4"]
+      end
+    end
+
+    describe "when the vdev is a raidz1" do
+      it "should call create_multi_array with raidz1" do
+        @zpool_data = ["mirrorpool", "raidz1", "disk1", "disk2"]
+        @provider.process_zpool_data(@zpool_data)[:raidz].should == ["disk1 disk2"]
+      end
+    end
+
+    describe "when the vdev is a raidz1 on solaris 10u9 or later" do
+      it "should call create_multi_array with raidz1" do
+        @zpool_data = ["mirrorpool", "raidz1-0", "disk1", "disk2"]
+        @provider.process_zpool_data(@zpool_data)[:raidz].should == ["disk1 disk2"]
+      end
+    end
+
+    describe "when the vdev is a raidz2" do
+      it "should call create_multi_array with raidz2 and set the raid_parity" do
+        @zpool_data = ["mirrorpool", "raidz2", "disk1", "disk2"]
+        pool = @provider.process_zpool_data(@zpool_data)
+        pool[:raidz].should == ["disk1 disk2"]
+        pool[:raid_parity].should == "raidz2"
+      end
+    end
+
+    describe "when the vdev is a raidz2 on solaris 10u9 or later" do
+      it "should call create_multi_array with raidz2 and set the raid_parity" do
+        @zpool_data = ["mirrorpool", "raidz2-0", "disk1", "disk2"]
+        pool = @provider.process_zpool_data(@zpool_data)
+        pool[:raidz].should == ["disk1 disk2"]
+        pool[:raid_parity].should == "raidz2"
+      end
+    end
+  end
+
+  describe "when calling the getters and setters" do
+    [:disk, :mirror, :raidz, :log, :spare].each do |field|
+      describe "when calling #{field}" do
+        it "should get the #{field} value from the current_pool hash" do
+          pool_hash = mock "pool hash"
+          pool_hash.expects(:[]).with(field)
+          @provider.stubs(:current_pool).returns(pool_hash)
+          @provider.send(field)
+        end
+      end
+
+      describe "when setting the #{field}" do
+        it "should warn the #{field} values were not in sync" do
+          Puppet.expects(:warning).with("NO CHANGES BEING MADE: zpool #{field} does not match, should be 'shouldvalue' currently is 'currentvalue'")
+          @provider.stubs(:current_pool).returns(Hash.new("currentvalue"))
+          @provider.send((field.to_s + "=").intern, "shouldvalue")
+        end
+      end
+    end
+  end
+
+  describe "when calling create", :'fails_on_ruby_1.9.2' => true do
+    before do
+      @resource.stubs(:[]).with(:pool).returns("mypool")
+      @provider.stubs(:zpool)
+    end
+
+
+    it "should call build_vdevs" do
+      @provider.expects(:build_vdevs).returns([])
+      @provider.create
+    end
+
+    it "should call build_named with 'spares' and 'log" do
+      @provider.expects(:build_named).with("spare").returns([])
+      @provider.expects(:build_named).with("log").returns([])
+      @provider.create
+    end
+
+    it "should call zpool with arguments from build_vdevs and build_named" do
+      @provider.expects(:zpool).with(:create, 'mypool', 'shouldvalue', 'spare', 'shouldvalue', 'log', 'shouldvalue')
+      @provider.create
+    end
+  end
+
+  describe "when calling delete" do
+    it "should call zpool with destroy and the pool name" do
+      @resource.stubs(:[]).with(:pool).returns("poolname")
+      @provider.expects(:zpool).with(:destroy, "poolname")
+      @provider.delete
+    end
+  end
+
+  describe "when calling exists?" do
+    before do
+      @current_pool = Hash.new(:absent)
+      @provider.stubs(:get_pool_data).returns([])
+      @provider.stubs(:process_zpool_data).returns(@current_pool)
+    end
+
+    it "should get the current pool" do
+      @provider.expects(:process_zpool_data).returns(@current_pool)
+      @provider.exists?
+    end
+
+    it "should return false if the current_pool is absent" do
+      #the before sets it up
+      @provider.exists?.should == false
+    end
+
+    it "should return true if the current_pool has values" do
+      @current_pool[:pool] = "mypool"
+      @provider.exists?.should == true
+    end
+  end
+
+end
diff --git a/spec/unit/type/zfs_spec.rb b/spec/unit/type/zfs_spec.rb
index e53c25e..6b43df0 100755
--- a/spec/unit/type/zfs_spec.rb
+++ b/spec/unit/type/zfs_spec.rb
@@ -20,12 +20,14 @@
     end
   end
 
-  it "should autorequire the containing zfss and the zpool" do
-    provider = mock "provider"
-    provider.stubs(:name).returns(:solaris)
-    zfs.stubs(:defaultprovider).returns(provider)
-    Puppet::Type.type(:zpool).stubs(:defaultprovider).returns(provider)
+  it "should autorequire the containing zfs and the zpool" do
+    zfs_provider = mock "provider"
+    zfs_provider.stubs(:name).returns(:zfs)
+    zfs.stubs(:defaultprovider).returns(zfs_provider)
 
+    zpool_provider = mock "provider"
+    zpool_provider.stubs(:name).returns(:zpool)
+    Puppet::Type.type(:zpool).stubs(:defaultprovider).returns(zpool_provider)
 
     foo_pool = Puppet::Type.type(:zpool).new(:name => "foo")
 
@@ -41,4 +43,8 @@
 
     [foo_pool.ref, foo_bar_zfs.ref, foo_bar_baz_zfs.ref].each { |ref| req.include?(ref).should == true }
   end
+
+  it "should select the zfs provider" do
+    zfs.new(:name => 'foo').provider.class.should be(Puppet::Type::Zfs::ProviderZfs)
+  end
 end
diff --git a/spec/unit/type/zpool_spec.rb b/spec/unit/type/zpool_spec.rb
index 9f18000..1b09f9e 100755
--- a/spec/unit/type/zpool_spec.rb
+++ b/spec/unit/type/zpool_spec.rb
@@ -24,6 +24,11 @@
       zpool.attrclass(parameter).ancestors.should be_include(Puppet::Parameter)
     end
   end
+
+  it "should select the zpool provider" do
+    zpool.new(:name => 'foo').provider.class.should be(Puppet::Type::Zpool::ProviderZpool)
+  end
+
 end
 
 vdev_property = Puppet::Property::VDev

    

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