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
|
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
allow(Puppet::Util).to receive(:which).with("opkg").and_return("/bin/opkg")
allow(provider).to receive(:package_lists).and_return(['.', '..', 'packages'])
end
describe "when installing" do
before do
allow(provider).to receive(:query).and_return({ :ensure => '1.0' })
end
context "when the package list is absent" do
before do
allow(provider).to receive(:package_lists).and_return(['.', '..']) #empty, no package list
end
it "fetches the package list when installing" do
expect(provider).to receive(:opkg).with('update')
expect(provider).to receive(:opkg).with("--force-overwrite", "install", resource[:name])
provider.install
end
end
context "when the package list is present" do
before do
allow(provider).to receive(:package_lists).and_return(['.', '..', 'lists']) # With a pre-downloaded package list
end
it "fetches the package list when installing" do
expect(provider).not_to receive(:opkg).with('update')
expect(provider).to receive(:opkg).with("--force-overwrite", "install", resource[:name])
provider.install
end
end
it "should call opkg install" do
expect(Puppet::Util::Execution).to receive(: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
expect(Puppet::Util::Execution).to receive(: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
expect(Puppet::Util::Execution).to receive(:execute).and_return(Puppet::Util::Execution::ProcessOutput.new("", 0))
provider.install
end
end
context "with invalid URL for opkg" do
before do
# Emulate the `opkg` command returning a non-zero exit value
allow(Puppet::Util::Execution).to receive(:execute).and_raise(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
expect(provider).to receive(:install).and_return("install return value")
expect(provider.update).to eq("install return value")
end
end
describe "when uninstalling" do
it "should run opkg remove bla" do
expect(Puppet::Util::Execution).to receive(: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
allow(Puppet::Util).to receive(:which).with("opkg").and_return("/bin/opkg")
allow(described_class).to receive(:which).with("opkg").and_return("/bin/opkg")
expect(described_class).to receive(:execpipe).with("/bin/opkg list-installed").and_yield(packages)
installed_packages = described_class.instances
expect(installed_packages.length).to eq(3)
expect(installed_packages[0].properties).to eq(
{
:provider => :opkg,
:name => "dropbear",
:ensure => "2011.54-2"
}
)
expect(installed_packages[1].properties).to eq(
{
:provider => :opkg,
:name => "kernel",
:ensure => "3.3.8-1-ba5cdb2523b4fc7722698b4a7ece6702"
}
)
expect(installed_packages[2].properties).to eq(
{
:provider => :opkg,
:name => "uhttpd",
:ensure => "2012-10-30-e57bf6d8bfa465a50eea2c30269acdfe751a46fd"
}
)
end
end
it "should return a nil if the package isn't found" do
expect(Puppet::Util::Execution).to receive(:execute).and_return(Puppet::Util::Execution::ProcessOutput.new("", 0))
expect(provider.query).to be_nil
end
it "should return a hash indicating that the package is missing on error" do
expect(Puppet::Util::Execution).to receive(:execute).and_raise(Puppet::ExecutionFailure.new("ERROR!"))
expect(provider.query).to eq({
:ensure => :purged,
:status => 'missing',
:name => resource[:name],
:error => 'ok',
})
end
end
end
|