-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: clean up charm and test code
- Loading branch information
Showing
3 changed files
with
136 additions
and
105 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,126 @@ | ||
# Copyright 2020 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
import unittest | ||
from tests.unit.test_charm import BaseTestCharm | ||
|
||
OAUTH_CLIENT_ID = "grafana_client_id" | ||
OAUTH_CLIENT_SECRET = "s3cR#T" | ||
OAUTH_PROVIDER_INFO = { | ||
"authorization_endpoint": "https://example.oidc.com/oauth2/auth", | ||
"introspection_endpoint": "https://example.oidc.com/admin/oauth2/introspect", | ||
"issuer_url": "https://example.oidc.com", | ||
"jwks_endpoint": "https://example.oidc.com/.well-known/jwks.json", | ||
"scope": "openid profile email phone", | ||
"token_endpoint": "https://example.oidc.com/oauth2/token", | ||
"userinfo_endpoint": "https://example.oidc.com/userinfo", | ||
} | ||
|
||
|
||
class TestOauth(BaseTestCharm): | ||
def test_config_is_updated_with_oauth_relation_data(self): | ||
self.harness.set_leader(True) | ||
self.harness.container_pebble_ready("grafana") | ||
|
||
# add oauth relation with provider endpoints details | ||
rel_id = self.harness.add_relation("oauth", "hydra") | ||
self.harness.add_relation_unit(rel_id, "hydra/0") | ||
secret_id = self.harness.add_model_secret("hydra", {"secret": OAUTH_CLIENT_SECRET}) | ||
self.harness.grant_secret(secret_id, "grafana-k8s") | ||
self.harness.update_relation_data( | ||
rel_id, | ||
"hydra", | ||
{ | ||
"client_id": OAUTH_CLIENT_ID, | ||
"client_secret_id": secret_id, | ||
**OAUTH_PROVIDER_INFO, | ||
}, | ||
) | ||
|
||
services = ( | ||
self.harness.charm.containers["workload"].get_plan().services["grafana"].to_dict() | ||
) | ||
env = services["environment"] | ||
|
||
expected_env = { | ||
"GF_AUTH_GENERIC_OAUTH_ENABLED": "True", | ||
"GF_AUTH_GENERIC_OAUTH_NAME": "external identity provider", | ||
"GF_AUTH_GENERIC_OAUTH_CLIENT_ID": OAUTH_CLIENT_ID, | ||
"GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET": OAUTH_CLIENT_SECRET, | ||
"GF_AUTH_GENERIC_OAUTH_SCOPES": "openid email offline_access", | ||
"GF_AUTH_GENERIC_OAUTH_AUTH_URL": OAUTH_PROVIDER_INFO["authorization_endpoint"], | ||
"GF_AUTH_GENERIC_OAUTH_TOKEN_URL": OAUTH_PROVIDER_INFO["token_endpoint"], | ||
"GF_AUTH_GENERIC_OAUTH_API_URL": OAUTH_PROVIDER_INFO["userinfo_endpoint"], | ||
"GF_AUTH_GENERIC_OAUTH_USE_REFRESH_TOKEN": "True", | ||
"GF_FEATURE_TOGGLES_ENABLE": "accessTokenExpirationCheck", | ||
} | ||
all(self.assertEqual(env[k], v) for k, v in expected_env.items()) | ||
|
||
def test_config_with_empty_oauth_relation_data(self): | ||
self.harness.set_leader(True) | ||
self.harness.container_pebble_ready("grafana") | ||
|
||
rel_id = self.harness.add_relation("oauth", "hydra") | ||
self.harness.add_relation_unit(rel_id, "hydra/0") | ||
|
||
services = ( | ||
self.harness.charm.containers["workload"].get_plan().services["grafana"].to_dict() | ||
) | ||
env = services["environment"] | ||
|
||
oauth_env = { | ||
"GF_AUTH_GENERIC_OAUTH_ENABLED", | ||
"GF_AUTH_GENERIC_OAUTH_NAME", | ||
"GF_AUTH_GENERIC_OAUTH_CLIENT_ID", | ||
"GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET", | ||
"GF_AUTH_GENERIC_OAUTH_SCOPES", | ||
"GF_AUTH_GENERIC_OAUTH_AUTH_URL", | ||
"GF_AUTH_GENERIC_OAUTH_TOKEN_URL", | ||
"GF_AUTH_GENERIC_OAUTH_API_URL", | ||
"GF_AUTH_GENERIC_OAUTH_USE_REFRESH_TOKEN", | ||
} | ||
all(self.assertNotIn(k, env) for k in oauth_env) | ||
|
||
# The oauth library tries to access the relation databag | ||
# when the relation is departing. This causes harness to throw an | ||
# error, a behavior not implemented in juju. This will be fixed | ||
# once https://github.com/canonical/operator/issues/940 is merged | ||
@unittest.expectedFailure | ||
def test_config_is_updated_with_oauth_relation_data_removed(self): | ||
self.harness.set_leader(True) | ||
self.harness.container_pebble_ready("grafana") | ||
|
||
# add oauth relation with provider endpoints details | ||
rel_id = self.harness.add_relation("oauth", "hydra") | ||
self.harness.add_relation_unit(rel_id, "hydra/0") | ||
secret_id = self.harness.add_model_secret("hydra", {"secret": OAUTH_CLIENT_SECRET}) | ||
self.harness.grant_secret(secret_id, "grafana-k8s") | ||
self.harness.update_relation_data( | ||
rel_id, | ||
"hydra", | ||
{ | ||
"client_id": OAUTH_CLIENT_ID, | ||
"client_secret_id": secret_id, | ||
**OAUTH_PROVIDER_INFO, | ||
}, | ||
) | ||
rel_id = self.harness.remove_relation(rel_id) | ||
|
||
services = ( | ||
self.harness.charm.containers["workload"].get_plan().services["grafana"].to_dict() | ||
) | ||
env = services["environment"] | ||
|
||
expected_env = { | ||
"GF_AUTH_GENERIC_OAUTH_ENABLED": "True", | ||
"GF_AUTH_GENERIC_OAUTH_NAME": "external identity provider", | ||
"GF_AUTH_GENERIC_OAUTH_CLIENT_ID": OAUTH_CLIENT_ID, | ||
"GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET": OAUTH_CLIENT_SECRET, | ||
"GF_AUTH_GENERIC_OAUTH_SCOPES": "openid email offline_access", | ||
"GF_AUTH_GENERIC_OAUTH_AUTH_URL": OAUTH_PROVIDER_INFO["authorization_endpoint"], | ||
"GF_AUTH_GENERIC_OAUTH_TOKEN_URL": OAUTH_PROVIDER_INFO["token_endpoint"], | ||
"GF_AUTH_GENERIC_OAUTH_API_URL": OAUTH_PROVIDER_INFO["userinfo_endpoint"], | ||
"GF_AUTH_GENERIC_OAUTH_USE_REFRESH_TOKEN": "True", | ||
"GF_FEATURE_TOGGLES_ENABLE": "accessTokenExpirationCheck", | ||
} | ||
all(self.assertEqual(env[k], v) for k, v in expected_env.items()) |