File: debian_spec.rb

package info (click to toggle)
puppet 5.5.10-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,116 kB
  • sloc: ruby: 250,669; sh: 1,620; xml: 218; makefile: 151; sql: 103
file content (154 lines) | stat: -rw-r--r-- 5,410 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
require 'spec_helper'

describe Puppet::Type.type(:service).provider(:debian) do
  if Puppet.features.microsoft_windows?
    # Get a pid for $CHILD_STATUS to latch on to
    command = "cmd.exe /c \"exit 0\""
    Puppet::Util::Execution.execute(command, {:failonfail => false})
  end

  before(:each) do
    # Create a mock resource
    @resource = stub 'resource'

    @provider = subject()

    # A catch all; no parameters set
    @resource.stubs(:[]).returns(nil)

    # But set name, source and path
    @resource.stubs(:[]).with(:name).returns "myservice"
    @resource.stubs(:[]).with(:ensure).returns :enabled
    @resource.stubs(:ref).returns "Service[myservice]"

    @provider.resource = @resource

    @provider.stubs(:command).with(:update_rc).returns "update_rc"
    @provider.stubs(:command).with(:invoke_rc).returns "invoke_rc"
    @provider.stubs(:command).with(:service).returns "service"

    @provider.stubs(:update_rc)
    @provider.stubs(:invoke_rc)
  end

  ['1','2'].each do |version|
    it "should be the default provider on CumulusLinux #{version}" do
      Facter.expects(:value).with(:operatingsystem).at_least_once.returns('CumulusLinux')
      Facter.expects(:value).with(:operatingsystemmajrelease).returns(version)
      expect(described_class.default?).to be_truthy
    end
  end

  it "should be the default provider on Debian" do
    Facter.expects(:value).with(:operatingsystem).at_least_once.returns('Debian')
    Facter.expects(:value).with(:operatingsystemmajrelease).returns('7')
    expect(described_class.default?).to be_truthy
  end

  it "should have an enabled? method" do
    expect(@provider).to respond_to(:enabled?)
  end

  it "should have an enable method" do
    expect(@provider).to respond_to(:enable)
  end

  it "should have a disable method" do
    expect(@provider).to respond_to(:disable)
  end

  context "when enabling" do
    it "should call update-rc.d twice" do
      @provider.expects(:update_rc).twice
      @provider.enable
    end
  end

  context "when disabling" do
    it "should be able to disable services with newer sysv-rc versions" do
      @provider.stubs(:`).with("dpkg --compare-versions $(dpkg-query -W --showformat '${Version}' sysv-rc) ge 2.88 ; echo $?").returns "0"

      @provider.expects(:update_rc).with(@resource[:name], "disable")

      @provider.disable
    end

    it "should be able to enable services with older sysv-rc versions" do
      @provider.stubs(:`).with("dpkg --compare-versions $(dpkg-query -W --showformat '${Version}' sysv-rc) ge 2.88 ; echo $?").returns "1"

      @provider.expects(:update_rc).with("-f", @resource[:name], "remove")
      @provider.expects(:update_rc).with(@resource[:name], "stop", "00", "1", "2", "3", "4", "5", "6", ".")

      @provider.disable
    end
  end

  context "when checking whether it is enabled" do
    it "should call Kernel.system() with the appropriate parameters" do
      @provider.expects(:system).with("/usr/sbin/invoke-rc.d", "--quiet", "--query", @resource[:name], "start").once
      $CHILD_STATUS.stubs(:exitstatus).returns(0)
      @provider.enabled?
    end

    it "should return true when invoke-rc.d exits with 104 status" do
      @provider.stubs(:system)
      $CHILD_STATUS.stubs(:exitstatus).returns(104)
      expect(@provider.enabled?).to eq(:true)
    end

    it "should return true when invoke-rc.d exits with 106 status" do
      @provider.stubs(:system)
      $CHILD_STATUS.stubs(:exitstatus).returns(106)
      expect(@provider.enabled?).to eq(:true)
    end

    shared_examples "manually queries service status" do |status|
      it "links count is 4" do
        @provider.stubs(:system)
        $CHILD_STATUS.stubs(:exitstatus).returns(status)
        @provider.stubs(:get_start_link_count).returns(4)
        expect(@provider.enabled?).to eq(:true)
      end
      it "links count is less than 4" do
        @provider.stubs(:system)
        $CHILD_STATUS.stubs(:exitstatus).returns(status)
        @provider.stubs(:get_start_link_count).returns(3)
        expect(@provider.enabled?).to eq(:false)
      end
    end

    context "when invoke-rc.d exits with 101 status" do
      it_should_behave_like "manually queries service status", 101
    end

    context "when invoke-rc.d exits with 105 status" do
      it_should_behave_like "manually queries service status", 105
    end

    context "when invoke-rc.d exits with 101 status" do
      it_should_behave_like "manually queries service status", 101
    end

    context "when invoke-rc.d exits with 105 status" do
      it_should_behave_like "manually queries service status", 105
    end

    # pick a range of non-[104.106] numbers, strings and booleans to test with.
    [-100, -1, 0, 1, 100, "foo", "", :true, :false].each do |exitstatus|
      it "should return false when invoke-rc.d exits with #{exitstatus} status" do
        @provider.stubs(:system)
        $CHILD_STATUS.stubs(:exitstatus).returns(exitstatus)
        expect(@provider.enabled?).to eq(:false)
      end
    end
  end

  context "when checking service status" do
    it "should use the service command" do
      Facter.stubs(:value).with(:operatingsystem).returns('Debian')
      Facter.stubs(:value).with(:operatingsystemmajrelease).returns('8')
      @resource.stubs(:[]).with(:hasstatus).returns(:true)
      expect(@provider.statuscmd).to eq(["service", @resource[:name], "status"])
    end
  end
end