File: posix_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 (230 lines) | stat: -rw-r--r-- 7,173 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
require 'spec_helper'

describe Puppet::Type.type(:file).provider(:posix), :if => Puppet.features.posix? do
  include PuppetSpec::Files

  let(:path) { tmpfile('posix_file_spec') }
  let(:resource) { Puppet::Type.type(:file).new :path => path, :mode => '0777', :provider => described_class.name }
  let(:provider) { resource.provider }

  describe "#mode" do
    it "should return a string with the higher-order bits stripped away" do
      FileUtils.touch(path)
      File.chmod(0644, path)

      expect(provider.mode).to eq('0644')
    end

    it "should return absent if the file doesn't exist" do
      expect(provider.mode).to eq(:absent)
    end
  end

  describe "#mode=" do
    it "should chmod the file to the specified value" do
      FileUtils.touch(path)
      File.chmod(0644, path)

      provider.mode = '0755'

      expect(provider.mode).to eq('0755')
    end

    it "should pass along any errors encountered" do
      expect do
        provider.mode = '0644'
      end.to raise_error(Puppet::Error, /failed to set mode/)
    end
  end

  describe "#uid2name" do
    it "should return the name of the user identified by the id" do
      allow(Etc).to receive(:getpwuid).with(501).and_return(Struct::Passwd.new('jilluser', nil, 501))

      expect(provider.uid2name(501)).to eq('jilluser')
    end

    it "should return the argument if it's already a name" do
      expect(provider.uid2name('jilluser')).to eq('jilluser')
    end

    it "should return nil if the argument is above the maximum uid" do
      expect(provider.uid2name(Puppet[:maximum_uid] + 1)).to eq(nil)
    end

    it "should return nil if the user doesn't exist" do
      expect(Etc).to receive(:getpwuid).and_raise(ArgumentError, "can't find user for 999")

      expect(provider.uid2name(999)).to eq(nil)
    end
  end

  describe "#name2uid" do
    it "should return the id of the user if it exists" do
      passwd = Struct::Passwd.new('bobbo', nil, 502)

      allow(Etc).to receive(:getpwnam).with('bobbo').and_return(passwd)
      allow(Etc).to receive(:getpwuid).with(502).and_return(passwd)

      expect(provider.name2uid('bobbo')).to eq(502)
    end

    it "should return the argument if it's already an id" do
      expect(provider.name2uid('503')).to eq(503)
    end

    it "should return false if the user doesn't exist" do
      allow(Etc).to receive(:getpwnam).with('chuck').and_raise(ArgumentError, "can't find user for chuck")

      expect(provider.name2uid('chuck')).to eq(false)
    end
  end

  describe "#owner" do
    it "should return the uid of the file owner" do
      FileUtils.touch(path)
      owner = Puppet::FileSystem.stat(path).uid

      expect(provider.owner).to eq(owner)
    end

    it "should return absent if the file can't be statted" do
      expect(provider.owner).to eq(:absent)
    end

    it "should warn and return :silly if the value is beyond the maximum uid" do
      stat = double('stat', :uid => Puppet[:maximum_uid] + 1)
      allow(resource).to receive(:stat).and_return(stat)

      expect(provider.owner).to eq(:silly)
      expect(@logs).to be_any {|log| log.level == :warning and log.message =~ /Apparently using negative UID/}
    end
  end

  describe "#owner=" do
    it "should set the owner but not the group of the file" do
      expect(File).to receive(:lchown).with(15, nil, resource[:path])

      provider.owner = 15
    end

    it "should chown a link if managing links" do
      resource[:links] = :manage
      expect(File).to receive(:lchown).with(20, nil, resource[:path])

      provider.owner = 20
    end

    it "should chown a link target if following links" do
      resource[:links] = :follow
      expect(File).to receive(:chown).with(20, nil, resource[:path])

      provider.owner = 20
    end

    it "should pass along any error encountered setting the owner" do
      expect(File).to receive(:lchown).and_raise(ArgumentError)

      expect { provider.owner = 25 }.to raise_error(Puppet::Error, /Failed to set owner to '25'/)
    end
  end

  describe "#gid2name" do
    it "should return the name of the group identified by the id" do
      allow(Etc).to receive(:getgrgid).with(501).and_return(Struct::Passwd.new('unicorns', nil, nil, 501))

      expect(provider.gid2name(501)).to eq('unicorns')
    end

    it "should return the argument if it's already a name" do
      expect(provider.gid2name('leprechauns')).to eq('leprechauns')
    end

    it "should return nil if the argument is above the maximum gid" do
      expect(provider.gid2name(Puppet[:maximum_uid] + 1)).to eq(nil)
    end

    it "should return nil if the group doesn't exist" do
      expect(Etc).to receive(:getgrgid).and_raise(ArgumentError, "can't find group for 999")

      expect(provider.gid2name(999)).to eq(nil)
    end
  end

  describe "#name2gid" do
    it "should return the id of the group if it exists" do
      passwd = Struct::Passwd.new('penguins', nil, nil, 502)

      allow(Etc).to receive(:getgrnam).with('penguins').and_return(passwd)
      allow(Etc).to receive(:getgrgid).with(502).and_return(passwd)

      expect(provider.name2gid('penguins')).to eq(502)
    end

    it "should return the argument if it's already an id" do
      expect(provider.name2gid('503')).to eq(503)
    end

    it "should return false if the group doesn't exist" do
      allow(Etc).to receive(:getgrnam).with('wombats').and_raise(ArgumentError, "can't find group for wombats")

      expect(provider.name2gid('wombats')).to eq(false)
    end

  end

  describe "#group" do
    it "should return the gid of the file group" do
      FileUtils.touch(path)
      group = Puppet::FileSystem.stat(path).gid

      expect(provider.group).to eq(group)
    end

    it "should return absent if the file can't be statted" do
      expect(provider.group).to eq(:absent)
    end

    it "should warn and return :silly if the value is beyond the maximum gid" do
      stat = double('stat', :gid => Puppet[:maximum_uid] + 1)
      allow(resource).to receive(:stat).and_return(stat)

      expect(provider.group).to eq(:silly)
      expect(@logs).to be_any {|log| log.level == :warning and log.message =~ /Apparently using negative GID/}
    end
  end

  describe "#group=" do
    it "should set the group but not the owner of the file" do
      expect(File).to receive(:lchown).with(nil, 15, resource[:path])

      provider.group = 15
    end

    it "should change the group for a link if managing links" do
      resource[:links] = :manage
      expect(File).to receive(:lchown).with(nil, 20, resource[:path])

      provider.group = 20
    end

    it "should change the group for a link target if following links" do
      resource[:links] = :follow
      expect(File).to receive(:chown).with(nil, 20, resource[:path])

      provider.group = 20
    end

    it "should pass along any error encountered setting the group" do
      expect(File).to receive(:lchown).and_raise(ArgumentError)

      expect { provider.group = 25 }.to raise_error(Puppet::Error, /Failed to set group to '25'/)
    end
  end

  describe "when validating" do
    it "should not perform any validation" do
      resource.validate
    end
  end
end