summaryrefslogtreecommitdiff
path: root/spec/unit/provider/package/opkg_spec.rb
blob: f472e22d27f2c32e63a0ef3cb21306cfe39376cf (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
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
#! /usr/bin/env ruby
require 'spec_helper'

describe Puppet::Type.type(:package).provider(:opkg) do

  let(:resource) do
    Puppet::Type.type(:package).new(:name => 'package')
  end

  let(:provider) { described_class.new(resource) }

  before do
    Puppet::Util::Execution.stubs(:execute).never
    Puppet::Util.stubs(:which).with("opkg").returns("/bin/opkg")
    Dir.stubs(:entries).with('/var/opkg-lists/').returns ['.', '..', 'packages']
  end

  describe "when installing" do
    before do
      provider.stubs(:query).returns({ :ensure => '1.0' })
    end

    context "when the package list is absent" do
      before do
        Dir.stubs(:entries).with('/var/opkg-lists/').returns ['.', '..']  #empty, no package list
      end

      it "fetches the package list when installing" do
        provider.expects(:opkg).with('update')
        provider.expects(:opkg).with("--force-overwrite", "install", resource[:name])

        provider.install
      end
    end

    context "when the package list is present" do
      before do
        Dir.stubs(:entries).with('/var/opkg-lists/').returns ['.', '..', 'lists']  # With a pre-downloaded package list
      end

      it "fetches the package list when installing" do
        provider.expects(:opkg).with('update').never
        provider.expects(:opkg).with("--force-overwrite", "install", resource[:name])

        provider.install
      end
    end

    it "should call opkg install" do
      Puppet::Util::Execution.expects(:execute).with(["/bin/opkg", "--force-overwrite", "install", resource[:name]], {:failonfail => true, :combine => true, :custom_environment => {}})
      provider.install
    end

    context "when :source is specified" do
      context "works on valid urls" do
        %w{
          /some/package/file
          http://some.package.in/the/air
          ftp://some.package.in/the/air
        }.each do |source|
          it "should install #{source} directly" do
            resource[:source] = source
            Puppet::Util::Execution.expects(:execute).with(["/bin/opkg", "--force-overwrite", "install", resource[:source]], {:failonfail => true, :combine => true, :custom_environment => {}})
            provider.install
          end
        end
      end

      context "as a file:// URL" do
        before do
          @package_file = "file:///some/package/file"
          @actual_file_path = "/some/package/file"
          resource[:source] = @package_file
        end

        it "should install from the path segment of the URL" do
          Puppet::Util::Execution.expects(:execute).returns("")
          provider.install
        end
      end

      context "with invalid URL for opkg" do
        before do
          # Emulate the `opkg` command returning a non-zero exit value
          Puppet::Util::Execution.stubs(:execute).raises Puppet::ExecutionFailure, 'oops'
        end

        context "puppet://server/whatever" do
          before do
            resource[:source] = "puppet://server/whatever"
          end

          it "should fail" do
            expect { provider.install }.to raise_error Puppet::ExecutionFailure
          end
        end

        context "as a malformed URL" do
          before do
            resource[:source] = "blah://"
          end

          it "should fail" do
            expect { provider.install }.to raise_error Puppet::ExecutionFailure
          end
        end
      end
    end # end when source is specified
  end # end when installing

  describe "when updating" do
    it "should call install" do
      provider.expects(:install).returns("install return value")
      provider.update.should == "install return value"
    end
  end

  describe "when uninstalling" do
    it "should run opkg remove bla" do
      Puppet::Util::Execution.expects(:execute).with(["/bin/opkg", "remove", resource[:name]], {:failonfail => true, :combine => true, :custom_environment => {}})
      provider.uninstall
    end
  end

  describe "when querying" do

    describe "self.instances" do
      let (:packages) do
        <<-OPKG_OUTPUT
dropbear - 2011.54-2
kernel - 3.3.8-1-ba5cdb2523b4fc7722698b4a7ece6702
uhttpd - 2012-10-30-e57bf6d8bfa465a50eea2c30269acdfe751a46fd
OPKG_OUTPUT
      end
      it "returns an array of packages" do
        Puppet::Util.stubs(:which).with("opkg").returns("/bin/opkg")
        described_class.stubs(:which).with("opkg").returns("/bin/opkg")
        described_class.expects(:execpipe).with("/bin/opkg list-installed").yields(packages)

        installed_packages = described_class.instances
        installed_packages.length.should == 3

        installed_packages[0].properties.should ==
          {
            :provider => :opkg,
            :name => "dropbear",
            :ensure => "2011.54-2"
          }
        installed_packages[1].properties.should ==
          {
            :provider => :opkg,
            :name => "kernel",
            :ensure => "3.3.8-1-ba5cdb2523b4fc7722698b4a7ece6702"
          }
        installed_packages[2].properties.should ==
          {
            :provider => :opkg,
            :name => "uhttpd",
            :ensure => "2012-10-30-e57bf6d8bfa465a50eea2c30269acdfe751a46fd"
          }
      end
    end

    it "should return a nil if the package isn't found" do
      Puppet::Util::Execution.expects(:execute).returns("")
      provider.query.should be_nil
    end

    it "should return a hash indicating that the package is missing on error" do
      Puppet::Util::Execution.expects(:execute).raises(Puppet::ExecutionFailure.new("ERROR!"))
      provider.query.should == {
        :ensure => :purged,
        :status => 'missing',
        :name => resource[:name],
        :error => 'ok',
      }
    end
  end #end when querying

end # end describe provider