-
Notifications
You must be signed in to change notification settings - Fork 245
/
test_NewsItem.py
399 lines (341 loc) · 14 KB
/
test_NewsItem.py
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# test_NewsItem.py -- Portage Unit Testing Functionality
# Copyright 2007-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
from portage.tests import TestCase
from portage.news import NewsItem, NewsManager
from portage.dbapi.virtual import fakedbapi
from dataclasses import dataclass
from string import Template
from typing import Optional, List
from unittest.mock import MagicMock, mock_open, patch
import textwrap
# The specification for news items is GLEP 42 ("Critical News Reporting"):
# https://www.gentoo.org/glep/glep-0042.html
@dataclass
class FakeNewsItem(NewsItem):
title: str
author: str
content_type: str
posted: str
revision: int
news_item_format: str
content: str
display_if_installed: Optional[List[str]] = None
display_if_profile: Optional[List[str]] = None
display_if_keyword: Optional[List[str]] = None
item_template_header = Template(
textwrap.dedent(
"""
Title: ${title}
Author: ${author}
Content-Type: ${content_type}
Posted: ${posted}
Revision: ${revision}
News-Item-Format: ${news_item_format}
"""
)
)
def __post_init__(self):
super().__init__(path="mocked_news", name=self.title)
def isValid(self):
with patch("builtins.open", mock_open(read_data=str(self))):
return super().isValid()
# TODO: Migrate __str__ to NewsItem? NewsItem doesn't actually parse
# all fields right now though.
def __str__(self) -> str:
item = self.item_template_header.substitute(
title=self.title,
author=self.author,
content_type=self.content_type,
posted=self.posted,
revision=self.revision,
news_item_format=self.news_item_format,
)
for package in self.display_if_installed:
item += f"Display-If-Installed: {package}\n"
for profile in self.display_if_profile:
item += f"Display-If-Profile: {profile}\n"
for keyword in self.display_if_keyword:
item += f"Display-If-Keyword: {keyword}\n"
item += f"\n{self.content}"
return item
class NewsItemTestCase(TestCase):
# Default values for testing
placeholders = {
"title": "YourSQL Upgrades from 4.0 to 4.1",
"author": "Ciaran McCreesh <ciaranm@gentoo.org>",
"content_type": "Content-Type: text/plain",
"posted": "01-Nov-2005",
"revision": 1,
"news_item_format": "1.0",
"display_if_installed": [],
"display_if_profile": [],
"display_if_keyword": [],
"content": textwrap.dedent(
"""
YourSQL databases created using YourSQL version 4.0 are incompatible
with YourSQL version 4.1 or later. There is no reliable way to
automate the database format conversion, so action from the system
administrator is required before an upgrade can take place.
Please see the Gentoo YourSQL Upgrade Guide for instructions:
https://gentoo.org/doc/en/yoursql-upgrading.xml
Also see the official YourSQL documentation:
https://dev.example.com/doc/refman/4.1/en/upgrading-from-4-0.html
After upgrading, you should also recompile any packages which link
against YourSQL:
revdep-rebuild --library=libyoursqlclient.so.12
The revdep-rebuild tool is provided by app-portage/gentoolkit.
"""
),
}
def setUp(self) -> None:
self.profile_base = "/var/db/repos/gentoo/profiles/default-linux"
self.profile = f"{self.profile_base}/x86/2007.0/"
self.keywords = "x86"
# Consumers only use ARCH, so avoid portage.settings by using a dict
self.settings = {"ARCH": "x86"}
# Use fake/test dbapi to avoid slow tests
self.vardb = fakedbapi(self.settings)
def _createNewsItem(self, *kwargs) -> FakeNewsItem:
# Use our placeholders unless overridden
news_args = self.placeholders.copy()
# Substitute in what we're given to allow for easily passing
# just custom values.
news_args.update(*kwargs)
return FakeNewsItem(**news_args)
def _checkAndCreateNewsItem(
self, news_args: dict, relevant: bool = True, reason: str = ""
) -> FakeNewsItem:
return self._checkNewsItem(self._createNewsItem(news_args), relevant, reason)
def _checkNewsItem(self, item: NewsItem, relevant: bool = True, reason: str = ""):
self.assertTrue(item.isValid())
if relevant:
self.assertTrue(
item.isRelevant(self.vardb, self.settings, self.profile),
msg=f"Expected {item} to be relevant, but it was not!",
)
else:
self.assertFalse(
item.isRelevant(self.vardb, self.settings, self.profile),
msg=f"Expected {item} to be irrelevant, but it was relevant!",
)
def testNewsManager(self):
vardb = MagicMock()
portdb = MagicMock()
portdb.repositories.mainRepoLocation = MagicMock(return_value="/tmp/repo")
portdb.settings.profile_path = "/tmp/repo/profiles/arch/amd64"
news_manager = NewsManager(portdb, vardb, portdb.portdir, portdb.portdir)
self.assertEqual(news_manager._profile_path, "arch/amd64")
self.assertNotEqual(news_manager._profile_path, "tmp/repo/profiles/arch/amd64")
def testBasicNewsItem(self):
# Simple test with no filter fields (Display-If-*)
item = self._createNewsItem()
self.assertTrue(item.isValid())
self.assertTrue(item.isRelevant(self.vardb, self.settings, self.profile))
# Does an invalid item fail? ("a" is not a valid package name)
item = self._createNewsItem({"display_if_installed": "a"})
self.assertFalse(item.isValid())
def testDisplayIfProfile(self):
# We repeat all of these with the full profile path (including repo)
# and a relative path, as we've had issues there before.
# Note that we can't use _checkNewsItem() here as we override the
# profile value passed to isRelevant.
for profile_prefix in ("", self.profile_base):
# First, just check the simple case of one profile matching ours.
item = self._createNewsItem(
{"display_if_profile": [profile_prefix + self.profile]}
)
self.assertTrue(item.isValid())
self.assertTrue(
item.isRelevant(
self.vardb, self.settings, profile_prefix + self.profile
),
msg=f"Expected {item} to be relevant, but it was not!",
)
# Test the negative case: what if the only profile listed
# does *not* match ours?
item = self._createNewsItem(
{"display_if_profile": [profile_prefix + "profiles/i-do-not-exist"]}
)
self.assertTrue(item.isValid())
self.assertFalse(
item.isRelevant(
self.vardb, self.settings, profile_prefix + self.profile
),
msg=f"Expected {item} to be irrelevant, but it was relevant!",
)
# What if several profiles are listed and we match one of them?
item = self._createNewsItem(
{
"display_if_profile": [
profile_prefix + self.profile,
profile_prefix + f"{self.profile_base}/amd64/2023.0",
]
}
)
self.assertTrue(item.isValid())
self.assertTrue(
item.isRelevant(
self.vardb, self.settings, profile_prefix + self.profile
),
msg=f"Expected {item} to be relevant, but it was not!",
)
# What if several profiles are listed and we match none of them?
item = self._createNewsItem(
{
"display_if_profile": [
profile_prefix + f"{self.profile_base}/x86/2023.0",
profile_prefix + f"{self.profile_base}/amd64/2023.0",
]
}
)
self.assertTrue(item.isValid())
self.assertFalse(
item.isRelevant(
self.vardb, self.settings, profile_prefix + self.profile
),
msg=f"Expected {item} to be irrelevant, but it was relevant!",
)
def testDisplayIfInstalled(self):
self.vardb.cpv_inject("sys-apps/portage-2.0", {"SLOT": "0"})
self._checkAndCreateNewsItem({"display_if_installed": ["sys-apps/portage"]})
# Test the negative case: a single Display-If-Installed listing
# a package we don't have.
self._checkAndCreateNewsItem(
{"display_if_installed": ["sys-apps/i-do-not-exist"]}, False
)
# What about several packages and we have none of them installed?
self._checkAndCreateNewsItem(
{
"display_if_installed": [
"dev-util/pkgcheck",
"dev-util/pkgdev",
"sys-apps/pkgcore",
]
},
False,
)
# What about several packages and we have one of them installed?
self.vardb.cpv_inject("net-misc/openssh-9.2_p1", {"SLOT": "0"})
self._checkAndCreateNewsItem(
{
"display_if_installed": [
"net-misc/openssh",
"net-misc/dropbear",
]
}
)
# What about several packages and we have all of them installed?
# Note: we already have openssh added from the above test
self.vardb.cpv_inject("net-misc/dropbear-2022.83", {"SLOT": "0"})
self._checkAndCreateNewsItem(
{
"display_if_installed": [
"net-misc/openssh",
"net-misc/dropbear",
]
}
)
# What if we have a newer version of the listed package which
# shouldn't match the constraint?
self._checkAndCreateNewsItem(
{
"display_if_installed": [
"<net-misc/openssh-9.2_p1",
]
},
False,
)
# What if we have a newer version of the listed package which
# should match the constraint?
self._checkAndCreateNewsItem(
{
"display_if_installed": [
">=net-misc/openssh-9.2_p1",
]
}
)
# What if the item lists multiple packages and we have one of
# them installed, but not all?
# (Note that openssh is already "installed" by this point because
# of a previous test.)
self._checkAndCreateNewsItem(
{
"display_if_installed": [
">=net-misc/openssh-9.2_p1",
"<net-misc/openssh-9.2_p1",
]
}
)
def testDisplayIfKeyword(self):
self._checkAndCreateNewsItem({"display_if_keyword": [self.keywords]})
# Test the negative case: a keyword we don't have set.
self._checkAndCreateNewsItem({"display_if_keyword": ["fake-keyword"]}, False)
# What if several keywords are listed and we match one of them?
self._checkAndCreateNewsItem(
{"display_if_keyword": [self.keywords, "amd64", "~hppa"]}
)
# What if several keywords are listed and we match none of them?
self._checkAndCreateNewsItem({"display_if_keyword": ["amd64", "~hppa"]}, False)
# What if the ~keyword (testing) keyword is listed but we're keyword (stable)?
self._checkAndCreateNewsItem(
{
"display_if_keyword": [
f"~{self.keywords}",
]
},
False,
)
# What if the stable keyword is listed but we're ~keyword (testing)?
self._checkAndCreateNewsItem(
{
"display_if_keyword": [
f"{self.keywords}",
]
}
)
def testMultipleRestrictions(self):
# GLEP 42 specifies an algorithm for how combining restrictions
# should work. See https://www.gentoo.org/glep/glep-0042.html#news-item-headers.
# Different types of Display-If-* are ANDed, not ORed.
# What if there's a Display-If-Keyword that matches and a
# Display-If-Installed which does too?
self.vardb.cpv_inject("sys-apps/portage-2.0", {"SLOT": "0"})
self._checkAndCreateNewsItem(
{
"display_if_keyword": [self.keywords],
"display_if_installed": ["sys-apps/portage"],
}
)
# What if there's a Display-If-Keyword that matches and a
# Display-If-Installed which doesn't?
self._checkAndCreateNewsItem(
{
"display_if_keyword": [self.keywords],
"display_if_installed": ["sys-apps/i-do-not-exist"],
},
False,
)
# What if there's a Display-If-{Installed,Keyword,Profile} and
# they all match?
# (Note that sys-apps/portage is already "installed" by this point
# because of the above test.)
self._checkAndCreateNewsItem(
{
"display_if_keyword": [self.keywords],
"display_if_installed": ["sys-apps/portage"],
"display_if_profile": [self.profile],
}
)
# What if there's a Display-If-{Installed,Keyword,Profile} and
# none of them match?
# (Note that sys-apps/portage is already "installed" by this point
# because of the above test.)
self._checkAndCreateNewsItem(
{
"display_if_keyword": ["i-do-not-exist"],
"display_if_installed": ["sys-apps/i-do-not-exist"],
"display_if_profile": [self.profile_base + "/i-do-not-exist"],
},
False,
)