-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathrelease_test.go
191 lines (160 loc) · 5.18 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
// -*- 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 (
"io/ioutil"
"path/filepath"
"testing"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/release"
)
// 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")
}
func mockOSRelease(c *C) string {
// FIXME: use AddCleanup here once available so that we
// can do release.SetLSBReleasePath() here directly
mockOSRelease := filepath.Join(c.MkDir(), "mock-os-release")
s := `
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/"
`
err := ioutil.WriteFile(mockOSRelease, []byte(s), 0644)
c.Assert(err, IsNil)
return mockOSRelease
}
func (s *ReleaseTestSuite) TestReadOSRelease(c *C) {
reset := release.MockOSReleasePath(mockOSRelease(c))
defer reset()
os := release.ReadOSRelease()
c.Check(os.ID, Equals, "ubuntu")
c.Check(os.VersionID, Equals, "18.09")
}
func (s *ReleaseTestSuite) TestReadWonkyOSRelease(c *C) {
mockOSRelease := filepath.Join(c.MkDir(), "mock-os-release")
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"`
err := ioutil.WriteFile(mockOSRelease, []byte(dump), 0644)
c.Assert(err, IsNil)
reset := release.MockOSReleasePath(mockOSRelease)
defer reset()
os := release.ReadOSRelease()
c.Check(os.ID, Equals, "elementary")
c.Check(os.VersionID, Equals, "0.4")
}
func (s *ReleaseTestSuite) TestFamilyOSRelease(c *C) {
mockOSRelease := filepath.Join(c.MkDir(), "mock-os-release")
dump := `NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"`
err := ioutil.WriteFile(mockOSRelease, []byte(dump), 0644)
c.Assert(err, IsNil)
reset := release.MockOSReleasePath(mockOSRelease)
defer reset()
os := release.ReadOSRelease()
c.Check(os.ID, Equals, "centos")
c.Check(os.VersionID, Equals, "7")
c.Check(os.IDLike, DeepEquals, []string{"rhel", "fedora"})
}
func (s *ReleaseTestSuite) TestReadOSReleaseNotFound(c *C) {
reset := release.MockOSReleasePath("not-there")
defer reset()
os := release.ReadOSRelease()
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) 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 release.MockIoutilReadfile(func(s string) ([]byte, error) {
c.Check(s, Equals, "/proc/version")
return []byte("Linux version 2.2.19 (herbert@gondolin) (gcc version 2.7.2.3) #1 Wed Mar 20 19:41:41 EST 2002"), nil
})()
c.Check(release.IsWSL(), Equals, false)
}
func (s *ReleaseTestSuite) TestWSL(c *C) {
defer release.MockIoutilReadfile(func(s string) ([]byte, error) {
c.Check(s, Equals, "/proc/version")
return []byte("Linux version 3.4.0-Microsoft (Microsoft@Microsoft.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014"), nil
})()
c.Check(release.IsWSL(), Equals, true)
}
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)
}
}