Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BL-143 | Version warning #299

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions leverage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
__version__ = "0.0.0"
__toolbox_version__ = "1.3.5-0.2.0"

MINIMUM_VERSIONS = {
"TERRAFORM": "1.3.5",
"TOOLBOX": "0.2.0",
}

import sys
from shutil import which

Expand Down
25 changes: 23 additions & 2 deletions leverage/leverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
Binbash Leverage Command-line tool.
"""

import rich
from packaging.version import Version

import click

from leverage import __version__
from leverage import __version__, conf, MINIMUM_VERSIONS
from leverage._internals import pass_state

from leverage.modules.aws import aws
from leverage.modules.credentials import credentials
from leverage.modules import run, project, terraform, tfautomv, kubectl, shell
from leverage.path import NotARepositoryError


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

# if there is a version restriction set, make sure we satisfy it
try:
config = conf.load()
except NotARepositoryError:
# restrictions are only verified within a leverage project
return

# check if the current versions are lower than the minimum required
if not (current_values := config.get("TERRAFORM_IMAGE_TAG")):
# at some points of the project (the init), the config file is not created yet
return
# validate both TOOLBOX and TF versions
for key, current in zip(MINIMUM_VERSIONS, current_values.split("-")):
if Version(current) < Version(MINIMUM_VERSIONS[key]):
rich.print(
f"[red]WARNING[/red]\tYour current {key} version ({current}) is lower than the required minimum ({MINIMUM_VERSIONS[key]})."
)


# Add modules to leverage
leverage.add_command(run)
Expand Down
25 changes: 24 additions & 1 deletion tests/test_conf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from unittest import mock

import pytest
from click.testing import CliRunner

from leverage import leverage
from leverage.conf import load


ROOT_ENV_FILE = """
# Project settings
PROJECT=foobar
Expand Down Expand Up @@ -52,3 +55,23 @@ def test_load_config(monkeypatch, click_context, tmp_path, write_files, expected
loaded_values = load()

assert dict(loaded_values) == expected_values


def test_version_validation():
"""
Test that we get a warning if we are working with a version lower than the required by the project.
"""
runner = CliRunner()
with (
mock.patch("leverage.conf.load", return_value={"TERRAFORM_IMAGE_TAG": "1.1.1-2.2.2"}),
mock.patch.dict("leverage.MINIMUM_VERSIONS", {"TERRAFORM": "3.3.3", "TOOLBOX": "4.4.4"}),
):
result = runner.invoke(leverage)

assert "Your current TERRAFORM version (1.1.1) is lower than the required minimum (3.3.3)" in result.output.replace(
"\n", ""
)
assert "Your current TOOLBOX version (2.2.2) is lower than the required minimum (4.4.4)" in result.output.replace(
"\n", ""
)
assert result.exit_code == 0
Loading