-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_utils.py
More file actions
162 lines (140 loc) · 6.72 KB
/
test_utils.py
File metadata and controls
162 lines (140 loc) · 6.72 KB
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
#
# Sonar Scanner Python
# Copyright (C) 2011-2026 SonarSource Sàrl
# mailto:info AT sonarsource DOT com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
# 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
from io import BytesIO
import pathlib
import unittest
import unittest.mock
import pyfakefs.fake_filesystem_unittest as pyfakefs
from pysonar_scanner.utils import Arch, Os, get_arch, get_os, remove_trailing_slash, calculate_checksum, extract_tar
class TestUtils(unittest.TestCase):
def test_removing_trailinlg_slash(self):
self.assertEqual(remove_trailing_slash("test/"), "test")
self.assertEqual(remove_trailing_slash(" test/ "), "test")
self.assertEqual(remove_trailing_slash(" test / "), "test")
self.assertEqual(remove_trailing_slash("test"), "test")
def test_get_os(self):
with self.subTest("os=Windows"), unittest.mock.patch("platform.system", return_value="Windows"):
self.assertEqual(get_os(), Os.WINDOWS)
with self.subTest("os=Darwin"), unittest.mock.patch("platform.system", return_value="Darwin"):
self.assertEqual(get_os(), Os.MACOS)
def test_get_arch(self):
x64_machine_strs = ["amd64", "AmD64", "x86_64", "X86_64"]
for machine_str in x64_machine_strs:
with self.subTest("amd64", machine_str=machine_str), unittest.mock.patch(
"platform.machine", return_value=machine_str
):
self.assertEqual(get_arch(), Arch.X64)
arm_machine_strs = ["arm64", "ARm64"]
for machine_str in arm_machine_strs:
with self.subTest("arm", machine_str=machine_str), unittest.mock.patch(
"platform.machine", return_value=machine_str
):
self.assertEqual(get_arch(), Arch.AARCH64)
class TestAlpineDetection(unittest.TestCase):
def setUp(self):
self.alpine_texts: list[str] = [
"""
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.21.3
PRETTY_NAME="Alpine Linux v3.21"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues""",
"""
ID="alpine"
VERSION_ID=3.21.3
PRETTY_NAME="Alpine Linux v3.21"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues""",
]
self.ubuntu_text = """
PRETTY_NAME="Ubuntu 22.04.5 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.5 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
"""
self.os_release_locations = [pathlib.Path("/etc/os-release"), pathlib.Path("/usr/lib/os-release")]
def test_os_release_for_alpine(self):
for os_release_location in self.os_release_locations:
for alpine_text in self.alpine_texts:
with (
self.subTest("os=alpine", text=alpine_text),
unittest.mock.patch("platform.system", return_value="Linux"),
pyfakefs.Patcher() as patcher,
):
assert patcher.fs is not None
patcher.fs.create_file(os_release_location, contents=alpine_text)
self.assertEqual(get_os(), Os.ALPINE)
def test_os_release_for_generic_linux(self):
for os_release_location in self.os_release_locations:
with (
self.subTest("os=ubuntu", text=self.ubuntu_text),
unittest.mock.patch("platform.system", return_value="Linux"),
pyfakefs.Patcher() as patcher,
):
assert patcher.fs is not None
patcher.fs.create_file(os_release_location, contents=self.ubuntu_text)
self.assertEqual(get_os(), Os.LINUX)
def test_os_release_does_not_exist(self):
with (
self.subTest("os_release does not exist"),
unittest.mock.patch("platform.system", return_value="Linux"),
pyfakefs.Patcher(),
):
self.assertFalse(pathlib.Path("/etc/os-release").exists())
self.assertFalse(pathlib.Path("/usr/lib/os-release").exists())
self.assertEqual(get_os(), Os.LINUX)
class TestCalculateChecksum(unittest.TestCase):
def test_calculate_checksum(self):
self.assertEqual(
calculate_checksum(BytesIO(b"test")), "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
)
self.assertEqual(
calculate_checksum(BytesIO(b"test test")),
"03ffdf45276dd38ffac79b0e9c6c14d89d9113ad783d5922580f4c66a3305591",
)
class TestExtractTar(unittest.TestCase):
def setUp(self):
self.test_path = pathlib.Path("/fake/path/archive.tar.gz")
self.test_target_dir = pathlib.Path("/fake/target/dir")
@unittest.mock.patch("tarfile.open")
@unittest.mock.patch("sys.version_info", (3, 12, 0))
def test_extract_tar_python_3_12_or_higher(self, mock_open):
mock_tar = unittest.mock.MagicMock()
mock_open.return_value.__enter__.return_value = mock_tar
extract_tar(self.test_path, self.test_target_dir)
mock_open.assert_called_once_with(self.test_path, "r:gz")
mock_tar.extractall.assert_called_once_with(self.test_target_dir, filter="data")
@unittest.mock.patch("tarfile.open")
@unittest.mock.patch("sys.version_info", (3, 11, 0))
def test_extract_tar_python_older_than_3_12(self, mock_open):
mock_tar = unittest.mock.MagicMock()
mock_open.return_value.__enter__.return_value = mock_tar
extract_tar(self.test_path, self.test_target_dir)
mock_open.assert_called_once_with(self.test_path, "r:gz")
mock_tar.extractall.assert_called_once_with(self.test_target_dir)