Skip to content

Commit

Permalink
Merge pull request #99 from mgor/bug/issue_347
Browse files Browse the repository at this point in the history
  • Loading branch information
boffman authored Sep 26, 2024
2 parents 9f68841 + b0ee7c1 commit 5daf132
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 39 deletions.
2 changes: 1 addition & 1 deletion grizzly_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _parse_arguments() -> argparse.Namespace:
grizzly_versions: Optional[Tuple[Optional[str], Optional[List[str]]]] = None

if args.version == 'all':
grizzly_versions, locust_version = get_dependency_versions()
grizzly_versions, locust_version = get_dependency_versions(False)
else:
grizzly_versions, locust_version = None, None

Expand Down
19 changes: 11 additions & 8 deletions grizzly_cli/distributed/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def create_parser(sub_parser: ArgumentSubParser) -> None:
# <!-- used during development, hide from help
build_parser.add_argument(
'--local-install',
action='store_true',
nargs='?',
const=True,
default=False,
help=SUPPRESS,
)
Expand All @@ -74,18 +75,20 @@ def getgid() -> int:


def _create_build_command(args: Arguments, containerfile: str, tag: str, context: str) -> List[str]:
(_, grizzly_extras, ), _ = get_dependency_versions()
local_install = getattr(args, 'local_install', False)

if local_install:
install_type = 'local'
else:
install_type = 'remote'

(_, grizzly_extras, ), _ = get_dependency_versions(local_install)

if grizzly_extras is not None and 'mq' in grizzly_extras:
grizzly_extra = 'mq'
else:
grizzly_extra = 'base'

if getattr(args, 'local_install', False):
install_type = 'local'
else:
install_type = 'remote'

extra_args: List[str] = []

ibm_mq_lib_host = os.environ.get('IBM_MQ_LIB_HOST', None)
Expand Down Expand Up @@ -145,7 +148,7 @@ def build(args: Arguments) -> int:
if args.container_system == 'docker':
build_env['DOCKER_BUILDKIT'] = '1'

spinner = 'building' if not args.no_progress else None
spinner = 'building' if not getattr(args, 'no_progress', False) else None

result = run_command(build_command, env=build_env, spinner=spinner, verbose=args.verbose)

Expand Down
12 changes: 10 additions & 2 deletions grizzly_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,22 @@ def rm_rf(path: Union[str, Path], *, missing_ok: bool = False) -> None:
raise


def get_dependency_versions() -> Tuple[Tuple[Optional[str], Optional[List[str]]], Optional[str]]:
def get_dependency_versions(local_install: Union[bool, str]) -> Tuple[Tuple[Optional[str], Optional[List[str]]], Optional[str]]:
grizzly_requirement: Optional[str] = None
grizzly_requirement_egg: str
locust_version: Optional[str] = None
grizzly_version: Optional[str] = None
grizzly_extras: Optional[List[str]] = None

project_requirements = path.join(grizzly_cli.EXECUTION_CONTEXT, 'requirements.txt')
args: tuple[str, ...] = (grizzly_cli.EXECUTION_CONTEXT,)
if isinstance(local_install, str):
args += (local_install,)
if not local_install.endswith('requirements.txt'):
args += ('requirements.txt',)
else:
args += ('requirements.txt',)

project_requirements = path.join(*args)

try:
with open(project_requirements, encoding='utf-8') as fd:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
requires-python = ">=3.8"
dependencies = [
"behave ==1.2.6",
"Jinja2 ==3.1.3",
"Jinja2 ==3.1.4",
"requests ==2.32.3",
"packaging ==24.0",
"chardet ==5.2.0",
Expand Down
66 changes: 39 additions & 27 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,14 +1042,14 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
mocker.patch('grizzly_cli.EXECUTION_CONTEXT', str(test_context))

try:
grizzly_versions, locust_version = get_dependency_versions()
grizzly_versions, locust_version = get_dependency_versions(False)

assert grizzly_versions == (None, None,)
assert locust_version is None

requirements_file.touch()

assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == f'!! unable to find grizzly dependency in {requirements_file.absolute()}\n'
Expand All @@ -1070,7 +1070,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
]),
)

assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == '!! unable to clone git repo https://github.com/Biometria-se/grizzly.git\n'
Expand All @@ -1090,7 +1090,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
assert kwargs.get('stdout', None) == subprocess.DEVNULL
assert kwargs.get('stderr', None) == subprocess.DEVNULL

assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == '!! unable to check branch name of HEAD in git repo https://github.com/Biometria-se/grizzly.git\n'
Expand All @@ -1109,7 +1109,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
assert kwargs.get('cwd', '').endswith('grizzly-loadtester_3f210f1809f6ca85ef414b2b4d450bf54353b5e0')
assert kwargs.get('universal_newlines', False)

assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(True)

capture = capsys.readouterr()
assert capture.err == '!! unable to checkout branch v1.5.3 from git repo https://github.com/Biometria-se/grizzly.git\n'
Expand All @@ -1130,7 +1130,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
assert kwargs.get('stderr', None) == subprocess.DEVNULL

with pytest.raises(FileNotFoundError):
get_dependency_versions()
get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == ''
Expand All @@ -1146,15 +1146,15 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
)

with pytest.raises(FileNotFoundError) as fne:
get_dependency_versions()
get_dependency_versions(False)
assert fne.value.errno == 2
assert fne.value.strerror == 'No such file or directory'

with unittest_patch('builtins.open', side_effect=[
mock_open(read_data='git+https://github.com/Biometria-se/[email protected]#egg=grizzly-loadtester\n').return_value,
mock_open(read_data='').return_value,
]) as open_mock:
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == '!! unable to find "__version__" declaration in grizzly/__init__.py from https://github.com/Biometria-se/grizzly.git\n'
Expand All @@ -1166,7 +1166,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
mock_open(read_data="__version__ = '0.0.0'").return_value,
mock_open(read_data='').return_value,
]) as open_mock:
assert (('(development)', [], ), '(unknown)',) == get_dependency_versions()
assert (('(development)', [], ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == '!! unable to find "locust" dependency in requirements.txt from https://github.com/Biometria-se/grizzly.git\n'
Expand All @@ -1179,7 +1179,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
mock_open(read_data="__version__ = '1.5.3'").return_value,
mock_open(read_data='locust').return_value,
]) as open_mock:
assert (('1.5.3', ['dev', 'mq'], ), '(unknown)',) == get_dependency_versions()
assert (('1.5.3', ['dev', 'mq'], ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == '!! unable to find locust version in "locust" specified in requirements.txt from https://github.com/Biometria-se/grizzly.git\n'
Expand All @@ -1192,7 +1192,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
mock_open(read_data="__version__ = '1.5.3'").return_value,
mock_open(read_data='locust==2.2.1 \\ ').return_value,
]) as open_mock:
assert (('1.5.3', [], ), '2.2.1',) == get_dependency_versions()
assert (('1.5.3', [], ), '2.2.1',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == ''
Expand All @@ -1203,15 +1203,15 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
mocker.patch('grizzly_cli.utils.path.exists', return_value=True)

with pytest.raises(FileNotFoundError) as fne:
get_dependency_versions()
get_dependency_versions(False)
assert fne.value.errno == 2
assert fne.value.strerror == 'No such file or directory'

with unittest_patch('builtins.open', side_effect=[
mock_open(read_data='git+https://github.com/Biometria-se/grizzly.git@main#egg=grizzly-loadtester\n').return_value,
mock_open(read_data='').return_value,
]) as open_mock:
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == '!! unable to find "version" declaration in setup.cfg from https://github.com/Biometria-se/grizzly.git\n'
Expand All @@ -1223,7 +1223,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
mock_open(read_data='name = grizzly-loadtester\nversion = 2.0.0').return_value,
mock_open(read_data='locust==2.8.4 \\ ').return_value,
]) as open_mock:
assert (('2.0.0', ['mq'], ), '2.8.4',) == get_dependency_versions()
assert (('2.0.0', ['mq'], ), '2.8.4',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == ''
Expand All @@ -1234,7 +1234,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
mock_open(read_data='grizzly-loadtester @ git+https://github.com/Biometria-se/grizzly.git@main\n').return_value,
mock_open(read_data='').return_value,
]) as open_mock:
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == '!! unable to find "version" declaration in setup.cfg from https://github.com/Biometria-se/grizzly.git\n'
Expand All @@ -1246,7 +1246,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
mock_open(read_data='name = grizzly-loadtester\nversion = 2.0.0').return_value,
mock_open(read_data='locust==2.8.4 \\ ').return_value,
]) as open_mock:
assert (('2.0.0', ['mq'], ), '2.8.4',) == get_dependency_versions()
assert (('2.0.0', ['mq'], ), '2.8.4',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == ''
Expand All @@ -1256,7 +1256,7 @@ def test_get_dependency_versions_git(mocker: MockerFixture, tmp_path_factory: Te
with unittest_patch('builtins.open', side_effect=[
mock_open(read_data='grizzly-loadtester[mq] % git+https://github.com/Biometria-se/grizzly.git@main\n').return_value,
]) as open_mock:
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == f'!! unable to find properly formatted grizzly dependency in {requirements_file}\n'
Expand All @@ -1274,14 +1274,14 @@ def test_get_dependency_versions_pypi(mocker: MockerFixture, tmp_path_factory: T
mocker.patch('grizzly_cli.EXECUTION_CONTEXT', str(test_context))

try:
grizzly_versions, locust_version = get_dependency_versions()
grizzly_versions, locust_version = get_dependency_versions(False)

assert grizzly_versions == (None, None,)
assert locust_version is None

requirements_file.touch()

assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == f'!! unable to find grizzly dependency in {requirements_file.absolute()}\n'
Expand All @@ -1291,7 +1291,7 @@ def test_get_dependency_versions_pypi(mocker: MockerFixture, tmp_path_factory: T

requests_mock.register_uri('GET', 'https://pypi.org/pypi/grizzly-loadtester/json', status_code=404)

assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == '!! unable to get grizzly package information from https://pypi.org/pypi/grizzly-loadtester/json (404)\n'
Expand All @@ -1300,15 +1300,15 @@ def test_get_dependency_versions_pypi(mocker: MockerFixture, tmp_path_factory: T
requests_mock.register_uri('GET', 'https://pypi.org/pypi/grizzly-loadtester/json', status_code=200, text='{"info": {"version": "1.1.1"}}')
requests_mock.register_uri('GET', 'https://pypi.org/pypi/grizzly-loadtester/1.1.1/json', status_code=400)

assert (('1.1.1', [], ), '(unknown)',) == get_dependency_versions()
assert (('1.1.1', [], ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == '!! unable to get grizzly 1.1.1 package information from https://pypi.org/pypi/grizzly-loadtester/1.1.1/json (400)\n'
assert capture.out == ''

requests_mock.register_uri('GET', 'https://pypi.org/pypi/grizzly-loadtester/1.1.1/json', status_code=200, text='{"info": {"requires_dist": []}}')

assert (('1.1.1', [], ), '(unknown)',) == get_dependency_versions()
assert (('1.1.1', [], ), '(unknown)',) == get_dependency_versions(True)

capture = capsys.readouterr()
assert capture.err == '!! could not find "locust" in requires_dist information for grizzly-loadtester 1.1.1\n'
Expand All @@ -1319,7 +1319,7 @@ def test_get_dependency_versions_pypi(mocker: MockerFixture, tmp_path_factory: T
requirements_file.unlink()
requirements_file.write_text('grizzly-loadtester[dev,mq]')

actual_dependency_versions = get_dependency_versions()
actual_dependency_versions = get_dependency_versions(True)

assert (('1.1.1', ['dev', 'mq'], ), '(unknown)',) == actual_dependency_versions

Expand All @@ -1329,7 +1329,7 @@ def test_get_dependency_versions_pypi(mocker: MockerFixture, tmp_path_factory: T

requests_mock.register_uri('GET', 'https://pypi.org/pypi/grizzly-loadtester/1.1.1/json', status_code=200, text='{"info": {"requires_dist": ["locust (==2.8.5)"]}}')

assert (('1.1.1', ['dev', 'mq'], ), '2.8.5',) == get_dependency_versions()
assert (('1.1.1', ['dev', 'mq'], ), '2.8.5',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == ''
Expand All @@ -1340,7 +1340,7 @@ def test_get_dependency_versions_pypi(mocker: MockerFixture, tmp_path_factory: T

requests_mock.register_uri('GET', 'https://pypi.org/pypi/grizzly-loadtester/json', status_code=200, text='{"releases": {"1.3.0": [], "1.5.0": []}}')

assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions()
assert (('(unknown)', None, ), '(unknown)',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == '!! could not resolve grizzly-loadtester[mq]==1.4.0 to one specific version available at pypi\n'
Expand All @@ -1354,7 +1354,7 @@ def test_get_dependency_versions_pypi(mocker: MockerFixture, tmp_path_factory: T
)
requests_mock.register_uri('GET', 'https://pypi.org/pypi/grizzly-loadtester/1.4.0/json', status_code=200, text='{"info": {"requires_dist": ["locust (==1.0.0)"]}}')

assert (('1.4.0', ['mq'], ), '1.0.0',) == get_dependency_versions()
assert (('1.4.0', ['mq'], ), '1.0.0',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == ''
Expand All @@ -1368,11 +1368,23 @@ def test_get_dependency_versions_pypi(mocker: MockerFixture, tmp_path_factory: T
)
requests_mock.register_uri('GET', 'https://pypi.org/pypi/grizzly-loadtester/1.5.0/json', status_code=200, text='{"info": {"requires_dist": ["locust (==1.1.1)"]}}')

assert (('1.5.0', ['mq'], ), '1.1.1',) == get_dependency_versions()
assert (('1.5.0', ['mq'], ), '1.1.1',) == get_dependency_versions(False)

capture = capsys.readouterr()
assert capture.err == ''
assert capture.out == ''

local_install_requirements_file = test_context / 'a' / 'b' / 'requirements.txt'
local_install_requirements_file.parent.mkdir(parents=True)
local_install_requirements_file.write_text('grizzly-loadtester[mq]>1.3.0,<1.5.0')

requests_mock.register_uri(
'GET', 'https://pypi.org/pypi/grizzly-loadtester/json', status_code=200, text='{"releases": {"1.3.0": [], "1.4.0": [], "1.5.0": [], "foobar": []}}',
)
requests_mock.register_uri('GET', 'https://pypi.org/pypi/grizzly-loadtester/1.4.0/json', status_code=200, text='{"info": {"requires_dist": ["locust (==1.0.0)"]}}')

assert (('1.4.0', ['mq'], ), '1.0.0',) == get_dependency_versions('a/b/')
assert (('1.4.0', ['mq'], ), '1.0.0',) == get_dependency_versions('a/b/requirements.txt')
finally:
rm_rf(test_context)

Expand Down

0 comments on commit 5daf132

Please sign in to comment.