summaryrefslogtreecommitdiff
path: root/spec/unit/provider/service/openwrt_spec.rb
blob: 8d385d1752481fd5b648cd75d2a6face27ee6d84 (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
104
105
106
107
108
109
#! /usr/bin/env ruby
#
# Unit testing for the OpenWrt service Provider
#
require 'spec_helper'

describe Puppet::Type.type(:service).provider(:openwrt), :as_platform => :posix do

  let(:resource) do
    resource = stub 'resource'
    resource.stubs(:[]).returns(nil)
    resource.stubs(:[]).with(:name).returns "myservice"
    resource.stubs(:[]).with(:path).returns ["/etc/init.d"]

    resource
  end

  let(:provider) do
    provider = described_class.new
    provider.stubs(:get).with(:hasstatus).returns false

    provider
  end


  before :each do
    resource.stubs(:provider).returns provider
    provider.resource = resource

    FileTest.stubs(:file?).with('/etc/rc.common').returns true
    FileTest.stubs(:executable?).with('/etc/rc.common').returns true

    # All OpenWrt tests operate on the init script directly. It must exist.
    File.stubs(:directory?).with('/etc/init.d').returns true

    Puppet::FileSystem.stubs(:exist?).with('/etc/init.d/myservice').returns true
    FileTest.stubs(:file?).with('/etc/init.d/myservice').returns true
    FileTest.stubs(:executable?).with('/etc/init.d/myservice').returns true
  end

  operatingsystem = 'openwrt'
  it "should be the default provider on #{operatingsystem}" do
    Facter.expects(:value).with(:operatingsystem).returns(operatingsystem)
    described_class.default?.should be_true
  end

  # test self.instances
  describe "when getting all service instances" do

    let(:services) {['dnsmasq', 'dropbear', 'firewall', 'led', 'puppet', 'uhttpd' ]}

    before :each do
      Dir.stubs(:entries).returns services
      FileTest.stubs(:directory?).returns(true)
      FileTest.stubs(:executable?).returns(true)
    end
    it "should return instances for all services" do
      services.each do |inst|
        described_class.expects(:new).with{|hash| hash[:name] == inst && hash[:path] == '/etc/init.d'}.returns("#{inst}_instance")
      end
      results = services.collect {|x| "#{x}_instance"}
      described_class.instances.should == results
    end
  end

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

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

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

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

      it "should use any provided explicit command" do
        resource.stubs(:[]).with(method).returns "/user/specified/command"
        provider.expects(:execute).with { |command, *args| command == ["/user/specified/command"] }
        provider.send(method)
      end

      it "should execute the init script with #{method} when no explicit command is provided" do
        resource.stubs(:[]).with("has#{method}".intern).returns :true
        provider.expects(:execute).with { |command, *args| command ==  ['/etc/init.d/myservice', method ]}
        provider.send(method)
      end
    end
  end

  describe "when checking status" do
    it "should consider the service :running if it has a pid" do
      provider.expects(:getpid).returns "1234"
      provider.status.should == :running
    end
    it "should consider the service :stopped if it doesn't have a pid" do
      provider.expects(:getpid).returns nil
      provider.status.should == :stopped
    end
  end

end