Debian Codegraph @

tests/sources/pip_build.py

raw
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 #
14
15 # Pylint doesn't play well with fixtures and dependency injection from pytest
16 # pylint: disable=redefined-outer-name
17
18 import os
19 import pytest
20
21 from buildstream import _yaml
22
23 from buildstream._testing import cli_integration as cli # pylint: disable=unused-import
24 from buildstream._testing.integration import assert_contains
25 from buildstream._testing.integration import integration_cache # pylint: disable=unused-import
26 from buildstream._testing._utils.site import HAVE_SANDBOX
27
28 from tests.testutils.python_repo import setup_pypi_repo # pylint: disable=unused-import
29
30
31 pytestmark = pytest.mark.integration
32
33
34 DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pip-build")
35
36
37 @pytest.mark.datafiles(DATA_DIR)
38 def test_pip_source_import_packages(cli, datafiles, setup_pypi_repo):
39 project = str(datafiles)
40 checkout = os.path.join(cli.directory, "checkout")
41 element_path = os.path.join(project, "elements")
42 element_name = "pip/hello.bst"
43
44 # check that exotically named packages are imported correctly
45 myreqs_packages = "hellolib"
46 dependencies = [
47 "app2",
48 "app.3",
49 "app-4",
50 "app_5",
51 "app.no.6",
52 "app-no-7",
53 "app_no_8",
54 ]
55 mock_packages = {myreqs_packages: {package: {} for package in dependencies}}
56
57 # create mock pypi repository
58 pypi_repo = os.path.join(project, "files", "pypi-repo")
59 os.makedirs(pypi_repo, exist_ok=True)
60 setup_pypi_repo(mock_packages, pypi_repo)
61
62 element = {
63 "kind": "import",
64 "sources": [
65 {"kind": "local", "path": "files/pip-source"},
66 {
67 "kind": "pip",
68 "url": "file://{}".format(os.path.realpath(pypi_repo)),
69 "packages": [myreqs_packages],
70 },
71 ],
72 }
73 os.makedirs(
74 os.path.dirname(os.path.join(element_path, element_name)),
75 exist_ok=True,
76 )
77 _yaml.roundtrip_dump(element, os.path.join(element_path, element_name))
78
79 result = cli.run(project=project, args=["source", "track", element_name])
80 assert result.exit_code == 0
81
82 result = cli.run(project=project, args=["build", element_name])
83 assert result.exit_code == 0
84
85 result = cli.run(
86 project=project,
87 args=["artifact", "checkout", element_name, "--directory", checkout],
88 )
89 assert result.exit_code == 0
90
91 assert_contains(
92 checkout,
93 [
94 "/.bst_pip_downloads",
95 "/.bst_pip_downloads/hellolib-0.1.tar.gz",
96 "/.bst_pip_downloads/app2-0.1.tar.gz",
97 "/.bst_pip_downloads/app_3-0.1.tar.gz",
98 "/.bst_pip_downloads/app_4-0.1.tar.gz",
99 "/.bst_pip_downloads/app_5-0.1.tar.gz",
100 "/.bst_pip_downloads/app_no_6-0.1.tar.gz",
101 "/.bst_pip_downloads/app_no_7-0.1.tar.gz",
102 "/.bst_pip_downloads/app_no_8-0.1.tar.gz",
103 ],
104 )
105
106
107 @pytest.mark.datafiles(DATA_DIR)
108 def test_pip_source_import_requirements_files(cli, datafiles, setup_pypi_repo):
109 project = str(datafiles)
110 checkout = os.path.join(cli.directory, "checkout")
111 element_path = os.path.join(project, "elements")
112 element_name = "pip/hello.bst"
113
114 # check that exotically named packages are imported correctly
115 myreqs_packages = "hellolib"
116 dependencies = [
117 "app2",
118 "app.3",
119 "app-4",
120 "app_5",
121 "app.no.6",
122 "app-no-7",
123 "app_no_8",
124 ]
125 mock_packages = {myreqs_packages: {package: {} for package in dependencies}}
126
127 # create mock pypi repository
128 pypi_repo = os.path.join(project, "files", "pypi-repo")
129 os.makedirs(pypi_repo, exist_ok=True)
130 setup_pypi_repo(mock_packages, pypi_repo)
131
132 element = {
133 "kind": "import",
134 "sources": [
135 {"kind": "local", "path": "files/pip-source"},
136 {
137 "kind": "pip",
138 "url": "file://{}".format(os.path.realpath(pypi_repo)),
139 "requirements-files": ["myreqs.txt"],
140 },
141 ],
142 }
143 os.makedirs(
144 os.path.dirname(os.path.join(element_path, element_name)),
145 exist_ok=True,
146 )
147 _yaml.roundtrip_dump(element, os.path.join(element_path, element_name))
148
149 result = cli.run(project=project, args=["source", "track", element_name])
150 assert result.exit_code == 0
151
152 result = cli.run(project=project, args=["build", element_name])
153 assert result.exit_code == 0
154
155 result = cli.run(
156 project=project,
157 args=["artifact", "checkout", element_name, "--directory", checkout],
158 )
159 assert result.exit_code == 0
160
161 assert_contains(
162 checkout,
163 [
164 "/.bst_pip_downloads",
165 "/.bst_pip_downloads/hellolib-0.1.tar.gz",
166 "/.bst_pip_downloads/app2-0.1.tar.gz",
167 "/.bst_pip_downloads/app_3-0.1.tar.gz",
168 "/.bst_pip_downloads/app_4-0.1.tar.gz",
169 "/.bst_pip_downloads/app_5-0.1.tar.gz",
170 "/.bst_pip_downloads/app_no_6-0.1.tar.gz",
171 "/.bst_pip_downloads/app_no_7-0.1.tar.gz",
172 "/.bst_pip_downloads/app_no_8-0.1.tar.gz",
173 ],
174 )
175
176
177 @pytest.mark.datafiles(DATA_DIR)
178 @pytest.mark.skipif(not HAVE_SANDBOX, reason="Only available with a functioning sandbox")
179 def test_pip_source_build(cli, datafiles, setup_pypi_repo):
180 project = str(datafiles)
181 element_path = os.path.join(project, "elements")
182 element_name = "pip/hello.bst"
183
184 # check that exotically named packages are imported correctly
185 myreqs_packages = "hellolib"
186 dependencies = [
187 "app2",
188 "app.3",
189 "app-4",
190 "app_5",
191 "app.no.6",
192 "app-no-7",
193 "app_no_8",
194 ]
195 mock_packages = {myreqs_packages: {package: {} for package in dependencies}}
196
197 # create mock pypi repository
198 pypi_repo = os.path.join(project, "files", "pypi-repo")
199 os.makedirs(pypi_repo, exist_ok=True)
200 setup_pypi_repo(mock_packages, pypi_repo)
201 realpath_repo = os.path.realpath(pypi_repo)
202
203 element = {
204 "kind": "manual",
205 "depends": ["base.bst"],
206 "sources": [
207 {"kind": "local", "path": "files/pip-source"},
208 {
209 "kind": "pip",
210 "url": "file://{}".format(realpath_repo),
211 "requirements-files": ["myreqs.txt"],
212 "packages": dependencies,
213 },
214 ],
215 "config": {
216 "install-commands": [
217 "pip3 install --no-index --prefix %{install-root}/usr .bst_pip_downloads/*.tar.gz",
218 "install app1.py %{install-root}/usr/bin/",
219 ]
220 },
221 }
222 os.makedirs(
223 os.path.dirname(os.path.join(element_path, element_name)),
224 exist_ok=True,
225 )
226 _yaml.roundtrip_dump(element, os.path.join(element_path, element_name))
227
228 result = cli.run(project=project, args=["source", "track", element_name])
229 assert result.exit_code == 0
230
231 #
232 # Lets sneak in here and test out that Source.collect_source_info() works as expected
233 #
234 # The ref for this generated pip source is:
235 #
236 # "app2==0.1\napp_3==0.1\napp_4==0.1\napp_5==0.1\napp_no_6==0.1\napp_no_7==0.1\napp_no_8==0.1\nhellolib==0.1"
237 #
238 # Lets just make an assertion on the first dependency app2, which is the second in the list after the local source for this element
239 #
240 result = cli.run(
241 project=project,
242 silent=True,
243 args=["show", "--deps", "none", "--format", "element:\n%{source-info}", element_name],
244 )
245 result.assert_success()
246 loaded = _yaml.load_data(result.output)
247 sources = loaded.get_sequence("element")
248 source_info = sources.mapping_at(1)
249 assert source_info.get_str("kind") == "pip"
250 assert source_info.get_str("url") == f"file://{realpath_repo}"
251 assert source_info.get_str("medium") == "pypi"
252 assert source_info.get_str("version-type") == "indexed-version"
253 assert source_info.get_str("version") == "0.1"
254 assert source_info.get_str("version-guess") == "0.1"
255 extra_data = source_info.get_mapping("extra-data", None)
256 assert extra_data is not None
257 assert extra_data.get_str("package-name", "app2")
258
259 # Go ahead and build
260 result = cli.run(project=project, args=["build", element_name])
261 assert result.exit_code == 0
262
263 # Use a build shell to assert the output of something we installed
264 result = cli.run(project=project, args=["shell", element_name, "/usr/bin/app1.py"])
265 assert result.exit_code == 0
266 assert result.output == "Hello App1! This is hellolib\n"