From b4ccad0a1e5833bd136d3f37897578d2d142c6f6 Mon Sep 17 00:00:00 2001 From: Joe Wesch Date: Wed, 14 Feb 2024 10:34:00 -0600 Subject: [PATCH 1/6] Fixes running tests locally via invoke commands --- tasks.py | 55 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/tasks.py b/tasks.py index 6cebec42..160b872a 100644 --- a/tasks.py +++ b/tasks.py @@ -1,4 +1,5 @@ """Tasks for use with Invoke.""" + from distutils.util import strtobool from invoke import Collection, task as invoke_task import os @@ -55,22 +56,22 @@ def task_wrapper(function=None): def docker_compose(context, command, **kwargs): - """Helper function for running a specific docker-compose command with all appropriate parameters and environment. + """Helper function for running a specific docker compose command with all appropriate parameters and environment. Args: context (obj): Used to run specific commands - command (str): Command string to append to the "docker-compose ..." command, such as "build", "up", etc. + command (str): Command string to append to the "docker compose ..." command, such as "build", "up", etc. **kwargs: Passed through to the context.run() call. """ build_env = { "NAUTOBOT_VER": context.nautobot_ansible.nautobot_ver, "PYTHON_VER": context.nautobot_ansible.python_ver, } - compose_command = f'docker-compose --project-name {context.nautobot_ansible.project_name} --project-directory "{context.nautobot_ansible.compose_dir}"' + compose_command = f'docker compose --project-name {context.nautobot_ansible.project_name} --project-directory "{context.nautobot_ansible.compose_dir}"' for compose_file in context.nautobot_ansible.compose_files: compose_file_path = os.path.join(context.nautobot_ansible.compose_dir, compose_file) compose_command += f' -f "{compose_file_path}"' compose_command += f" {command}" - print(f'Running docker-compose command "{command}"') + print(f'Running docker compose command "{command}"') return context.run(compose_command, env=build_env, **kwargs) @@ -218,29 +219,53 @@ def post_upgrade(context): def lint(context): """Run linting tools""" context.run( - "docker-compose up --build --force-recreate --quiet-pull --exit-code-from lint lint", + "docker compose up --build --force-recreate --exit-code-from lint lint", env={"PYTHON_VER": context["nautobot_ansible"]["python_ver"]}, ) -@task -def unit(context): +@task( + help={ + "verbose": "Run the tests with verbose output; can be provided multiple times for more verbosity (e.g. -v, -vv, -vvv)", + }, + incrementable=["verbose"], +) +def unit(context, verbose=0): """Run unit tests""" - context.run( - "docker-compose up --build --force-recreate --quiet-pull --exit-code-from unit unit", - env={"PYTHON_VER": context["nautobot_ansible"]["python_ver"]}, - ) + env = {"PYTHON_VER": context["nautobot_ansible"]["python_ver"]} + if verbose: + env["ANSIBLE_UNIT_ARGS"] = f"-{'v' * verbose}" + context.run("docker compose up --build --force-recreate --exit-code-from unit unit", env=env) -@task -def integration(context): +@task( + help={ + "verbose": "Run the tests with verbose output; can be provided multiple times for more verbosity (e.g. -v, -vv, -vvv)", + "tags": "Run specific test tags (e.g. 'device' or 'location'); can be provided multiple times (e.g. --tags device --tags location)", + }, + iterable=["tags"], + incrementable=["verbose"], +) +def integration(context, verbose=0, tags=None): """Run all tests including integration tests""" + build(context) + # Destroy any existing containers and volumes that may be left over from a previous run destroy(context) start(context) + env = {"PYTHON_VER": context["nautobot_ansible"]["python_ver"]} + ansible_args = [] + if verbose: + ansible_args.append(f"-{'v' * verbose}") + if tags: + ansible_args.append(f"--tags {','.join(tags)}") + if ansible_args: + env["ANSIBLE_INTEGRATION_ARGS"] = " ".join(ansible_args) context.run( - "docker-compose up --build --force-recreate --exit-code-from integration integration", - env={"PYTHON_VER": context["nautobot_ansible"]["python_ver"]}, + "docker compose up --build --force-recreate --exit-code-from integration integration", + env=env, ) + # Clean up after the tests + destroy(context) @task( From 6425f4003d2c064eb31c3d63884aa09fc3e64de6 Mon Sep 17 00:00:00 2001 From: Joe Wesch Date: Wed, 14 Feb 2024 11:23:32 -0600 Subject: [PATCH 2/6] Changes network to be external --- docker-compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.yml b/docker-compose.yml index a6eb009a..fa2a73eb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -41,3 +41,4 @@ services: networks: integration_network: name: nautobot_ansible_default + external: true From 2e03019637648dd4af3f9a13dc123e6c3156e424 Mon Sep 17 00:00:00 2001 From: Joe Wesch Date: Wed, 14 Feb 2024 11:36:24 -0600 Subject: [PATCH 3/6] Changes network name --- docker-compose.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index fa2a73eb..5d15c939 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,5 +40,4 @@ services: # Attach these services to the Nautobot network that gets spun up from invoke start networks: integration_network: - name: nautobot_ansible_default - external: true + name: nautobot-ansible_default From 07d8ee4320e7e1c158eff5d012fe5d82ef7df691 Mon Sep 17 00:00:00 2001 From: Joe Wesch Date: Wed, 14 Feb 2024 13:24:20 -0600 Subject: [PATCH 4/6] Revert previous change --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 5d15c939..a6eb009a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,4 +40,4 @@ services: # Attach these services to the Nautobot network that gets spun up from invoke start networks: integration_network: - name: nautobot-ansible_default + name: nautobot_ansible_default From 5872c0111f541d6df64e2d9162959fcc36d0b489 Mon Sep 17 00:00:00 2001 From: Joe Wesch Date: Wed, 14 Feb 2024 13:24:39 -0600 Subject: [PATCH 5/6] Sets the project name for tests --- tasks.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tasks.py b/tasks.py index 160b872a..e186bc97 100644 --- a/tasks.py +++ b/tasks.py @@ -219,7 +219,7 @@ def post_upgrade(context): def lint(context): """Run linting tools""" context.run( - "docker compose up --build --force-recreate --exit-code-from lint lint", + "docker compose --project-name nautobot_ansible up --build --force-recreate --exit-code-from lint lint", env={"PYTHON_VER": context["nautobot_ansible"]["python_ver"]}, ) @@ -235,7 +235,7 @@ def unit(context, verbose=0): env = {"PYTHON_VER": context["nautobot_ansible"]["python_ver"]} if verbose: env["ANSIBLE_UNIT_ARGS"] = f"-{'v' * verbose}" - context.run("docker compose up --build --force-recreate --exit-code-from unit unit", env=env) + context.run("docker compose --project-name nautobot_ansible up --build --force-recreate --exit-code-from unit unit", env=env) @task( @@ -261,7 +261,7 @@ def integration(context, verbose=0, tags=None): if ansible_args: env["ANSIBLE_INTEGRATION_ARGS"] = " ".join(ansible_args) context.run( - "docker compose up --build --force-recreate --exit-code-from integration integration", + "docker compose --project-name nautobot_ansible up --build --force-recreate --exit-code-from integration integration", env=env, ) # Clean up after the tests From 041a34c2c78a43462e5201e47e8c95c2ed0413b0 Mon Sep 17 00:00:00 2001 From: Joe Wesch Date: Wed, 14 Feb 2024 13:31:33 -0600 Subject: [PATCH 6/6] Removes network label --- docker-compose.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index a6eb009a..b61a9df6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -34,10 +34,3 @@ services: build: <<: *build target: integration - networks: - - integration_network - -# Attach these services to the Nautobot network that gets spun up from invoke start -networks: - integration_network: - name: nautobot_ansible_default