Merge ~lamoura/update-notifier:show-only-esm-infra-alerts-on-esm-machine into update-notifier:master

Proposed by Lucas Albuquerque Medeiros de Moura
Status: Merged
Merged at revision: 1374e16b36c9217721ee42a79e1ce1691121ed2f
Proposed branch: ~lamoura/update-notifier:show-only-esm-infra-alerts-on-esm-machine
Merge into: update-notifier:master
Diff against target: 560 lines (+314/-53)
3 files modified
data/apt_check.py (+76/-22)
debian/changelog (+11/-0)
tests/test_motd.py (+227/-31)
Reviewer Review Type Date Requested Status
Brian Murray Needs Fixing
Review via email: mp+402163@code.launchpad.net

Description of the change

Currently, we are showing esm-apps alerts if the user has an unauthenticated esm-apps source file and is using a LTS distro. However, we don't want to show this alert if the user is using a ESM distro, since esm-apps will not do much in that situation. We are updating that alert to show only
if the user is not using a ESM distro.

To test that change, we have used the following script:
--------------------------------------------------------

#!/bin/sh
set -x

series=xenial
name=$series-dev

function generate_motd_message() {
    message=$1
    echo "-----------------------------------------------------------"
    echo $message
    lxc exec $name -- /usr/lib/update-notifier/update-motd-updates-available --force
    lxc exec $name -- update-motd
    echo "-----------------------------------------------------------"
}

function setup_update_notifier_from_ppa() {
    lxc exec $name -- add-apt-repository ppa:lamoura/update-notifier-test-ppa -y
    lxc exec $name -- apt update
    lxc exec $name -- sh -c "apt install update-notifier -yq > /dev/null"
}

function setup_update_notifier_from_proposed() {
    lxc exec $name -- sh -c "cat <<EOF >/etc/apt/sources.list.d/ubuntu-$series-proposed.list
    deb http://archive.ubuntu.com/ubuntu/ $series-proposed restricted main multiverse universe"
    lxc exec $name -- apt update
    lxc exec $name -- sh -c "apt install update-notifier -yq > /dev/null"
}

function install_ua() {

config_file="# Ubuntu-Advantage client config file.
contract_url: 'https://contracts.canonical.com'
security_url: 'https://ubuntu.com/security'
data_dir: /var/lib/ubuntu-advantage
log_level: debug
log_file: /var/log/ubuntu-advantage.log
features:
  allow_beta: true"

    lxc exec $name -- add-apt-repository ppa:ua-client/daily -y
    lxc exec $name -- sudo apt-get update
    lxc exec $name -- mkdir /etc/ubuntu-advantage/
    lxc exec $name -- touch /etc/ubuntu-advantage/uaclient.conf
    lxc exec $name -- sh -c "echo '$config_file' >> /etc/ubuntu-advantage/uaclient.conf"
    lxc exec $name -- sh -c "yes N | apt-get install ubuntu-advantage-tools -y"
    lxc exec $name -- ua version
    lxc exec $name -- sudo apt-get update
}

function install_update_motd() {
    lxc exec $name -- apt install update-motd -yq
}

lxc delete --force $name
lxc launch ubuntu-daily:$series $name
sleep 10

setup_update_notifier_from_proposed
install_update_motd
install_ua
generate_motd_message "xenial-esm"
setup_update_notifier_from_ppa
generate_motd_message "xenial-esm"

set +x
------------------------------

Additionally, the xenial version of this package can already be found on the following ppa:
https://launchpad.net/~lamoura/+archive/ubuntu/update-notifier-test-ppa/

To post a comment you must log in.
Revision history for this message
Chad Smith (chad.smith) wrote :

Lucas , thanks for this. A note about a change Lech requested today on https://github.com/canonical/ubuntu-advantage-client/issues/1584 would ask that we also adapt the following text:

Learn more about enabling UA Infra: ESM service at https://ubuntu.com/esm

TO

Learn more about enabling UA Infra: ESM service for Ubuntu 16.04 at https://ubuntu.com/16-04

on active ESM releases for Infra service.

Note that when Bionic or Fcal goes into ESM we'll need something like:

Learn more about enabling UA Infra: ESM service for Ubuntu 18.04 at https://ubuntu.com/18-04

Revision history for this message
Brian Murray (brian-murray) wrote :

These changes look good to me but I imagine you'll want to SRU them. Do you have SRU bugs prepared? If you do could please add and updated changelog entry too?

review: Needs Information
Revision history for this message
Lucas Albuquerque Medeiros de Moura (lamoura) wrote :

Hi Brian, thanks for the review. About the SRU bug, yes we have one already for it:
https://bugs.launchpad.net/ubuntu/+source/update-notifier/+bug/1926990

I have also added the code to retry on calling lsb_release and I have added a new entry on changelog too

Revision history for this message
Brian Murray (brian-murray) wrote :

I think instead of retrying a call to lsb_release a number of times it'd be better to use a different mechanism to determine the codename i.e. by reading it out of /etc/os-release. Additionally, if there is such a fallback mechanism in place it should also exist for get_distro_version(). Thanks!

review: Needs Fixing
Revision history for this message
Lucas Albuquerque Medeiros de Moura (lamoura) wrote :

Thank you for the review Brian, I have updated the code to source both distro name and version from
/etc/os-release

Revision history for this message
Lucas Albuquerque Medeiros de Moura (lamoura) wrote :

Brian, also we could have named exceptions here, but I don't know if that is an overkill for the script

Revision history for this message
Brian Murray (brian-murray) wrote :

I've one inline comment.

Revision history for this message
Lucas Albuquerque Medeiros de Moura (lamoura) wrote :

Good catch Brian, I have updated the code

Revision history for this message
Chad Smith (chad.smith) :
Revision history for this message
Chad Smith (chad.smith) wrote :

integration Test runs:
#### xenial should prefer infra messages
 # esm-infra and esm-apps disabled
UA Infra: Extended Security Maintenance (ESM) is not enabled.

25 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable

Enable UA Infra: ESM to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo ua status

# esm-infra enabled
UA Infra: Extended Security Maintenance (ESM) is enabled.

25 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable

# esm-infra and esm-apps enabled
UA Infra: Extended Security Maintenance (ESM) is enabled.

25 updates can be applied immediately.
1 of these updates is a UA Apps: ESM security update.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable

# esm-apps enabled
UA Infra: Extended Security Maintenance (ESM) is not enabled.

24 updates can be applied immediately.
1 of these updates is a UA Apps: ESM security update.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable

Enable UA Infra: ESM to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo ua status

# Bionic, I'm surprised we don't also have a message about "UA Apps: Extended Security Maintenance (ESM) is enabled." on LTS releases that are not in active ESM (bionic++). I was under the impression that we needed it there when ESM-Apps gets released. This can be handled later, but something we should check on for next Bionic SRU

Revision history for this message
Lucas Albuquerque Medeiros de Moura (lamoura) wrote :

Hi Chad, thank you for the review.

I have opened a PR to handle the esm-apps header situation:
https://code.launchpad.net/~lamoura/update-notifier/+git/update-notifier/+merge/402587

And for the version id case, I don't know if we should handle it. This is corner case, and I think we should fail earlier on that situation, because as you said, we will not have the distro info as well. But, the corner case of the corner case is if we have os-release, but not VERSION_ID on it. Maybe, in that situation it is worth making that check

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/data/apt_check.py b/data/apt_check.py
2index 2f2bb3d..3b9998c 100755
3--- a/data/apt_check.py
4+++ b/data/apt_check.py
5@@ -8,14 +8,45 @@ import apt_pkg
6 import os
7 import sys
8 from optparse import OptionParser
9+import re
10 import gettext
11-import subprocess
12 import distro_info
13
14+
15 SYNAPTIC_PINFILE = "/var/lib/synaptic/preferences"
16-DISTRO = subprocess.check_output(
17- ["lsb_release", "-c", "-s"],
18- universal_newlines=True).strip()
19+OS_RELEASE_PATH = "/etc/os-release"
20+
21+
22+def _get_info_from_os_release(key):
23+ " get info directly from os-release file "
24+ if os.path.exists(OS_RELEASE_PATH):
25+ with open(OS_RELEASE_PATH) as f:
26+ search_res = re.search(
27+ r"{}=(?P<name>.*)".format(key),
28+ f.read()
29+ )
30+ if search_res:
31+ return search_res.group("name")
32+ else:
33+ raise Exception(
34+ "Could not find {} in {}".format(
35+ key, OS_RELEASE_PATH
36+ )
37+ )
38+ else:
39+ raise Exception(
40+ "File {} was not found on the system".format(
41+ OS_RELEASE_PATH
42+ )
43+ )
44+
45+
46+def get_distro():
47+ " get distro name "
48+ return _get_info_from_os_release(key="UBUNTU_CODENAME")
49+
50+
51+DISTRO = get_distro()
52
53 ESM_INFRA_ORIGIN = "UbuntuESM"
54 ESM_APPS_ORIGIN = "UbuntuESMApps"
55@@ -31,6 +62,11 @@ def _handleException(type, value, tb):
56 sys.exit(-1)
57
58
59+def get_distro_version():
60+ " get distro version "
61+ return _get_info_from_os_release(key="VERSION_ID").replace('"', "")
62+
63+
64 def clean(cache, depcache):
65 " unmark (clean) all changes from the given depcache "
66 # mvo: looping is too inefficient with the new auto-mark code
67@@ -112,7 +148,7 @@ def _output_esm_package_count(outstream, service_type, esm_pkg_count):
68 outstream.write("\n")
69 outstream.write(gettext.dngettext("update-notifier",
70 "%d of these updates "
71- "is an UA %s: ESM "
72+ "is a UA %s: ESM "
73 "security update.",
74 "%d of these updates "
75 "are UA %s: ESM "
76@@ -122,27 +158,37 @@ def _output_esm_package_count(outstream, service_type, esm_pkg_count):
77
78
79 def _output_esm_package_alert(
80- outstream, service_type, disabled_pkg_count
81+ outstream, service_type, disabled_pkg_count, is_esm=False
82 ):
83 " output the number of upgradable packages if esm service was enabled "
84 outstream.write("\n")
85 if disabled_pkg_count > 0:
86 outstream.write("\n")
87+
88+ if is_esm:
89+ distro_version = get_distro_version()
90+ esm_info_url = "https://ubuntu.com/{}".format(
91+ distro_version.replace(".", "-")
92+ )
93+ learn_msg_suffix = "for Ubuntu {} at\n{}".format(
94+ distro_version, esm_info_url)
95+ else:
96+ learn_msg_suffix = "at https://ubuntu.com/esm"
97+
98 outstream.write(gettext.dngettext("update-notifier",
99 "%i additional security "
100 "update can be applied "
101 "with UA %s: ESM\nLearn "
102 "more about enabling UA "
103- "%s: ESM service at "
104- "https://ubuntu.com/esm",
105+ "%s: ESM service %s",
106 "%i additional security "
107 "updates can be applied "
108 "with UA %s: ESM\nLearn "
109 "more about enabling UA "
110- "%s: ESM service at "
111- "https://ubuntu.com/esm",
112+ "%s: ESM service %s",
113 disabled_pkg_count) %
114- (disabled_pkg_count, service_type, service_type))
115+ (disabled_pkg_count, service_type, service_type,
116+ learn_msg_suffix))
117 else:
118 outstream.write("\n")
119 outstream.write(gettext.dgettext("update-notifier",
120@@ -179,15 +225,14 @@ def write_human_readable_summary(outstream, upgrades, security_updates,
121 "UA Infra: Extended "
122 "Security Maintenance (ESM) is "
123 "not enabled."))
124- if upgrades > 0:
125- outstream.write("\n\n")
126- if upgrades > 0:
127- outstream.write(
128- gettext.dngettext("update-notifier",
129- "%i update can be applied immediately.",
130- "%i updates can be applied immediately.",
131- upgrades) % upgrades
132- )
133+ outstream.write("\n\n")
134+
135+ outstream.write(
136+ gettext.dngettext("update-notifier",
137+ "%i update can be applied immediately.",
138+ "%i updates can be applied immediately.",
139+ upgrades) % upgrades
140+ )
141
142 _output_esm_package_count(
143 outstream, service_type="Infra", esm_pkg_count=esm_infra_updates)
144@@ -210,7 +255,14 @@ def write_human_readable_summary(outstream, upgrades, security_updates,
145 "To see these additional updates "
146 "run: apt list --upgradable"))
147
148- if have_esm_apps is not None and not have_esm_apps and lts_distro:
149+ if all(
150+ [
151+ have_esm_apps is not None,
152+ not have_esm_apps,
153+ lts_distro,
154+ not esm_distro
155+ ]
156+ ):
157 _output_esm_package_alert(
158 outstream, service_type="Apps",
159 disabled_pkg_count=disabled_esm_apps_updates)
160@@ -218,7 +270,9 @@ def write_human_readable_summary(outstream, upgrades, security_updates,
161 if have_esm_infra is not None and not have_esm_infra and esm_distro:
162 _output_esm_package_alert(
163 outstream, service_type="Infra",
164- disabled_pkg_count=disabled_esm_infra_updates)
165+ disabled_pkg_count=disabled_esm_infra_updates,
166+ is_esm=True
167+ )
168
169 outstream.write("\n")
170
171diff --git a/debian/changelog b/debian/changelog
172index afd3937..af2f8c3 100644
173--- a/debian/changelog
174+++ b/debian/changelog
175@@ -1,3 +1,14 @@
176+update-notifier (3.192.43) impish; urgency=medium
177+
178+ * data/apt_check.py
179+ - Only show esm-apps alerts on lts and non-esm distros (LP: #1926990)
180+ - Update esm-infra alert for distros on ESM mode
181+ - Show message with number of upgradable packages even if that
182+ number is zero (LP: #1926819)
183+ - Get distro name and version directly from /etc/os-release
184+
185+ -- Lucas Moura <lucas.moura@canonical.com> Fri, 07 May 2021 10:47:49 -0300
186+
187 update-notifier (3.192.42) impish; urgency=medium
188
189 [ Dan Bungert ]
190diff --git a/tests/test_motd.py b/tests/test_motd.py
191index 335adce..26d4d4d 100755
192--- a/tests/test_motd.py
193+++ b/tests/test_motd.py
194@@ -18,10 +18,11 @@ def get_message(*args, **kwds):
195 class TestMotd(unittest.TestCase):
196 """ Validate /etc/motd text """
197
198+ @mock.patch("apt_check.get_distro_version", return_value="16.04")
199 @mock.patch("apt_check.is_lts_distro", return_value=True)
200 @mock.patch("apt_check.is_esm_distro", return_value=True)
201 def test_esm_infra_disabled_upto_date_esm_avail(
202- self, _m_esm_distro, _m_is_lts
203+ self, _m_esm_distro, _m_is_lts, _m_distro_version
204 ):
205 self.assertEqual(
206 get_message(upgrades=0, security_updates=0,
207@@ -33,17 +34,18 @@ class TestMotd(unittest.TestCase):
208 """\
209 UA Infra: Extended Security Maintenance (ESM) is not enabled.
210
211- Enable UA Apps: ESM to receive additional future security updates.
212- See https://ubuntu.com/esm or run: sudo ua status
213+ 0 updates can be applied immediately.
214
215 1 additional security update can be applied with UA Infra: ESM
216- Learn more about enabling UA Infra: ESM service at https://ubuntu.com/esm
217+ Learn more about enabling UA Infra: ESM service for Ubuntu 16.04 at
218+ https://ubuntu.com/16-04
219 """))
220
221+ @mock.patch("apt_check.get_distro_version", return_value="18.04")
222 @mock.patch("apt_check.is_lts_distro", return_value=True)
223 @mock.patch("apt_check.is_esm_distro", return_value=True)
224 def test_esm_infra_disabled_security_esm_avail(
225- self, _m_esm_distro, _m_is_lts
226+ self, _m_esm_distro, _m_is_lts, _m_distro_version
227 ):
228 self.assertEqual(
229 get_message(upgrades=15, security_updates=1,
230@@ -59,11 +61,9 @@ class TestMotd(unittest.TestCase):
231 1 of these updates is a standard security update.
232 To see these additional updates run: apt list --upgradable
233
234- Enable UA Apps: ESM to receive additional future security updates.
235- See https://ubuntu.com/esm or run: sudo ua status
236-
237 23 additional security updates can be applied with UA Infra: ESM
238- Learn more about enabling UA Infra: ESM service at https://ubuntu.com/esm
239+ Learn more about enabling UA Infra: ESM service for Ubuntu 18.04 at
240+ https://ubuntu.com/18-04
241 """))
242
243 @mock.patch("apt_check.is_lts_distro", return_value=True)
244@@ -85,9 +85,6 @@ class TestMotd(unittest.TestCase):
245 7 of these updates are standard security updates.
246 To see these additional updates run: apt list --upgradable
247
248- Enable UA Apps: ESM to receive additional future security updates.
249- See https://ubuntu.com/esm or run: sudo ua status
250-
251 Enable UA Infra: ESM to receive additional future security updates.
252 See https://ubuntu.com/esm or run: sudo ua status
253 """))
254@@ -110,9 +107,6 @@ class TestMotd(unittest.TestCase):
255 15 updates can be applied immediately.
256 To see these additional updates run: apt list --upgradable
257
258- Enable UA Apps: ESM to receive additional future security updates.
259- See https://ubuntu.com/esm or run: sudo ua status
260-
261 Enable UA Infra: ESM to receive additional future security updates.
262 See https://ubuntu.com/esm or run: sudo ua status
263 """))
264@@ -132,8 +126,7 @@ class TestMotd(unittest.TestCase):
265 """\
266 UA Infra: Extended Security Maintenance (ESM) is not enabled.
267
268- Enable UA Apps: ESM to receive additional future security updates.
269- See https://ubuntu.com/esm or run: sudo ua status
270+ 0 updates can be applied immediately.
271
272 Enable UA Infra: ESM to receive additional future security updates.
273 See https://ubuntu.com/esm or run: sudo ua status
274@@ -157,9 +150,6 @@ class TestMotd(unittest.TestCase):
275 35 updates can be applied immediately.
276 13 of these updates are UA Infra: ESM security updates.
277 To see these additional updates run: apt list --upgradable
278-
279- Enable UA Apps: ESM to receive additional future security updates.
280- See https://ubuntu.com/esm or run: sudo ua status
281 """))
282
283 @mock.patch("apt_check.is_lts_distro", return_value=True)
284@@ -181,9 +171,6 @@ class TestMotd(unittest.TestCase):
285 13 of these updates are UA Infra: ESM security updates.
286 7 of these updates are standard security updates.
287 To see these additional updates run: apt list --upgradable
288-
289- Enable UA Apps: ESM to receive additional future security updates.
290- See https://ubuntu.com/esm or run: sudo ua status
291 """))
292
293 @mock.patch("apt_check.is_lts_distro", return_value=True)
294@@ -201,8 +188,7 @@ class TestMotd(unittest.TestCase):
295 """\
296 UA Infra: Extended Security Maintenance (ESM) is enabled.
297
298- 10 additional security updates can be applied with UA Apps: ESM
299- Learn more about enabling UA Apps: ESM service at https://ubuntu.com/esm
300+ 0 updates can be applied immediately.
301 """))
302
303 @mock.patch("apt_check.is_lts_distro", return_value=True)
304@@ -250,10 +236,11 @@ class TestMotd(unittest.TestCase):
305 See https://ubuntu.com/esm or run: sudo ua status
306 """).lstrip())
307
308+ @mock.patch("apt_check.get_distro_version", return_value="16.04")
309 @mock.patch("apt_check.is_lts_distro", return_value=True)
310 @mock.patch("apt_check.is_esm_distro", return_value=True)
311 def test_esm_infra_disabled_wih_pkgs_and_esm_apps_enabled(
312- self, _m_esm_distro, _m_is_lts
313+ self, _m_esm_distro, _m_is_lts, _m_distro_version
314 ):
315 self.assertEqual(
316 get_message(upgrades=30, security_updates=15,
317@@ -266,12 +253,13 @@ class TestMotd(unittest.TestCase):
318 UA Infra: Extended Security Maintenance (ESM) is not enabled.
319
320 30 updates can be applied immediately.
321- 1 of these updates is an UA Apps: ESM security update.
322+ 1 of these updates is a UA Apps: ESM security update.
323 15 of these updates are standard security updates.
324 To see these additional updates run: apt list --upgradable
325
326 40 additional security updates can be applied with UA Infra: ESM
327- Learn more about enabling UA Infra: ESM service at https://ubuntu.com/esm
328+ Learn more about enabling UA Infra: ESM service for Ubuntu 16.04 at
329+ https://ubuntu.com/16-04
330 """).lstrip())
331
332 @mock.patch("apt_check.is_lts_distro", return_value=True)
333@@ -293,9 +281,6 @@ class TestMotd(unittest.TestCase):
334 18 of these updates are standard security updates.
335 To see these additional updates run: apt list --upgradable
336
337- 40 additional security updates can be applied with UA Apps: ESM
338- Learn more about enabling UA Apps: ESM service at https://ubuntu.com/esm
339-
340 Enable UA Infra: ESM to receive additional future security updates.
341 See https://ubuntu.com/esm or run: sudo ua status
342 """))
343@@ -398,6 +383,217 @@ class TestMotd(unittest.TestCase):
344 Learn more about enabling UA Apps: ESM service at https://ubuntu.com/esm
345 """))
346
347+ @mock.patch("apt_check.is_lts_distro", return_value=False)
348+ @mock.patch("apt_check.is_esm_distro", return_value=False)
349+ def test_message_no_upgrades_non_lts_distro_without_esm_sources(
350+ self, _m_is_esm, _m_is_lts
351+ ):
352+ self.assertEqual(
353+ get_message(upgrades=0, security_updates=0,
354+ esm_infra_updates=0, esm_apps_updates=0,
355+ have_esm_infra=None, have_esm_apps=None,
356+ disabled_esm_infra_updates=0,
357+ disabled_esm_apps_updates=0),
358+ textwrap.dedent(
359+ """\
360+ 0 updates can be applied immediately.
361+ """))
362+
363+ @mock.patch("apt_check.is_lts_distro", return_value=False)
364+ @mock.patch("apt_check.is_esm_distro", return_value=False)
365+ def test_message_one_upgrade_non_lts_distro_without_esm_sources(
366+ self, _m_is_esm, _m_is_lts
367+ ):
368+ self.assertEqual(
369+ get_message(upgrades=1, security_updates=0,
370+ esm_infra_updates=0, esm_apps_updates=0,
371+ have_esm_infra=None, have_esm_apps=None,
372+ disabled_esm_infra_updates=0,
373+ disabled_esm_apps_updates=0),
374+ textwrap.dedent(
375+ """\
376+ 1 update can be applied immediately.
377+ To see these additional updates run: apt list --upgradable
378+ """))
379+
380+ @mock.patch("apt_check.is_lts_distro", return_value=False)
381+ @mock.patch("apt_check.is_esm_distro", return_value=False)
382+ def test_message_one_security_upgrade_non_lts_distro_without_esm_sources(
383+ self, _m_is_esm, _m_is_lts
384+ ):
385+ self.assertEqual(
386+ get_message(upgrades=1, security_updates=1,
387+ esm_infra_updates=0, esm_apps_updates=0,
388+ have_esm_infra=None, have_esm_apps=None,
389+ disabled_esm_infra_updates=0,
390+ disabled_esm_apps_updates=0),
391+ textwrap.dedent(
392+ """\
393+ 1 update can be applied immediately.
394+ 1 of these updates is a standard security update.
395+ To see these additional updates run: apt list --upgradable
396+ """))
397+
398+ @mock.patch("apt_check.is_lts_distro", return_value=True)
399+ @mock.patch("apt_check.is_esm_distro", return_value=False)
400+ def test_message_no_upgrades_lts_distro_without_esm_sources(
401+ self, _m_is_esm, _m_is_lts
402+ ):
403+ self.assertEqual(
404+ get_message(upgrades=0, security_updates=0,
405+ esm_infra_updates=0, esm_apps_updates=0,
406+ have_esm_infra=None, have_esm_apps=None,
407+ disabled_esm_infra_updates=0,
408+ disabled_esm_apps_updates=0),
409+ textwrap.dedent(
410+ """\
411+ 0 updates can be applied immediately.
412+ """))
413+
414+ @mock.patch("apt_check.is_lts_distro", return_value=True)
415+ @mock.patch("apt_check.is_esm_distro", return_value=False)
416+ def test_message_no_upgrades_lts_distro_with_esm_sources(
417+ self, _m_is_esm, _m_is_lts
418+ ):
419+ self.assertEqual(
420+ get_message(upgrades=0, security_updates=0,
421+ esm_infra_updates=0, esm_apps_updates=0,
422+ have_esm_infra=False, have_esm_apps=False,
423+ disabled_esm_infra_updates=0,
424+ disabled_esm_apps_updates=0),
425+ textwrap.dedent(
426+ """\
427+ 0 updates can be applied immediately.
428+
429+ Enable UA Apps: ESM to receive additional future security updates.
430+ See https://ubuntu.com/esm or run: sudo ua status
431+ """))
432+
433+ @mock.patch("apt_check.is_lts_distro", return_value=True)
434+ @mock.patch("apt_check.is_esm_distro", return_value=False)
435+ def test_message_one_upgrade_esm_distro_without_esm_sources(
436+ self, _m_is_esm, _m_is_lts
437+ ):
438+ self.assertEqual(
439+ get_message(upgrades=1, security_updates=0,
440+ esm_infra_updates=0, esm_apps_updates=0,
441+ have_esm_infra=None, have_esm_apps=None,
442+ disabled_esm_infra_updates=0,
443+ disabled_esm_apps_updates=0),
444+ textwrap.dedent(
445+ """\
446+ 1 update can be applied immediately.
447+ To see these additional updates run: apt list --upgradable
448+ """))
449+
450+ @mock.patch("apt_check.is_lts_distro", return_value=True)
451+ @mock.patch("apt_check.is_esm_distro", return_value=False)
452+ def test_message_one_security_upgrade_esm_distro_without_esm_sources(
453+ self, _m_is_esm, _m_is_lts
454+ ):
455+ self.assertEqual(
456+ get_message(upgrades=1, security_updates=1,
457+ esm_infra_updates=0, esm_apps_updates=0,
458+ have_esm_infra=None, have_esm_apps=None,
459+ disabled_esm_infra_updates=0,
460+ disabled_esm_apps_updates=0),
461+ textwrap.dedent(
462+ """\
463+ 1 update can be applied immediately.
464+ 1 of these updates is a standard security update.
465+ To see these additional updates run: apt list --upgradable
466+ """))
467+
468+
469+class TestGetDistro(unittest.TestCase):
470+ @mock.patch("os.path.exists", return_value=True)
471+ def test_get_distro_happy_path(self, _m_path):
472+ read_data = (
473+ "UBUNTU_CODENAME=test\n"
474+ "NAME=Ubuntu\n"
475+ "VERSION=version\n"
476+ "ID=ubuntu\n"
477+ "ID_LIKE=debian\n"
478+ "PRETTY_NAME=Ubuntu XX.XX\n"
479+ "VERSION_ID=XX.XX\n"
480+ )
481+
482+ expected_value = "test"
483+ with mock.patch(
484+ "builtins.open",
485+ mock.mock_open(read_data=read_data)
486+ ) as m_open:
487+ assert apt_check.get_distro() == expected_value
488+ assert 1 == _m_path.call_count
489+ assert [
490+ mock.call(apt_check.OS_RELEASE_PATH)
491+ ] == m_open.call_args_list
492+
493+ @mock.patch("os.path.exists", return_value=True)
494+ def test_get_distro_no_ubuntu_codename(self, _m_path):
495+ read_data = (
496+ "NAME=Ubuntu\n"
497+ "VERSION=version\n"
498+ "ID=ubuntu\n"
499+ "ID_LIKE=debian\n"
500+ "PRETTY_NAME=Ubuntu XX.XX\n"
501+ "VERSION_ID=XX.XX\n"
502+ )
503+ expected_msg = (
504+ "Could not find UBUNTU_CODENAME in "
505+ ) + apt_check.OS_RELEASE_PATH
506+
507+ with mock.patch(
508+ "builtins.open",
509+ mock.mock_open(read_data=read_data)
510+ ) as m_open:
511+ with self.assertRaises(Exception) as context:
512+ apt_check.get_distro()
513+
514+ assert expected_msg == str(context.exception)
515+
516+ assert 1 == _m_path.call_count
517+ assert [
518+ mock.call(apt_check.OS_RELEASE_PATH)
519+ ] == m_open.call_args_list
520+
521+ @mock.patch("os.path.exists", return_value=False)
522+ def test_get_distro_no_os_release(self, _m_path):
523+ expected_msg = "File {} was not found on the system".format(
524+ apt_check.OS_RELEASE_PATH
525+ )
526+
527+ with self.assertRaises(Exception) as context:
528+ apt_check.get_distro()
529+
530+ assert expected_msg == str(context.exception)
531+ assert 1 == _m_path.call_count
532+
533+
534+class TestDistroVersion(unittest.TestCase):
535+ @mock.patch("os.path.exists", return_value=True)
536+ def test_get_distro_version_happy_path(self, _m_path):
537+ read_data = (
538+ "UBUNTU_CODENAME=test\n"
539+ "NAME=Ubuntu\n"
540+ "VERSION=version\n"
541+ "ID=ubuntu\n"
542+ "ID_LIKE=debian\n"
543+ "PRETTY_NAME=Ubuntu XX.XX\n"
544+ 'VERSION_ID="XX.XX"\n'
545+ )
546+
547+ expected_value = "XX.XX"
548+ with mock.patch(
549+ "builtins.open",
550+ mock.mock_open(read_data=read_data)
551+ ) as m_open:
552+ assert apt_check.get_distro_version() == expected_value
553+ assert 1 == _m_path.call_count
554+ assert [
555+ mock.call(apt_check.OS_RELEASE_PATH)
556+ ] == m_open.call_args_list
557+
558
559 if __name__ == "__main__":
560 import logging

Subscribers

People subscribed via source and target branches