|
1
|
+import unittest
|
|
2
|
+from types import SimpleNamespace
|
|
3
|
+from unittest.mock import MagicMock, patch
|
|
4
|
+
|
|
5
|
+import mozunit
|
|
6
|
+
|
|
7
|
+from mozbuild.tbbutils import get_artifact_path, list_files_http
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+class TestGetArtifactName(unittest.TestCase):
|
|
11
|
+ def setUp(self):
|
|
12
|
+ self.artifact = "artifact"
|
|
13
|
+ self.host = "linux64"
|
|
14
|
+
|
|
15
|
+ @patch("mozbuild.tbbutils.TOR_BROWSER_BUILD_ARTIFACTS", new=["artifact"])
|
|
16
|
+ def test_artifact_in_tbb_artifacts(self):
|
|
17
|
+ from mozbuild.tbbutils import get_artifact_name
|
|
18
|
+
|
|
19
|
+ result = get_artifact_name(self.artifact, self.host)
|
|
20
|
+ self.assertEqual(result, self.artifact)
|
|
21
|
+
|
|
22
|
+ @patch("mozbuild.tbbutils.ARTIFACT_NAME_MAP", new={"artifact": "tcafitra"})
|
|
23
|
+ def test_host_is_not_linux64(self):
|
|
24
|
+ from mozbuild.tbbutils import get_artifact_name
|
|
25
|
+
|
|
26
|
+ result = get_artifact_name(self.artifact, "linux64-aarch64")
|
|
27
|
+ self.assertIsNone(result)
|
|
28
|
+
|
|
29
|
+ @patch("mozbuild.tbbutils.ARTIFACT_NAME_MAP", new={"artifact": "tcafitra"})
|
|
30
|
+ def test_mapped_artifact(self):
|
|
31
|
+ from mozbuild.tbbutils import get_artifact_name
|
|
32
|
+
|
|
33
|
+ result = get_artifact_name(self.artifact, self.host)
|
|
34
|
+ self.assertEqual(result, self.artifact[::-1])
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+class TestGetArtifactPath(unittest.TestCase):
|
|
38
|
+ def setUp(self):
|
|
39
|
+ self.url = "http://example.com"
|
|
40
|
+ self.artifact = "artifact"
|
|
41
|
+ # This is just an example target which is valid. But it doesn't make
|
|
42
|
+ # any difference and could be anything for these tests.
|
|
43
|
+ self.target = SimpleNamespace(tor_browser_build_alias="linux", cpu="x86_64")
|
|
44
|
+
|
|
45
|
+ @patch("mozbuild.tbbutils.list_files_http")
|
|
46
|
+ def test_no_files_returns_none(self, mock_list_files):
|
|
47
|
+ mock_list_files.return_value = []
|
|
48
|
+ result = get_artifact_path(self.url, self.artifact, self.target)
|
|
49
|
+ self.assertIsNone(result)
|
|
50
|
+
|
|
51
|
+ @patch("mozbuild.tbbutils.list_files_http")
|
|
52
|
+ def test_single_artifact_match(self, mock_list_files):
|
|
53
|
+ mock_list_files.return_value = ["artifact-1.zip"]
|
|
54
|
+ result = get_artifact_path(self.url, self.artifact, self.target)
|
|
55
|
+ self.assertEqual(result, f"{self.url}/{self.artifact}/artifact-1.zip")
|
|
56
|
+
|
|
57
|
+ @patch("mozbuild.tbbutils.list_files_http")
|
|
58
|
+ def test_artifact_without_os_returns_first(self, mock_list_files):
|
|
59
|
+ mock_list_files.return_value = ["artifact-1.zip", "artifact-2.zip"]
|
|
60
|
+ result = get_artifact_path(self.url, self.artifact, self.target)
|
|
61
|
+ self.assertTrue(result.startswith(f"{self.url}/{self.artifact}/"))
|
|
62
|
+ self.assertIn("artifact-", result)
|
|
63
|
+
|
|
64
|
+ @patch("mozbuild.tbbutils.list_files_http")
|
|
65
|
+ def test_artifact_with_os_match(self, mock_list_files):
|
|
66
|
+ mock_list_files.return_value = [
|
|
67
|
+ "artifact-windows.zip",
|
|
68
|
+ "artifact-linux.zip",
|
|
69
|
+ ]
|
|
70
|
+ result = get_artifact_path(self.url, self.artifact, self.target)
|
|
71
|
+ self.assertEqual(result, f"{self.url}/{self.artifact}/artifact-linux.zip")
|
|
72
|
+
|
|
73
|
+ @patch("mozbuild.tbbutils.list_files_http")
|
|
74
|
+ def test_artifact_with_cpu_match(self, mock_list_files):
|
|
75
|
+ mock_list_files.return_value = [
|
|
76
|
+ "artifact-linux-arm.zip",
|
|
77
|
+ "artifact-linux-x86_64.zip",
|
|
78
|
+ ]
|
|
79
|
+ result = get_artifact_path(self.url, self.artifact, self.target)
|
|
80
|
+ self.assertEqual(
|
|
81
|
+ result, f"{self.url}/{self.artifact}/artifact-linux-x86_64.zip"
|
|
82
|
+ )
|
|
83
|
+
|
|
84
|
+ @patch("mozbuild.tbbutils.list_files_http")
|
|
85
|
+ def test_artifact_with_prefix(self, mock_list_files):
|
|
86
|
+ mock_list_files.return_value = ["artifact-1.zip"]
|
|
87
|
+
|
|
88
|
+ prefix = "prefix"
|
|
89
|
+ result = get_artifact_path(self.url, self.artifact, self.target, prefix=prefix)
|
|
90
|
+ self.assertEqual(result, f"{self.url}/{prefix}/artifact-1.zip")
|
|
91
|
+ mock_list_files.assert_called_with(f"{self.url}/{prefix}?C=M;O=D")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+class TestListFilesHttp(unittest.TestCase):
|
|
95
|
+ def setUp(self):
|
|
96
|
+ self.url = "http://example.com"
|
|
97
|
+
|
|
98
|
+ @patch("mozbuild.tbbutils.urlopen")
|
|
99
|
+ def test_non_200_status_returns_empty(self, mock_urlopen):
|
|
100
|
+ mock_resp = MagicMock()
|
|
101
|
+ mock_resp.status = 404
|
|
102
|
+ mock_resp.read.return_value = b""
|
|
103
|
+ mock_urlopen.return_value.__enter__.return_value = mock_resp
|
|
104
|
+
|
|
105
|
+ result = list_files_http(self.url)
|
|
106
|
+ self.assertEqual(result, [])
|
|
107
|
+
|
|
108
|
+ @patch("mozbuild.tbbutils.urlopen")
|
|
109
|
+ def test_exception_returns_empty(self, mock_urlopen):
|
|
110
|
+ mock_urlopen.side_effect = Exception("network error")
|
|
111
|
+ result = list_files_http(self.url)
|
|
112
|
+ self.assertEqual(result, [])
|
|
113
|
+
|
|
114
|
+ @patch("mozbuild.tbbutils.urlopen")
|
|
115
|
+ def test_regular_links(self, mock_urlopen):
|
|
116
|
+ html = b"""
|
|
117
|
+ <html><body>
|
|
118
|
+ <a href="../">Parent</a>
|
|
119
|
+ <a href="file1.zip">file1</a>
|
|
120
|
+ <a href="file2.zip">file2</a>
|
|
121
|
+ </body></html>
|
|
122
|
+ """
|
|
123
|
+ mock_resp = MagicMock()
|
|
124
|
+ mock_resp.status = 200
|
|
125
|
+ mock_resp.read.return_value = html
|
|
126
|
+ mock_urlopen.return_value.__enter__.return_value = mock_resp
|
|
127
|
+
|
|
128
|
+ result = list_files_http(self.url)
|
|
129
|
+ self.assertEqual(result, ["file1.zip", "file2.zip"])
|
|
130
|
+
|
|
131
|
+ @patch("mozbuild.tbbutils.urlopen")
|
|
132
|
+ def test_tor_expert_bundle_rewrites(self, mock_urlopen):
|
|
133
|
+ html = """
|
|
134
|
+ <a href="tor-expert-bundle">bundle</a>
|
|
135
|
+ """
|
|
136
|
+ mock_resp = MagicMock()
|
|
137
|
+ mock_resp.status = 200
|
|
138
|
+ mock_resp.read.return_value = html.encode()
|
|
139
|
+ mock_urlopen.return_value.__enter__.return_value = mock_resp
|
|
140
|
+
|
|
141
|
+ result = list_files_http(self.url)
|
|
142
|
+ self.assertEqual(
|
|
143
|
+ result,
|
|
144
|
+ [
|
|
145
|
+ "tor-expert-bundle/tor-expert-bundle.tar.gz",
|
|
146
|
+ ],
|
|
147
|
+ )
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+if __name__ == "__main__":
|
|
151
|
+ mozunit.main() |