Skip to content

Commit 2b9a201

Browse files
authored
BL-143 | Version warning (#299)
* version validation * test * black * only verify version over already started projects * check toolbox version rather than ref-arch * black * more black * avoid validation when the config is not yet created * last black round * is toolbox not cli version * fix comment * black
1 parent 1160874 commit 2b9a201

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

leverage/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
__version__ = "0.0.0"
88
__toolbox_version__ = "1.3.5-0.2.0"
99

10+
MINIMUM_VERSIONS = {
11+
"TERRAFORM": "1.3.5",
12+
"TOOLBOX": "0.2.0",
13+
}
14+
1015
import sys
1116
from shutil import which
1217

leverage/leverage.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
Binbash Leverage Command-line tool.
33
"""
44

5+
import rich
6+
from packaging.version import Version
7+
58
import click
69

7-
from leverage import __version__
10+
from leverage import __version__, conf, MINIMUM_VERSIONS
811
from leverage._internals import pass_state
9-
1012
from leverage.modules.aws import aws
1113
from leverage.modules.credentials import credentials
1214
from leverage.modules import run, project, terraform, tfautomv, kubectl, shell
15+
from leverage.path import NotARepositoryError
1316

1417

1518
@click.group(invoke_without_command=True)
@@ -25,6 +28,24 @@ def leverage(context, state, verbose):
2528
# leverage called with no subcommand
2629
click.echo(context.get_help())
2730

31+
# if there is a version restriction set, make sure we satisfy it
32+
try:
33+
config = conf.load()
34+
except NotARepositoryError:
35+
# restrictions are only verified within a leverage project
36+
return
37+
38+
# check if the current versions are lower than the minimum required
39+
if not (current_values := config.get("TERRAFORM_IMAGE_TAG")):
40+
# at some points of the project (the init), the config file is not created yet
41+
return
42+
# validate both TOOLBOX and TF versions
43+
for key, current in zip(MINIMUM_VERSIONS, current_values.split("-")):
44+
if Version(current) < Version(MINIMUM_VERSIONS[key]):
45+
rich.print(
46+
f"[red]WARNING[/red]\tYour current {key} version ({current}) is lower than the required minimum ({MINIMUM_VERSIONS[key]})."
47+
)
48+
2849

2950
# Add modules to leverage
3051
leverage.add_command(run)

tests/test_conf.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from unittest import mock
2+
13
import pytest
4+
from click.testing import CliRunner
25

6+
from leverage import leverage
37
from leverage.conf import load
48

5-
69
ROOT_ENV_FILE = """
710
# Project settings
811
PROJECT=foobar
@@ -52,3 +55,23 @@ def test_load_config(monkeypatch, click_context, tmp_path, write_files, expected
5255
loaded_values = load()
5356

5457
assert dict(loaded_values) == expected_values
58+
59+
60+
def test_version_validation():
61+
"""
62+
Test that we get a warning if we are working with a version lower than the required by the project.
63+
"""
64+
runner = CliRunner()
65+
with (
66+
mock.patch("leverage.conf.load", return_value={"TERRAFORM_IMAGE_TAG": "1.1.1-2.2.2"}),
67+
mock.patch.dict("leverage.MINIMUM_VERSIONS", {"TERRAFORM": "3.3.3", "TOOLBOX": "4.4.4"}),
68+
):
69+
result = runner.invoke(leverage)
70+
71+
assert "Your current TERRAFORM version (1.1.1) is lower than the required minimum (3.3.3)" in result.output.replace(
72+
"\n", ""
73+
)
74+
assert "Your current TOOLBOX version (2.2.2) is lower than the required minimum (4.4.4)" in result.output.replace(
75+
"\n", ""
76+
)
77+
assert result.exit_code == 0

0 commit comments

Comments
 (0)