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/ng-ui/.gitignore b/installers/charm/ng-ui/.gitignore
index 9c7c049..ecfb4dc 100644
--- a/installers/charm/ng-ui/.gitignore
+++ b/installers/charm/ng-ui/.gitignore
@@ -1,19 +1,37 @@
-# 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 a copy of the License at
+# 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
+#         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.
+# 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
+<<<<<<< HEAD
 ng-ui.charm
 .stestr
 .coverage*
-cover/
\ No newline at end of file
+cover/
+=======
+*.charm
+.coverage
+coverage.xml
+.stestr
+cover
+release
+>>>>>>> WIP: Improve osm charms
diff --git a/installers/charm/ng-ui/.jujuignore b/installers/charm/ng-ui/.jujuignore
new file mode 100644
index 0000000..bf04eb4
--- /dev/null
+++ b/installers/charm/ng-ui/.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/ng-ui/.yamllint.yaml b/installers/charm/ng-ui/.yamllint.yaml
index ab52c60..d71fb69 100644
--- a/installers/charm/ng-ui/.yamllint.yaml
+++ b/installers/charm/ng-ui/.yamllint.yaml
@@ -1,16 +1,24 @@
-# 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 a copy of the License at
+# 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
+#         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.
+# 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
+##
+
 ---
 extends: default
 
@@ -20,4 +28,7 @@
   - ".yamllint"
 ignore: |
   .tox
-  mod
+  cover/
+  build/
+  venv
+  release/
diff --git a/installers/charm/ng-ui/metadata.yaml b/installers/charm/ng-ui/metadata.yaml
index 7765b26..3ff7a97 100644
--- a/installers/charm/ng-ui/metadata.yaml
+++ b/installers/charm/ng-ui/metadata.yaml
@@ -24,7 +24,7 @@
   service: cluster
 requires:
   nbi:
-    interface: osm-nbi
+    interface: http
 resources:
   image:
     type: oci-image
diff --git a/installers/charm/ng-ui/requirements-test.txt b/installers/charm/ng-ui/requirements-test.txt
new file mode 100644
index 0000000..d7585f3
--- /dev/null
+++ b/installers/charm/ng-ui/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/ng-ui/requirements.txt b/installers/charm/ng-ui/requirements.txt
index a178e33..2eaba28 100644
--- a/installers/charm/ng-ui/requirements.txt
+++ b/installers/charm/ng-ui/requirements.txt
@@ -1,16 +1,23 @@
-# 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 a copy of the License at
+# 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
+#         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.
-ops
-pydantic
-git+https://github.com/juju-solutions/resource-oci-image/@c5778285d332edf3d9a538f9d0c06154b7ec1b0b#egg=oci-image
+# 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
+##
+
+pydantic  # TODO: remove it
+git+https://github.com/davigar15/ops-lib-charmed-osm/@e7f26cd29b322e175a23cadbe4546b7f2bbf111c
\ No newline at end of file
diff --git a/installers/charm/ng-ui/src/charm.py b/installers/charm/ng-ui/src/charm.py
index 944d8ce..4d2bb85 100755
--- a/installers/charm/ng-ui/src/charm.py
+++ b/installers/charm/ng-ui/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,175 +20,164 @@
 # osm-charmers@lists.launchpad.net
 ##
 
+# pylint: disable=E0213
+
+
 import logging
-from typing import Any, Dict, NoReturn
-from pydantic import ValidationError
+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,
+    FilesV3Builder,
+    IngressResourceV3Builder,
+)
+
+
+from opslib.osm.validator import (
+    ModelValidator,
+    validator,
+)
+
+from opslib.osm.interfaces.http import HttpClient
+from string import Template
+from pathlib import Path
 
 logger = logging.getLogger(__name__)
 
-NGUI_PORT = 80
+
+class ConfigModel(ModelValidator):
+    port: int
+    server_name: str
+    max_file_size: int
+    site_url: Optional[str]
+    ingress_whitelist_source_range: Optional[str]
+    tls_secret_name: Optional[str]
+
+    @validator("port")
+    def validate_port(cls, v):
+        if v <= 0:
+            raise ValueError("value must be greater than 0")
+        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 ConfigurePodEvent(EventBase):
-    """Configure Pod event"""
-
-    pass
-
-
-class NgUiEvents(CharmEvents):
-    """NGUI Events"""
-
-    configure_pod = EventSource(ConfigurePodEvent)
-
-
-class NgUiCharm(CharmBase):
-    """NGUI Charm."""
-
-    state = StoredState()
-    on = NgUiEvents()
-
+class NgUiCharm(CharmedOsmBase):
     def __init__(self, *args) -> NoReturn:
-        """NGUI Charm constructor."""
-        super().__init__(*args)
+        super().__init__(*args, oci_image="image")
 
-        # Internal state initialization
-        self.state.set_default(pod_spec=None)
+        self.nbi_client = HttpClient(self, "nbi")
+        self.framework.observe(self.on["nbi"].relation_changed, self.configure_pod)
+        self.framework.observe(self.on["nbi"].relation_broken, self.configure_pod)
 
-        # North bound interface initialization
-        self.state.set_default(nbi_host=None)
-        self.state.set_default(nbi_port=None)
+    def _check_missing_dependencies(self, config: ConfigModel):
+        missing_relations = []
 
-        self.http_port = NGUI_PORT
-        self.image = OCIImageResource(self, "image")
+        if self.nbi_client.is_missing_data_in_app():
+            missing_relations.append("nbi")
 
-        # 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)
+        if missing_relations:
+            raise RelationsMissing(missing_relations)
 
-        # Registering custom internal events
-        self.framework.observe(self.on.configure_pod, self.configure_pod)
-
-        # Registering required relation changed events
-        self.framework.observe(
-            self.on.nbi_relation_changed, self._on_nbi_relation_changed
+    def _build_files(self, config: ConfigModel):
+        files_builder = FilesV3Builder()
+        files_builder.add_file(
+            "default",
+            Template(Path("files/default").read_text()).substitute(
+                port=config.port,
+                server_name=config.server_name,
+                max_file_size=config.max_file_size,
+                nbi_host=self.nbi_client.host,
+                nbi_port=self.nbi_client.port,
+            ),
         )
+        return files_builder.build()
 
-        # Registering required relation departed events
-        self.framework.observe(
-            self.on.nbi_relation_departed, self._on_nbi_relation_departed
+    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 Container
+        container_builder = ContainerV3Builder(self.app.name, image_info)
+        container_builder.add_port(name=self.app.name, port=config.port)
+        container = container_builder.build()
+        container_builder.add_tcpsocket_readiness_probe(
+            config.port,
+            initial_delay_seconds=45,
+            timeout_seconds=5,
         )
-
-    def _on_nbi_relation_changed(self, event: EventBase) -> NoReturn:
-        """Reads information about the nbi relation.
-
-        Args:
-            event (EventBase): NBI relation event.
-        """
-        if event.unit not in event.relation.data:
-            return
-        relation_data = event.relation.data[event.unit]
-        nbi_host = relation_data.get("host")
-        nbi_port = relation_data.get("port")
-
-        if (
-            nbi_host
-            and nbi_port
-            and (self.state.nbi_host != nbi_host or self.state.nbi_port != nbi_port)
-        ):
-            self.state.nbi_host = nbi_host
-            self.state.nbi_port = nbi_port
-            self.on.configure_pod.emit()
-
-    def _on_nbi_relation_departed(self, event: EventBase) -> NoReturn:
-        """Clears data from nbi relation.
-
-        Args:
-            event (EventBase): NBI relation event.
-        """
-        self.state.nbi_host = None
-        self.state.nbi_port = None
-        self.on.configure_pod.emit()
-
-    def _missing_relations(self) -> str:
-        """Checks if there missing relations.
-
-        Returns:
-            str: string with missing relations
-        """
-        data_status = {
-            "nbi": self.state.nbi_host,
-        }
-
-        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 = {
-            "nbi_host": self.state.nbi_host,
-            "nbi_port": self.state.nbi_port,
-        }
-        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_builder.add_tcpsocket_liveness_probe(
+            config.port,
+            initial_delay_seconds=45,
+            timeout_seconds=15,
+        )
+        container_builder.add_volume_config(
+            "configuration",
+            "/etc/nginx/sites-available/",
+            self._build_files(config),
+        )
+        # 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
+                ),
+            }
+            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.config,
-                self.relation_state,
-                self.model.app.name,
+            ingress_resource_builder.add_rule(
+                parsed.hostname, self.app.name, config.port
             )
-        except ValidationError 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 = ingress_resource_builder.build()
+            pod_spec_builder.add_ingress_resource(ingress_resource)
+        return pod_spec_builder.build()
 
 
 if __name__ == "__main__":
diff --git a/installers/charm/ng-ui/src/pod_spec.py b/installers/charm/ng-ui/src/pod_spec.py
index 1687756..95d5f72 100644
--- a/installers/charm/ng-ui/src/pod_spec.py
+++ b/installers/charm/ng-ui/src/pod_spec.py
@@ -20,6 +20,9 @@
 # osm-charmers@lists.launchpad.net
 ##
 
+# pylint: disable=E0213,E0611
+
+
 import logging
 from pydantic import (
     BaseModel,
diff --git a/installers/charm/ng-ui/tests/__init__.py b/installers/charm/ng-ui/tests/__init__.py
index 6004c6d..d0d973a 100644
--- a/installers/charm/ng-ui/tests/__init__.py
+++ b/installers/charm/ng-ui/tests/__init__.py
@@ -22,10 +22,10 @@
 
 """Init mocking for unit tests."""
 
-# import sys
-# import mock
+import sys
+import mock
 
-# sys.path.append("src")
+sys.path.append("src")
 
-# oci_image = mock.MagicMock()
-# sys.modules["oci_image"] = oci_image
+oci_image = mock.MagicMock()
+sys.modules["oci_image"] = oci_image
diff --git a/installers/charm/ng-ui/tests/test_charm.py b/installers/charm/ng-ui/tests/test_charm.py
index 1cde2df..d9a4d3e 100644
--- a/installers/charm/ng-ui/tests/test_charm.py
+++ b/installers/charm/ng-ui/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,27 +20,77 @@
 # osm-charmers@lists.launchpad.net
 ##
 
+import sys
 from typing import NoReturn
 import unittest
-
+from ops.model import ActiveStatus, BlockedStatus
 from ops.testing import Harness
 
 from charm import NgUiCharm
 
 
 class TestCharm(unittest.TestCase):
-    """PLA 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(NgUiCharm)
         self.harness.set_leader(is_leader=True)
         self.harness.begin()
+        self.config = {
+            "server_name": "localhost",
+            "port": 80,
+            "max_file_size": 0,
+            "ingress_whitelist_source_range": "",
+            "tls_secret_name": "",
+            "site_url": "https://ui.192.168.100.100.xip.io",
+        }
+        self.harness.update_config(self.config)
 
-    def test_on_start_without_relations(self) -> NoReturn:
-        """Test installation without any relation."""
+    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 ["nbi"]
+            )
+        )
+
+    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(
+        self,
+    ) -> NoReturn:
+        "Test with relations (internal)"
+        self.initialize_nbi_relation()
+        # Verifying status
+        self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
+
+    def initialize_nbi_relation(self):
+        http_relation_id = self.harness.add_relation("nbi", "nbi")
+        self.harness.add_relation_unit(http_relation_id, "nbi")
+        self.harness.update_relation_data(
+            http_relation_id,
+            "nbi",
+            {"host": "nbi", "port": 9999},
+        )
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/installers/charm/ng-ui/tox.ini b/installers/charm/ng-ui/tox.ini
index e60f0b8..1f9442e 100644
--- a/installers/charm/ng-ui/tox.ini
+++ b/installers/charm/ng-ui/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,65 +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 = ng-ui
+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 ng-ui.charm
   charmcraft build
-  unzip ng-ui.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
-  -rrequirements.txt
-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/|release/"
-  yamllint .
-  flake8 . --max-line-length=100 --ignore="E501,W503,W504,F722" --exclude "build/ .tox/ mod/ lib/ release/"
-
-[coverage:run]
-branch = True
-concurrency = multiprocessing
-parallel = True
-source =
-  .
-omit =  
-  .tox/*
-  tests/*