summaryrefslogtreecommitdiffstats
path: root/spec/unit/provider/package/dpkg_spec.rb
blob: 68ffb19a48e5e05d53446b67d684ea17a4453cfa (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env ruby

Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }

provider = Puppet::Type.type(:package).provider(:dpkg)

describe provider do
    before do
        @resource = stub 'resource', :[] => "asdf"
        @provider = provider.new(@resource)
        @provider.expects(:execute).never # forbid "manual" executions

        @fakeresult = "install ok installed asdf 1.0\n"
    end

    it "should have documentation" do
        provider.doc.should be_instance_of(String)
    end

    describe "when listing all instances" do
        before do
            provider.stubs(:command).with(:dpkgquery).returns "myquery"
        end

        it "should use dpkg-query" do
            provider.expects(:command).with(:dpkgquery).returns "myquery"
            provider.expects(:execpipe).with("myquery -W --showformat '${Status} ${Package} ${Version}\\n'").returns @fakeresult

            provider.instances
        end

        it "should create and return an instance with each parsed line from dpkg-query" do
            pipe = mock 'pipe'
            pipe.expects(:each).yields @fakeresult
            provider.expects(:execpipe).yields pipe

            asdf = mock 'pkg1'
            provider.expects(:new).with(:ensure => "1.0", :error => "ok", :desired => "install", :name => "asdf", :status => "installed", :provider => :dpkg).returns asdf

            provider.instances.should == [asdf]
        end

        it "should warn on and ignore any lines it does not understand" do
            pipe = mock 'pipe'
            pipe.expects(:each).yields "foobar"
            provider.expects(:execpipe).yields pipe

            Puppet.expects(:warning)
            provider.expects(:new).never

            provider.instances.should == []
        end
    end

    describe "when querying the current state" do
        it "should use dpkg-query" do
            @provider.expects(:dpkgquery).with("-W", "--showformat",'${Status} ${Package} ${Version}\\n', "asdf").returns @fakeresult

            @provider.query
        end

        it "should consider the package purged if dpkg-query fails" do
            @provider.expects(:dpkgquery).raises Puppet::ExecutionFailure.new("eh")

            @provider.query[:ensure].should == :purged
        end

        it "should return a hash of the found status with the desired state, error state, status, name, and 'ensure'" do
            @provider.expects(:dpkgquery).returns @fakeresult

            @provider.query.should == {:ensure => "1.0", :error => "ok", :desired => "install", :name => "asdf", :status => "installed", :provider => :dpkg}
        end

        it "should consider the package absent if the dpkg-query result cannot be interpreted" do
            @provider.expects(:dpkgquery).returns "somebaddata"

            @provider.query[:ensure].should == :absent
        end

        it "should fail if an error is discovered" do
            @provider.expects(:dpkgquery).returns @fakeresult.sub("ok", "error")

            lambda { @provider.query }.should raise_error(Puppet::Error)
        end

        it "should consider the package purged if it is marked 'not-installed'" do
            @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "not-installed")

            @provider.query[:ensure].should == :purged
        end

        it "should consider the package absent if it is marked 'config-files'" do
            @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "config-files")
            @provider.query[:ensure].should == :absent
        end

        it "should consider the package absent if it is marked 'half-installed'" do
            @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "half-installed")
            @provider.query[:ensure].should == :absent
        end

        it "should consider the package absent if it is marked 'unpacked'" do
            @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "unpacked")
            @provider.query[:ensure].should == :absent
        end

        it "should consider the package absent if it is marked 'half-configured'" do
            @provider.expects(:dpkgquery).returns @fakeresult.sub("installed", "half-configured")
            @provider.query[:ensure].should == :absent
        end

        it "should consider the package held if its state is 'hold'" do
            @provider.expects(:dpkgquery).returns @fakeresult.sub("install", "hold")
            @provider.query[:ensure].should == :held
        end
    end

    it "should be able to install" do
        @provider.should respond_to(:install)
    end

    describe "when installing" do
        before do
            @resource.stubs(:[]).with(:source).returns "mypkg"
        end

        it "should fail to install if no source is specified in the resource" do
            @resource.expects(:[]).with(:source).returns nil

            lambda { @provider.install }.should raise_error(ArgumentError)
        end

        it "should use 'dpkg -i' to install the package" do
            @resource.expects(:[]).with(:source).returns "mypackagefile"
            @provider.expects(:unhold)
            @provider.expects(:dpkg).with { |*command| command[-1] == "mypackagefile"  and command[-2] == "-i" }

            @provider.install
        end

        it "should keep old config files if told to do so" do
            @resource.expects(:[]).with(:configfiles).returns :keep
            @provider.expects(:unhold)
            @provider.expects(:dpkg).with { |*command| command[0] == "--force-confold" }

            @provider.install
        end

        it "should replace old config files if told to do so" do
            @resource.expects(:[]).with(:configfiles).returns :replace
            @provider.expects(:unhold)
            @provider.expects(:dpkg).with { |*command| command[0] == "--force-confnew" }

            @provider.install
        end

        it "should ensure any hold is removed" do
            @provider.expects(:unhold).once
            @provider.expects(:dpkg)
            @provider.install
        end
    end

    describe "when holding or unholding" do
        before do
            @tempfile = stub 'tempfile', :print => nil, :close => nil, :flush => nil, :path => "/other/file"
            @tempfile.stubs(:write)
            Tempfile.stubs(:new).returns @tempfile
        end

        it "should install first if holding" do
            @provider.stubs(:execute)
            @provider.expects(:install).once
            @provider.hold
        end

        it "should execute dpkg --set-selections when holding" do
            @provider.stubs(:install)
            @provider.expects(:execute).with([:dpkg, '--set-selections'], {:stdinfile => @tempfile.path}).once
            @provider.hold
        end

        it "should execute dpkg --set-selections when unholding" do
            @provider.stubs(:install)
            @provider.expects(:execute).with([:dpkg, '--set-selections'], {:stdinfile => @tempfile.path}).once
            @provider.hold
        end
    end

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

    describe "when determining latest available version" do
        it "should return the version found by dpkg-deb" do
            @resource.expects(:[]).with(:source).returns "myfile"
            @provider.expects(:dpkg_deb).with { |*command| command[-1] == "myfile" }.returns "asdf\t1.0"
            @provider.latest.should == "1.0"
        end

        it "should warn if the package file contains a different package" do
            @provider.expects(:dpkg_deb).returns("foo\tversion")
            @provider.expects(:warning)
            @provider.latest
        end

        it "should cope with names containing ++" do
            @resource = stub 'resource', :[] => "asdf++"
            @provider = provider.new(@resource)
            @provider.expects(:dpkg_deb).returns "asdf++\t1.0"
            @provider.latest.should == "1.0"
        end
    end

    it "should use 'dpkg -r' to uninstall" do
        @provider.expects(:dpkg).with("-r", "asdf")
        @provider.uninstall
    end

    it "should use 'dpkg --purge' to purge" do
        @provider.expects(:dpkg).with("--purge", "asdf")
        @provider.purge
    end
end