|
| 1 | +require "spec_helper" |
| 2 | +require "stringio" |
| 3 | + |
| 4 | +describe Puppet::Type.type(:package).provider(:xbps) do |
| 5 | + before do |
| 6 | + @resource = Puppet::Type.type(:package).new(name: "gcc", provider: "xbps") |
| 7 | + @provider = described_class.new(@resource) |
| 8 | + @resolver = Puppet::Util |
| 9 | + |
| 10 | + allow(described_class).to receive(:which).with("/usr/bin/xbps-install").and_return("/usr/bin/xbps-install") |
| 11 | + allow(described_class).to receive(:which).with("/usr/bin/xbps-remove").and_return("/usr/bin/xbps-remove") |
| 12 | + allow(described_class).to receive(:which).with("/usr/bin/xbps-query").and_return("/usr/bin/xbps-query") |
| 13 | + end |
| 14 | + |
| 15 | + it { is_expected.to be_installable } |
| 16 | + it { is_expected.to be_uninstallable } |
| 17 | + it { is_expected.to be_install_options } |
| 18 | + it { is_expected.to be_uninstall_options } |
| 19 | + it { is_expected.to be_upgradeable } |
| 20 | + it { is_expected.to be_holdable } |
| 21 | + it { is_expected.to be_virtual_packages } |
| 22 | + |
| 23 | + it "should be the default provider on 'os.name' => Void" do |
| 24 | + expect(Facter).to receive(:value).with('os.name').and_return("Void") |
| 25 | + expect(described_class.default?).to be_truthy |
| 26 | + end |
| 27 | + |
| 28 | + describe "when determining instances" do |
| 29 | + it "should return installed packages" do |
| 30 | + sample_installed_packages = %{ |
| 31 | +ii gcc-12.2.0_1 GNU Compiler Collection |
| 32 | +ii ruby-devel-3.1.3_1 Ruby programming language - development files |
| 33 | +} |
| 34 | + |
| 35 | + expect(described_class).to receive(:execpipe).with(["/usr/bin/xbps-query", "-l"]) |
| 36 | + .and_yield(StringIO.new(sample_installed_packages)) |
| 37 | + |
| 38 | + instances = described_class.instances |
| 39 | + expect(instances.length).to eq(2) |
| 40 | + |
| 41 | + expect(instances[0].properties).to eq({ |
| 42 | + :name => "gcc", |
| 43 | + :ensures => "12.2.0_1", |
| 44 | + :provider => :xbps, |
| 45 | + }) |
| 46 | + |
| 47 | + expect(instances[1].properties).to eq({ |
| 48 | + :name => "ruby-devel", |
| 49 | + :ensures => "3.1.3_1", |
| 50 | + :provider => :xbps, |
| 51 | + }) |
| 52 | + end |
| 53 | + |
| 54 | + it "should warn on invalid input" do |
| 55 | + expect(described_class).to receive(:execpipe).and_yield(StringIO.new("blah")) |
| 56 | + expect(described_class).to receive(:warning).with('Failed to match line \'blah\'') |
| 57 | + expect(described_class.instances).to eq([]) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + describe "when installing" do |
| 62 | + it "and install_options are given it should call xbps to install the package quietly with the passed options" do |
| 63 | + @resource[:install_options] = ["-x", { "--arg" => "value" }] |
| 64 | + args = ["-S", "-y", "-x", "--arg=value", @resource[:name]] |
| 65 | + expect(@provider).to receive(:xbps_install).with(*args).and_return("") |
| 66 | + expect(described_class).to receive(:execpipe).with(["/usr/bin/xbps-query", "-l"]) |
| 67 | + |
| 68 | + @provider.install |
| 69 | + end |
| 70 | + |
| 71 | + it "and source is given it should call xbps to install the package from the source as repository" do |
| 72 | + @resource[:source] = "/path/to/xbps/containing/directory" |
| 73 | + args = ["-S", "-y", "--repository=#{@resource[:source]}", @resource[:name]] |
| 74 | + expect(@provider).to receive(:xbps_install).at_least(:once).with(*args).and_return("") |
| 75 | + expect(described_class).to receive(:execpipe).with(["/usr/bin/xbps-query", "-l"]) |
| 76 | + |
| 77 | + @provider.install |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + describe "when updating" do |
| 82 | + it "should call install" do |
| 83 | + expect(@provider).to receive(:install).and_return("ran install") |
| 84 | + expect(@provider.update).to eq("ran install") |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe "when uninstalling" do |
| 89 | + it "should call xbps to remove the right package quietly" do |
| 90 | + args = ["-R", "-y", @resource[:name]] |
| 91 | + expect(@provider).to receive(:xbps_remove).with(*args).and_return("") |
| 92 | + @provider.uninstall |
| 93 | + end |
| 94 | + |
| 95 | + it "adds any uninstall_options" do |
| 96 | + @resource[:uninstall_options] = ["-x", { "--arg" => "value" }] |
| 97 | + args = ["-R", "-y", "-x", "--arg=value", @resource[:name]] |
| 98 | + expect(@provider).to receive(:xbps_remove).with(*args).and_return("") |
| 99 | + @provider.uninstall |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + describe "when determining the latest version" do |
| 104 | + it "should return the latest version number of the package" do |
| 105 | + @resource[:name] = "ruby-devel" |
| 106 | + |
| 107 | + expect(described_class).to receive(:execpipe).with(["/usr/bin/xbps-query", "-l"]).and_yield(StringIO.new(%{ |
| 108 | +ii ruby-devel-3.1.3_1 Ruby programming language - development files |
| 109 | +})) |
| 110 | + |
| 111 | + expect(@provider.latest).to eq("3.1.3_1") |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + describe "when querying" do |
| 116 | + it "should call self.instances and return nil if the package is missing" do |
| 117 | + expect(described_class).to receive(:instances) |
| 118 | + .and_return([]) |
| 119 | + |
| 120 | + expect(@provider.query).to be_nil |
| 121 | + end |
| 122 | + |
| 123 | + it "should get real-package in case allow_virtual is true" do |
| 124 | + @resource[:name] = "nodejs-runtime" |
| 125 | + @resource[:allow_virtual] = true |
| 126 | + |
| 127 | + expect(described_class).to receive(:execpipe).with(["/usr/bin/xbps-query", "-l"]) |
| 128 | + .and_yield(StringIO.new("")) |
| 129 | + |
| 130 | + args = ["-Rs", @resource[:name]] |
| 131 | + expect(@provider).to receive(:xbps_query).with(*args).and_return(%{ |
| 132 | +[*] nodejs-16.19.0_1 Evented I/O for V8 javascript |
| 133 | +[-] nodejs-lts-12.22.10_2 Evented I/O for V8 javascript' |
| 134 | +}) |
| 135 | + |
| 136 | + expect(@provider.query).to eq({ |
| 137 | + :name => "nodejs", |
| 138 | + :ensures => "16.19.0_1", |
| 139 | + :provider => :xbps, |
| 140 | + }) |
| 141 | + end |
| 142 | + end |
| 143 | +end |
0 commit comments