-
Notifications
You must be signed in to change notification settings - Fork 3
/
apt_test.go
224 lines (201 loc) · 6.96 KB
/
apt_test.go
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
package main
import (
"errors"
"fmt"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestPackagesThatNeedUpdates(t *testing.T) {
packageManager = DebianPackageManager{}
Convey("Given pending updates", t, func() {
Convey("There should be a list of packages available for update", func() {
mockValue := `
Inst apport [2.20.11-0ubuntu82.4] (2.20.11-0ubuntu82.5 Ubuntu:22.04/jammy-updates [all])
`
dpkMock := `
Package: apport
Status: install ok installed
Priority: optional
Section: utils
Installed-Size: 812
Maintainer: Ubuntu Developers <[email protected]>
Architecture: all
Version: 2.20.11-0ubuntu82.4
Replaces: core-dump-handler, python-apport (<< 2.2-0ubuntu1)
Provides: core-dump-handler
Depends: python3, python3-apport (>= 2.20.11-0ubuntu82.4), lsb-base (>= 3.0-6), python3-gi, gir1.2-glib-2.0 (>= 1.29.17)
Recommends: apport-symptoms, python3-systemd
Suggests: apport-gtk | apport-kde, policykit-1
Breaks: python-apport (<< 2.2-0ubuntu1)
Conflicts: core-dump-handler
Conffiles:
/etc/apport/blacklist.d/README.blacklist c2ed1eb9a17ec2550747b4960cf4b73c
/etc/apport/blacklist.d/apport 44503501302b80099552bac0204a45c1
/etc/apport/crashdb.conf 4202dae3eccfa5bbb33a0a9acfcd3724
/etc/bash_completion.d/apport_completion dfe766d9328bb5c895038b44185133f9
/etc/cron.daily/apport df5d3bc9ab3a67b58156376318077304
/etc/default/apport 3446c6cac185f44237f59786e006ebe4
/etc/init.d/apport 3d51dc9135014bb49b4a19ff8dab61f1
/etc/logrotate.d/apport fa54dab59ef899b48d5455c976008df4
Description: automatically generate crash reports for debugging
apport automatically collects data from crashed processes and
compiles a problem report in /var/crash/. This utilizes the crashdump
helper hook provided by the Ubuntu kernel.
.
This package also provides a command line frontend for browsing and
handling the crash reports. For desktops, you should consider
installing the GTK+ or Qt user interface (apport-gtk or apport-kde).
Homepage: https://wiki.ubuntu.com/Apport
`
r := new(MockRunner)
r.On("RunBytes", "apt-get", []string{"-s", "upgrade"}).Return(mockValue, nil)
r.On("RunBytes", "dpkg", []string{"-s", "apport"}).Return(dpkMock, nil)
runner = r
osPackages := packageManager.BuildPackageList()
So(osPackages[0].Name, ShouldEqual, "apport")
So(osPackages[0].Security, ShouldEqual, false)
r.Mock.AssertExpectations(t)
})
})
}
func TestChangeLog(t *testing.T) {
packageManager = DebianPackageManager{}
Convey("Changelong gets 64bit encoded", t, func() {
r := new(MockRunner)
r.On("Run", "apt-get", []string{"changelog", "apt"}).Return("foobar", nil)
runner = r
So(packageManager.GetChangelog("apt"), ShouldEqual, "Zm9vYmFy")
r.Mock.AssertExpectations(t)
})
}
func TestPackageUpdates(t *testing.T) {
packageManager = DebianPackageManager{}
Convey("Given a package name", t, func() {
Convey("The package should be upgraded", func() {
r := new(MockRunner)
r.On("Run", "apt-get", []string{
"install",
"-y",
"-o",
fmt.Sprintf("Dpkg::Options::=--force-confdef"),
"-o",
fmt.Sprintf("Dpkg::Options::=--force-confold"),
"apt"}).Return("", nil)
runner = r
err := packageManager.UpdatePackage("apt")
So(err, ShouldBeNil)
r.Mock.AssertExpectations(t)
})
Convey("The package should not upgrade if held", func() {
r := new(MockRunner)
r.On("Run", "apt-get", []string{
"install",
"-y",
"-o",
fmt.Sprintf("Dpkg::Options::=--force-confdef"),
"-o",
fmt.Sprintf("Dpkg::Options::=--force-confold"),
"apt"}).Return("", errors.New("fail"))
runner = r
err := packageManager.UpdatePackage("apt")
So(err, ShouldNotBeNil)
r.Mock.AssertExpectations(t)
})
})
}
func TestPackageHolding(t *testing.T) {
packageManager = DebianPackageManager{}
Convey("Given holding a package", t, func() {
Convey("The package should be held", func() {
r := new(MockRunner)
r.On("Run", "apt-mark", []string{"hold", "apt"}).Return("", nil)
runner = r
err := packageManager.HoldPackage("apt")
So(err, ShouldBeNil)
r.Mock.AssertExpectations(t)
})
})
Convey("Given unholding a package", t, func() {
Convey("The package should be unheld", func() {
r := new(MockRunner)
r.On("Run", "apt-mark", []string{"unhold", "apt"}).Return("", nil)
runner = r
err := packageManager.UnholdPackage("apt")
So(err, ShouldBeNil)
r.Mock.AssertExpectations(t)
})
})
}
func TestSourceList(t *testing.T) {
packageManager = DebianPackageManager{}
Convey("Given /etc/apt/sources.list exists", t, func() {
Convey("There should be a source list", func() {
packageList := []string{"deb http://us.archive.ubuntu.com/ubuntu/ trusty main restricted",
"deb-src http://us.archive.ubuntu.com/ubuntu/ trusty main restricted"}
r := new(MockRunner)
r.On("Run", "grep", []string{"-h", "^deb", "/etc/apt/sources.list", "/etc/apt/sources.list.d/*"}).Return(strings.Join(packageList, "\n"), nil)
runner = r
sourcesList := packageManager.GetSourcesList()
src_one := sourcesList[0]
src_two := sourcesList[1]
So(src_one.Url, ShouldEqual, "http://us.archive.ubuntu.com/ubuntu/")
So(src_one.Src, ShouldBeFalse)
So(src_two.Src, ShouldBeTrue)
r.Mock.AssertExpectations(t)
})
})
}
func TestInstalledPackages(t *testing.T) {
packageManager = DebianPackageManager{}
Convey("Given I want to view all installed packages", t, func() {
Convey("It returns a list of all installed packages", func() {
r := new(MockRunner)
r.On("Run", "dpkg", []string{"--get-selections"}).Return("apt\u0009installed", nil)
runner = r
packages := packageManager.BuildInstalledPackageList()
So(packages[0], ShouldEqual, "apt")
So(len(packages), ShouldEqual, 1)
r.Mock.AssertExpectations(t)
})
})
}
func TestUpdatingThePackageList(t *testing.T) {
packageManager = DebianPackageManager{}
Convey("Given I want to have the latest source list", t, func() {
Convey("apt-update gets run", func() {
r := new(MockRunner)
r.On("Run", "apt-get", []string{"update"}).Return("", nil)
runner = r
err := packageManager.UpdatePackageLists()
So(err, ShouldBeNil)
r.Mock.AssertExpectations(t)
})
})
}
//func TestUpdateCounts(t *testing.T) {
// packageManager = DebianPackageManager{}
// Convey("Given there are security and regular updates", t, func() {
//
// Convey("The number of security and regular updates is > 0", func() {
// //r := new(MockRunner)
// //r.On("Run", "/usr/lib/update-notifier/apt-check", []string{}).Return("1;2", nil)
// //runner = r
// updates := packageManager.UpdateCounts()
// So(updates.Regular, ShouldEqual, 1)
// So(updates.Security, ShouldEqual, 2)
// //r.Mock.AssertExpectations(t)
// })
//
// Convey("There are no security updates", func() {
// r := new(MockRunner)
// r.On("Run", "/usr/lib/update-notifier/apt-check", []string{}).Return("2;0", nil)
// runner = r
// updates := packageManager.UpdateCounts()
// So(updates.Regular, ShouldEqual, 2)
// So(updates.Security, ShouldEqual, 0)
// r.Mock.AssertExpectations(t)
// })
//
// })
//}