summaryrefslogtreecommitdiff
path: root/spec/unit/settings/autosign_setting_spec.rb
blob: 0dbfe4ecb42fd67414659ffdd794dbbdf01e1519 (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
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
require 'spec_helper'

require 'puppet/settings'
require 'puppet/settings/autosign_setting'

describe Puppet::Settings::AutosignSetting do
  let(:settings) do
    s = stub('settings')
    s.stubs(:[]).with(:mkusers).returns true
    s.stubs(:[]).with(:user).returns 'puppet'
    s.stubs(:[]).with(:group).returns 'puppet'
    s.stubs(:[]).with(:manage_internal_file_permissions).returns true
    s
  end

  let(:setting) { described_class.new(:name => 'autosign', :section => 'section', :settings => settings, :desc => "test") }

  it "is of type :file" do
    expect(setting.type).to eq :file
  end

  describe "when munging the setting" do
    it "passes boolean values through" do
      expect(setting.munge(true)).to eq true
      expect(setting.munge(false)).to eq false
    end

    it "converts nil to false" do
      expect(setting.munge(nil)).to eq false
    end

    it "munges string 'true' to boolean true" do
      expect(setting.munge('true')).to eq true
    end

    it "munges string 'false' to boolean false" do
      expect(setting.munge('false')).to eq false
    end

    it "passes absolute paths through" do
      path = File.expand_path('/path/to/autosign.conf')
      expect(setting.munge(path)).to eq path
    end

    it "fails if given anything else" do
      cases = [1.0, 'sometimes', 'relative/autosign.conf']

      cases.each do |invalid|
        expect {
          setting.munge(invalid)
        }.to raise_error Puppet::Settings::ValidationError, /Invalid autosign value/
      end
    end
  end

  describe "setting additional setting values" do
    it "can set the file mode" do
      setting.mode = '0664'
      expect(setting.mode).to eq '0664'
    end

    it "can set the file owner" do
      setting.owner = 'service'
      expect(setting.owner).to eq 'puppet'
    end

    it "can set the file group" do
      setting.group = 'service'
      expect(setting.group).to eq 'puppet'
    end
  end

  describe "converting the setting to a resource" do
    it "converts the file path to a file resource" do
      path = File.expand_path('/path/to/autosign.conf')
      settings.stubs(:value).with('autosign', nil, false).returns(path)
      Puppet::FileSystem.stubs(:exist?).with(path).returns true
      Puppet.stubs(:features).returns(stub(:root? => true, :microsoft_windows? => false))

      setting.mode = '0664'
      setting.owner = 'service'
      setting.group = 'service'

      resource = setting.to_resource

      expect(resource.title).to eq path
      expect(resource[:ensure]).to eq :file
      expect(resource[:mode]).to eq '664'
      expect(resource[:owner]).to eq 'puppet'
      expect(resource[:group]).to eq 'puppet'
    end

    it "returns nil when the setting is a boolean" do
      settings.stubs(:value).with('autosign', nil, false).returns 'true'

      setting.mode = '0664'
      setting.owner = 'service'
      setting.group = 'service'

      expect(setting.to_resource).to be_nil
    end
  end
end