~ubuntu-core-dev/update-manager/main

2511.1.2 by Michael Terry
all tests are now pep8
1
#!/usr/bin/python3
2
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-
2095 by Michael Vogt
UpdateManager/Core/utils.py: add is_child_of_process_name() and add test
3
4
import logging
2428.5.41 by Michael Terry
use DistUpgrade's config parser, since the metapkg file is not parsable by GLib.KeyFile and add some tests
5
import mock
2095 by Michael Vogt
UpdateManager/Core/utils.py: add is_child_of_process_name() and add test
6
import sys
7
import unittest
8
2428.5.41 by Michael Terry
use DistUpgrade's config parser, since the metapkg file is not parsable by GLib.KeyFile and add some tests
9
from UpdateManager.Core import utils
2511.1.2 by Michael Terry
all tests are now pep8
10
2095 by Michael Vogt
UpdateManager/Core/utils.py: add is_child_of_process_name() and add test
11
12
class TestUtils(unittest.TestCase):
13
2320 by Michael Vogt
* lp:~evfool/update-manager/lp351665:
14
    def test_humanize_size(self):
15
        # humanize size is a bit funny, it rounds up to kB as the meaningful
16
        # unit for users
2428.5.41 by Michael Terry
use DistUpgrade's config parser, since the metapkg file is not parsable by GLib.KeyFile and add some tests
17
        self.assertEqual(utils.humanize_size(1000), "1 kB")
18
        self.assertEqual(utils.humanize_size(10), "1 kB")
19
        self.assertEqual(utils.humanize_size(1200), "2 kB")
2320 by Michael Vogt
* lp:~evfool/update-manager/lp351665:
20
        # but not for MB as well
2428.5.41 by Michael Terry
use DistUpgrade's config parser, since the metapkg file is not parsable by GLib.KeyFile and add some tests
21
        self.assertEqual(utils.humanize_size(1200 * 1000), "1.2 MB")
22
        self.assertEqual(utils.humanize_size(1478 * 1000), "1.5 MB")
2320 by Michael Vogt
* lp:~evfool/update-manager/lp351665:
23
        # and we don't go to Gb  just yet (as its not really needed
24
        # in a upgrade context most of the time
2428.5.41 by Michael Terry
use DistUpgrade's config parser, since the metapkg file is not parsable by GLib.KeyFile and add some tests
25
        self.assertEqual(utils.humanize_size(1000 * 1000 * 1000), "1000.0 MB")
2320 by Michael Vogt
* lp:~evfool/update-manager/lp351665:
26
2095 by Michael Vogt
UpdateManager/Core/utils.py: add is_child_of_process_name() and add test
27
    def test_is_child_of_process_name(self):
2784 by Brian Murray
test_utils.py: mock /proc/net/tcp and processes in proc so that we are
28
        data = "1 (systemd) S 0 1 1 0 -1 4194560 255183 71659081 87 18403 \
29
816 737 430168 107409 20 0 1 0 2 218931200 1882 18446744073709551615 1 1 0 0 \
30
0 0 671173123 4096 1260 0 0 0 17 3 0 0 469245113 0 258124 0 0 0 0 0 0 0 0"
31
        with mock.patch("builtins.open",
32
                        mock.mock_open(read_data=data)) as mock_file:
33
            assert open("/proc/1/stat").read() == data
34
            mock_file.assert_called_with("/proc/1/stat")
2841.1.1 by Balint Reczey
Ignore PEP 8 W503 instead of E502 and drop many added backslashes
35
            self.assertTrue(utils.is_child_of_process_name("init")
2840.1.1 by Balint Reczey
Fix PEP 8 warnings
36
                            or utils.is_child_of_process_name("systemd"))
2784 by Brian Murray
test_utils.py: mock /proc/net/tcp and processes in proc so that we are
37
            self.assertFalse(utils.is_child_of_process_name("mvo"))
2511.1.15 by Michael Terry
auto-pep8'd more warnings
38
2262 by Michael Vogt
* AutoUpgradeTester/UpgradeTestBackendQemu.py:
39
    def test_is_port_listening(self):
2511 by Michael Terry
fix one last split import from the tests
40
        from UpdateManager.Core.utils import is_port_already_listening
2784 by Brian Murray
test_utils.py: mock /proc/net/tcp and processes in proc so that we are
41
        data = "9: 00000000:0016 00000000:0000 0A 00000000:00000000 \
42
00:00000000 00000000     0        0 11366514 1 0000000000000000 100 \
43
0 0 10 0"
44
        with mock.patch("builtins.open",
45
                        mock.mock_open(read_data=data)) as mock_file:
46
            assert open("/proc/net/tcp").readlines() == [data]
47
            mock_file.assert_called_with("/proc/net/tcp")
48
            self.assertTrue(is_port_already_listening(22))
2095 by Michael Vogt
UpdateManager/Core/utils.py: add is_child_of_process_name() and add test
49
2235 by Michael Vogt
* DistUpgrade/DistUpgradeController.py, UpdateManager/Core/utils.py:
50
    def test_strip_auth_from_source_entry(self):
51
        from aptsources.sourceslist import SourceEntry
52
        # entry with PW
53
        s = SourceEntry("deb http://user:pass@some-ppa/ ubuntu main")
54
        self.assertTrue(
2685 by Michael Vogt
pep8 fixes to fix autopkgtest failure with the latest pep8
55
            "user" not in utils.get_string_with_no_auth_from_source_entry(s))
2235 by Michael Vogt
* DistUpgrade/DistUpgradeController.py, UpdateManager/Core/utils.py:
56
        self.assertTrue(
2685 by Michael Vogt
pep8 fixes to fix autopkgtest failure with the latest pep8
57
            "pass" not in utils.get_string_with_no_auth_from_source_entry(s))
2428.5.41 by Michael Terry
use DistUpgrade's config parser, since the metapkg file is not parsable by GLib.KeyFile and add some tests
58
        self.assertEqual(utils.get_string_with_no_auth_from_source_entry(s),
2235 by Michael Vogt
* DistUpgrade/DistUpgradeController.py, UpdateManager/Core/utils.py:
59
                         "deb http://hidden-u:hidden-p@some-ppa/ ubuntu main")
60
        # no pw
61
        s = SourceEntry("deb http://some-ppa/ ubuntu main")
2428.5.41 by Michael Terry
use DistUpgrade's config parser, since the metapkg file is not parsable by GLib.KeyFile and add some tests
62
        self.assertEqual(utils.get_string_with_no_auth_from_source_entry(s),
2235 by Michael Vogt
* DistUpgrade/DistUpgradeController.py, UpdateManager/Core/utils.py:
63
                         "deb http://some-ppa/ ubuntu main")
2511.1.15 by Michael Terry
auto-pep8'd more warnings
64
2428.5.41 by Michael Terry
use DistUpgrade's config parser, since the metapkg file is not parsable by GLib.KeyFile and add some tests
65
    @mock.patch('UpdateManager.Core.utils._load_meta_pkg_list')
66
    def test_flavor_package_ubuntu_first(self, mock_load):
67
        cache = {'ubuntu-desktop': mock.MagicMock(),
68
                 'other-desktop': mock.MagicMock()}
69
        cache['ubuntu-desktop'].is_installed = True
70
        cache['other-desktop'].is_installed = True
71
        mock_load.return_value = ['other-desktop']
72
        self.assertEqual(utils.get_ubuntu_flavor_package(cache=cache),
73
                         'ubuntu-desktop')
74
75
    @mock.patch('UpdateManager.Core.utils._load_meta_pkg_list')
76
    def test_flavor_package_match(self, mock_load):
77
        cache = {'a': mock.MagicMock(),
78
                 'b': mock.MagicMock(),
79
                 'c': mock.MagicMock()}
80
        cache['a'].is_installed = True
81
        cache['b'].is_installed = True
82
        cache['c'].is_installed = True
83
        mock_load.return_value = ['c', 'a', 'b']
84
        # Must pick alphabetically first
85
        self.assertEqual(utils.get_ubuntu_flavor_package(cache=cache), 'a')
86
87
    def test_flavor_package_default(self):
88
        self.assertEqual(utils.get_ubuntu_flavor_package(cache={}),
89
                         'ubuntu-desktop')
90
91
    def test_flavor_default(self):
92
        self.assertEqual(utils.get_ubuntu_flavor(cache={}), 'ubuntu')
93
94
    @mock.patch('UpdateManager.Core.utils.get_ubuntu_flavor_package')
95
    def test_flavor_simple(self, mock_package):
96
        mock_package.return_value = 'd'
97
        self.assertEqual(utils.get_ubuntu_flavor(), 'd')
98
99
    @mock.patch('UpdateManager.Core.utils.get_ubuntu_flavor_package')
100
    def test_flavor_chop(self, mock_package):
101
        mock_package.return_value = 'd-pkg'
102
        self.assertEqual(utils.get_ubuntu_flavor(), 'd')
103
104
    @mock.patch('UpdateManager.Core.utils.get_ubuntu_flavor_package')
105
    def test_flavor_name_desktop(self, mock_package):
106
        mock_package.return_value = 'something-desktop'
107
        self.assertEqual(utils.get_ubuntu_flavor_name(), 'Something')
108
109
    @mock.patch('UpdateManager.Core.utils.get_ubuntu_flavor_package')
110
    def test_flavor_name_netbook(self, mock_package):
111
        mock_package.return_value = 'something-netbook'
112
        self.assertEqual(utils.get_ubuntu_flavor_name(), 'Something')
113
114
    @mock.patch('UpdateManager.Core.utils.get_ubuntu_flavor_package')
115
    def test_flavor_name_studio(self, mock_package):
116
        mock_package.return_value = 'ubuntustudio-desktop'
117
        self.assertEqual(utils.get_ubuntu_flavor_name(), 'Ubuntu Studio')
118
2235 by Michael Vogt
* DistUpgrade/DistUpgradeController.py, UpdateManager/Core/utils.py:
119
2095 by Michael Vogt
UpdateManager/Core/utils.py: add is_child_of_process_name() and add test
120
if __name__ == '__main__':
121
    if len(sys.argv) > 1 and sys.argv[1] == "-v":
122
        logging.basicConfig(level=logging.DEBUG)
123
    unittest.main()