summaryrefslogtreecommitdiffstats
path: root/spec/unit/provider/selmodule_spec.rb
blob: 9ac5a7a5d0817dace36620ff107ff92d62e10f21 (plain)
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
#!/usr/bin/env ruby

# Note: This unit test depends on having a sample SELinux policy file
# in the same directory as this test called selmodule-example.pp
# with version 1.5.0.  The provided selmodule-example.pp is the first
# 256 bytes taken from /usr/share/selinux/targeted/nagios.pp on Fedora 9

require 'spec_helper'

provider_class = Puppet::Type.type(:selmodule).provider(:semodule)

describe provider_class do
  before :each do
    @resource = stub("resource", :name => "foo")
    @resource.stubs(:[]).returns "foo"
    @provider = provider_class.new(@resource)
  end

  describe "exists? method" do
    it "should find a module if it is already loaded" do
      @provider.expects(:command).with(:semodule).returns "/usr/sbin/semodule"
      @provider.expects(:execpipe).with("/usr/sbin/semodule --list").yields ["bar\t1.2.3\n", "foo\t4.4.4\n", "bang\t1.0.0\n"]
      @provider.exists?.should == :true
    end

    it "should return nil if not loaded" do
      @provider.expects(:command).with(:semodule).returns "/usr/sbin/semodule"
      @provider.expects(:execpipe).with("/usr/sbin/semodule --list").yields ["bar\t1.2.3\n", "bang\t1.0.0\n"]
      @provider.exists?.should be_nil
    end

    it "should return nil if no modules are loaded" do
      @provider.expects(:command).with(:semodule).returns "/usr/sbin/semodule"
      @provider.expects(:execpipe).with("/usr/sbin/semodule --list").yields []
      @provider.exists?.should be_nil
    end
  end

  describe "selmodversion_file" do
    it "should return 1.5.0 for the example policy file" do
      @provider.expects(:selmod_name_to_filename).returns "#{File.dirname(__FILE__)}/selmodule-example.pp"
      @provider.selmodversion_file.should == "1.5.0"
    end
  end

  describe "syncversion" do
    it "should return :true if loaded and file modules are in sync" do
      @provider.expects(:selmodversion_loaded).returns "1.5.0"
      @provider.expects(:selmodversion_file).returns "1.5.0"
      @provider.syncversion.should == :true
    end

    it "should return :false if loaded and file modules are not in sync" do
      @provider.expects(:selmodversion_loaded).returns "1.4.0"
      @provider.expects(:selmodversion_file).returns "1.5.0"
      @provider.syncversion.should == :false
    end

    it "should return before checking file version if no loaded policy" do
      @provider.expects(:selmodversion_loaded).returns nil
      @provider.syncversion.should == :false
    end

  end

end