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

provider_class = Puppet::Type.type(:package).provider(:apt)

describe provider_class do
  let(:name) { 'asdf' }

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

  let(:provider) do
    provider = provider_class.new
    provider.resource = resource
    provider
  end

  it "should be versionable" do
    provider_class.should be_versionable
  end

  it "should use :install to update" do
    provider.expects(:install)
    provider.update
  end

  it "should use 'apt-get remove' to uninstall" do
    provider.expects(:aptget).with("-y", "-q", :remove, name)
    provider.uninstall
  end

  it "should use 'apt-get purge' and 'dpkg purge' to purge" do
    provider.expects(:aptget).with("-y", "-q", :remove, "--purge", name)
    provider.expects(:dpkg).with("--purge", name)
    provider.purge
  end

  it "should use 'apt-cache policy' to determine the latest version of a package" do
    provider.expects(:aptcache).with(:policy, name).returns "#{name}:
Installed: 1:1.0
Candidate: 1:1.1
Version table:
1:1.0
  650 http://ftp.osuosl.org testing/main Packages
*** 1:1.1
  100 /var/lib/dpkg/status"

    provider.latest.should == "1:1.1"
  end

  it "should print and error and return nil if no policy is found" do
    provider.expects(:aptcache).with(:policy, name).returns "#{name}:"

    provider.expects(:err)
    provider.latest.should be_nil
  end

  it "should be able to preseed" do
    provider.should respond_to(:run_preseed)
  end

  it "should preseed with the provided responsefile when preseeding is called for" do
    resource[:responsefile] = '/my/file'
    Puppet::FileSystem.expects(:exist?).with('/my/file').returns true

    provider.expects(:info)
    provider.expects(:preseed).with('/my/file')

    provider.run_preseed
  end

  it "should not preseed if no responsefile is provided" do
    provider.expects(:info)
    provider.expects(:preseed).never

    provider.run_preseed
  end

  describe "when installing" do
    it "should preseed if a responsefile is provided" do
      resource[:responsefile] = "/my/file"
      provider.expects(:run_preseed)

      provider.stubs(:aptget)
      provider.install
    end

    it "should check for a cdrom" do
      provider.expects(:checkforcdrom)

      provider.stubs(:aptget)
      provider.install
    end

    it "should use 'apt-get install' with the package name if no version is asked for" do
      resource[:ensure] = :installed
      provider.expects(:aptget).with { |*command| command[-1] == name and command[-2] == :install }

      provider.install
    end

    it "should specify the package version if one is asked for" do
      resource[:ensure] = '1.0'
      provider.expects(:aptget).with { |*command| command[-1] == "#{name}=1.0" }

      provider.install
    end

    it "should use --force-yes if a package version is specified" do
      resource[:ensure] = '1.0'
      provider.expects(:aptget).with { |*command| command.include?("--force-yes") }

      provider.install
    end

    it "should do a quiet install" do
      provider.expects(:aptget).with { |*command| command.include?("-q") }

      provider.install
    end

    it "should default to 'yes' for all questions" do
      provider.expects(:aptget).with { |*command| command.include?("-y") }

      provider.install
    end

    it "should keep config files if asked" do
      resource[:configfiles] = :keep
      provider.expects(:aptget).with { |*command| command.include?("DPkg::Options::=--force-confold") }

      provider.install
    end

    it "should replace config files if asked" do
      resource[:configfiles] = :replace
      provider.expects(:aptget).with { |*command| command.include?("DPkg::Options::=--force-confnew") }

      provider.install
    end

    it 'should support string install options' do
      resource[:install_options] = ['--foo', '--bar']
      provider.expects(:aptget).with('-q', '-y', '-o', 'DPkg::Options::=--force-confold', '--foo', '--bar', :install, name)

      provider.install
    end

    it 'should support hash install options' do
      resource[:install_options] = ['--foo', { '--bar' => 'baz', '--baz' => 'foo' }]
      provider.expects(:aptget).with('-q', '-y', '-o', 'DPkg::Options::=--force-confold', '--foo', '--bar=baz', '--baz=foo', :install, name)

      provider.install
    end
  end
end