File: zfs_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (171 lines) | stat: -rw-r--r-- 5,454 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require 'spec_helper'

describe Puppet::Type.type(:zfs).provider(:zfs) do
  let(:name) { 'myzfs' }
  let(:zfs) { '/usr/sbin/zfs' }

  let(:resource) do
    Puppet::Type.type(:zfs).new(:name => name, :provider => :zfs)
  end

  let(:provider) { resource.provider }

  before do
    allow(provider.class).to receive(:which).with('zfs').and_return(zfs)
  end

  context ".instances" do
    it "should have an instances method" do
      expect(provider.class).to respond_to(:instances)
    end

    it "should list instances" do
      expect(provider.class).to receive(:zfs).with(:list,'-H').and_return(File.read(my_fixture('zfs-list.out')))
      instances = provider.class.instances.map { |p| {:name => p.get(:name), :ensure => p.get(:ensure)} }
      expect(instances.size).to eq(2)
      expect(instances[0]).to eq({:name => 'rpool', :ensure => :present})
      expect(instances[1]).to eq({:name => 'rpool/ROOT', :ensure => :present})
    end
  end

  context '#add_properties' do
    it 'should return an array of properties' do
      resource[:mountpoint] = '/foo'

      expect(provider.add_properties).to eq(['-o', "mountpoint=/foo"])
    end

    it 'should return an empty array' do
      expect(provider.add_properties).to eq([])
    end
  end

  context "#create" do
    it "should execute zfs create" do
      expect(provider).to receive(:zfs).with(:create, name)

      provider.create
    end

    Puppet::Type.type(:zfs).validproperties.each do |prop|
      next if [:ensure, :volsize].include?(prop)
      it "should include property #{prop}" do
        resource[prop] = prop

        expect(provider).to receive(:zfs).with(:create, '-o', "#{prop}=#{prop}", name)

        provider.create
      end
    end

    it "should use -V for the volsize property" do
      resource[:volsize] = "10"
      expect(provider).to receive(:zfs).with(:create, '-V', "10", name)
      provider.create
    end
  end

  context "#destroy" do
    it "should execute zfs destroy" do
      expect(provider).to receive(:zfs).with(:destroy, name)

      provider.destroy
    end
  end

  context "#exists?" do
    it "should return true if the resource exists" do
      #return stuff because we have to slice and dice it
      expect(provider).to receive(:zfs).with(:list, name)

      expect(provider).to be_exists
    end

    it "should return false if returned values don't match the name" do
      expect(provider).to receive(:zfs).with(:list, name).and_raise(Puppet::ExecutionFailure, "Failed")

      expect(provider).not_to be_exists
    end
  end

  describe "zfs properties" do
    [: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].each do |prop|
      it "should get #{prop}" do
        expect(provider).to receive(:zfs).with(:get, '-H', '-o', 'value', prop, name).and_return("value\n")

        expect(provider.send(prop)).to eq('value')
      end

      it "should set #{prop}=value" do
        expect(provider).to receive(:zfs).with(:set, "#{prop}=value", name)

        provider.send("#{prop}=", "value")
      end
    end
  end

  describe "zoned" do
    context "on FreeBSD" do
      before do
        allow(Facter).to receive(:value).with(:operatingsystem).and_return("FreeBSD")
      end

      it "should get 'jailed' property" do
        expect(provider).to receive(:zfs).with(:get, '-H', '-o', 'value', :jailed, name).and_return("value\n")
        expect(provider.send("zoned")).to eq('value')
      end

      it "should set jalied=value" do
        expect(provider).to receive(:zfs).with(:set, "jailed=value", name)
        provider.send("zoned=", "value")
      end
    end

    context "when not running FreeBSD" do
      before do
        allow(Facter).to receive(:value).with(:operatingsystem).and_return("Solaris")
      end

      it "should get 'zoned' property" do
        expect(provider).to receive(:zfs).with(:get, '-H', '-o', 'value', :zoned, name).and_return("value\n")
        expect(provider.send("zoned")).to eq('value')
      end

      it "should set zoned=value" do
        expect(provider).to receive(:zfs).with(:set, "zoned=value", name)
        provider.send("zoned=", "value")
      end
    end
  end

  describe "acltype" do
    context "when available" do
      it "should get 'acltype' property" do
        expect(provider).to receive(:zfs).with(:get, '-H', '-o', 'value', :acltype, name).and_return("value\n")
        expect(provider.send("acltype")).to eq('value')
      end

      it "should set acltype=value" do
        expect(provider).to receive(:zfs).with(:set, "acltype=value", name)
        provider.send("acltype=", "value")
      end
    end

    context "when not available" do
      it "should get '-' for the acltype property" do
        expect(provider).to receive(:zfs).with(:get, '-H', '-o', 'value', :acltype, name).and_raise(RuntimeError, 'not valid')
        expect(provider.send("acltype")).to eq('-')
      end

      it "should not error out when trying to set acltype" do
        expect(provider).to receive(:zfs).with(:set, "acltype=value", name).and_raise(RuntimeError, 'not valid')
        expect{provider.send("acltype=", "value")}.to_not raise_error
      end
    end
  end
end