-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use scenario for unit testing (pt.2) (#225)
Signed-off-by: guillaume <[email protected]>
- Loading branch information
Showing
6 changed files
with
239 additions
and
190 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
from unittest.mock import patch | ||
|
||
import pytest | ||
import scenario | ||
from ops.model import ActiveStatus, BlockedStatus | ||
|
||
from charm import SelfSignedCertificatesCharm | ||
|
||
|
||
class TestCharmCollectStatus: | ||
@pytest.fixture(autouse=True) | ||
def context(self): | ||
self.ctx = scenario.Context( | ||
charm_type=SelfSignedCertificatesCharm, | ||
) | ||
|
||
def test_given_invalid_config_when_collect_unit_status_then_status_is_blocked(self): | ||
state_in = scenario.State( | ||
config={ | ||
"ca-common-name": "", | ||
"certificate-validity": 100, | ||
}, | ||
leader=True, | ||
) | ||
|
||
state_out = self.ctx.run(event="collect_unit_status", state=state_in) | ||
|
||
assert state_out.unit_status == BlockedStatus( | ||
"The following configuration values are not valid: ['ca-common-name']" | ||
) | ||
|
||
def test_given_invalid_validity_config_when_collect_unit_status_then_status_is_blocked(self): | ||
state_in = scenario.State( | ||
config={ | ||
"ca-common-name": "pizza.com", | ||
"certificate-validity": 0, | ||
}, | ||
leader=True, | ||
) | ||
|
||
state_out = self.ctx.run(event="collect_unit_status", state=state_in) | ||
|
||
assert state_out.unit_status == BlockedStatus( | ||
"The following configuration values are not valid: ['certificate-validity']" | ||
) | ||
|
||
@patch("charm.generate_private_key") | ||
@patch("charm.generate_ca") | ||
def test_given_valid_config_when_collect_unit_status_then_status_is_active( | ||
self, | ||
patch_generate_ca, | ||
patch_generate_private_key, | ||
): | ||
patch_generate_ca.return_value = "whatever CA certificate" | ||
patch_generate_private_key.return_value = "whatever private key" | ||
state_in = scenario.State( | ||
config={ | ||
"ca-common-name": "pizza.com", | ||
"certificate-validity": 100, | ||
}, | ||
leader=True, | ||
) | ||
|
||
state_out = self.ctx.run(event="collect_unit_status", state=state_in) | ||
|
||
assert state_out.unit_status == ActiveStatus() |
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,49 @@ | ||
# Copyright 2023 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
|
||
import pytest | ||
import scenario | ||
|
||
from charm import SelfSignedCertificatesCharm | ||
|
||
|
||
class TestCharmGetCACertificate: | ||
@pytest.fixture(autouse=True) | ||
def context(self): | ||
self.ctx = scenario.Context( | ||
charm_type=SelfSignedCertificatesCharm, | ||
) | ||
|
||
def test_given_ca_cert_generated_when_get_ca_certificate_action_then_returns_ca_certificate( | ||
self, | ||
): | ||
ca_certificate = "whatever CA certificate" | ||
ca_certificates_secret = scenario.Secret( | ||
id="0", | ||
label="ca-certificates", | ||
contents={ | ||
0: { | ||
"ca-certificate": ca_certificate, | ||
} | ||
}, | ||
owner="app", | ||
) | ||
state_in = scenario.State( | ||
leader=True, | ||
secrets=[ca_certificates_secret], | ||
) | ||
|
||
action_output = self.ctx.run_action("get-ca-certificate", state=state_in) | ||
assert action_output.results | ||
assert action_output.results["ca-certificate"] == ca_certificate | ||
|
||
def test_given_ca_cert_not_generated_when_get_ca_certificate_action_then_action_fails(self): | ||
state_in = scenario.State( | ||
leader=True, | ||
) | ||
|
||
action_output = self.ctx.run_action("get-ca-certificate", state=state_in) | ||
|
||
assert not action_output.success | ||
assert action_output.failure == "Root Certificate is not yet generated" |
Oops, something went wrong.