-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1150 from james-garner-canonical/24.10/tests/vali…
…date-client-facades #1150 #### Description `client_facades` in `juju/client/connection.py` is manually updated. It doesn't reflect the actual facade code present, which was recently generated from the current 3.1.X and 3.3.0 schemas currently checked in. Nor did it reflect the state of the generated code with the previous set of schemas (same + 3.2.X schemas), nor the state of the actually checked in code before the last regeneration of the code (which contained some orphan facades not present in the schemas). This PR updates `client_schemas` and adds a test to ensure that its facade versions always match the schemas present in the generated client code. The test can be run with `tox -e validate`, and is run in a separate github job as it didn't seem to fit in well as either a unit or integration test. In future we could add more tests to this group to validate other aspects of the codebase related to the code generation. #### QA Steps All non-quarantined tests should continue to work. ``` tox -e validate ``` Should pass currently. #### Notes & Discussion Does the current state of client_facades make sense? One thing that stood out to me was the addition of `Admin`. I'm wondering if any non-client facades are being added here, since facade code is generated from the full schemas still. If that's the case, then this should be deferred till after we switch to client-only schemas.
- Loading branch information
Showing
4 changed files
with
140 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2023 Canonical Ltd. | ||
# Licensed under the Apache V2, see LICENCE file for details. | ||
|
||
import importlib | ||
from collections import defaultdict | ||
from pathlib import Path | ||
from typing import Dict, List, TypedDict | ||
|
||
import pytest | ||
|
||
from juju.client.connection import client_facades, excluded_facades | ||
|
||
|
||
class Versions(TypedDict, total=True): | ||
versions: List[int] | ||
|
||
|
||
ClientFacades = Dict[str, Versions] | ||
|
||
|
||
@pytest.fixture | ||
def project_root(pytestconfig: pytest.Config) -> Path: | ||
return pytestconfig.rootpath | ||
|
||
|
||
@pytest.fixture | ||
def generated_code_facades(project_root: Path) -> ClientFacades: | ||
"""Return a client_facades dictionary from generated code under project_root. | ||
Iterates through all the generated files matching juju/client/_client*.py, | ||
extracting facade types (those that have .name and .version properties). | ||
Excludes facades in juju.client.connection.excluded_facades, as these are | ||
manually marked as incompatible with the current version of python-libjuju. | ||
""" | ||
facades: Dict[str, List[int]] = defaultdict(list) | ||
for file in project_root.glob('juju/client/_client*.py'): | ||
module = importlib.import_module(f'juju.client.{file.stem}') | ||
for cls_name in dir(module): | ||
cls = getattr(module, cls_name) | ||
try: # duck typing check for facade types | ||
cls.name | ||
cls.version | ||
except AttributeError: | ||
continue | ||
if cls.version in excluded_facades.get(cls.name, []): | ||
continue | ||
facades[cls.name].append(cls.version) | ||
return {name: {'versions': sorted(facades[name])} for name in sorted(facades)} | ||
|
||
|
||
def test_client_facades(project_root: Path, generated_code_facades: ClientFacades) -> None: | ||
"""Ensure that juju.client.connection.client_facades matches expected facades. | ||
See generated_code_facades for how expected facades are computed. | ||
""" | ||
assert { | ||
k: v['versions'] for k, v in client_facades.items() | ||
} == { | ||
k: v['versions'] for k, v in generated_code_facades.items() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters