From fa763656241c398e618b8b36f02b041680a8c088 Mon Sep 17 00:00:00 2001 From: studioj <22102283+studioj@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:36:03 +0100 Subject: [PATCH] fix: small fix for dev docker (#1786) * fix: small fix for dev docker * fix: bump dev container to jammy * also prevent undo in pytest style tests * install cli by default so tests work * Update .devcontainer/Dockerfile Co-authored-by: Adel Haddad <26027314+adehad@users.noreply.github.com> * allow GITHUB env vars * Use default CI vars * use CI prefix but shorten hash * clean up after tests more reliably --------- Co-authored-by: adehad <26027314+adehad@users.noreply.github.com> --- .devcontainer/Dockerfile | 12 ++++++------ .devcontainer/devcontainer.json | 9 +++------ .devcontainer/post_create.sh | 2 +- tests/conftest.py | 29 +++++++++++++++++++++++------ tests/test_client.py | 20 +++++++------------- tox.ini | 1 + 6 files changed, 41 insertions(+), 32 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 9b8963240..d6dab3556 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,9 +1,9 @@ -# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.231.5/containers/ubuntu/.devcontainer/base.Dockerfile +# See here for image contents: https://github.com/devcontainers/images/blob/main/src/base-ubuntu/.devcontainer/Dockerfile -# [Choice] Ubuntu version (use hirsuite or bionic on local arm64/Apple Silicon): hirsute, focal, bionic -ARG VARIANT="hirsute" -FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT} +# [Choice] Ubuntu version: https://github.com/devcontainers/images/tree/main/src/base-ubuntu +ARG VARIANT="ubuntu-22.04" +FROM mcr.microsoft.com/vscode/devcontainers/base:${VARIANT} # [Optional] Uncomment this section to install additional OS packages. -# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ -# && apt-get -y install --no-install-recommends \ +RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ + && apt-get -y install --no-install-recommends libkrb5-dev diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index dab6df3b5..3261f36c7 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,13 +1,12 @@ // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: -// https://github.com/microsoft/vscode-dev-containers/tree/v0.231.5/containers/ubuntu +// https://mcr.microsoft.com/en-us/product/devcontainers/base/about { "name": "Ubuntu", "build": { "dockerfile": "Dockerfile", - // Update 'VARIANT' to pick an Ubuntu version: hirsute, focal, bionic - // Use hirsute or bionic on local arm64/Apple Silicon. + // Update 'VARIANT' to pick an Ubuntu version "args": { - "VARIANT": "focal" + "VARIANT": "ubuntu-22.04" } }, // Set *default* container specific settings.json values on container create. @@ -30,7 +29,6 @@ "color": "#ff000065" }, ], - "python.defaultInterpreterPath": "/usr/local/python/bin/python", "mypy-type-checker.args": [ // "--follow-imports=silent", @@ -84,7 +82,6 @@ ], } }, - // Use 'forwardPorts' to make a list of ports inside the container available locally. "forwardPorts": [ // The jira server instance we run via docker is exposed on: diff --git a/.devcontainer/post_create.sh b/.devcontainer/post_create.sh index 5882eb626..272b6ba23 100755 --- a/.devcontainer/post_create.sh +++ b/.devcontainer/post_create.sh @@ -31,4 +31,4 @@ else fi # Install package in editable mode with test dependencies -pip install -e .[test] +pip install -e .[cli,opt,test] diff --git a/tests/conftest.py b/tests/conftest.py index e493e7c30..140cb07d9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,7 +8,9 @@ import re import string import sys +import time import unittest +import weakref from time import sleep from typing import Any @@ -71,6 +73,12 @@ def setUp(self) -> None: self.user_normal = self.test_manager.user_normal # use this user where possible self.project_b = self.test_manager.project_b self.project_a = self.test_manager.project_a + weakref.finalize( + self, + self._cleanup, + test_manager=self.test_manager, + projects=[self.project_a, self.project_b], + ) @property def identifying_user_property(self) -> str: @@ -82,6 +90,14 @@ def is_jira_cloud_ci(self) -> bool: """is running on Jira Cloud""" return self.test_manager._cloud_ci + def _cleanup(self, test_manager: JiraTestManager, projects: list[str]) -> None: + """This is called when the object is set to be garbage collected.""" + for proj in projects: + try: + test_manager._remove_project(proj) + except Exception: + LOGGER.exception(f"Failed to remove project {proj}") + def rndstr(): return "".join(random.sample(string.ascii_lowercase, 6)) @@ -99,19 +115,19 @@ def rndpassword(): def hashify(some_string, max_len=8): - return hashlib.sha256(some_string.encode("utf-8")).hexdigest()[:8].upper() + return hashlib.sha256(some_string.encode("utf-8")).hexdigest()[:max_len].upper() def get_unique_project_name(): user = re.sub("[^A-Z_]", "", getpass.getuser().upper()) if "GITHUB_ACTION" in os.environ and "GITHUB_RUN_NUMBER" in os.environ: + run_number = os.environ["GITHUB_RUN_NUMBER"] # please note that user underline (_) is not supported by # Jira even if it is documented as supported. - return "GH" + hashify(user + os.environ["GITHUB_RUN_NUMBER"]) - identifier = ( - user + chr(ord("A") + sys.version_info[0]) + chr(ord("A") + sys.version_info[1]) - ) - return "Z" + hashify(identifier) + return f"CI{hashify(f'{user}{run_number}',max_len=7)}" + sep = chr(ord("A")) + identifier = f"{user}{sep}{sys.version_info[0]}{sep}{sys.version_info[1]}" + return f"Z{hashify(identifier)}" class JiraTestManager: @@ -259,6 +275,7 @@ def _create_project( except JIRAError as e: if "A project with that name already exists" not in str(e): raise e + time.sleep(1) return self.jira_admin.project(project_key).id def create_some_data(self): diff --git a/tests/test_client.py b/tests/test_client.py index 2f088b1e0..ddfb278fe 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -32,18 +32,9 @@ def cl_normal(test_manager: JiraTestManager) -> jira.client.JIRA: @pytest.fixture(scope="function") -def slug(request, cl_admin): +def slug(request: pytest.FixtureRequest, cl_admin: jira.client.JIRA): """Project slug.""" - - def remove_by_slug(): - try: - cl_admin.delete_project(slug) - except (ValueError, JIRAError): - # Some tests have project already removed, so we stay silent - pass - slug = get_unique_project_name() - project_name = f"Test user={getpass.getuser()} key={slug} A" try: @@ -52,9 +43,12 @@ def remove_by_slug(): proj = cl_admin.create_project(slug, project_name) assert proj - request.addfinalizer(remove_by_slug) - - return slug + yield slug + try: + cl_admin.delete_project(slug, enable_undo=False) + except (ValueError, JIRAError): + # Some tests have project already removed, so we stay silent + pass def test_delete_project(cl_admin, cl_normal, slug): diff --git a/tox.ini b/tox.ini index 2f6b49ae4..b398da265 100644 --- a/tox.ini +++ b/tox.ini @@ -57,6 +57,7 @@ passenv = REQUESTS_CA_BUNDLE SSL_CERT_FILE TWINE_* + GITHUB_* XDG_CACHE_HOME # For Windows users, getpass.get_user() needs USERNAME USERNAME