Debian Codegraph @

tests/elements/pip.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
20 import pytest
21
22 from buildstream import _yaml
23
24 from buildstream._testing import cli_integration as cli # pylint: disable=unused-import
25 from buildstream._testing.integration import assert_contains
26 from buildstream._testing.integration import integration_cache # pylint: disable=unused-import
27 from buildstream._testing._utils.site import HAVE_SANDBOX
28
29 from tests.testutils.python_repo import setup_pypi_repo # pylint: disable=unused-import
30
31
32 pytestmark = pytest.mark.integration
33
34
35 DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pip")
36
37
38 @pytest.mark.datafiles(DATA_DIR)
39 @pytest.mark.skipif(not HAVE_SANDBOX, reason="Only available with a functioning sandbox")
40 def test_pip_build(cli, datafiles):
41 project = str(datafiles)
42 checkout = os.path.join(cli.directory, "checkout")
43 element_path = os.path.join(project, "elements")
44 element_name = "pip/hello.bst"
45
46 element = {
47 "kind": "pip",
48 "variables": {"pip": "pip3"},
49 "depends": [{"filename": "base.bst"}],
50 "sources": [
51 {
52 "kind": "tar",
53 "url": "file://{}/files/piphello.tar.xz".format(project),
54 "ref": "ad96570b552498807abec33c06210bf68378d854ced6753b77916c5ed517610d",
55 }
56 ],
57 }
58 os.makedirs(
59 os.path.dirname(os.path.join(element_path, element_name)),
60 exist_ok=True,
61 )
62 _yaml.roundtrip_dump(element, os.path.join(element_path, element_name))
63
64 result = cli.run(project=project, args=["build", element_name])
65 assert result.exit_code == 0
66
67 result = cli.run(
68 project=project,
69 args=["artifact", "checkout", element_name, "--directory", checkout],
70 )
71 assert result.exit_code == 0
72
73 assert_contains(
74 checkout,
75 [
76 "/usr",
77 "/usr/lib",
78 "/usr/bin",
79 "/usr/bin/hello",
80 "/usr/lib/python3.6",
81 ],
82 )
83
84
85 # Test running an executable built with pip
86 @pytest.mark.datafiles(DATA_DIR)
87 @pytest.mark.skipif(not HAVE_SANDBOX, reason="Only available with a functioning sandbox")
88 def test_pip_run(cli, datafiles):
89 # Create and build our test element
90 test_pip_build(cli, datafiles)
91
92 project = str(datafiles)
93 element_name = "pip/hello.bst"
94
95 result = cli.run(project=project, args=["shell", element_name, "/usr/bin/hello"])
96 assert result.exit_code == 0
97 assert result.output == "Hello, world!\n"
98
99
100 @pytest.mark.datafiles(DATA_DIR)
101 @pytest.mark.skipif(not HAVE_SANDBOX, reason="Only available with a functioning sandbox")
102 def test_pip_element_should_install_pip_deps(cli, datafiles, setup_pypi_repo):
103 project = str(datafiles)
104 elements_path = os.path.join(project, "elements")
105 element_name = "pip/hello.bst"
106
107 # check that exotically named packages are imported correctly
108 myreqs_packages = "alohalib"
109 dependencies = [
110 "app2",
111 "app.3",
112 "app-4",
113 "app_5",
114 "app.no.6",
115 "app-no-7",
116 "app_no_8",
117 ]
118 mock_packages = {myreqs_packages: {package: {} for package in dependencies}}
119
120 # set up directories
121 pypi_repo = os.path.join(project, "files", "pypi-repo")
122 os.makedirs(pypi_repo, exist_ok=True)
123 os.makedirs(
124 os.path.dirname(os.path.join(elements_path, element_name)),
125 exist_ok=True,
126 )
127 setup_pypi_repo(mock_packages, pypi_repo)
128
129 # create pip element
130 element = {
131 "kind": "pip",
132 "variables": {"pip": "pip3"},
133 "depends": [{"filename": "base.bst"}],
134 "sources": [
135 {
136 "kind": "tar",
137 "url": "file://{}/files/piphello.tar.xz".format(project),
138 # FIXME: remove hardcoded ref once issue #1010 is closed
139 "ref": "ad96570b552498807abec33c06210bf68378d854ced6753b77916c5ed517610d",
140 },
141 {
142 "kind": "pip",
143 "url": "file://{}".format(os.path.realpath(pypi_repo)),
144 "packages": [myreqs_packages],
145 },
146 ],
147 }
148 _yaml.roundtrip_dump(element, os.path.join(elements_path, element_name))
149
150 result = cli.run(project=project, args=["source", "track", element_name])
151 assert result.exit_code == 0
152
153 result = cli.run(project=project, args=["build", element_name])
154 assert result.exit_code == 0
155
156 # get installed packages in sandbox
157 installed_packages = set(
158 cli.run(project=project, args=["shell", element_name, "pip3", "freeze"]).output.split("\n")
159 )
160 # compare with packages that are expected to be installed
161 pip_source_packages = {package.replace("_", "-") + "==0.1" for package in dependencies + [myreqs_packages]}
162 assert pip_source_packages.issubset(installed_packages)