forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease_test.go
339 lines (285 loc) · 9.15 KB
/
release_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package release_test
import (
"fmt"
"os"
"path/filepath"
"testing"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/testutil"
)
// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }
type ReleaseTestSuite struct {
}
var _ = Suite(&ReleaseTestSuite{})
func (s *ReleaseTestSuite) TestSetup(c *C) {
c.Check(release.Series, Equals, "16")
}
const refMockOSRelease = `
NAME="Ubuntu"
VERSION="18.09 (Awesome Artichoke)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="I'm not real!"
VERSION_ID="18.09"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
`
const refMockUbuntuCoreInitrdRelease = `
NAME="Ubuntu Core Initramfs"
VERSION="1"
ID=ubuntucoreinitramfs
PRETTY_NAME="Ubuntu Core Initramfs Kelly's Eye - Number One"
VERSION_ID="1"
VERSION_CODENAME=keelyseye
`
func mockOSRelease(c *C, root string, filesWithContent [][]string) {
for _, e := range filesWithContent {
target := filepath.Join(root, e[0])
c.Assert(os.MkdirAll(filepath.Dir(target), 0o755), IsNil)
c.Assert(os.WriteFile(target, []byte(e[1]), 0o644), IsNil)
}
}
// MockFilesystemRootType changes relase.ProcMountsPath so that it points to a temp file
// generated to contain the provided filesystem type
func MockFilesystemRootType(c *C, fsType string) (restorer func()) {
tmpfile, err := os.CreateTemp(c.MkDir(), "proc_mounts_mock_")
c.Assert(err, IsNil)
// Sample contents of /proc/mounts. The second line is the one that matters.
_, err = tmpfile.Write([]byte(fmt.Sprintf(`none /usr/lib/wsl/lib overlay rw,relatime,lowerdir=/gpu_lib_packaged:/gpu_lib_inbox,upperdir=/gpu_lib/rw/upper,workdir=/gpu_lib/rw/work 0 0
/dev/sdc / %s rw,relatime,discard,errors=remount-ro,data=ordered 0 0
none /mnt/wslg tmpfs rw,relatime 0 0
`, fsType)))
c.Assert(err, IsNil)
restorer = testutil.Backup(release.ProcMountsPath)
*release.ProcMountsPath = tmpfile.Name()
return restorer
}
type mockWsl struct {
ExistsInterop bool
ExistsRunWSL bool
FsType string
}
func mockWSLsetup(c *C, settings mockWsl) func() {
restoreFileExists := release.MockFileExists(func(s string) bool {
if s == "/proc/sys/fs/binfmt_misc/WSLInterop" {
return settings.ExistsInterop
}
if s == "/run/WSL" {
return settings.ExistsRunWSL
}
return osutil.FileExists(s)
})
restoreFilesystemRootType := MockFilesystemRootType(c, settings.FsType)
return func() {
restoreFileExists()
restoreFilesystemRootType()
}
}
func (s *ReleaseTestSuite) TestReadOSRelease(c *C) {
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/os-release", refMockOSRelease},
})
os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "ubuntu")
c.Check(os.VersionID, Equals, "18.09")
}
func (s *ReleaseTestSuite) TestReadOSReleaseSymlink(c *C) {
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/mock-release", refMockOSRelease},
})
// now try again but with a symlink
c.Assert(os.Symlink("mock-release", filepath.Join(root, "/etc/os-release")), IsNil)
osRel := release.ReadOSReleaseFromRoot(root)
c.Check(osRel.ID, Equals, "ubuntu")
c.Check(osRel.VersionID, Equals, "18.09")
}
func (s *ReleaseTestSuite) TestReadOSReleaseFallback(c *C) {
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/usr/lib/os-release", refMockOSRelease},
})
os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "ubuntu")
c.Check(os.VersionID, Equals, "18.09")
}
func (s *ReleaseTestSuite) TestReadOSReleaseInitrdDirect(c *C) {
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/initrd-release", refMockUbuntuCoreInitrdRelease},
})
os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "ubuntucoreinitramfs")
c.Check(os.VersionID, Equals, "1")
}
func (s *ReleaseTestSuite) TestReadOSReleaseInitrdSymlink(c *C) {
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/mock-initrd-release", refMockUbuntuCoreInitrdRelease},
})
// now try again but with a symlink
c.Assert(os.Symlink("mock-initrd-release", filepath.Join(root, "/etc/os-release")), IsNil)
osRel := release.ReadOSReleaseFromRoot(root)
c.Check(osRel.ID, Equals, "ubuntucoreinitramfs")
c.Check(osRel.VersionID, Equals, "1")
}
func (s *ReleaseTestSuite) TestReadWonkyOSRelease(c *C) {
dump := `NAME="elementary OS"
VERSION="0.4 Loki"
ID="elementary OS"
ID_LIKE=ubuntu
PRETTY_NAME="elementary OS Loki"
VERSION_ID="0.4"
HOME_URL="http://elementary.io/"
SUPPORT_URL="http://elementary.io/support/"
BUG_REPORT_URL="https://bugs.launchpad.net/elementary/+filebug"`
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/os-release", dump},
})
os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "elementary")
c.Check(os.VersionID, Equals, "0.4")
}
func (s *ReleaseTestSuite) TestFamilyOSRelease(c *C) {
dump := `NAME="CentOS Stream"
VERSION="9"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="9"
PLATFORM_ID="platform:el9"
PRETTY_NAME="CentOS Stream 9"
ANSI_COLOR="0;31"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:centos:centos:9"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://issues.redhat.com/"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 9"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"`
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/os-release", dump},
})
os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "centos")
c.Check(os.VersionID, Equals, "9")
c.Check(os.IDLike, DeepEquals, []string{"rhel", "fedora"})
}
func (s *ReleaseTestSuite) TestUbuntuCoreVariantRelease(c *C) {
dump := `NAME="Ubuntu Core Desktop"
VERSION="22"
ID="ubuntu-core"
VARIANT_ID="desktop"
VERSION_ID="22"
"`
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/os-release", dump},
})
os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "ubuntu-core")
c.Check(os.VariantID, Equals, "desktop")
c.Check(os.VersionID, Equals, "22")
}
func (s *ReleaseTestSuite) TestReadOSReleaseNotFound(c *C) {
root := c.MkDir()
os := release.ReadOSReleaseFromRoot(root)
c.Assert(os, DeepEquals, release.OS{ID: "linux"})
}
func (s *ReleaseTestSuite) TestOnClassic(c *C) {
reset := release.MockOnClassic(true)
defer reset()
c.Assert(release.OnClassic, Equals, true)
reset = release.MockOnClassic(false)
defer reset()
c.Assert(release.OnClassic, Equals, false)
}
func (s *ReleaseTestSuite) TestOnCoreDesktop(c *C) {
reset := release.MockOnCoreDesktop(true)
defer reset()
c.Assert(release.OnCoreDesktop, Equals, true)
reset = release.MockOnCoreDesktop(false)
defer reset()
c.Assert(release.OnCoreDesktop, Equals, false)
}
func (s *ReleaseTestSuite) TestReleaseInfo(c *C) {
reset := release.MockReleaseInfo(&release.OS{
ID: "distro-id",
})
defer reset()
c.Assert(release.ReleaseInfo.ID, Equals, "distro-id")
}
func (s *ReleaseTestSuite) TestNonWSL(c *C) {
defer mockWSLsetup(c, mockWsl{ExistsInterop: false, ExistsRunWSL: false, FsType: "ext4"})()
v := release.GetWSLVersion()
c.Check(v, Equals, 0)
}
func (s *ReleaseTestSuite) TestWSL1(c *C) {
defer mockWSLsetup(c, mockWsl{ExistsInterop: true, ExistsRunWSL: true, FsType: "wslfs"})()
v := release.GetWSLVersion()
c.Check(v, Equals, 1)
}
func (s *ReleaseTestSuite) TestWSL1Old(c *C) {
defer mockWSLsetup(c, mockWsl{ExistsInterop: true, ExistsRunWSL: true, FsType: "lxfs"})()
v := release.GetWSLVersion()
c.Check(v, Equals, 1)
}
func (s *ReleaseTestSuite) TestWSL2(c *C) {
defer mockWSLsetup(c, mockWsl{ExistsInterop: true, ExistsRunWSL: true, FsType: "ext4"})()
v := release.GetWSLVersion()
c.Check(v, Equals, 2)
}
func (s *ReleaseTestSuite) TestWSL2NoInterop(c *C) {
defer mockWSLsetup(c, mockWsl{ExistsInterop: false, ExistsRunWSL: true, FsType: "ext4"})()
v := release.GetWSLVersion()
c.Check(v, Equals, 2)
}
func (s *ReleaseTestSuite) TestLXDInWSL2(c *C) {
defer mockWSLsetup(c, mockWsl{ExistsInterop: true, ExistsRunWSL: false, FsType: "btrfs"})()
v := release.GetWSLVersion()
c.Check(v, Equals, 2)
}
func (s *ReleaseTestSuite) TestSystemctlSupportsUserUnits(c *C) {
for _, t := range []struct {
id, versionID string
supported bool
}{
// Non-Ubuntu releases are assumed to be new enough
{"distro-id", "version", true},
// Ubuntu 14.04's systemd is too old for user units
{"ubuntu", "14.04", false},
// Other Ubuntu releases are fine
{"ubuntu", "16.04", true},
{"ubuntu", "18.04", true},
{"ubuntu", "20.04", true},
} {
reset := release.MockReleaseInfo(&release.OS{
ID: t.id,
VersionID: t.versionID,
})
defer reset()
c.Check(release.SystemctlSupportsUserUnits(), Equals, t.supported)
}
}