File: bsd_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 (126 lines) | stat: -rw-r--r-- 5,262 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
require 'spec_helper'

describe Puppet::Type.type(:service).provider(:bsd), :unless => Puppet.features.microsoft_windows? do
  before :each do
    Puppet::Type.type(:service).stubs(:defaultprovider).returns described_class
    Facter.stubs(:value).with(:operatingsystem).returns :netbsd
    Facter.stubs(:value).with(:osfamily).returns 'NetBSD'
    described_class.stubs(:defpath).returns('/etc/rc.conf.d')
    @provider = subject()
    @provider.stubs(:initscript)
  end

  context "#instances" do
    it "should have an instances method" do
      expect(described_class).to respond_to :instances
    end

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

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

    it "should remove a service file to disable" do
      provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
      Puppet::FileSystem.stubs(:exist?).with('/etc/rc.conf.d/sshd').returns(true)
      Puppet::FileSystem.expects(:exist?).with('/etc/rc.conf.d/sshd').returns(true)
      File.stubs(:delete).with('/etc/rc.conf.d/sshd')
      provider.disable
    end

    it "should not remove a service file if it doesn't exist" do
      provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
      File.stubs(:exist?).with('/etc/rc.conf.d/sshd').returns(false)
      Puppet::FileSystem.expects(:exist?).with('/etc/rc.conf.d/sshd').returns(false)
      provider.disable
    end
  end

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

    it "should set the proper contents to enable" do
      provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
      Dir.stubs(:mkdir).with('/etc/rc.conf.d')
      fh = stub 'fh'
      File.stubs(:open).with('/etc/rc.conf.d/sshd', File::WRONLY | File::APPEND | File::CREAT, 0644).yields(fh)
      fh.expects(:<<).with("sshd_enable=\"YES\"\n")
      provider.enable
    end

    it "should set the proper contents to enable when disabled" do
      provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
      Dir.stubs(:mkdir).with('/etc/rc.conf.d')
      File.stubs(:read).with('/etc/rc.conf.d/sshd').returns("sshd_enable=\"NO\"\n")
      fh = stub 'fh'
      File.stubs(:open).with('/etc/rc.conf.d/sshd', File::WRONLY | File::APPEND | File::CREAT, 0644).yields(fh)
      fh.expects(:<<).with("sshd_enable=\"YES\"\n")
      provider.enable
    end
  end

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

    it "should return false if the service file does not exist" do
      provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
      Puppet::FileSystem.stubs(:exist?).with('/etc/rc.conf.d/sshd').returns(false)
      # File.stubs(:read).with('/etc/rc.conf.d/sshd').returns("sshd_enable=\"NO\"\n")
      expect(provider.enabled?).to eq(:false)
    end

    it "should return true if the service file exists" do
      provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
      Puppet::FileSystem.stubs(:exist?).with('/etc/rc.conf.d/sshd').returns(true)
      # File.stubs(:read).with('/etc/rc.conf.d/sshd').returns("sshd_enable=\"YES\"\n")
      expect(provider.enabled?).to eq(:true)
    end
  end

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

    it "should use the supplied start command if specified" do
      provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :start => '/bin/foo'))
      provider.expects(:execute).with(['/bin/foo'], :failonfail => true, :override_locale => false, :squelch => false, :combine => true)
      provider.start
    end

    it "should start the serviced directly otherwise" do
      provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
      provider.expects(:execute).with(['/etc/rc.d/sshd', :onestart], :failonfail => true, :override_locale => false, :squelch => false, :combine => true)
      provider.expects(:search).with('sshd').returns('/etc/rc.d/sshd')
      provider.start
    end
  end

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

    it "should use the supplied stop command if specified" do
      provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :stop => '/bin/foo'))
      provider.expects(:execute).with(['/bin/foo'], :failonfail => true, :override_locale => false, :squelch => false, :combine => true)
      provider.stop
    end

    it "should stop the serviced directly otherwise" do
      provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
      provider.expects(:execute).with(['/etc/rc.d/sshd', :onestop], :failonfail => true, :override_locale => false, :squelch => false, :combine => true)
      provider.expects(:search).with('sshd').returns('/etc/rc.d/sshd')
      provider.stop
    end
  end
end