Skip to content

Commit a3246a6

Browse files
committed
(PUP-11022) Use stub_const/hide_const to define Selinux
Fix order dependent test failures when running spec/unit/util/selinux_spec.rb followed by spec/unit/transaction_spec.r:b Failure/Error: expect(Selinux).to receive(:matchpathcon_fini) Selinux does not implement: matchpathcon_fini stub_const/hide_const automatically restore the previous value when the test completes.
1 parent adb4662 commit a3246a6

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

spec/unit/transaction_spec.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -793,16 +793,8 @@ def post_resource_eval
793793
end
794794

795795
it "should call Selinux.matchpathcon_fini in case Selinux is enabled ", :if => Puppet.features.posix? do
796-
unless defined?(Selinux)
797-
module Selinux
798-
def self.is_selinux_enabled
799-
true
800-
end
801-
802-
def self.matchpathcon_fini
803-
end
804-
end
805-
end
796+
selinux = double('selinux', is_selinux_enabled: true, matchpathcon_fini: nil)
797+
stub_const('Selinux', selinux)
806798

807799
resource = Puppet::Type.type(:file).new(:path => make_absolute("/tmp/foo"))
808800
transaction = transaction_with_resource(resource)

spec/unit/util/selinux_spec.rb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,29 @@
33
require 'pathname'
44
require 'puppet/util/selinux'
55

6-
unless defined?(Selinux)
7-
module Selinux
8-
def self.is_selinux_enabled
9-
false
10-
end
11-
end
12-
end
13-
146
describe Puppet::Util::SELinux do
157
include Puppet::Util::SELinux
168

9+
let(:selinux) { double('selinux', is_selinux_enabled: false) }
10+
11+
before :each do
12+
stub_const('Selinux', selinux)
13+
end
14+
1715
describe "selinux_support?" do
18-
it "should return :true if this system has SELinux enabled" do
16+
it "should return true if this system has SELinux enabled" do
1917
expect(Selinux).to receive(:is_selinux_enabled).and_return(1)
20-
expect(selinux_support?).to be_truthy
18+
expect(selinux_support?).to eq(true)
2119
end
2220

23-
it "should return :false if this system lacks SELinux" do
21+
it "should return false if this system has SELinux disabled" do
2422
expect(Selinux).to receive(:is_selinux_enabled).and_return(0)
25-
expect(selinux_support?).to be_falsey
23+
expect(selinux_support?).to eq(false)
24+
end
25+
26+
it "should return false if this system lacks SELinux" do
27+
hide_const('Selinux')
28+
expect(selinux_support?).to eq(false)
2629
end
2730

2831
it "should return nil if /proc/mounts does not exist" do

0 commit comments

Comments
 (0)