Major improvement in OSM charms
- Adapt all new operator charms to use the same pattern. They are all
using now this library that encapsulates the common logic for all
charms: https://github.com/davigar15/ops-lib-charmed-osm. That will be
eventually moved to gitlab, when it has a PyPI repository available
- Add unit tests to all charms
- Modify installer and bundles to point to the new charms
- Improve the build.sh script for building the charms
Change-Id: I0896ceb082d1b6a76b3560c07482a4135a220a3f
Signed-off-by: David Garcia <david.garcia@canonical.com>
diff --git a/installers/charm/nbi/.gitignore b/installers/charm/nbi/.gitignore
index 05bdb52..21a2062 100644
--- a/installers/charm/nbi/.gitignore
+++ b/installers/charm/nbi/.gitignore
@@ -1,4 +1,4 @@
-# Copyright 2020 Canonical Ltd.
+# Copyright 2021 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@@ -22,7 +22,14 @@
venv
.vscode
build
+<<<<<<< HEAD
nbi.charm
.coverage*
+=======
+*.charm
+.coverage
+coverage.xml
+>>>>>>> WIP: Improve osm charms
.stestr
-cover
\ No newline at end of file
+cover
+release
\ No newline at end of file
diff --git a/installers/charm/nbi/.jujuignore b/installers/charm/nbi/.jujuignore
new file mode 100644
index 0000000..bf04eb4
--- /dev/null
+++ b/installers/charm/nbi/.jujuignore
@@ -0,0 +1,28 @@
+# Copyright 2021 Canonical Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: legal@canonical.com
+#
+# To get in touch with the maintainers, please contact:
+# osm-charmers@lists.launchpad.net
+##
+
+venv
+.vscode
+build
+prometheus.charm
+.coverage
+.stestr
+cover
diff --git a/installers/charm/nbi/.yamllint.yaml b/installers/charm/nbi/.yamllint.yaml
index c20ac8d..d71fb69 100644
--- a/installers/charm/nbi/.yamllint.yaml
+++ b/installers/charm/nbi/.yamllint.yaml
@@ -1,4 +1,4 @@
-# Copyright 2020 Canonical Ltd.
+# Copyright 2021 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@@ -28,6 +28,7 @@
- ".yamllint"
ignore: |
.tox
+ cover/
build/
- mod/
- lib/
+ venv
+ release/
diff --git a/installers/charm/nbi/metadata.yaml b/installers/charm/nbi/metadata.yaml
index fadfa46..098387f 100644
--- a/installers/charm/nbi/metadata.yaml
+++ b/installers/charm/nbi/metadata.yaml
@@ -41,12 +41,16 @@
requires:
kafka:
interface: kafka
+ limit: 1
mongodb:
interface: mongodb
+ limit: 1
keystone:
interface: keystone
+ limit: 1
prometheus:
interface: prometheus
+ limit: 1
provides:
nbi:
- interface: osm-nbi
+ interface: http
diff --git a/installers/charm/nbi/requirements-test.txt b/installers/charm/nbi/requirements-test.txt
new file mode 100644
index 0000000..d7585f3
--- /dev/null
+++ b/installers/charm/nbi/requirements-test.txt
@@ -0,0 +1,31 @@
+# Copyright 2021 Canonical Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: legal@canonical.com
+#
+# To get in touch with the maintainers, please contact:
+# osm-charmers@lists.launchpad.net
+##
+-r requirements.txt
+coverage
+stestr
+mock
+black
+yamllint
+flake8
+safety
+requests-mock
+asynctest
+nose2
\ No newline at end of file
diff --git a/installers/charm/nbi/requirements.txt b/installers/charm/nbi/requirements.txt
index a26601f..f10a199 100644
--- a/installers/charm/nbi/requirements.txt
+++ b/installers/charm/nbi/requirements.txt
@@ -1,4 +1,4 @@
-# Copyright 2020 Canonical Ltd.
+# Copyright 2021 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@@ -19,5 +19,4 @@
# osm-charmers@lists.launchpad.net
##
-ops
-git+https://github.com/juju-solutions/resource-oci-image/@c5778285d332edf3d9a538f9d0c06154b7ec1b0b#egg=oci-image
+git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
diff --git a/installers/charm/nbi/src/charm.py b/installers/charm/nbi/src/charm.py
index 848b53d..9d62fe2 100755
--- a/installers/charm/nbi/src/charm.py
+++ b/installers/charm/nbi/src/charm.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
-# Copyright 2020 Canonical Ltd.
+# Copyright 2021 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@@ -20,360 +20,245 @@
# osm-charmers@lists.launchpad.net
##
+# pylint: disable=E0213
+
+
import logging
-from typing import Any, Dict, NoReturn
+from typing import Optional, NoReturn
+from ipaddress import ip_network
+from urllib.parse import urlparse
-from ops.charm import CharmBase, CharmEvents
-from ops.framework import EventBase, EventSource, StoredState
from ops.main import main
-from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus
-from oci_image import OCIImageResource, OCIImageResourceError
-from pod_spec import make_pod_spec
+from opslib.osm.charm import CharmedOsmBase, RelationsMissing
+
+from opslib.osm.pod import (
+ ContainerV3Builder,
+ PodSpecV3Builder,
+ IngressResourceV3Builder,
+)
+
+
+from opslib.osm.validator import (
+ ModelValidator,
+ validator,
+)
+
+from opslib.osm.interfaces.kafka import KafkaClient
+from opslib.osm.interfaces.mongo import MongoClient
+from opslib.osm.interfaces.prometheus import PrometheusClient
+from opslib.osm.interfaces.keystone import KeystoneClient
+from opslib.osm.interfaces.http import HttpServer
+
logger = logging.getLogger(__name__)
-NBI_PORT = 9999
+PORT = 9999
-class ConfigurePodEvent(EventBase):
- """Configure Pod event"""
+class ConfigModel(ModelValidator):
+ enable_test: bool
+ auth_backend: str
+ database_commonkey: str
+ log_level: str
+ max_file_size: int
+ site_url: Optional[str]
+ ingress_whitelist_source_range: Optional[str]
+ tls_secret_name: Optional[str]
- pass
+ @validator("auth_backend")
+ def validate_auth_backend(cls, v):
+ if v not in {"internal", "keystone"}:
+ raise ValueError("value must be 'internal' or 'keystone'")
+ return v
+
+ @validator("log_level")
+ def validate_log_level(cls, v):
+ if v not in {"INFO", "DEBUG"}:
+ raise ValueError("value must be INFO or DEBUG")
+ return v
+
+ @validator("max_file_size")
+ def validate_max_file_size(cls, v):
+ if v < 0:
+ raise ValueError("value must be equal or greater than 0")
+ return v
+
+ @validator("site_url")
+ def validate_site_url(cls, v):
+ if v:
+ parsed = urlparse(v)
+ if not parsed.scheme.startswith("http"):
+ raise ValueError("value must start with http")
+ return v
+
+ @validator("ingress_whitelist_source_range")
+ def validate_ingress_whitelist_source_range(cls, v):
+ if v:
+ ip_network(v)
+ return v
-class NbiEvents(CharmEvents):
- """NBI Events"""
-
- configure_pod = EventSource(ConfigurePodEvent)
-
-
-class NbiCharm(CharmBase):
- """NBI Charm."""
-
- state = StoredState()
- on = NbiEvents()
-
+class NbiCharm(CharmedOsmBase):
def __init__(self, *args) -> NoReturn:
- """NBI Charm constructor."""
- super().__init__(*args)
+ super().__init__(*args, oci_image="image")
- # Internal state initialization
- self.state.set_default(pod_spec=None)
+ self.kafka_client = KafkaClient(self, "kafka")
+ self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod)
+ self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod)
- # Message bus data initialization
- self.state.set_default(message_host=None)
- self.state.set_default(message_port=None)
+ self.mongodb_client = MongoClient(self, "mongodb")
+ self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod)
+ self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod)
- # Database data initialization
- self.state.set_default(database_uri=None)
-
- # Prometheus data initialization
- self.state.set_default(prometheus_host=None)
- self.state.set_default(prometheus_port=None)
-
- # Keystone data initialization
- self.state.set_default(keystone_host=None)
- self.state.set_default(keystone_port=None)
- self.state.set_default(keystone_user_domain_name=None)
- self.state.set_default(keystone_project_domain_name=None)
- self.state.set_default(keystone_username=None)
- self.state.set_default(keystone_password=None)
- self.state.set_default(keystone_service=None)
-
- self.port = NBI_PORT
- self.image = OCIImageResource(self, "image")
-
- # Registering regular events
- self.framework.observe(self.on.start, self.configure_pod)
- self.framework.observe(self.on.config_changed, self.configure_pod)
- self.framework.observe(self.on.upgrade_charm, self.configure_pod)
-
- # Registering custom internal events
- self.framework.observe(self.on.configure_pod, self.configure_pod)
-
- # Registering required relation changed events
+ self.prometheus_client = PrometheusClient(self, "prometheus")
self.framework.observe(
- self.on.kafka_relation_changed, self._on_kafka_relation_changed
+ self.on["prometheus"].relation_changed, self.configure_pod
)
self.framework.observe(
- self.on.mongodb_relation_changed, self._on_mongodb_relation_changed
- )
- self.framework.observe(
- self.on.keystone_relation_changed, self._on_keystone_relation_changed
- )
- self.framework.observe(
- self.on.prometheus_relation_changed, self._on_prometheus_relation_changed
+ self.on["prometheus"].relation_broken, self.configure_pod
)
- # Registering required relation departed events
- self.framework.observe(
- self.on.kafka_relation_departed, self._on_kafka_relation_departed
- )
- self.framework.observe(
- self.on.mongodb_relation_departed, self._on_mongodb_relation_departed
- )
- self.framework.observe(
- self.on.keystone_relation_departed, self._on_keystone_relation_departed
- )
- self.framework.observe(
- self.on.prometheus_relation_departed, self._on_prometheus_relation_departed
- )
+ self.keystone_client = KeystoneClient(self, "keystone")
+ self.framework.observe(self.on["keystone"].relation_changed, self.configure_pod)
+ self.framework.observe(self.on["keystone"].relation_broken, self.configure_pod)
- # Registering provided relation events
- self.framework.observe(self.on.nbi_relation_joined, self._publish_nbi_info)
+ self.http_server = HttpServer(self, "nbi")
+ self.framework.observe(self.on["nbi"].relation_joined, self._publish_nbi_info)
- def _on_kafka_relation_changed(self, event: EventBase) -> NoReturn:
- """Reads information about the kafka relation.
-
- Args:
- event (EventBase): Kafka relation event.
- """
- message_host = event.relation.data[event.unit].get("host")
- message_port = event.relation.data[event.unit].get("port")
-
- if (
- message_host
- and message_port
- and (
- self.state.message_host != message_host
- or self.state.message_port != message_port
- )
- ):
- self.state.message_host = message_host
- self.state.message_port = int(message_port)
- self.on.configure_pod.emit()
-
- def _on_kafka_relation_departed(self, event: EventBase) -> NoReturn:
- """Clears data from kafka relation.
-
- Args:
- event (EventBase): Kafka relation event.
- """
- self.state.message_host = None
- self.state.message_port = None
- self.on.configure_pod.emit()
-
- def _on_mongodb_relation_changed(self, event: EventBase) -> NoReturn:
- """Reads information about the DB relation.
-
- Args:
- event (EventBase): DB relation event.
- """
- database_uri = event.relation.data[event.unit].get("connection_string")
-
- if database_uri and self.state.database_uri != database_uri:
- self.state.database_uri = database_uri
- self.on.configure_pod.emit()
-
- def _on_mongodb_relation_departed(self, event: EventBase) -> NoReturn:
- """Clears data from mongodb relation.
-
- Args:
- event (EventBase): DB relation event.
- """
- self.state.database_uri = None
- self.on.configure_pod.emit()
-
- def _on_keystone_relation_changed(self, event: EventBase) -> NoReturn:
- """Reads information about the keystone relation.
-
- Args:
- event (EventBase): Keystone relation event.
- """
- keystone_host = event.relation.data[event.unit].get("host")
- keystone_port = event.relation.data[event.unit].get("port")
- keystone_user_domain_name = event.relation.data[event.unit].get(
- "user_domain_name"
- )
- keystone_project_domain_name = event.relation.data[event.unit].get(
- "project_domain_name"
- )
- keystone_username = event.relation.data[event.unit].get("username")
- keystone_password = event.relation.data[event.unit].get("password")
- keystone_service = event.relation.data[event.unit].get("service")
-
- if (
- keystone_host
- and keystone_port
- and keystone_user_domain_name
- and keystone_project_domain_name
- and keystone_username
- and keystone_password
- and keystone_service
- and (
- self.state.keystone_host != keystone_host
- or self.state.keystone_port != keystone_port
- or self.state.keystone_user_domain_name != keystone_user_domain_name
- or self.state.keystone_project_domain_name
- != keystone_project_domain_name
- or self.state.keystone_username != keystone_username
- or self.state.keystone_password != keystone_password
- or self.state.keystone_service != keystone_service
- )
- ):
- self.state.keystone_host = keystone_host
- self.state.keystone_port = int(keystone_port)
- self.state.keystone_user_domain_name = keystone_user_domain_name
- self.state.keystone_project_domain_name = keystone_project_domain_name
- self.state.keystone_username = keystone_username
- self.state.keystone_password = keystone_password
- self.state.keystone_service = keystone_service
- self.on.configure_pod.emit()
-
- def _on_keystone_relation_departed(self, event: EventBase) -> NoReturn:
- """Clears data from keystone relation.
-
- Args:
- event (EventBase): Keystone relation event.
- """
- self.state.keystone_host = None
- self.state.keystone_port = None
- self.state.keystone_user_domain_name = None
- self.state.keystone_project_domain_name = None
- self.state.keystone_username = None
- self.state.keystone_password = None
- self.state.keystone_service = None
- self.on.configure_pod.emit()
-
- def _on_prometheus_relation_changed(self, event: EventBase) -> NoReturn:
- """Reads information about the prometheus relation.
-
- Args:
- event (EventBase): Prometheus relation event.
- """
- prometheus_host = event.relation.data[event.unit].get("hostname")
- prometheus_port = event.relation.data[event.unit].get("port")
-
- if (
- prometheus_host
- and prometheus_port
- and (
- self.state.prometheus_host != prometheus_host
- or self.state.prometheus_port != prometheus_port
- )
- ):
- self.state.prometheus_host = prometheus_host
- self.state.prometheus_port = int(prometheus_port)
- self.on.configure_pod.emit()
-
- def _on_prometheus_relation_departed(self, event: EventBase) -> NoReturn:
- """Clears data from prometheus relation.
-
- Args:
- event (EventBase): Prometheus relation event.
- """
- self.state.prometheus_host = None
- self.state.prometheus_port = None
- self.on.configure_pod.emit()
-
- def _publish_nbi_info(self, event: EventBase) -> NoReturn:
+ def _publish_nbi_info(self, event):
"""Publishes NBI information.
Args:
- event (EventBase): NBI relation event.
+ event (EventBase): RO relation event.
"""
- rel_data = {
- "host": self.model.app.name,
- "port": str(NBI_PORT),
- }
- for k, v in rel_data.items():
- event.relation.data[self.unit][k] = v
+ if self.unit.is_leader():
+ self.http_server.publish_info(self.app.name, PORT)
- def _missing_relations(self) -> str:
- """Checks if there missing relations.
+ def _check_missing_dependencies(self, config: ConfigModel):
+ missing_relations = []
- Returns:
- str: string with missing relations
- """
- data_status = {
- "kafka": self.state.message_host,
- "mongodb": self.state.database_uri,
- "prometheus": self.state.prometheus_host,
- }
+ if self.kafka_client.is_missing_data_in_unit():
+ missing_relations.append("kafka")
+ if self.mongodb_client.is_missing_data_in_unit():
+ missing_relations.append("mongodb")
+ if self.prometheus_client.is_missing_data_in_app():
+ missing_relations.append("prometheus")
+ if config.auth_backend == "keystone":
+ if self.keystone_client.is_missing_data_in_app():
+ missing_relations.append("keystone")
- if self.model.config["auth_backend"] == "keystone":
- data_status["keystone"] = self.state.keystone_host
+ if missing_relations:
+ raise RelationsMissing(missing_relations)
- missing_relations = [k for k, v in data_status.items() if not v]
-
- return ", ".join(missing_relations)
-
- @property
- def relation_state(self) -> Dict[str, Any]:
- """Collects relation state configuration for pod spec assembly.
-
- Returns:
- Dict[str, Any]: relation state information.
- """
- relation_state = {
- "message_host": self.state.message_host,
- "message_port": self.state.message_port,
- "database_uri": self.state.database_uri,
- "prometheus_host": self.state.prometheus_host,
- "prometheus_port": self.state.prometheus_port,
- }
-
- if self.model.config["auth_backend"] == "keystone":
- relation_state.update(
+ def build_pod_spec(self, image_info):
+ # Validate config
+ config = ConfigModel(**dict(self.config))
+ # Check relations
+ self._check_missing_dependencies(config)
+ # Create Builder for the PodSpec
+ pod_spec_builder = PodSpecV3Builder()
+ # Build Init Container
+ pod_spec_builder.add_init_container(
+ {
+ "name": "init-check",
+ "image": "alpine:latest",
+ "command": [
+ "sh",
+ "-c",
+ f"until (nc -zvw1 {self.kafka_client.host} {self.kafka_client.port} ); do sleep 3; done; exit 0",
+ ],
+ }
+ )
+ # Build Container
+ container_builder = ContainerV3Builder(self.app.name, image_info)
+ container_builder.add_port(name=self.app.name, port=PORT)
+ container_builder.add_tcpsocket_readiness_probe(
+ PORT,
+ initial_delay_seconds=5,
+ timeout_seconds=5,
+ )
+ container_builder.add_tcpsocket_liveness_probe(
+ PORT,
+ initial_delay_seconds=45,
+ timeout_seconds=10,
+ )
+ container_builder.add_envs(
+ {
+ # General configuration
+ "ALLOW_ANONYMOUS_LOGIN": "yes",
+ "OSMNBI_SERVER_ENABLE_TEST": config.enable_test,
+ "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
+ # Kafka configuration
+ "OSMNBI_MESSAGE_HOST": self.kafka_client.host,
+ "OSMNBI_MESSAGE_DRIVER": "kafka",
+ "OSMNBI_MESSAGE_PORT": self.kafka_client.port,
+ # Database configuration
+ "OSMNBI_DATABASE_DRIVER": "mongo",
+ "OSMNBI_DATABASE_URI": self.mongodb_client.connection_string,
+ "OSMNBI_DATABASE_COMMONKEY": config.database_commonkey,
+ # Storage configuration
+ "OSMNBI_STORAGE_DRIVER": "mongo",
+ "OSMNBI_STORAGE_PATH": "/app/storage",
+ "OSMNBI_STORAGE_COLLECTION": "files",
+ "OSMNBI_STORAGE_URI": self.mongodb_client.connection_string,
+ # Prometheus configuration
+ "OSMNBI_PROMETHEUS_HOST": self.prometheus_client.hostname,
+ "OSMNBI_PROMETHEUS_PORT": self.prometheus_client.port,
+ # Log configuration
+ "OSMNBI_LOG_LEVEL": config.log_level,
+ }
+ )
+ if config.auth_backend == "internal":
+ container_builder.add_env("OSMNBI_AUTHENTICATION_BACKEND", "internal")
+ elif config.auth_backend == "keystone":
+ container_builder.add_envs(
{
- "keystone_host": self.state.keystone_host,
- "keystone_port": self.state.keystone_port,
- "keystone_user_domain_name": self.state.keystone_user_domain_name,
- "keystone_project_domain_name": self.state.keystone_project_domain_name,
- "keystone_username": self.state.keystone_username,
- "keystone_password": self.state.keystone_password,
- "keystone_service": self.state.keystone_service,
+ "OSMNBI_AUTHENTICATION_BACKEND": "keystone",
+ "OSMNBI_AUTHENTICATION_AUTH_URL": self.keystone_client.host,
+ "OSMNBI_AUTHENTICATION_AUTH_PORT": self.keystone_client.port,
+ "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": self.keystone_client.user_domain_name,
+ "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": self.keystone_client.project_domain_name,
+ "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": self.keystone_client.username,
+ "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": self.keystone_client.password,
+ "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": self.keystone_client.service,
}
)
-
- return relation_state
-
- def configure_pod(self, event: EventBase) -> NoReturn:
- """Assemble the pod spec and apply it, if possible.
-
- Args:
- event (EventBase): Hook or Relation event that started the
- function.
- """
- if missing := self._missing_relations():
- self.unit.status = BlockedStatus(
- f"Waiting for {missing} relation{'s' if ',' in missing else ''}"
+ container = container_builder.build()
+ # Add container to pod spec
+ pod_spec_builder.add_container(container)
+ # Add ingress resources to pod spec if site url exists
+ if config.site_url:
+ parsed = urlparse(config.site_url)
+ annotations = {
+ "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format(
+ str(config.max_file_size) + "m"
+ if config.max_file_size > 0
+ else config.max_file_size
+ ),
+ "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS",
+ }
+ ingress_resource_builder = IngressResourceV3Builder(
+ f"{self.app.name}-ingress", annotations
)
- return
- if not self.unit.is_leader():
- self.unit.status = ActiveStatus("ready")
- return
+ if config.ingress_whitelist_source_range:
+ annotations[
+ "nginx.ingress.kubernetes.io/whitelist-source-range"
+ ] = config.ingress_whitelist_source_range
- self.unit.status = MaintenanceStatus("Assembling pod spec")
+ if parsed.scheme == "https":
+ ingress_resource_builder.add_tls(
+ [parsed.hostname], config.tls_secret_name
+ )
+ else:
+ annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
- # Fetch image information
- try:
- self.unit.status = MaintenanceStatus("Fetching image information")
- image_info = self.image.fetch()
- except OCIImageResourceError:
- self.unit.status = BlockedStatus("Error fetching image information")
- return
-
- try:
- pod_spec = make_pod_spec(
- image_info,
- self.model.config,
- self.relation_state,
- self.model.app.name,
- self.port,
- )
- except ValueError as exc:
- logger.exception("Config/Relation data validation error")
- self.unit.status = BlockedStatus(str(exc))
- return
-
- if self.state.pod_spec != pod_spec:
- self.model.pod.set_spec(pod_spec)
- self.state.pod_spec = pod_spec
-
- self.unit.status = ActiveStatus("ready")
+ ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT)
+ ingress_resource = ingress_resource_builder.build()
+ pod_spec_builder.add_ingress_resource(ingress_resource)
+ logger.debug(pod_spec_builder.build())
+ return pod_spec_builder.build()
if __name__ == "__main__":
diff --git a/installers/charm/nbi/src/pod_spec.py b/installers/charm/nbi/src/pod_spec.py
index b6291d4..b8f5904 100644
--- a/installers/charm/nbi/src/pod_spec.py
+++ b/installers/charm/nbi/src/pod_spec.py
@@ -98,12 +98,15 @@
"""
config_validators = {
"enable_test": lambda value, _: isinstance(value, bool),
- "database_commonkey": lambda value, _: isinstance(value, str)
- and len(value) > 1,
- "log_level": lambda value, _: isinstance(value, str)
- and value in ("INFO", "DEBUG"),
- "auth_backend": lambda value, _: isinstance(value, str)
- and (value == "internal" or value == "keystone"),
+ "database_commonkey": lambda value, _: (
+ isinstance(value, str) and len(value) > 1
+ ),
+ "log_level": lambda value, _: (
+ isinstance(value, str) and value in ("INFO", "DEBUG")
+ ),
+ "auth_backend": lambda value, _: (
+ isinstance(value, str) and (value == "internal" or value == "keystone")
+ ),
"site_url": lambda value, _: isinstance(value, str)
if value is not None
else True,
@@ -118,8 +121,9 @@
relation_validators = {
"message_host": lambda value, _: isinstance(value, str),
"message_port": lambda value, _: isinstance(value, int) and value > 0,
- "database_uri": lambda value, _: isinstance(value, str)
- and value.startswith("mongodb://"),
+ "database_uri": lambda value, _: (
+ isinstance(value, str) and value.startswith("mongodb://")
+ ),
"prometheus_host": lambda value, _: isinstance(value, str),
"prometheus_port": lambda value, _: isinstance(value, int) and value > 0,
"keystone_host": lambda value, _: _validate_keystone_config(
diff --git a/installers/charm/nbi/tests/test_charm.py b/installers/charm/nbi/tests/test_charm.py
index 657e89b..be24aa6 100644
--- a/installers/charm/nbi/tests/test_charm.py
+++ b/installers/charm/nbi/tests/test_charm.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
-# Copyright 2020 Canonical Ltd.
+# Copyright 2021 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@@ -20,241 +20,104 @@
# osm-charmers@lists.launchpad.net
##
+import sys
from typing import NoReturn
import unittest
-from ops.model import BlockedStatus
-
+from ops.model import ActiveStatus, BlockedStatus
from ops.testing import Harness
from charm import NbiCharm
class TestCharm(unittest.TestCase):
- """NBI Charm unit tests."""
+ """Prometheus Charm unit tests."""
def setUp(self) -> NoReturn:
"""Test setup"""
+ self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
self.harness = Harness(NbiCharm)
self.harness.set_leader(is_leader=True)
self.harness.begin()
-
- def test_on_start_without_relations(self) -> NoReturn:
- """Test installation without any relation."""
- self.harness.charm.on.start.emit()
-
- # Verifying status
- self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
-
- # Verifying status message
- self.assertGreater(len(self.harness.charm.unit.status.message), 0)
- self.assertTrue(
- self.harness.charm.unit.status.message.startswith("Waiting for ")
- )
- self.assertIn("kafka", self.harness.charm.unit.status.message)
- self.assertIn("mongodb", self.harness.charm.unit.status.message)
- self.assertIn("prometheus", self.harness.charm.unit.status.message)
- self.assertNotIn("keystone", self.harness.charm.unit.status.message)
- self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
-
- def test_on_start_without_relations_with_keystone(self) -> NoReturn:
- """Test installation without any relation and keystone enabled."""
- self.harness.update_config({"auth_backend": "keystone"})
-
- self.harness.charm.on.start.emit()
-
- # Verifying status
- self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
-
- # Verifying status message
- self.assertGreater(len(self.harness.charm.unit.status.message), 0)
- self.assertTrue(
- self.harness.charm.unit.status.message.startswith("Waiting for ")
- )
- self.assertIn("kafka", self.harness.charm.unit.status.message)
- self.assertIn("mongodb", self.harness.charm.unit.status.message)
- self.assertIn("prometheus", self.harness.charm.unit.status.message)
- self.assertIn("keystone", self.harness.charm.unit.status.message)
- self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
-
- def test_on_start_with_relations(self) -> NoReturn:
- """Test deployment without keystone."""
- expected_result = {
- "version": 3,
- "containers": [
- {
- "name": "nbi",
- "imageDetails": self.harness.charm.image.fetch(),
- "imagePullPolicy": "Always",
- "ports": [
- {
- "name": "nbi",
- "containerPort": 9999,
- "protocol": "TCP",
- }
- ],
- "envConfig": {
- "ALLOW_ANONYMOUS_LOGIN": "yes",
- "OSMNBI_SERVER_ENABLE_TEST": False,
- "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
- "OSMNBI_MESSAGE_HOST": "kafka",
- "OSMNBI_MESSAGE_DRIVER": "kafka",
- "OSMNBI_MESSAGE_PORT": 9092,
- "OSMNBI_DATABASE_DRIVER": "mongo",
- "OSMNBI_DATABASE_URI": "mongodb://mongo:27017",
- "OSMNBI_DATABASE_COMMONKEY": "osm",
- "OSMNBI_STORAGE_DRIVER": "mongo",
- "OSMNBI_STORAGE_PATH": "/app/storage",
- "OSMNBI_STORAGE_COLLECTION": "files",
- "OSMNBI_STORAGE_URI": "mongodb://mongo:27017",
- "OSMNBI_PROMETHEUS_HOST": "prometheus",
- "OSMNBI_PROMETHEUS_PORT": 9090,
- "OSMNBI_LOG_LEVEL": "INFO",
- "OSMNBI_AUTHENTICATION_BACKEND": "internal",
- },
- }
- ],
- "kubernetesResources": {
- "ingressResources": [],
- },
+ self.config = {
+ "enable_test": False,
+ "auth_backend": "internal",
+ "database_commonkey": "key",
+ "log_level": "INFO",
+ "max_file_size": 0,
+ "ingress_whitelist_source_range": "",
+ "tls_secret_name": "",
+ "site_url": "https://nbi.192.168.100.100.xip.io",
}
+ self.harness.update_config(self.config)
- self.harness.charm.on.start.emit()
+ def test_config_changed_no_relations(
+ self,
+ ) -> NoReturn:
+ """Test ingress resources without HTTP."""
- # Check if kafka datastore is initialized
- self.assertIsNone(self.harness.charm.state.message_host)
- self.assertIsNone(self.harness.charm.state.message_port)
+ self.harness.charm.on.config_changed.emit()
- # Check if mongodb datastore is initialized
- self.assertIsNone(self.harness.charm.state.database_uri)
-
- # Check if prometheus datastore is initialized
- self.assertIsNone(self.harness.charm.state.prometheus_host)
- self.assertIsNone(self.harness.charm.state.prometheus_port)
-
- # Initializing the kafka relation
- kafka_relation_id = self.harness.add_relation("kafka", "kafka")
- self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
- self.harness.update_relation_data(
- kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
+ # Assertions
+ self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
+ self.assertTrue(
+ all(
+ relation in self.harness.charm.unit.status.message
+ for relation in ["mongodb", "kafka", "prometheus"]
+ )
)
- # Initializing the mongo relation
- mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
- self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
- self.harness.update_relation_data(
- mongodb_relation_id,
- "mongodb/0",
- {"connection_string": "mongodb://mongo:27017"},
- )
+ def test_config_changed_non_leader(
+ self,
+ ) -> NoReturn:
+ """Test ingress resources without HTTP."""
+ self.harness.set_leader(is_leader=False)
+ self.harness.charm.on.config_changed.emit()
- # Initializing the prometheus relation
- prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
- self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
- self.harness.update_relation_data(
- prometheus_relation_id,
- "prometheus/0",
- {"hostname": "prometheus", "port": 9090},
- )
+ # Assertions
+ self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
- # Checking if kafka data is stored
- self.assertEqual(self.harness.charm.state.message_host, "kafka")
- self.assertEqual(self.harness.charm.state.message_port, 9092)
-
- # Checking if mongodb data is stored
- self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
-
- # Checking if prometheus data is stored
- self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
- self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
-
+ def test_with_relations_internal(
+ self,
+ ) -> NoReturn:
+ "Test with relations (internal)"
+ self.initialize_kafka_relation()
+ self.initialize_mongo_relation()
+ self.initialize_prometheus_relation()
# Verifying status
self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
- pod_spec, _ = self.harness.get_pod_spec()
-
- self.assertDictEqual(expected_result, pod_spec)
-
- def test_on_start_with_relations_with_keystone(self) -> NoReturn:
- """Test deployment with keystone."""
- expected_result = {
- "version": 3,
- "containers": [
- {
- "name": "nbi",
- "imageDetails": self.harness.charm.image.fetch(),
- "imagePullPolicy": "Always",
- "ports": [
- {
- "name": "nbi",
- "containerPort": 9999,
- "protocol": "TCP",
- }
- ],
- "envConfig": {
- "ALLOW_ANONYMOUS_LOGIN": "yes",
- "OSMNBI_SERVER_ENABLE_TEST": False,
- "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
- "OSMNBI_MESSAGE_HOST": "kafka",
- "OSMNBI_MESSAGE_DRIVER": "kafka",
- "OSMNBI_MESSAGE_PORT": 9092,
- "OSMNBI_DATABASE_DRIVER": "mongo",
- "OSMNBI_DATABASE_URI": "mongodb://mongo:27017",
- "OSMNBI_DATABASE_COMMONKEY": "osm",
- "OSMNBI_STORAGE_DRIVER": "mongo",
- "OSMNBI_STORAGE_PATH": "/app/storage",
- "OSMNBI_STORAGE_COLLECTION": "files",
- "OSMNBI_STORAGE_URI": "mongodb://mongo:27017",
- "OSMNBI_PROMETHEUS_HOST": "prometheus",
- "OSMNBI_PROMETHEUS_PORT": 9090,
- "OSMNBI_LOG_LEVEL": "INFO",
- "OSMNBI_AUTHENTICATION_BACKEND": "keystone",
- "OSMNBI_AUTHENTICATION_AUTH_URL": "keystone",
- "OSMNBI_AUTHENTICATION_AUTH_PORT": 5000,
- "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": "default",
- "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": "default",
- "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": "nbi",
- "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": "nbi",
- "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": "service",
- },
- }
- ],
- "kubernetesResources": {
- "ingressResources": [],
- },
- }
-
+ def test_with_relations_keystone_missing(
+ self,
+ ) -> NoReturn:
+ "Test with relations (keystone)"
self.harness.update_config({"auth_backend": "keystone"})
+ self.initialize_kafka_relation()
+ self.initialize_mongo_relation()
+ self.initialize_prometheus_relation()
+ # Verifying status
+ self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
+ self.assertTrue("keystone" in self.harness.charm.unit.status.message)
- self.harness.charm.on.start.emit()
+ def test_with_relations_keystone(
+ self,
+ ) -> NoReturn:
+ "Test with relations (keystone)"
+ self.harness.update_config({"auth_backend": "keystone"})
+ self.initialize_kafka_relation()
+ self.initialize_mongo_relation()
+ self.initialize_prometheus_relation()
+ self.initialize_keystone_relation()
+ # Verifying status
+ self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
- # Check if kafka datastore is initialized
- self.assertIsNone(self.harness.charm.state.message_host)
- self.assertIsNone(self.harness.charm.state.message_port)
-
- # Check if mongodb datastore is initialized
- self.assertIsNone(self.harness.charm.state.database_uri)
-
- # Check if prometheus datastore is initialized
- self.assertIsNone(self.harness.charm.state.prometheus_host)
- self.assertIsNone(self.harness.charm.state.prometheus_port)
-
- # Check if keystone datastore is initialized
- self.assertIsNone(self.harness.charm.state.keystone_host)
- self.assertIsNone(self.harness.charm.state.keystone_port)
- self.assertIsNone(self.harness.charm.state.keystone_user_domain_name)
- self.assertIsNone(self.harness.charm.state.keystone_project_domain_name)
- self.assertIsNone(self.harness.charm.state.keystone_username)
- self.assertIsNone(self.harness.charm.state.keystone_password)
- self.assertIsNone(self.harness.charm.state.keystone_service)
-
- # Initializing the kafka relation
+ def initialize_kafka_relation(self):
kafka_relation_id = self.harness.add_relation("kafka", "kafka")
self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
self.harness.update_relation_data(
kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
)
- # Initializing the mongodb relation
+ def initialize_mongo_relation(self):
mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
self.harness.update_relation_data(
@@ -263,608 +126,122 @@
{"connection_string": "mongodb://mongo:27017"},
)
- # Initializing the prometheus relation
- promethues_relation_id = self.harness.add_relation("prometheus", "prometheus")
- self.harness.add_relation_unit(promethues_relation_id, "prometheus/0")
- self.harness.update_relation_data(
- promethues_relation_id,
- "prometheus/0",
- {"hostname": "prometheus", "port": 9090},
- )
-
- # Initializing the keystone relation
+ def initialize_keystone_relation(self):
keystone_relation_id = self.harness.add_relation("keystone", "keystone")
self.harness.add_relation_unit(keystone_relation_id, "keystone/0")
self.harness.update_relation_data(
keystone_relation_id,
- "keystone/0",
+ "keystone",
{
- "host": "keystone",
+ "host": "host",
"port": 5000,
- "user_domain_name": "default",
- "project_domain_name": "default",
- "username": "nbi",
- "password": "nbi",
- "service": "service",
+ "user_domain_name": "ud",
+ "project_domain_name": "pd",
+ "username": "u",
+ "password": "p",
+ "service": "s",
+ "keystone_db_password": "something",
+ "region_id": "something",
+ "admin_username": "something",
+ "admin_password": "something",
+ "admin_project_name": "something",
},
)
- # Checking if kafka data is stored
- self.assertEqual(self.harness.charm.state.message_host, "kafka")
- self.assertEqual(self.harness.charm.state.message_port, 9092)
-
- # Checking if mongodb data is stored
- self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
-
- # Checking if prometheus data is stored
- self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
- self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
-
- # Checking if keystone data is stored
- self.assertEqual(self.harness.charm.state.keystone_host, "keystone")
- self.assertEqual(self.harness.charm.state.keystone_port, 5000)
- self.assertEqual(self.harness.charm.state.keystone_user_domain_name, "default")
- self.assertEqual(
- self.harness.charm.state.keystone_project_domain_name, "default"
- )
- self.assertEqual(self.harness.charm.state.keystone_username, "nbi")
- self.assertEqual(self.harness.charm.state.keystone_password, "nbi")
- self.assertEqual(self.harness.charm.state.keystone_service, "service")
-
- # Verifying status
- self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
-
- pod_spec, _ = self.harness.get_pod_spec()
-
- self.assertDictEqual(expected_result, pod_spec)
-
- def test_ingress_resources_without_http(self) -> NoReturn:
- """Test ingress resources without HTTP."""
- expected_result = {
- "version": 3,
- "containers": [
- {
- "name": "nbi",
- "imageDetails": self.harness.charm.image.fetch(),
- "imagePullPolicy": "Always",
- "ports": [
- {
- "name": "nbi",
- "containerPort": 9999,
- "protocol": "TCP",
- }
- ],
- "envConfig": {
- "ALLOW_ANONYMOUS_LOGIN": "yes",
- "OSMNBI_SERVER_ENABLE_TEST": False,
- "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
- "OSMNBI_MESSAGE_HOST": "kafka",
- "OSMNBI_MESSAGE_DRIVER": "kafka",
- "OSMNBI_MESSAGE_PORT": 9092,
- "OSMNBI_DATABASE_DRIVER": "mongo",
- "OSMNBI_DATABASE_URI": "mongodb://mongo:27017",
- "OSMNBI_DATABASE_COMMONKEY": "osm",
- "OSMNBI_STORAGE_DRIVER": "mongo",
- "OSMNBI_STORAGE_PATH": "/app/storage",
- "OSMNBI_STORAGE_COLLECTION": "files",
- "OSMNBI_STORAGE_URI": "mongodb://mongo:27017",
- "OSMNBI_PROMETHEUS_HOST": "prometheus",
- "OSMNBI_PROMETHEUS_PORT": 9090,
- "OSMNBI_LOG_LEVEL": "INFO",
- "OSMNBI_AUTHENTICATION_BACKEND": "internal",
- },
- }
- ],
- "kubernetesResources": {
- "ingressResources": [],
- },
- }
-
- self.harness.charm.on.start.emit()
-
- # Initializing the kafka relation
- kafka_relation_id = self.harness.add_relation("kafka", "kafka")
- self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
+ def initialize_prometheus_relation(self):
+ prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
+ self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
self.harness.update_relation_data(
- kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
- )
-
- # Initializing the mongodb relation
- mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
- self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
- self.harness.update_relation_data(
- mongodb_relation_id,
- "mongodb/0",
- {"connection_string": "mongodb://mongo:27017"},
- )
-
- # Initializing the prometheus relation
- promethues_relation_id = self.harness.add_relation("prometheus", "prometheus")
- self.harness.add_relation_unit(promethues_relation_id, "prometheus/0")
- self.harness.update_relation_data(
- promethues_relation_id,
- "prometheus/0",
+ prometheus_relation_id,
+ "prometheus",
{"hostname": "prometheus", "port": 9090},
)
- self.harness.update_config({"site_url": "nbi"})
-
- pod_spec, _ = self.harness.get_pod_spec()
-
- self.assertDictEqual(expected_result, pod_spec)
-
- def test_ingress_resources_with_http(self) -> NoReturn:
- """Test ingress resources with HTTP."""
- expected_result = {
- "version": 3,
- "containers": [
- {
- "name": "nbi",
- "imageDetails": self.harness.charm.image.fetch(),
- "imagePullPolicy": "Always",
- "ports": [
- {
- "name": "nbi",
- "containerPort": 9999,
- "protocol": "TCP",
- }
- ],
- "envConfig": {
- "ALLOW_ANONYMOUS_LOGIN": "yes",
- "OSMNBI_SERVER_ENABLE_TEST": False,
- "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
- "OSMNBI_MESSAGE_HOST": "kafka",
- "OSMNBI_MESSAGE_DRIVER": "kafka",
- "OSMNBI_MESSAGE_PORT": 9092,
- "OSMNBI_DATABASE_DRIVER": "mongo",
- "OSMNBI_DATABASE_URI": "mongodb://mongo:27017",
- "OSMNBI_DATABASE_COMMONKEY": "osm",
- "OSMNBI_STORAGE_DRIVER": "mongo",
- "OSMNBI_STORAGE_PATH": "/app/storage",
- "OSMNBI_STORAGE_COLLECTION": "files",
- "OSMNBI_STORAGE_URI": "mongodb://mongo:27017",
- "OSMNBI_PROMETHEUS_HOST": "prometheus",
- "OSMNBI_PROMETHEUS_PORT": 9090,
- "OSMNBI_LOG_LEVEL": "INFO",
- "OSMNBI_AUTHENTICATION_BACKEND": "internal",
- },
- }
- ],
- "kubernetesResources": {
- "ingressResources": [
- {
- "name": "nbi-ingress",
- "annotations": {
- "nginx.ingress.kubernetes.io/proxy-body-size": "0",
- "nginx.ingress.kubernetes.io/ssl-redirect": "false",
- "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS",
- },
- "spec": {
- "rules": [
- {
- "host": "nbi",
- "http": {
- "paths": [
- {
- "path": "/",
- "backend": {
- "serviceName": "nbi",
- "servicePort": 9999,
- },
- }
- ]
- },
- }
- ]
- },
- }
- ],
- },
- }
-
- self.harness.charm.on.start.emit()
-
- # Initializing the kafka relation
- kafka_relation_id = self.harness.add_relation("kafka", "kafka")
- self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
- self.harness.update_relation_data(
- kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
- )
-
- # Initializing the mongodb relation
- mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
- self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
- self.harness.update_relation_data(
- mongodb_relation_id,
- "mongodb/0",
- {"connection_string": "mongodb://mongo:27017"},
- )
-
- # Initializing the prometheus relation
- promethues_relation_id = self.harness.add_relation("prometheus", "prometheus")
- self.harness.add_relation_unit(promethues_relation_id, "prometheus/0")
- self.harness.update_relation_data(
- promethues_relation_id,
- "prometheus/0",
- {"hostname": "prometheus", "port": 9090},
- )
-
- self.harness.update_config({"site_url": "http://nbi"})
-
- pod_spec, _ = self.harness.get_pod_spec()
-
- self.assertDictEqual(expected_result, pod_spec)
-
- def test_ingress_resources_with_https(self) -> NoReturn:
- """Test ingress resources with HTTPS."""
- expected_result = {
- "version": 3,
- "containers": [
- {
- "name": "nbi",
- "imageDetails": self.harness.charm.image.fetch(),
- "imagePullPolicy": "Always",
- "ports": [
- {
- "name": "nbi",
- "containerPort": 9999,
- "protocol": "TCP",
- }
- ],
- "envConfig": {
- "ALLOW_ANONYMOUS_LOGIN": "yes",
- "OSMNBI_SERVER_ENABLE_TEST": False,
- "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
- "OSMNBI_MESSAGE_HOST": "kafka",
- "OSMNBI_MESSAGE_DRIVER": "kafka",
- "OSMNBI_MESSAGE_PORT": 9092,
- "OSMNBI_DATABASE_DRIVER": "mongo",
- "OSMNBI_DATABASE_URI": "mongodb://mongo:27017",
- "OSMNBI_DATABASE_COMMONKEY": "osm",
- "OSMNBI_STORAGE_DRIVER": "mongo",
- "OSMNBI_STORAGE_PATH": "/app/storage",
- "OSMNBI_STORAGE_COLLECTION": "files",
- "OSMNBI_STORAGE_URI": "mongodb://mongo:27017",
- "OSMNBI_PROMETHEUS_HOST": "prometheus",
- "OSMNBI_PROMETHEUS_PORT": 9090,
- "OSMNBI_LOG_LEVEL": "INFO",
- "OSMNBI_AUTHENTICATION_BACKEND": "internal",
- },
- }
- ],
- "kubernetesResources": {
- "ingressResources": [
- {
- "name": "nbi-ingress",
- "annotations": {
- "nginx.ingress.kubernetes.io/proxy-body-size": "0",
- "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS",
- },
- "spec": {
- "rules": [
- {
- "host": "nbi",
- "http": {
- "paths": [
- {
- "path": "/",
- "backend": {
- "serviceName": "nbi",
- "servicePort": 9999,
- },
- }
- ]
- },
- }
- ],
- "tls": [{"hosts": ["nbi"], "secretName": "nbi"}],
- },
- }
- ],
- },
- }
-
- self.harness.charm.on.start.emit()
-
- # Initializing the kafka relation
- kafka_relation_id = self.harness.add_relation("kafka", "kafka")
- self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
- self.harness.update_relation_data(
- kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
- )
-
- # Initializing the mongodb relation
- mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
- self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
- self.harness.update_relation_data(
- mongodb_relation_id,
- "mongodb/0",
- {"connection_string": "mongodb://mongo:27017"},
- )
-
- # Initializing the prometheus relation
- promethues_relation_id = self.harness.add_relation("prometheus", "prometheus")
- self.harness.add_relation_unit(promethues_relation_id, "prometheus/0")
- self.harness.update_relation_data(
- promethues_relation_id,
- "prometheus/0",
- {"hostname": "prometheus", "port": 9090},
- )
-
- self.harness.update_config(
- {"site_url": "https://nbi", "tls_secret_name": "nbi"}
- )
-
- pod_spec, _ = self.harness.get_pod_spec()
-
- self.assertDictEqual(expected_result, pod_spec)
-
- def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
- """Test ingress resources with HTTPS and ingress whitelist."""
- expected_result = {
- "version": 3,
- "containers": [
- {
- "name": "nbi",
- "imageDetails": self.harness.charm.image.fetch(),
- "imagePullPolicy": "Always",
- "ports": [
- {
- "name": "nbi",
- "containerPort": 9999,
- "protocol": "TCP",
- }
- ],
- "envConfig": {
- "ALLOW_ANONYMOUS_LOGIN": "yes",
- "OSMNBI_SERVER_ENABLE_TEST": False,
- "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
- "OSMNBI_MESSAGE_HOST": "kafka",
- "OSMNBI_MESSAGE_DRIVER": "kafka",
- "OSMNBI_MESSAGE_PORT": 9092,
- "OSMNBI_DATABASE_DRIVER": "mongo",
- "OSMNBI_DATABASE_URI": "mongodb://mongo:27017",
- "OSMNBI_DATABASE_COMMONKEY": "osm",
- "OSMNBI_STORAGE_DRIVER": "mongo",
- "OSMNBI_STORAGE_PATH": "/app/storage",
- "OSMNBI_STORAGE_COLLECTION": "files",
- "OSMNBI_STORAGE_URI": "mongodb://mongo:27017",
- "OSMNBI_PROMETHEUS_HOST": "prometheus",
- "OSMNBI_PROMETHEUS_PORT": 9090,
- "OSMNBI_LOG_LEVEL": "INFO",
- "OSMNBI_AUTHENTICATION_BACKEND": "internal",
- },
- }
- ],
- "kubernetesResources": {
- "ingressResources": [
- {
- "name": "nbi-ingress",
- "annotations": {
- "nginx.ingress.kubernetes.io/proxy-body-size": "0",
- "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS",
- "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
- },
- "spec": {
- "rules": [
- {
- "host": "nbi",
- "http": {
- "paths": [
- {
- "path": "/",
- "backend": {
- "serviceName": "nbi",
- "servicePort": 9999,
- },
- }
- ]
- },
- }
- ],
- "tls": [{"hosts": ["nbi"], "secretName": "nbi"}],
- },
- }
- ],
- },
- }
-
- self.harness.charm.on.start.emit()
-
- # Initializing the kafka relation
- kafka_relation_id = self.harness.add_relation("kafka", "kafka")
- self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
- self.harness.update_relation_data(
- kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
- )
-
- # Initializing the mongodb relation
- mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
- self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
- self.harness.update_relation_data(
- mongodb_relation_id,
- "mongodb/0",
- {"connection_string": "mongodb://mongo:27017"},
- )
-
- # Initializing the prometheus relation
- promethues_relation_id = self.harness.add_relation("prometheus", "prometheus")
- self.harness.add_relation_unit(promethues_relation_id, "prometheus/0")
- self.harness.update_relation_data(
- promethues_relation_id,
- "prometheus/0",
- {"hostname": "prometheus", "port": 9090},
- )
-
- self.harness.update_config(
- {
- "site_url": "https://nbi",
- "tls_secret_name": "nbi",
- "ingress_whitelist_source_range": "0.0.0.0/0",
- }
- )
-
- pod_spec, _ = self.harness.get_pod_spec()
-
- self.assertDictEqual(expected_result, pod_spec)
-
- def test_on_kafka_unit_relation_changed(self) -> NoReturn:
- """Test to see if kafka relation is updated."""
- self.harness.charm.on.start.emit()
-
- self.assertIsNone(self.harness.charm.state.message_host)
- self.assertIsNone(self.harness.charm.state.message_port)
-
- relation_id = self.harness.add_relation("kafka", "kafka")
- self.harness.add_relation_unit(relation_id, "kafka/0")
- self.harness.update_relation_data(
- relation_id, "kafka/0", {"host": "kafka", "port": 9092}
- )
-
- self.assertEqual(self.harness.charm.state.message_host, "kafka")
- self.assertEqual(self.harness.charm.state.message_port, 9092)
-
- # Verifying status
- self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
-
- # Verifying status message
- self.assertGreater(len(self.harness.charm.unit.status.message), 0)
- self.assertTrue(
- self.harness.charm.unit.status.message.startswith("Waiting for ")
- )
- self.assertNotIn("kafka", self.harness.charm.unit.status.message)
- self.assertIn("mongodb", self.harness.charm.unit.status.message)
- self.assertIn("prometheus", self.harness.charm.unit.status.message)
- self.assertNotIn("keystone", self.harness.charm.unit.status.message)
- self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
-
- def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
- """Test to see if mongodb relation is updated."""
- self.harness.charm.on.start.emit()
-
- self.assertIsNone(self.harness.charm.state.database_uri)
-
- relation_id = self.harness.add_relation("mongodb", "mongodb")
- self.harness.add_relation_unit(relation_id, "mongodb/0")
- self.harness.update_relation_data(
- relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"}
- )
-
- self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
-
- # Verifying status
- self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
-
- # Verifying status message
- self.assertGreater(len(self.harness.charm.unit.status.message), 0)
- self.assertTrue(
- self.harness.charm.unit.status.message.startswith("Waiting for ")
- )
- self.assertIn("kafka", self.harness.charm.unit.status.message)
- self.assertNotIn("mongodb", self.harness.charm.unit.status.message)
- self.assertIn("prometheus", self.harness.charm.unit.status.message)
- self.assertNotIn("keystone", self.harness.charm.unit.status.message)
- self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
-
- def test_on_prometheus_unit_relation_changed(self) -> NoReturn:
- """Test to see if prometheus relation is updated."""
- self.harness.charm.on.start.emit()
-
- self.assertIsNone(self.harness.charm.state.prometheus_host)
- self.assertIsNone(self.harness.charm.state.prometheus_port)
-
- relation_id = self.harness.add_relation("prometheus", "prometheus")
- self.harness.add_relation_unit(relation_id, "prometheus/0")
- self.harness.update_relation_data(
- relation_id, "prometheus/0", {"hostname": "prometheus", "port": 9090}
- )
-
- self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus")
- self.assertEqual(self.harness.charm.state.prometheus_port, 9090)
-
- # Verifying status
- self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
-
- # Verifying status message
- self.assertGreater(len(self.harness.charm.unit.status.message), 0)
- self.assertTrue(
- self.harness.charm.unit.status.message.startswith("Waiting for ")
- )
- self.assertIn("kafka", self.harness.charm.unit.status.message)
- self.assertIn("mongodb", self.harness.charm.unit.status.message)
- self.assertNotIn("prometheus", self.harness.charm.unit.status.message)
- self.assertNotIn("keystone", self.harness.charm.unit.status.message)
- self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
-
- def test_on_keystone_unit_relation_changed(self) -> NoReturn:
- """Test to see if keystone relation is updated."""
- self.harness.update_config({"auth_backend": "keystone"})
-
- self.harness.charm.on.start.emit()
-
- self.assertIsNone(self.harness.charm.state.keystone_host)
- self.assertIsNone(self.harness.charm.state.keystone_port)
- self.assertIsNone(self.harness.charm.state.keystone_user_domain_name)
- self.assertIsNone(self.harness.charm.state.keystone_project_domain_name)
- self.assertIsNone(self.harness.charm.state.keystone_username)
- self.assertIsNone(self.harness.charm.state.keystone_password)
- self.assertIsNone(self.harness.charm.state.keystone_service)
-
- relation_id = self.harness.add_relation("keystone", "keystone")
- self.harness.add_relation_unit(relation_id, "keystone/0")
- self.harness.update_relation_data(
- relation_id,
- "keystone/0",
- {
- "host": "keystone",
- "port": 5000,
- "user_domain_name": "default",
- "project_domain_name": "default",
- "username": "nbi",
- "password": "nbi",
- "service": "service",
- },
- )
-
- self.assertEqual(self.harness.charm.state.keystone_host, "keystone")
- self.assertEqual(self.harness.charm.state.keystone_port, 5000)
- self.assertEqual(self.harness.charm.state.keystone_user_domain_name, "default")
- self.assertEqual(
- self.harness.charm.state.keystone_project_domain_name, "default"
- )
- self.assertEqual(self.harness.charm.state.keystone_username, "nbi")
- self.assertEqual(self.harness.charm.state.keystone_password, "nbi")
- self.assertEqual(self.harness.charm.state.keystone_service, "service")
-
- # Verifying status
- self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
-
- # Verifying status message
- self.assertGreater(len(self.harness.charm.unit.status.message), 0)
- self.assertTrue(
- self.harness.charm.unit.status.message.startswith("Waiting for ")
- )
- self.assertIn("kafka", self.harness.charm.unit.status.message)
- self.assertIn("mongodb", self.harness.charm.unit.status.message)
- self.assertIn("prometheus", self.harness.charm.unit.status.message)
- self.assertNotIn("keystone", self.harness.charm.unit.status.message)
- self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
-
- def test_publish_nbi_info(self) -> NoReturn:
- """Test to see if nbi relation is updated."""
- expected_result = {
- "host": "nbi",
- "port": "9999",
- }
-
- self.harness.charm.on.start.emit()
-
- relation_id = self.harness.add_relation("nbi", "ng-ui")
- self.harness.add_relation_unit(relation_id, "ng-ui/0")
- relation_data = self.harness.get_relation_data(relation_id, "nbi/0")
-
- self.assertDictEqual(expected_result, relation_data)
-
if __name__ == "__main__":
unittest.main()
+
+
+# class TestCharm(unittest.TestCase):
+# """Prometheus Charm unit tests."""
+
+# def setUp(self) -> NoReturn:
+# """Test setup"""
+# self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
+# self.harness = Harness(NbiCharm)
+# self.harness.set_leader(is_leader=True)
+# self.harness.begin()
+# self.config = {
+# "enable_ng_ro": True,
+# "database_commonkey": "commonkey",
+# "log_level": "INFO",
+# "vim_database": "db_name",
+# "ro_database": "ro_db_name",
+# "openmano_tenant": "mano",
+# }
+
+# def test_config_changed_no_relations(
+# self,
+# ) -> NoReturn:
+# """Test ingress resources without HTTP."""
+
+# self.harness.charm.on.config_changed.emit()
+
+# # Assertions
+# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
+# self.assertTrue(
+# all(
+# relation in self.harness.charm.unit.status.message
+# for relation in ["mongodb", "kafka"]
+# )
+# )
+
+# # Disable ng-ro
+# self.harness.update_config({"enable_ng_ro": False})
+# self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
+# self.assertTrue(
+# all(
+# relation in self.harness.charm.unit.status.message
+# for relation in ["mysql"]
+# )
+# )
+
+# def test_config_changed_non_leader(
+# self,
+# ) -> NoReturn:
+# """Test ingress resources without HTTP."""
+# self.harness.set_leader(is_leader=False)
+# self.harness.charm.on.config_changed.emit()
+
+# # Assertions
+# self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
+
+# def test_with_relations_ng(
+# self,
+# ) -> NoReturn:
+# "Test with relations (ng-ro)"
+
+# # Initializing the kafka relation
+# kafka_relation_id = self.harness.add_relation("kafka", "kafka")
+# self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
+# self.harness.update_relation_data(
+# kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
+# )
+
+# # Initializing the mongo relation
+# mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
+# self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
+# self.harness.update_relation_data(
+# mongodb_relation_id,
+# "mongodb/0",
+# {"connection_string": "mongodb://mongo:27017"},
+# )
+
+# self.harness.charm.on.config_changed.emit()
+
+# # Verifying status
+# self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
+
+
+# if __name__ == "__main__":
+# unittest.main()
diff --git a/installers/charm/nbi/tox.ini b/installers/charm/nbi/tox.ini
index 5989669..1f9442e 100644
--- a/installers/charm/nbi/tox.ini
+++ b/installers/charm/nbi/tox.ini
@@ -1,4 +1,4 @@
-# Copyright 2020 Canonical Ltd.
+# Copyright 2021 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@@ -18,64 +18,98 @@
# To get in touch with the maintainers, please contact:
# osm-charmers@lists.launchpad.net
##
+#######################################################################################
[tox]
+envlist = flake8, cover, pylint, safety, yamllint
skipsdist = True
-envlist = unit, lint
-sitepackages = False
-skip_missing_interpreters = False
[testenv]
basepython = python3.8
setenv =
+ VIRTUAL_ENV={envdir}
PYTHONHASHSEED=0
PYTHONPATH = {toxinidir}/src
- CHARM_NAME = nbi
+deps = -r{toxinidir}/requirements.txt
+#######################################################################################
+[testenv:cover]
+deps = {[testenv]deps}
+ -r{toxinidir}/requirements-test.txt
+commands =
+ sh -c 'rm -f nosetests.xml'
+ coverage erase
+ nose2 -C --coverage src
+ coverage report --omit='*tests*'
+ coverage html -d ./cover --omit='*tests*'
+ coverage xml -o coverage.xml --omit=*tests*
+whitelist_externals = sh
+
+#######################################################################################
+[testenv:safety]
+setenv =
+ LC_ALL=C.UTF-8
+ LANG=C.UTF-8
+deps = {[testenv]deps}
+ -r{toxinidir}/requirements-test.txt
+commands =
+ - safety check --full-report
+
+#######################################################################################
+[testenv:flake8]
+deps = flake8
+commands =
+ flake8 src/ tests/
+
+#######################################################################################
+[testenv:pylint]
+deps = {[testenv]deps}
+ -r{toxinidir}/requirements-test.txt
+ pylint
+commands =
+ pylint -E src
+
+#######################################################################################
+[testenv:black]
+deps = {[testenv]deps}
+ -r{toxinidir}/requirements-test.txt
+ black
+commands = black --check --diff . --exclude "build/|.tox/|mod/|lib/"
+
+#######################################################################################
+[testenv:yamllint]
+deps = {[testenv]deps}
+ -r{toxinidir}/requirements-test.txt
+ yamllint
+commands = yamllint .
+
+#######################################################################################
[testenv:build]
passenv=HTTP_PROXY HTTPS_PROXY NO_PROXY
+deps = {[testenv]deps}
+ -r{toxinidir}/requirements-test.txt
+ charmcraft
whitelist_externals =
charmcraft
- rm
- unzip
+ cp
commands =
- rm -rf release nbi.charm
charmcraft build
- unzip nbi.charm -d release
+ cp -r build release
-[testenv:unit]
-commands =
- coverage erase
- stestr run --slowest --test-path=./tests --top-dir=./
- coverage combine
- coverage html -d cover
- coverage xml -o cover/coverage.xml
- coverage report
-deps =
- coverage
- stestr
- mock
- ops
-setenv =
- {[testenv]setenv}
- PYTHON=coverage run
+#######################################################################################
+[flake8]
+ignore =
+ W291,
+ W293,
+ E123,
+ E125,
+ E226,
+ E241,
+exclude =
+ .git,
+ __pycache__,
+ .tox,
+max-line-length = 120
+show-source = True
+builtins = _
-[testenv:lint]
-deps =
- black
- yamllint
- flake8
-commands =
- black --check --diff . --exclude "build/|.tox/|mod/|lib/"
- yamllint .
- flake8 . --max-line-length=100 --ignore="E501,W503,W504,F722" --exclude "build/ .tox/ mod/ lib/"
-
-[coverage:run]
-branch = True
-concurrency = multiprocessing
-parallel = True
-source =
- .
-omit =
- .tox/*
- tests/*