Skip to content

Commit f62b162

Browse files
committed
(PUP-7559) Use symbols for desired ensure parameter
Puppet uses symbols to represent the desired ensure parameter, so just use that. Also eliminated the `when` block for `absent`.
1 parent fe9b634 commit f62b162

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

lib/puppet/type/file/selcontext.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def retrieve_default_context(property)
4242
return nil
4343
end
4444

45-
context = self.get_selinux_default_context(@resource[:path], @resource[:ensure].to_s)
45+
context = self.get_selinux_default_context(@resource[:path], @resource[:ensure])
4646
unless context
4747
return nil
4848
end

lib/puppet/util/selinux.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,13 @@ def selinux_label_support?(file)
213213
# what context a new resource being created should have.
214214
def get_create_mode(resource_ensure)
215215
mode = 0
216-
return mode if resource_ensure == 'absent'
217216
case resource_ensure
218-
when "present", "file"
219-
mode = 0 | S_IFREG
220-
when "directory"
221-
mode = 0 | S_IFDIR
222-
when "link"
223-
mode = 0 | S_IFLNK
217+
when :present, :file
218+
mode |= S_IFREG
219+
when :directory
220+
mode |= S_IFDIR
221+
when :link
222+
mode |= S_IFLNK
224223
end
225224
mode
226225
end

spec/unit/type/file/selinux_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
before do
99
@path = make_absolute("/my/file")
10-
@resource = Puppet::Type.type(:file).new :path => @path
10+
@resource = Puppet::Type.type(:file).new(:path => @path, :ensure => :file)
1111
@sel = property.new :resource => @resource
1212
end
1313

@@ -50,13 +50,13 @@
5050
end
5151

5252
it "should handle no default gracefully" do
53-
expect(@sel).to receive(:get_selinux_default_context).with(@path, @resource[:ensure].to_s).and_return(nil)
53+
expect(@sel).to receive(:get_selinux_default_context).with(@path, :file).and_return(nil)
5454
expect(@sel.default).to be_nil
5555
end
5656

5757
it "should be able to detect matchpathcon defaults" do
5858
allow(@sel).to receive(:debug)
59-
expect(@sel).to receive(:get_selinux_default_context).with(@path, @resource[:ensure].to_s).and_return("user_u:role_r:type_t:s0")
59+
expect(@sel).to receive(:get_selinux_default_context).with(@path, :file).and_return("user_u:role_r:type_t:s0")
6060
expectedresult = case param
6161
when :seluser; "user_u"
6262
when :selrole; "role_r"

spec/unit/util/selinux_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@
177177
allow(Selinux).to receive(:matchpathcon).with("/root/chuj", 32768).and_return(-1)
178178
allow(self).to receive(:file_lstat).with("/root/chuj").and_raise(Errno::ENOENT, "/root/chuj")
179179

180-
expect(get_selinux_default_context("/root/chuj", "present")).to be_nil
181-
expect(get_selinux_default_context("/root/chuj", "file")).to be_nil
180+
expect(get_selinux_default_context("/root/chuj", :present)).to be_nil
181+
expect(get_selinux_default_context("/root/chuj", :file)).to be_nil
182182
end
183183
end
184184

@@ -189,7 +189,7 @@
189189
allow(Selinux).to receive(:matchpathcon).with("/root/chuj", 16384).and_return(-1)
190190
allow(self).to receive(:file_lstat).with("/root/chuj").and_raise(Errno::ENOENT, "/root/chuj")
191191

192-
expect(get_selinux_default_context("/root/chuj", "directory")).to be_nil
192+
expect(get_selinux_default_context("/root/chuj", :directory)).to be_nil
193193
end
194194
end
195195

@@ -200,7 +200,7 @@
200200
allow(Selinux).to receive(:matchpathcon).with("/root/chuj", 40960).and_return(-1)
201201
allow(self).to receive(:file_lstat).with("/root/chuj").and_raise(Errno::ENOENT, "/root/chuj")
202202

203-
expect(get_selinux_default_context("/root/chuj", "link")).to be_nil
203+
expect(get_selinux_default_context("/root/chuj", :link)).to be_nil
204204
end
205205
end
206206

@@ -394,20 +394,20 @@
394394

395395
describe "get_create_mode" do
396396
it "should return 0 if the resource is absent" do
397-
expect(get_create_mode("absent")).to eq(0)
397+
expect(get_create_mode(:absent)).to eq(0)
398398
end
399399

400400
it "should return mode with file type set to S_IFREG when resource is file" do
401-
expect(get_create_mode("present")).to eq(32768)
402-
expect(get_create_mode("file")).to eq(32768)
401+
expect(get_create_mode(:present)).to eq(32768)
402+
expect(get_create_mode(:file)).to eq(32768)
403403
end
404404

405405
it "should return mode with file type set to S_IFDIR when resource is dir" do
406-
expect(get_create_mode("directory")).to eq(16384)
406+
expect(get_create_mode(:directory)).to eq(16384)
407407
end
408408

409409
it "should return mode with file type set to S_IFLNK when resource is link" do
410-
expect(get_create_mode("link")).to eq(40960)
410+
expect(get_create_mode(:link)).to eq(40960)
411411
end
412412

413413
it "should return 0 for everything else" do

0 commit comments

Comments
 (0)