File: init_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (316 lines) | stat: -rw-r--r-- 12,347 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
require 'spec_helper'

describe 'Puppet::Type::Service::Provider::Init',
         unless: Puppet::Util::Platform.windows? || Puppet::Util::Platform.jruby? do
  let(:provider_class) { Puppet::Type.type(:service).provider(:init) }

  before do
    Puppet::Type.type(:service).defaultprovider = provider_class
  end

  after do
    Puppet::Type.type(:service).defaultprovider = nil
  end

  let :provider do
    resource.provider
  end

  let :resource do
    Puppet::Type.type(:service).new(
      :name     => 'myservice',
      :ensure   => :running,
      :path     => paths
    )
  end

  let :paths do
    ["/service/path","/alt/service/path"]
  end

  let :excludes do
    # Taken from redhat, gentoo, and debian
    %w{functions.sh reboot.sh shutdown.sh functions halt killall single linuxconf reboot boot wait-for-state rcS module-init-tools}
  end

  let :process_output do
    Puppet::Util::Execution::ProcessOutput.new('', 0)
  end

  describe "when running on FreeBSD" do
    before :each do
      allow(Facter).to receive(:value).with(:operatingsystem).and_return('FreeBSD')
      allow(Facter).to receive(:value).with(:osfamily).and_return('FreeBSD')
    end

    it "should set its default path to include /etc/rc.d and /usr/local/etc/rc.d" do
      expect(provider_class.defpath).to eq(["/etc/rc.d", "/usr/local/etc/rc.d"])
    end
  end

  describe "when running on HP-UX" do
    before :each do
      allow(Facter).to receive(:value).with(:operatingsystem).and_return('HP-UX')
    end

    it "should set its default path to include /sbin/init.d" do
      expect(provider_class.defpath).to eq("/sbin/init.d")
    end
  end

  describe "when running on Archlinux" do
    before :each do
      allow(Facter).to receive(:value).with(:operatingsystem).and_return('Archlinux')
    end

    it "should set its default path to include /etc/rc.d" do
      expect(provider_class.defpath).to eq("/etc/rc.d")
    end
  end

  describe "when not running on FreeBSD, HP-UX or Archlinux" do
    before :each do
      allow(Facter).to receive(:value).with(:operatingsystem).and_return('RedHat')
    end

    it "should set its default path to include /etc/init.d" do
      expect(provider_class.defpath).to eq("/etc/init.d")
    end
  end

  describe "when getting all service instances" do
    before :each do
      allow(provider_class).to receive(:defpath).and_return('tmp')

      @services = ['one', 'two', 'three', 'four', 'umountfs']
      allow(Dir).to receive(:entries).and_call_original
      allow(Dir).to receive(:entries).with('tmp').and_return(@services)
      allow(Puppet::FileSystem).to receive(:directory?).and_call_original
      allow(Puppet::FileSystem).to receive(:directory?).with('tmp').and_return(true)
      allow(Puppet::FileSystem).to receive(:executable?).and_return(true)
    end

    it "should return instances for all services" do
      expect(provider_class.instances.map(&:name)).to eq(@services)
    end

    it "should omit directories from the service list" do
      expect(Puppet::FileSystem).to receive(:directory?).with('tmp/four').and_return(true)
      expect(provider_class.instances.map(&:name)).to eq(@services - ['four'])
    end

    it "should omit an array of services from exclude list" do
      exclude = ['two', 'four']
      expect(provider_class.get_services(provider_class.defpath, exclude).map(&:name)).to eq(@services - exclude)
    end

    it "should omit a single service from the exclude list" do
      exclude = 'two'
      expect(provider_class.get_services(provider_class.defpath, exclude).map(&:name)).to eq(@services - [exclude])
    end

    it "should omit Yocto services on cisco-wrlinux" do
      allow(Facter).to receive(:value).with(:osfamily).and_return('cisco-wrlinux')
      exclude = 'umountfs'
      expect(provider_class.get_services(provider_class.defpath).map(&:name)).to eq(@services - [exclude])
    end

    it "should not omit Yocto services on non cisco-wrlinux platforms" do
      expect(provider_class.get_services(provider_class.defpath).map(&:name)).to eq(@services)
    end

    it "should use defpath" do
      expect(provider_class.instances).to be_all { |provider| provider.get(:path) == provider_class.defpath }
    end

    it "should set hasstatus to true for providers" do
      expect(provider_class.instances).to be_all { |provider| provider.get(:hasstatus) == true }
    end

    it "should discard upstart jobs", :if => Puppet.features.manages_symlinks? do
      not_init_service, *valid_services = @services
      path = "tmp/#{not_init_service}"
      allow(Puppet::FileSystem).to receive(:symlink?).at_least(:once).and_return(false)
      allow(Puppet::FileSystem).to receive(:symlink?).with(Puppet::FileSystem.pathname(path)).and_return(true)
      allow(Puppet::FileSystem).to receive(:readlink).with(Puppet::FileSystem.pathname(path)).and_return("/lib/init/upstart-job")
      expect(provider_class.instances.map(&:name)).to eq(valid_services)
    end

    it "should discard non-initscript scripts" do
      valid_services = @services
      all_services = valid_services + excludes
      expect(Dir).to receive(:entries).with('tmp').and_return(all_services)
      expect(provider_class.instances.map(&:name)).to match_array(valid_services)
    end
  end

  describe "when checking valid paths" do
    it "should discard paths that do not exist" do
      expect(Puppet::FileSystem).to receive(:directory?).with(paths[0]).and_return(false)
      expect(Puppet::FileSystem).to receive(:exist?).with(paths[0]).and_return(false)
      expect(Puppet::FileSystem).to receive(:directory?).with(paths[1]).and_return(true)

      expect(provider.paths).to eq([paths[1]])
    end

    it "should discard paths that are not directories" do
      paths.each do |path|
        expect(Puppet::FileSystem).to receive(:exist?).with(path).and_return(true)
        expect(Puppet::FileSystem).to receive(:directory?).with(path).and_return(false)
      end
      expect(provider.paths).to be_empty
    end
  end

  describe "when searching for the init script" do
    before :each do
      paths.each {|path| expect(Puppet::FileSystem).to receive(:directory?).with(path).and_return(true) }
    end

    it "should be able to find the init script in the service path" do
      expect(Puppet::FileSystem).to receive(:exist?).with("#{paths[0]}/myservice").and_return(true)
      expect(Puppet::FileSystem).not_to receive(:exist?).with("#{paths[1]}/myservice") # first one wins
      expect(provider.initscript).to eq("/service/path/myservice")
    end

    it "should be able to find the init script in an alternate service path" do
      expect(Puppet::FileSystem).to receive(:exist?).with("#{paths[0]}/myservice").and_return(false)
      expect(Puppet::FileSystem).to receive(:exist?).with("#{paths[1]}/myservice").and_return(true)
      expect(provider.initscript).to eq("/alt/service/path/myservice")
    end

    it "should be able to find the init script if it ends with .sh" do
      expect(Puppet::FileSystem).to receive(:exist?).with("#{paths[0]}/myservice").and_return(false)
      expect(Puppet::FileSystem).to receive(:exist?).with("#{paths[1]}/myservice").and_return(false)
      expect(Puppet::FileSystem).to receive(:exist?).with("#{paths[0]}/myservice.sh").and_return(true)
      expect(provider.initscript).to eq("/service/path/myservice.sh")
    end

    it "should fail if the service isn't there" do
      paths.each do |path|
        expect(Puppet::FileSystem).to receive(:exist?).with("#{path}/myservice").and_return(false)
        expect(Puppet::FileSystem).to receive(:exist?).with("#{path}/myservice.sh").and_return(false)
      end
      expect { provider.initscript }.to raise_error(Puppet::Error, "Could not find init script for 'myservice'")
    end
  end

  describe "if the init script is present" do
    before :each do
      allow(Puppet::FileSystem).to receive(:directory?).and_call_original
      allow(Puppet::FileSystem).to receive(:directory?).with("/service/path").and_return(true)
      allow(Puppet::FileSystem).to receive(:directory?).with("/alt/service/path").and_return(true)
      allow(Puppet::FileSystem).to receive(:exist?).with("/service/path/myservice").and_return(true)
    end

    [:start, :stop, :status, :restart].each do |method|
      it "should have a #{method} method" do
        expect(provider).to respond_to(method)
      end

      describe "when running #{method}" do
        before :each do
          allow(provider).to receive(:execute).and_return(process_output)
        end

        it "should use any provided explicit command" do
          resource[method] = "/user/specified/command"
          expect(provider).to receive(:execute).with(["/user/specified/command"], any_args).and_return(process_output)

          provider.send(method)
        end

        it "should pass #{method} to the init script when no explicit command is provided" do
          resource[:hasrestart] = :true
          resource[:hasstatus] = :true
          expect(provider).to receive(:execute).with(["/service/path/myservice", method], any_args).and_return(process_output)

          provider.send(method)
        end
      end
    end

    describe "when checking status" do
      describe "when hasstatus is :true" do
        before :each do
          resource[:hasstatus] = :true
        end

        it "should execute the command" do
          expect(provider).to receive(:execute)
            .with(['/service/path/myservice', :status], hash_including(failonfail: false))
            .and_return(process_output)
          provider.status
        end

        it "should consider the process running if the command returns 0" do
          expect(provider).to receive(:execute)
            .with(['/service/path/myservice', :status], hash_including(failonfail: false))
            .and_return(process_output)
          expect(provider.status).to eq(:running)
        end

        [-10,-1,1,10].each { |ec|
          it "should consider the process stopped if the command returns something non-0" do
            expect(provider).to receive(:execute)
              .with(['/service/path/myservice', :status], hash_including(failonfail: false))
              .and_return(Puppet::Util::Execution::ProcessOutput.new('', ec))
            expect(provider.status).to eq(:stopped)
          end
        }
      end

      describe "when hasstatus is not :true" do
        before :each do
          resource[:hasstatus] = :false
        end

        it "should consider the service :running if it has a pid" do
          expect(provider).to receive(:getpid).and_return("1234")
          expect(provider.status).to eq(:running)
        end

        it "should consider the service :stopped if it doesn't have a pid" do
          expect(provider).to receive(:getpid).and_return(nil)
          expect(provider.status).to eq(:stopped)
        end
      end
    end

    describe "when restarting and hasrestart is not :true" do
      before :each do
        resource[:hasrestart] = :false
      end

      it "should stop and restart the process" do
        expect(provider).to receive(:execute)
          .with(['/service/path/myservice', :stop], hash_including(failonfail: true))
          .and_return(process_output)
        expect(provider).to receive(:execute)
          .with(['/service/path/myservice', :start], hash_including(failonfail: true))
          .and_return(process_output)
        provider.restart
      end
    end

    describe "when starting a service on Solaris" do
      it "should use ctrun" do
        allow(Facter).to receive(:value).with(:osfamily).and_return('Solaris')
        expect(provider).to receive(:execute)
          .with('/usr/bin/ctrun -l child /service/path/myservice start', {:failonfail => true, :override_locale => false, :squelch => false, :combine => true})
          .and_return(process_output)
        provider.start
      end
    end

    describe "when starting a service on RedHat" do
      it "should not use ctrun" do
        allow(Facter).to receive(:value).with(:osfamily).and_return('RedHat')
        expect(provider).to receive(:execute)
          .with(['/service/path/myservice', :start], {:failonfail => true, :override_locale => false, :squelch => false, :combine => true})
          .and_return(process_output)
        provider.start
      end
    end
  end
end