Skip to content

Commit

Permalink
chore: replace harness with scenario (#277)
Browse files Browse the repository at this point in the history
Signed-off-by: guillaume <[email protected]>
  • Loading branch information
gruyaume authored Sep 3, 2024
1 parent 746c976 commit f84b158
Show file tree
Hide file tree
Showing 14 changed files with 2,256 additions and 1,721 deletions.
1 change: 1 addition & 0 deletions test-requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pytest-asyncio==0.21.2
pytest-dependency
pytest-operator
ruff
ops-scenario
25 changes: 19 additions & 6 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ iniconfig==2.0.0
# pytest
ipdb==0.13.13
# via pytest-operator
ipython==8.26.0
ipython==8.27.0
# via ipdb
jedi==0.19.1
# via ipython
Expand Down Expand Up @@ -84,6 +84,14 @@ oauthlib==3.2.2
# via
# kubernetes
# requests-oauthlib
ops==2.15.0
# via
# -c requirements.txt
# ops-scenario
ops-scenario==6.1.6
# via
# -c requirements.txt
# -r test-requirements.in
packaging==24.1
# via
# -c requirements.txt
Expand All @@ -101,7 +109,7 @@ pluggy==1.5.0
# pytest
prompt-toolkit==3.0.47
# via ipython
protobuf==5.27.3
protobuf==5.28.0
# via macaroonbakery
ptyprocess==0.7.0
# via pexpect
Expand Down Expand Up @@ -131,7 +139,7 @@ pyrfc3339==1.1
# via
# juju
# macaroonbakery
pyright==1.1.377
pyright==1.1.378
# via -r test-requirements.in
pytest==8.3.2
# via
Expand Down Expand Up @@ -159,6 +167,8 @@ pyyaml==6.0.2
# -c requirements.txt
# juju
# kubernetes
# ops
# ops-scenario
# pytest-operator
requests==2.32.3
# via
Expand All @@ -171,7 +181,7 @@ requests-oauthlib==2.0.0
# via kubernetes
rsa==4.9
# via google-auth
ruff==0.6.2
ruff==0.6.3
# via -r test-requirements.in
six==1.16.0
# via
Expand All @@ -192,7 +202,6 @@ traitlets==5.14.3
typing-extensions==4.12.2
# via
# -c requirements.txt
# ipython
# typing-inspect
typing-inspect==0.9.0
# via juju
Expand All @@ -207,5 +216,9 @@ websocket-client==1.8.0
# via
# -c requirements.txt
# kubernetes
websockets==13.0
# ops
websockets==13.0.1
# via juju

# The following packages are considered to be unsafe in a requirements file:
# setuptools
63 changes: 63 additions & 0 deletions tests/unit/certificates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

import datetime

from charms.tls_certificates_interface.v3.tls_certificates import (
ProviderCertificate,
RequirerCSR,
generate_ca,
generate_certificate,
generate_csr,
generate_private_key,
)


def generate_example_provider_certificate(
common_name: str,
relation_id: int,
) -> ProviderCertificate:
private_key = generate_private_key()
csr = generate_csr(
private_key=private_key,
subject=common_name,
)
ca_private_key = generate_private_key()
ca_certificate = generate_ca(
private_key=ca_private_key,
subject="ca.com",
validity=365,
)
certificate = generate_certificate(
csr=csr,
ca=ca_certificate,
ca_key=ca_private_key,
validity=365,
)

provider_certificate = ProviderCertificate(
application_name="vault",
relation_id=relation_id,
csr=csr.decode(),
certificate=certificate.decode(),
ca=ca_certificate.decode(),
chain=[ca_certificate.decode()],
revoked=False,
expiry_time=datetime.datetime.now(),
)
return provider_certificate


def generate_example_requirer_csr(common_name: str, relation_id: int) -> RequirerCSR:
private_key = generate_private_key()
csr = generate_csr(
private_key=private_key,
subject=common_name,
)
return RequirerCSR(
relation_id=relation_id,
application_name="tls-requirer",
unit_name="tls-requirer/0",
csr=csr.decode(),
is_ca=False,
)
105 changes: 105 additions & 0 deletions tests/unit/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

from unittest.mock import patch

import pytest
import scenario
from charms.data_platform_libs.v0.s3 import S3Requirer
from charms.vault_k8s.v0.vault_client import Vault
from charms.vault_k8s.v0.vault_s3 import S3
from charms.vault_k8s.v0.vault_tls import VaultTLSManager

from charm import VaultOperatorCharm


class VaultCharmFixtures:
patcher_tls = patch("charm.VaultTLSManager", autospec=VaultTLSManager)
patcher_vault = patch("charm.Vault", autospec=Vault)
patcher_s3_requirer = patch("charm.S3Requirer", autospec=S3Requirer)
patcher_s3 = patch("charm.S3", autospec=S3)
patcher_socket_fqdn = patch("socket.getfqdn")
patcher_pki_requirer_request_certificate_creation = patch(
"charm.TLSCertificatesRequiresV3.request_certificate_creation"
)
patcher_pki_requirer_get_assigned_certificates = patch(
"charm.TLSCertificatesRequiresV3.get_assigned_certificates"
)
patcher_pki_provider_get_outstanding_certificate_requests = patch(
"charm.TLSCertificatesProvidesV3.get_outstanding_certificate_requests"
)
patcher_pki_provider_set_relation_certificate = patch(
"charm.TLSCertificatesProvidesV3.set_relation_certificate"
)
patcher_autounseal_provides_get_outstanding_requests = patch(
"charm.VaultAutounsealProvides.get_outstanding_requests"
)
patcher_autounseal_provides_set_data = patch(
"charm.VaultAutounsealProvides.set_autounseal_data"
)
patcher_autounseal_requires_get_details = patch("charm.VaultAutounsealRequires.get_details")
patcher_kv_provides_get_outstanding_kv_requests = patch(
"charm.VaultKvProvides.get_outstanding_kv_requests"
)
patcher_kv_provides_set_ca_certificate = patch("charm.VaultKvProvides.set_ca_certificate")
patcher_kv_provides_set_egress_subnets = patch("charm.VaultKvProvides.set_egress_subnets")
patcher_kv_provides_set_vault_url = patch("charm.VaultKvProvides.set_vault_url")
patcher_kv_provides_get_credentials = patch("charm.VaultKvProvides.get_credentials")
patcher_snap_cache = patch("charm.snap.SnapCache")
patcher_machine = patch("charm.Machine")
patcher_get_common_name_from_csr = patch("charm.get_common_name_from_csr")

@pytest.fixture(autouse=True)
def setup(self):
self.mock_tls = VaultCharmFixtures.patcher_tls.start().return_value
self.mock_vault = VaultCharmFixtures.patcher_vault.start().return_value
self.mock_s3_requirer = VaultCharmFixtures.patcher_s3_requirer.start().return_value
self.mock_s3 = VaultCharmFixtures.patcher_s3.start()
self.mock_socket_fqdn = VaultCharmFixtures.patcher_socket_fqdn.start()
self.mock_pki_requirer_request_certificate_creation = (
VaultCharmFixtures.patcher_pki_requirer_request_certificate_creation.start()
)
self.mock_pki_requirer_get_assigned_certificates = (
VaultCharmFixtures.patcher_pki_requirer_get_assigned_certificates.start()
)
self.mock_pki_provider_get_outstanding_certificate_requests = (
VaultCharmFixtures.patcher_pki_provider_get_outstanding_certificate_requests.start()
)
self.mock_pki_provider_set_relation_certificate = (
VaultCharmFixtures.patcher_pki_provider_set_relation_certificate.start()
)
self.mock_autounseal_provides_get_outstanding_requests = (
VaultCharmFixtures.patcher_autounseal_provides_get_outstanding_requests.start()
)
self.mock_autounseal_provides_set_data = (
VaultCharmFixtures.patcher_autounseal_provides_set_data.start()
)
self.mock_autounseal_requires_get_details = (
VaultCharmFixtures.patcher_autounseal_requires_get_details.start()
)
self.mock_kv_provides_get_outstanding_kv_requests = (
VaultCharmFixtures.patcher_kv_provides_get_outstanding_kv_requests.start()
)
self.mock_kv_provides_set_ca_certificate = (
VaultCharmFixtures.patcher_kv_provides_set_ca_certificate.start()
)
self.mock_kv_provides_set_egress_subnets = (
VaultCharmFixtures.patcher_kv_provides_set_egress_subnets.start()
)
self.mock_kv_provides_set_vault_url = (
VaultCharmFixtures.patcher_kv_provides_set_vault_url.start()
)
self.mock_kv_provides_get_credentials = (
VaultCharmFixtures.patcher_kv_provides_get_credentials.start()
)
self.mock_snap_cache = VaultCharmFixtures.patcher_snap_cache.start()
self.mock_machine = VaultCharmFixtures.patcher_machine.start().return_value
self.mock_get_common_name_from_csr = (
VaultCharmFixtures.patcher_get_common_name_from_csr.start()
)

@pytest.fixture(autouse=True)
def context(self):
self.ctx = scenario.Context(
charm_type=VaultOperatorCharm,
)
Loading

0 comments on commit f84b158

Please sign in to comment.