From 3936ac5a48742fc68ee1fdd548319ee9b72c8cc1 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Wed, 4 Sep 2024 15:38:33 +0200 Subject: [PATCH 01/23] common-exit-hook for Worker --- src/cosl/coordinated_workers/worker.py | 41 ++++++++------------------ 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/src/cosl/coordinated_workers/worker.py b/src/cosl/coordinated_workers/worker.py index 931abb3..e7cfd21 100644 --- a/src/cosl/coordinated_workers/worker.py +++ b/src/cosl/coordinated_workers/worker.py @@ -170,14 +170,11 @@ def __init__( if self._resources_requests_getter else None ) + # holistic update logic + self._holistic_update() # Event listeners - self.framework.observe(self._charm.on.config_changed, self._on_config_changed) - self.framework.observe(self._charm.on.upgrade_charm, self._on_upgrade_charm) self.framework.observe(self._charm.on.collect_unit_status, self._on_collect_status) - - self.framework.observe(self.cluster.on.config_received, self._on_worker_config_received) - self.framework.observe(self.cluster.on.created, self._on_cluster_created) self.framework.observe(self.cluster.on.removed, self._log_forwarder.disable_logging) self.framework.observe(self._charm.on[self._name].pebble_ready, self._on_pebble_ready) @@ -188,10 +185,10 @@ def __init__( self._charm.on[name].pebble_check_recovered, self._on_pebble_check_recovered ) + # Event handlers def _on_pebble_ready(self, _: ops.PebbleReadyEvent): self._charm.unit.set_workload_version(self.running_version() or "") - self._update_config() def _on_pebble_check_failed(self, event: ops.PebbleCheckFailedEvent): if event.info.name == "ready": @@ -203,26 +200,6 @@ def _on_pebble_check_recovered(self, event: ops.PebbleCheckFailedEvent): logger.info("Pebble `ready` check is now passing: " "worker node is up.") # collect-status will detect that we're ready and set active status. - def _on_worker_config_received(self, _: ops.EventBase): - self._update_config() - - def _on_upgrade_charm(self, _: ops.UpgradeCharmEvent): - self._update_cluster_relation() - - def _on_cluster_created(self, _: ops.EventBase): - self._update_cluster_relation() - self._update_config() - - def _on_cluster_changed(self, _: ops.EventBase): - self._update_config() - - def _on_config_changed(self, _: ops.ConfigChangedEvent): - # If the user has changed roles, publish them to relation data - self._update_cluster_relation() - # If there is a config, start the worker - if self._worker_config: - self._update_worker_config() - @property def _worker_config(self): """The configuration that this worker should run with, as received from the coordinator. @@ -357,6 +334,10 @@ def roles(self) -> List[str]: def _update_config(self) -> None: """Update the worker config and restart the workload if necessary.""" + if not self._container.can_connect(): + logger.debug("container cannot connect, skipping update_config.") + return + restart = any( ( self._update_tls_certificates(), @@ -416,6 +397,11 @@ def _add_readiness_check(self, new_layer: Layer): "ready", {"override": "replace", "http": {"url": self._readiness_check_endpoint(self)}} ) + def _holistic_update(self): + """Run all unconditional logic.""" + self._update_cluster_relation() + self._update_config() + def _update_cluster_relation(self) -> None: """Publish all the worker information to relation data.""" self.cluster.publish_unit_address(socket.getfqdn()) @@ -423,9 +409,6 @@ def _update_cluster_relation(self) -> None: logger.info(f"publishing roles: {self.roles}") self.cluster.publish_app_roles(self.roles) - if self._worker_config: - self._update_config() - def _running_worker_config(self) -> Optional[Dict[str, Any]]: """Return the worker config as dict, or None if retrieval failed.""" if not self._container.can_connect(): From 6d9417065a156b33cb264ad4a1446a24feab4a88 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Wed, 4 Sep 2024 15:55:25 +0200 Subject: [PATCH 02/23] test fix --- src/cosl/coordinated_workers/worker.py | 1 - tests/test_coordinated_workers/test_worker.py | 31 +++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/cosl/coordinated_workers/worker.py b/src/cosl/coordinated_workers/worker.py index e7cfd21..05f10bd 100644 --- a/src/cosl/coordinated_workers/worker.py +++ b/src/cosl/coordinated_workers/worker.py @@ -185,7 +185,6 @@ def __init__( self._charm.on[name].pebble_check_recovered, self._on_pebble_check_recovered ) - # Event handlers def _on_pebble_ready(self, _: ops.PebbleReadyEvent): self._charm.unit.set_workload_version(self.running_version() or "") diff --git a/tests/test_coordinated_workers/test_worker.py b/tests/test_coordinated_workers/test_worker.py index e0f87cf..e2ac453 100644 --- a/tests/test_coordinated_workers/test_worker.py +++ b/tests/test_coordinated_workers/test_worker.py @@ -1,4 +1,5 @@ import json +from contextlib import ExitStack from pathlib import Path from unittest.mock import MagicMock, patch @@ -264,19 +265,31 @@ def test_worker_raises_if_service_restart_fails_for_too_long(tmp_path): }, ) - # WHEN service restart fails def raise_change_error(*args): raise ops.pebble.ChangeError("something", MagicMock()) - with patch("ops.model.Container.restart", new=raise_change_error): - # THEN the charm errors out - with pytest.raises(Exception): - # technically an ops.pebble.ChangeError but the context manager doesn't catch it for some reason + with ExitStack() as stack: + # WHEN service restart fails + stack.enter_context(patch("ops.model.Container.restart", new=raise_change_error)) - with ctx.manager(container.pebble_ready_event, State(containers=[container])) as mgr: - # so we don't have to wait for minutes: - mgr.charm.worker.SERVICE_START_RETRY_WAIT = tenacity.wait_none() - mgr.charm.worker.SERVICE_START_RETRY_STOP = tenacity.stop_after_delay(2) + # so we don't have to wait for minutes: + stack.enter_context( + patch( + "cosl.coordinated_workers.worker.Worker.SERVICE_START_RETRY_WAIT", + new=tenacity.wait_none(), + ) + ) + stack.enter_context( + patch( + "cosl.coordinated_workers.worker.Worker.SERVICE_START_RETRY_STOP", + new=tenacity.stop_after_delay(2), + ) + ) + + # THEN the charm errors out + # technically an ops.pebble.ChangeError but the context manager doesn't catch it for some reason + stack.enter_context(pytest.raises(Exception)) + ctx.run(container.pebble_ready_event, State(containers=[container])) @pytest.mark.parametrize( From efe239696db0c02d2e8e2d7a7cbf11e42e92c597 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Wed, 4 Sep 2024 16:09:00 +0200 Subject: [PATCH 03/23] vbump --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b5412dc..251650d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "cosl" -version = "0.0.27" +version = "0.0.28" authors = [ { name="sed-i", email="82407168+sed-i@users.noreply.github.com" }, ] From d0f23903c8a99b20780c2326735b0207f61531c6 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Wed, 4 Sep 2024 16:22:47 +0200 Subject: [PATCH 04/23] tests --- .../test_worker_status.py | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/test_coordinated_workers/test_worker_status.py b/tests/test_coordinated_workers/test_worker_status.py index 5de8867..f0ca840 100644 --- a/tests/test_coordinated_workers/test_worker_status.py +++ b/tests/test_coordinated_workers/test_worker_status.py @@ -128,11 +128,11 @@ def test_status_check_no_pebble(ctx, base_state, caplog): @k8s_patch() def test_status_check_no_config(ctx, base_state, caplog): - # GIVEN there is no config file on disk state = base_state.with_can_connect("workload", True) - + # GIVEN there is no config file on disk # WHEN we run any event - state_out = ctx.run("update_status", state) + with patch("cosl.coordinated_workers.worker.Worker._running_worker_config", new=lambda _: None): + state_out = ctx.run("update_status", state) # THEN the charm sets blocked assert state_out.unit_status == BlockedStatus("node down (see logs)") @@ -176,7 +176,7 @@ def __init__(self, framework: Framework): self.worker = Worker( self, "workload", - lambda _: Layer(""), + lambda _: Layer({"services": {"foo": {"command": "foo"}}}), {"cluster": "cluster"}, ) @@ -209,12 +209,13 @@ def test_access_status_no_endpoint_raises(): # GIVEN the caller doesn't pass an endpoint to Worker caller = MagicMock() with patch("cosl.juju_topology.JujuTopology.from_charm"): - worker = Worker( - caller, - "workload", - lambda _: Layer(""), - {"cluster": "cluster"}, - ) + with patch("cosl.coordinated_workers.worker.Worker._holistic_update"): + worker = Worker( + caller, + "workload", + lambda _: Layer({"services": {"foo": {"command": "foo"}}}), + {"cluster": "cluster"}, + ) # THEN calling .status raises with pytest.raises(WorkerError): From fb982bcac0e30e608eff398a71b5f34844ed29b9 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 5 Sep 2024 09:10:58 +0200 Subject: [PATCH 05/23] lint --- tests/test_coordinated_workers/test_worker_status.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_coordinated_workers/test_worker_status.py b/tests/test_coordinated_workers/test_worker_status.py index f0ca840..417f3f5 100644 --- a/tests/test_coordinated_workers/test_worker_status.py +++ b/tests/test_coordinated_workers/test_worker_status.py @@ -131,7 +131,9 @@ def test_status_check_no_config(ctx, base_state, caplog): state = base_state.with_can_connect("workload", True) # GIVEN there is no config file on disk # WHEN we run any event - with patch("cosl.coordinated_workers.worker.Worker._running_worker_config", new=lambda _: None): + with patch( + "cosl.coordinated_workers.worker.Worker._running_worker_config", new=lambda _: None + ): state_out = ctx.run("update_status", state) # THEN the charm sets blocked From e8a76995cbb5cf8c3b905459a501b19b34c8360a Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 5 Sep 2024 09:52:38 +0200 Subject: [PATCH 06/23] add CI to validate cosl against worker and coordinator (tempo only for now) --- .../distributed-solutions-tests.yaml | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 .github/workflows/distributed-solutions-tests.yaml diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml new file mode 100644 index 0000000..06f7c77 --- /dev/null +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -0,0 +1,75 @@ +name: Distributed Solutions Tests + +on: + push: + branches: + - main + pull_request: + workflow_call: + +jobs: + test-coordinators: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + include: + - repo: canonical/tempo-coordinator-k8s-operator + commit: 82f832cdf4d40300366a86f752c8bbfbdde41382 # unreleased rev, 5 sept 2024 + steps: + - name: Checkout the ${{ matrix.repo }} repository + uses: actions/checkout@v4 + with: + repository: ${{ matrix.repo }} + ref: ${{ matrix.commit }} + + - name: Update 'cosl' dependency in test charm to this branch + run: | + sed -i -e "/^cosl[ ><=]/d" -e "/canonical\/cos-lib/d" -e "/#egg=cosl/d" requirements.txt + echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@$GITHUB_SHA#egg=cosl" >> requirements.txt + + - name: Install dependencies + run: pip install tox~=4.2 + + - name: Run the charm's unit tests + run: tox -vve unit + + - name: Run the charm's static analysis checks + run: tox -vve static-charm + + - name: Run the charm's integration tests + run: tox -vve integration + + test-workers: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + include: + - repo: canonical/tempo-worker-k8s-operator + commit: 74c3f8057d81a9e91088b54752b7b26daf43501e # rev11, 5 sept 2024 + steps: + - name: Checkout the ${{ matrix.repo }} repository + uses: actions/checkout@v4 + with: + repository: ${{ matrix.repo }} + ref: ${{ matrix.commit }} + + - name: Update 'cosl' dependency in test charm to this branch + run: | + sed -i -e "/^cosl[ ><=]/d" -e "/canonical\/cos-lib/d" -e "/#egg=cosl/d" requirements.txt + echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@$GITHUB_SHA#egg=cosl" >> requirements.txt + + - name: Install dependencies + run: pip install tox~=4.2 + + - name: Run the charm's unit tests + run: tox -vve unit + + - name: Run the charm's static analysis checks + run: tox -vve static-charm + + - name: Run the charm's integration tests + run: tox -vve integration \ No newline at end of file From e5abc1d8575e51d2ae1109f5dfa6d15fbe759125 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 5 Sep 2024 10:00:05 +0200 Subject: [PATCH 07/23] unified matrix --- .../distributed-solutions-tests.yaml | 40 +++---------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index 06f7c77..8f7a0dd 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -8,7 +8,7 @@ on: workflow_call: jobs: - test-coordinators: + test-distributed-charms: runs-on: ubuntu-latest strategy: @@ -17,39 +17,9 @@ jobs: include: - repo: canonical/tempo-coordinator-k8s-operator commit: 82f832cdf4d40300366a86f752c8bbfbdde41382 # unreleased rev, 5 sept 2024 - steps: - - name: Checkout the ${{ matrix.repo }} repository - uses: actions/checkout@v4 - with: - repository: ${{ matrix.repo }} - ref: ${{ matrix.commit }} - - - name: Update 'cosl' dependency in test charm to this branch - run: | - sed -i -e "/^cosl[ ><=]/d" -e "/canonical\/cos-lib/d" -e "/#egg=cosl/d" requirements.txt - echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@$GITHUB_SHA#egg=cosl" >> requirements.txt - - - name: Install dependencies - run: pip install tox~=4.2 - - - name: Run the charm's unit tests - run: tox -vve unit - - - name: Run the charm's static analysis checks - run: tox -vve static-charm - - - name: Run the charm's integration tests - run: tox -vve integration - - test-workers: - runs-on: ubuntu-latest - - strategy: - fail-fast: false - matrix: - include: - repo: canonical/tempo-worker-k8s-operator commit: 74c3f8057d81a9e91088b54752b7b26daf43501e # rev11, 5 sept 2024 + steps: - name: Checkout the ${{ matrix.repo }} repository uses: actions/checkout@v4 @@ -65,11 +35,11 @@ jobs: - name: Install dependencies run: pip install tox~=4.2 - - name: Run the charm's unit tests - run: tox -vve unit + - name: Run the charm's unit & scenario tests + run: tox -vve unit,scenario - name: Run the charm's static analysis checks - run: tox -vve static-charm + run: tox -vve static-charm,static-lib - name: Run the charm's integration tests run: tox -vve integration \ No newline at end of file From 779991690b7cb7ca52bb6f21c049166061737358 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 5 Sep 2024 10:17:34 +0200 Subject: [PATCH 08/23] from main --- .github/workflows/distributed-solutions-tests.yaml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index 8f7a0dd..8821cdd 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -10,22 +10,21 @@ on: jobs: test-distributed-charms: runs-on: ubuntu-latest + continue-on-error: true strategy: fail-fast: false matrix: include: - repo: canonical/tempo-coordinator-k8s-operator - commit: 82f832cdf4d40300366a86f752c8bbfbdde41382 # unreleased rev, 5 sept 2024 - repo: canonical/tempo-worker-k8s-operator - commit: 74c3f8057d81a9e91088b54752b7b26daf43501e # rev11, 5 sept 2024 steps: - name: Checkout the ${{ matrix.repo }} repository uses: actions/checkout@v4 with: repository: ${{ matrix.repo }} - ref: ${{ matrix.commit }} + ref: main - name: Update 'cosl' dependency in test charm to this branch run: | From 3bae581960d785ca55bb1d992aa677c00f731c13 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Fri, 6 Sep 2024 10:18:35 +0200 Subject: [PATCH 09/23] fixed itest ci --- .../distributed-solutions-tests.yaml | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index 8821cdd..d6bed73 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -23,6 +23,7 @@ jobs: - name: Checkout the ${{ matrix.repo }} repository uses: actions/checkout@v4 with: + fetch-depth: 1 repository: ${{ matrix.repo }} ref: main @@ -35,10 +36,32 @@ jobs: run: pip install tox~=4.2 - name: Run the charm's unit & scenario tests - run: tox -vve unit,scenario + run: tox -e unit,scenario - name: Run the charm's static analysis checks - run: tox -vve static-charm,static-lib + run: tox -e static-charm,static-lib + + - name: Setup Charmcraft's pip cache + uses: actions/cache@v4 + with: + path: /home/runner/snap/charmcraft/common/cache/charmcraft/ + key: charmcraft-cache-${{ github.job }}-${{ strategy.job-index }}-${{ github.run_id }}-${{ github.run_attempt }} + restore-keys: charmcraft-cache + + - name: Get IP range + id: ip_range + run: | + echo "ip_range=$(ip -4 -j route get 2.2.2.2 | jq -r '.[] | .prefsrc')/32" >> $GITHUB_OUTPUT + + - name: Setup operator environment (k8s) + uses: charmed-kubernetes/actions-operator@main + with: + juju-channel: 3.4/stable + provider: microk8s + channel: 1.26-strict/stable + microk8s-group: snap_microk8s + microk8s-addons: "hostpath-storage dns metallb:${{ steps.ip_range.outputs.ip_range }}" + charmcraft-channel: "2.x/stable" - name: Run the charm's integration tests - run: tox -vve integration \ No newline at end of file + run: tox -e integration \ No newline at end of file From 5fe78746914f73c2eb416cb3b98683eae72c6067 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Fri, 6 Sep 2024 13:04:08 +0200 Subject: [PATCH 10/23] verbose --- .github/workflows/distributed-solutions-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index d6bed73..93fe426 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -64,4 +64,4 @@ jobs: charmcraft-channel: "2.x/stable" - name: Run the charm's integration tests - run: tox -e integration \ No newline at end of file + run: tox -vve integration \ No newline at end of file From 894446c642f2e18ad42a78fef92137e677bab49a Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Mon, 9 Sep 2024 11:27:03 +0200 Subject: [PATCH 11/23] install git --- .github/workflows/distributed-solutions-tests.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index 93fe426..dd9988a 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -32,8 +32,8 @@ jobs: sed -i -e "/^cosl[ ><=]/d" -e "/canonical\/cos-lib/d" -e "/#egg=cosl/d" requirements.txt echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@$GITHUB_SHA#egg=cosl" >> requirements.txt - - name: Install dependencies - run: pip install tox~=4.2 + - name: Install dependencies (tox & git) + run: pip install tox~=4.2 && sudo apt install git -y - name: Run the charm's unit & scenario tests run: tox -e unit,scenario From aa31ff53319412b79cd232f12765ea0235511292 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Mon, 9 Sep 2024 11:52:54 +0200 Subject: [PATCH 12/23] pin scenario to <7 for now --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 84a99c1..f7136ac 100644 --- a/tox.ini +++ b/tox.ini @@ -77,7 +77,7 @@ deps = PyYAML typing_extensions coverage[toml] - ops-scenario + ops-scenario<7.0 cryptography jsonschema lightkube>=v0.15.4 From c2e2d92e719101128958ca7adca0805c55cae61b Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Mon, 9 Sep 2024 17:34:16 +0200 Subject: [PATCH 13/23] install git once more --- .github/workflows/distributed-solutions-tests.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index dd9988a..02d2f31 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -33,7 +33,7 @@ jobs: echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@$GITHUB_SHA#egg=cosl" >> requirements.txt - name: Install dependencies (tox & git) - run: pip install tox~=4.2 && sudo apt install git -y + run: pip install tox~=4.2 && apt install git -y - name: Run the charm's unit & scenario tests run: tox -e unit,scenario @@ -64,4 +64,4 @@ jobs: charmcraft-channel: "2.x/stable" - name: Run the charm's integration tests - run: tox -vve integration \ No newline at end of file + run: apt install git -y ; tox -vve integration \ No newline at end of file From 1d16fdd82b947add1dabf0d48ed7cbc254d6f921 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Mon, 9 Sep 2024 17:49:42 +0200 Subject: [PATCH 14/23] install git once more --- .github/workflows/distributed-solutions-tests.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index 02d2f31..ba9369c 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -33,7 +33,7 @@ jobs: echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@$GITHUB_SHA#egg=cosl" >> requirements.txt - name: Install dependencies (tox & git) - run: pip install tox~=4.2 && apt install git -y + run: pip install tox~=4.2 && sudo apt install git -y - name: Run the charm's unit & scenario tests run: tox -e unit,scenario @@ -64,4 +64,4 @@ jobs: charmcraft-channel: "2.x/stable" - name: Run the charm's integration tests - run: apt install git -y ; tox -vve integration \ No newline at end of file + run: sudo apt install git -y ; tox -vve integration \ No newline at end of file From 731ce68e9d46b3999bd2b2fa18afbfa0a874a267 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Wed, 11 Sep 2024 11:32:02 +0200 Subject: [PATCH 15/23] uniformed coverage report --- .gitignore | 2 +- pyproject.toml | 2 ++ tox.ini | 14 +++++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 81dd65a..ebd845a 100644 --- a/.gitignore +++ b/.gitignore @@ -38,7 +38,7 @@ pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports -htmlcov/ +results/ .tox/ .nox/ .coverage diff --git a/pyproject.toml b/pyproject.toml index 8358b86..7a479ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,8 @@ include = ["py.typed"] # Testing tools configuration [tool.coverage.run] branch = true +parallel = true +omit = ["tests/**", "lib/**"] [tool.coverage.report] show_missing = true diff --git a/tox.ini b/tox.ini index 65bf24b..5e83511 100644 --- a/tox.ini +++ b/tox.ini @@ -73,10 +73,10 @@ deps = deepdiff fs pytest + pytest-cov ops PyYAML typing_extensions - coverage[toml] ops-scenario<7.0.0 cryptography jsonschema @@ -90,7 +90,11 @@ setenv = commands = python -m doctest {[vars]src_path}/cosl/mandatory_relation_pairs.py /usr/bin/env sh -c 'stat cos-tool-amd64 > /dev/null 2>&1 || curl -L -O https://github.com/canonical/cos-tool/releases/latest/download/cos-tool-amd64' - coverage run \ - --source={toxinidir} \ - -m pytest -v --tb native --log-cli-level=INFO -s {posargs} {[vars]tst_path} - coverage report + pytest {tty:--color=yes} --cov={[vars]src_path} --cov-config={tox_root}/pyproject.toml \ + ; for us + --cov-report=html:{tox_root}/results/html-cov/ \ + ; for tiobe + --cov-report=xml:{tox_root}/results/coverage-{env_name}.xml \ + ; for sparta + --cov-report=json:{tox_root}/results/tox-{env_name}.json \ + --junit-xml={tox_root}/results/test-results-{env_name}.xml {posargs} From 5e9af329e7c68e7294c4aa77bb31a407a7d301cb Mon Sep 17 00:00:00 2001 From: michael Date: Thu, 12 Sep 2024 08:37:01 +0300 Subject: [PATCH 16/23] add yq --- .github/workflows/distributed-solutions-tests.yaml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index ba9369c..d05cb0b 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -33,7 +33,10 @@ jobs: echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@$GITHUB_SHA#egg=cosl" >> requirements.txt - name: Install dependencies (tox & git) - run: pip install tox~=4.2 && sudo apt install git -y + run: pip install tox~=4.2 && sudo snap install yq + + - name: Add charmcraft build dependencies + run: yq e '.parts.charm.build-packages += ["git"]' -i charmcraft.yaml - name: Run the charm's unit & scenario tests run: tox -e unit,scenario @@ -64,4 +67,4 @@ jobs: charmcraft-channel: "2.x/stable" - name: Run the charm's integration tests - run: sudo apt install git -y ; tox -vve integration \ No newline at end of file + run: tox -vve integration \ No newline at end of file From b55b1646b34e61b314b3ae893c292f6df3613235 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 12 Sep 2024 09:23:17 +0200 Subject: [PATCH 17/23] typing --- src/cosl/loki_logger.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cosl/loki_logger.py b/src/cosl/loki_logger.py index 54c89fe..00edd31 100644 --- a/src/cosl/loki_logger.py +++ b/src/cosl/loki_logger.py @@ -119,11 +119,18 @@ def build_labels(self, record: logging.LogRecord) -> Dict[str, str]: labels[self.level_label] = record.levelname.lower() labels[self.logger_label] = record.name - extra_labels = getattr(record, "labels", {}) + # if the user implemented a logrecord subclass with a .labels attributes, attempt to + # respect it and add those labels on top of those registered on the LokiEmitter class. + extra_labels: Any = getattr(record, "labels") if not isinstance(extra_labels, dict): return labels + label_name: Any + label_value: Any for label_name, label_value in extra_labels.items(): + if not isinstance(label_name, str) or not isinstance(label_value, str): + return labels + cleared_name = self.format_label(label_name) if cleared_name: labels[cleared_name] = label_value From 6e1d44828721519db19b7f330811e7c4c0715bc8 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 12 Sep 2024 09:27:19 +0200 Subject: [PATCH 18/23] yqw --- .github/workflows/distributed-solutions-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index d05cb0b..dd9a10d 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -32,7 +32,7 @@ jobs: sed -i -e "/^cosl[ ><=]/d" -e "/canonical\/cos-lib/d" -e "/#egg=cosl/d" requirements.txt echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@$GITHUB_SHA#egg=cosl" >> requirements.txt - - name: Install dependencies (tox & git) + - name: Install dependencies (tox & yq) run: pip install tox~=4.2 && sudo snap install yq - name: Add charmcraft build dependencies From 7c1d917bec14c39ba99698421da17efa269b3b37 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 12 Sep 2024 09:30:27 +0200 Subject: [PATCH 19/23] silenced debug databag log if empty --- src/cosl/coordinated_workers/interface.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cosl/coordinated_workers/interface.py b/src/cosl/coordinated_workers/interface.py index 6a99957..e1bcef0 100644 --- a/src/cosl/coordinated_workers/interface.py +++ b/src/cosl/coordinated_workers/interface.py @@ -85,7 +85,8 @@ def load(cls, databag: _RawDatabag): return cls.model_validate_json(json.dumps(data)) # type: ignore except pydantic.ValidationError as e: msg = f"failed to validate databag: {databag}" - log.debug(msg, exc_info=True) + if databag: + log.debug(msg, exc_info=True) raise DataValidationError(msg) from e def dump(self, databag: Optional[_RawDatabag] = None, clear: bool = True) -> _RawDatabag: From 865f11b978a1abbeb9933e8382cf9ea8e294eae8 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 12 Sep 2024 09:31:52 +0200 Subject: [PATCH 20/23] vbump --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 7a479ee..c658188 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "cosl" -version = "0.0.32" +version = "0.0.32+test" authors = [ { name="sed-i", email="82407168+sed-i@users.noreply.github.com" }, ] From e5de169fe98810161e4046d5d789058ee3542281 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Thu, 12 Sep 2024 09:41:28 +0200 Subject: [PATCH 21/23] using latest commit sha --- .github/workflows/distributed-solutions-tests.yaml | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index dd9a10d..751e5dc 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -30,7 +30,7 @@ jobs: - name: Update 'cosl' dependency in test charm to this branch run: | sed -i -e "/^cosl[ ><=]/d" -e "/canonical\/cos-lib/d" -e "/#egg=cosl/d" requirements.txt - echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@$GITHUB_SHA#egg=cosl" >> requirements.txt + echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@${{ github.event.pull_request.head.sha }}#egg=cosl" >> requirements.txt - name: Install dependencies (tox & yq) run: pip install tox~=4.2 && sudo snap install yq diff --git a/pyproject.toml b/pyproject.toml index c658188..d1f1e3d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "cosl" -version = "0.0.32+test" +version = "0.0.35" authors = [ { name="sed-i", email="82407168+sed-i@users.noreply.github.com" }, ] From 22066840dc7b481f312e47e10f9f0b348181781b Mon Sep 17 00:00:00 2001 From: michael Date: Thu, 12 Sep 2024 11:32:47 +0300 Subject: [PATCH 22/23] try with branch name --- .github/workflows/distributed-solutions-tests.yaml | 4 ++-- src/cosl/loki_logger.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index 751e5dc..07e65bb 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -30,12 +30,12 @@ jobs: - name: Update 'cosl' dependency in test charm to this branch run: | sed -i -e "/^cosl[ ><=]/d" -e "/canonical\/cos-lib/d" -e "/#egg=cosl/d" requirements.txt - echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@${{ github.event.pull_request.head.sha }}#egg=cosl" >> requirements.txt + echo -e "\ngit+$GITHUB_SERVER_URL/$GITHUB_REPOSITORY@${{ github.head_ref || github.ref_name }}#egg=cosl" >> requirements.txt - name: Install dependencies (tox & yq) run: pip install tox~=4.2 && sudo snap install yq - - name: Add charmcraft build dependencies + - name: Add charmcraft build dependencies (git) run: yq e '.parts.charm.build-packages += ["git"]' -i charmcraft.yaml - name: Run the charm's unit & scenario tests diff --git a/src/cosl/loki_logger.py b/src/cosl/loki_logger.py index 00edd31..93b97f5 100644 --- a/src/cosl/loki_logger.py +++ b/src/cosl/loki_logger.py @@ -121,7 +121,7 @@ def build_labels(self, record: logging.LogRecord) -> Dict[str, str]: # if the user implemented a logrecord subclass with a .labels attributes, attempt to # respect it and add those labels on top of those registered on the LokiEmitter class. - extra_labels: Any = getattr(record, "labels") + extra_labels: Any = getattr(record, "labels", {}) if not isinstance(extra_labels, dict): return labels From 921ef653f224585ad72953aeeef03a6a37c5cec4 Mon Sep 17 00:00:00 2001 From: michael Date: Thu, 12 Sep 2024 12:30:14 +0300 Subject: [PATCH 23/23] downgrade version + conditional run --- .github/workflows/distributed-solutions-tests.yaml | 5 ++++- pyproject.toml | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/distributed-solutions-tests.yaml b/.github/workflows/distributed-solutions-tests.yaml index 07e65bb..a3ab433 100644 --- a/.github/workflows/distributed-solutions-tests.yaml +++ b/.github/workflows/distributed-solutions-tests.yaml @@ -39,9 +39,11 @@ jobs: run: yq e '.parts.charm.build-packages += ["git"]' -i charmcraft.yaml - name: Run the charm's unit & scenario tests + id: unit run: tox -e unit,scenario - name: Run the charm's static analysis checks + id: static run: tox -e static-charm,static-lib - name: Setup Charmcraft's pip cache @@ -67,4 +69,5 @@ jobs: charmcraft-channel: "2.x/stable" - name: Run the charm's integration tests - run: tox -vve integration \ No newline at end of file + run: tox -vve integration + if: steps.unit.outcome == 'success' && steps.static.outcome == 'success' \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index d1f1e3d..7a479ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "cosl" -version = "0.0.35" +version = "0.0.32" authors = [ { name="sed-i", email="82407168+sed-i@users.noreply.github.com" }, ]