From 95ba7e1641316616ce5a9442d485753456ff9525 Mon Sep 17 00:00:00 2001 From: David Garcia Date: Wed, 3 Feb 2021 11:10:28 +0100 Subject: [PATCH] Add tox tests for osm charms in devops Change-Id: Ibbf1154e514bf8a91ed6d19b6bbf5fce0079da0c Signed-off-by: David Garcia --- Dockerfile | 25 +++- devops-stages/stage-test.sh | 25 ++++ installers/charm/grafana/.gitignore | 2 +- installers/charm/grafana/tox.ini | 2 +- installers/charm/keystone/.gitignore | 5 +- installers/charm/keystone/src/charm.py | 4 +- installers/charm/keystone/tests/__init__.py | 31 +++++ installers/charm/keystone/tests/test_charm.py | 46 +++++++ installers/charm/keystone/tox.ini | 95 ++++++++++----- installers/charm/lcm/tox.ini | 2 +- installers/charm/mon/tox.ini | 7 +- installers/charm/nbi/.gitignore | 2 +- installers/charm/nbi/tox.ini | 2 +- installers/charm/ng-ui/.gitignore | 5 +- installers/charm/ng-ui/src/charm.py | 2 +- installers/charm/ng-ui/tests/__init__.py | 31 +++++ installers/charm/ng-ui/tests/test_charm.py | 46 +++++++ installers/charm/ng-ui/tox.ini | 115 ++++++++++-------- installers/charm/pla/.gitignore | 5 +- installers/charm/pla/src/charm.py | 12 +- installers/charm/pla/tests/__init__.py | 31 +++++ installers/charm/pla/tests/test_charm.py | 46 +++++++ installers/charm/pla/tox.ini | 114 +++++++++-------- installers/charm/pol/tox.ini | 2 +- installers/charm/prometheus/tox.ini | 2 +- installers/charm/ro/tox.ini | 2 +- 26 files changed, 504 insertions(+), 157 deletions(-) create mode 100644 installers/charm/keystone/tests/__init__.py create mode 100644 installers/charm/keystone/tests/test_charm.py create mode 100644 installers/charm/ng-ui/tests/__init__.py create mode 100644 installers/charm/ng-ui/tests/test_charm.py create mode 100644 installers/charm/pla/tests/__init__.py create mode 100644 installers/charm/pla/tests/test_charm.py diff --git a/Dockerfile b/Dockerfile index d08f3bb6..ffc36135 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,24 @@ -FROM ubuntu:16.04 +# 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 +## -RUN apt-get update && apt-get -y install git build-essential dh-make +FROM ubuntu:18.04 + +RUN apt-get update && apt-get -y install git build-essential dh-make tox python python3.8 python3-dev python3-pip diff --git a/devops-stages/stage-test.sh b/devops-stages/stage-test.sh index 1a248525..3e5196f0 100755 --- a/devops-stages/stage-test.sh +++ b/devops-stages/stage-test.sh @@ -1 +1,26 @@ #!/bin/sh +# 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. + +set -eux +CURRENT_DIR=`pwd` + +# Execute tests for charms +CHARM_PATH="./installers/charm" +CHARM_NAMES="keystone lcm mon nbi ng-ui pla pol prometheus ro" +for charm in $CHARM_NAMES; do + cd $CHARM_PATH/$charm + tox --recreate + cd $CURRENT_DIR +done \ No newline at end of file diff --git a/installers/charm/grafana/.gitignore b/installers/charm/grafana/.gitignore index 3ca6f3ae..01ec352a 100644 --- a/installers/charm/grafana/.gitignore +++ b/installers/charm/grafana/.gitignore @@ -23,6 +23,6 @@ venv .vscode build grafana.charm -.coverage +.coverage* .stestr cover diff --git a/installers/charm/grafana/tox.ini b/installers/charm/grafana/tox.ini index 12a1f85d..069cf105 100644 --- a/installers/charm/grafana/tox.ini +++ b/installers/charm/grafana/tox.ini @@ -26,7 +26,7 @@ sitepackages = False skip_missing_interpreters = False [testenv] -basepython = python3 +basepython = python3.8 setenv = PYTHONHASHSEED=0 PYTHONPATH = {toxinidir}/src diff --git a/installers/charm/keystone/.gitignore b/installers/charm/keystone/.gitignore index 97fc8b44..43fcb62c 100644 --- a/installers/charm/keystone/.gitignore +++ b/installers/charm/keystone/.gitignore @@ -14,4 +14,7 @@ .vscode .tox build -keystone.charm \ No newline at end of file +keystone.charm +.stestr +.coverage* +cover/ \ No newline at end of file diff --git a/installers/charm/keystone/src/charm.py b/installers/charm/keystone/src/charm.py index 637461e3..d5c169b6 100755 --- a/installers/charm/keystone/src/charm.py +++ b/installers/charm/keystone/src/charm.py @@ -68,6 +68,7 @@ class KeystoneEvents(CharmEvents): configure_pod = EventSource(ConfigurePodEvent) + class KeystoneCharm(CharmBase): """Keystone K8s Charm""" @@ -142,7 +143,7 @@ class KeystoneCharm(CharmBase): event (EventBase): DB relation event to access database information. """ - if not event.unit in event.relation.data: + if event.unit not in event.relation.data: return relation_data = event.relation.data[event.unit] db_host = relation_data.get("host") @@ -168,7 +169,6 @@ class KeystoneCharm(CharmBase): self.state.db_password = db_password self.on.configure_pod.emit() - def _on_db_relation_departed(self, event: EventBase) -> NoReturn: """Clears data from db relation. diff --git a/installers/charm/keystone/tests/__init__.py b/installers/charm/keystone/tests/__init__.py new file mode 100644 index 00000000..6004c6d5 --- /dev/null +++ b/installers/charm/keystone/tests/__init__.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# Copyright 2020 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 +## + +"""Init mocking for unit tests.""" + +# import sys +# import mock + +# sys.path.append("src") + +# oci_image = mock.MagicMock() +# sys.modules["oci_image"] = oci_image diff --git a/installers/charm/keystone/tests/test_charm.py b/installers/charm/keystone/tests/test_charm.py new file mode 100644 index 00000000..8cb86048 --- /dev/null +++ b/installers/charm/keystone/tests/test_charm.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# Copyright 2020 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 +## + +from typing import NoReturn +import unittest + +from ops.testing import Harness + +from charm import KeystoneCharm + + +class TestCharm(unittest.TestCase): + """Keystone Charm unit tests.""" + + def setUp(self) -> NoReturn: + """Test setup""" + self.harness = Harness(KeystoneCharm) + 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.config_changed.emit() + + +if __name__ == "__main__": + unittest.main() diff --git a/installers/charm/keystone/tox.ini b/installers/charm/keystone/tox.ini index cff5193b..781f74ee 100644 --- a/installers/charm/keystone/tox.ini +++ b/installers/charm/keystone/tox.ini @@ -1,47 +1,82 @@ # Copyright 2020 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 +## + [tox] -envlist = pep8 skipsdist = True +envlist = unit, lint +sitepackages = False +skip_missing_interpreters = False [testenv] -setenv = VIRTUAL_ENV={envdir} - PYTHONHASHSEED=0 -install_command = - pip install {opts} {packages} +basepython = python3.8 +setenv = + PYTHONHASHSEED=0 + PYTHONPATH = {toxinidir}/src + CHARM_NAME = keystone [testenv:build] -basepython = python3 passenv=HTTP_PROXY HTTPS_PROXY NO_PROXY -whitelist_externals = charmcraft - rm - unzip +whitelist_externals = + charmcraft + rm + unzip commands = - rm -rf release - charmcraft build - unzip keystone.charm -d release + rm -rf release keystone.charm + charmcraft build + unzip keystone.charm -d 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 [testenv:lint] -basepython = python3 deps = - black - yamllint - flake8 + black + yamllint + flake8 commands = - black --check --diff . --exclude "build/|.tox/|mod/|lib/" - yamllint . - flake8 . --max-line-length=100 --exclude "build/ .tox/ mod/ lib/" + 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/" -[testenv:venv] -commands = {posargs} +[coverage:run] +branch = True +concurrency = multiprocessing +parallel = True +source = + . +omit = + .tox/* + tests/* diff --git a/installers/charm/lcm/tox.ini b/installers/charm/lcm/tox.ini index bc7ee37a..068b4c55 100644 --- a/installers/charm/lcm/tox.ini +++ b/installers/charm/lcm/tox.ini @@ -26,7 +26,7 @@ sitepackages = False skip_missing_interpreters = False [testenv] -basepython = python3 +basepython = python3.8 setenv = PYTHONHASHSEED=0 PYTHONPATH = {toxinidir}/src diff --git a/installers/charm/mon/tox.ini b/installers/charm/mon/tox.ini index 0b1ab82c..7ddaf8d1 100644 --- a/installers/charm/mon/tox.ini +++ b/installers/charm/mon/tox.ini @@ -26,7 +26,7 @@ sitepackages = False skip_missing_interpreters = False [testenv] -basepython = python3 +basepython = python3.8 setenv = PYTHONHASHSEED=0 PYTHONPATH = {toxinidir}/src @@ -66,10 +66,9 @@ deps = yamllint flake8 commands = - black --check --diff . --exclude "build/|.tox/|mod/|lib/" + 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/" - + flake8 . --max-line-length=100 --ignore="E501,W503,W504,F722" --exclude "build/ .tox/ mod/ lib/ release/" [coverage:run] branch = True concurrency = multiprocessing diff --git a/installers/charm/nbi/.gitignore b/installers/charm/nbi/.gitignore index f920a7f1..05bdb52d 100644 --- a/installers/charm/nbi/.gitignore +++ b/installers/charm/nbi/.gitignore @@ -23,6 +23,6 @@ venv .vscode build nbi.charm -.coverage +.coverage* .stestr cover \ No newline at end of file diff --git a/installers/charm/nbi/tox.ini b/installers/charm/nbi/tox.ini index b7b09123..5989669d 100644 --- a/installers/charm/nbi/tox.ini +++ b/installers/charm/nbi/tox.ini @@ -26,7 +26,7 @@ sitepackages = False skip_missing_interpreters = False [testenv] -basepython = python3 +basepython = python3.8 setenv = PYTHONHASHSEED=0 PYTHONPATH = {toxinidir}/src diff --git a/installers/charm/ng-ui/.gitignore b/installers/charm/ng-ui/.gitignore index dfaa805f..9c7c0495 100644 --- a/installers/charm/ng-ui/.gitignore +++ b/installers/charm/ng-ui/.gitignore @@ -13,4 +13,7 @@ # limitations under the License. .vscode build -ng-ui.charm \ No newline at end of file +ng-ui.charm +.stestr +.coverage* +cover/ \ 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 21102e4c..944d8cec 100755 --- a/installers/charm/ng-ui/src/charm.py +++ b/installers/charm/ng-ui/src/charm.py @@ -93,7 +93,7 @@ class NgUiCharm(CharmBase): Args: event (EventBase): NBI relation event. """ - if not event.unit in event.relation.data: + if event.unit not in event.relation.data: return relation_data = event.relation.data[event.unit] nbi_host = relation_data.get("host") diff --git a/installers/charm/ng-ui/tests/__init__.py b/installers/charm/ng-ui/tests/__init__.py new file mode 100644 index 00000000..6004c6d5 --- /dev/null +++ b/installers/charm/ng-ui/tests/__init__.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# Copyright 2020 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 +## + +"""Init mocking for unit tests.""" + +# import sys +# import mock + +# sys.path.append("src") + +# 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 new file mode 100644 index 00000000..1cde2dfa --- /dev/null +++ b/installers/charm/ng-ui/tests/test_charm.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# Copyright 2020 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 +## + +from typing import NoReturn +import unittest + +from ops.testing import Harness + +from charm import NgUiCharm + + +class TestCharm(unittest.TestCase): + """PLA Charm unit tests.""" + + def setUp(self) -> NoReturn: + """Test setup""" + self.harness = Harness(NgUiCharm) + 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.config_changed.emit() + + +if __name__ == "__main__": + unittest.main() diff --git a/installers/charm/ng-ui/tox.ini b/installers/charm/ng-ui/tox.ini index 1c22bd55..e60f0b8c 100644 --- a/installers/charm/ng-ui/tox.ini +++ b/installers/charm/ng-ui/tox.ini @@ -1,65 +1,82 @@ -# Copyright 2020 Canonical Ltd. +# Copyright 2020 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 +## + [tox] -envlist = pep8 skipsdist = True +envlist = unit, lint +sitepackages = False +skip_missing_interpreters = False [testenv] -setenv = VIRTUAL_ENV={envdir} - PYTHONHASHSEED=0 -whitelist_externals = juju -passenv = HOME TERM CS_API_* OS_* AMULET_* -install_command = - pip install {opts} {packages} +basepython = python3.8 +setenv = + PYTHONHASHSEED=0 + PYTHONPATH = {toxinidir}/src + CHARM_NAME = ng-ui [testenv:build] -basepython = python3 passenv=HTTP_PROXY HTTPS_PROXY NO_PROXY -whitelist_externals = charmcraft - rm - unzip +whitelist_externals = + charmcraft + rm + unzip commands = - rm -rf release - charmcraft build - unzip ng-ui.charm -d release + rm -rf release ng-ui.charm + charmcraft build + unzip ng-ui.charm -d release -[testenv:black] -basepython = python3 -deps = - black - yamllint +[testenv:unit] commands = - black --check --diff src - yamllint . - -[testenv:pep8] -basepython = python3 -deps=charm-tools -commands = charm-proof + 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 -[testenv:func-noop] -basepython = python3 +[testenv:lint] +deps = + black + yamllint + flake8 commands = - true - -[testenv:func] -basepython = python3 -commands = functest-run-suite - -[testenv:func-smoke] -basepython = python3 -commands = functest-run-suite --keep-model --smoke + 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/" -[testenv:venv] -commands = {posargs} +[coverage:run] +branch = True +concurrency = multiprocessing +parallel = True +source = + . +omit = + .tox/* + tests/* diff --git a/installers/charm/pla/.gitignore b/installers/charm/pla/.gitignore index db6d41ae..dad4b21d 100644 --- a/installers/charm/pla/.gitignore +++ b/installers/charm/pla/.gitignore @@ -13,4 +13,7 @@ # limitations under the License. .vscode build -pla.charm \ No newline at end of file +pla.charm +.stestr +.coverage* +cover/ \ No newline at end of file diff --git a/installers/charm/pla/src/charm.py b/installers/charm/pla/src/charm.py index 16e7303f..6847580f 100755 --- a/installers/charm/pla/src/charm.py +++ b/installers/charm/pla/src/charm.py @@ -13,21 +13,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -from glob import glob import logging -from pathlib import Path -from string import Template -import sys from ops.charm import CharmBase -from ops.framework import StoredState, Object +from ops.framework import StoredState from ops.main import main from ops.model import ( ActiveStatus, MaintenanceStatus, WaitingStatus, ) - +from typing import NoReturn logger = logging.getLogger(__name__) @@ -35,8 +31,8 @@ logger = logging.getLogger(__name__) class PLACharm(CharmBase): state = StoredState() - def __init__(self, framework, key): - super().__init__(framework, key) + def __init__(self, *args) -> NoReturn: + super().__init__(*args) self.state.set_default(spec=None) self.state.set_default(kafka_host=None) self.state.set_default(kafka_port=None) diff --git a/installers/charm/pla/tests/__init__.py b/installers/charm/pla/tests/__init__.py new file mode 100644 index 00000000..6004c6d5 --- /dev/null +++ b/installers/charm/pla/tests/__init__.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# Copyright 2020 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 +## + +"""Init mocking for unit tests.""" + +# import sys +# import mock + +# sys.path.append("src") + +# oci_image = mock.MagicMock() +# sys.modules["oci_image"] = oci_image diff --git a/installers/charm/pla/tests/test_charm.py b/installers/charm/pla/tests/test_charm.py new file mode 100644 index 00000000..dbc7be3b --- /dev/null +++ b/installers/charm/pla/tests/test_charm.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# Copyright 2020 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 +## + +from typing import NoReturn +import unittest + +from ops.testing import Harness + +from charm import PLACharm + + +class TestCharm(unittest.TestCase): + """PLA Charm unit tests.""" + + def setUp(self) -> NoReturn: + """Test setup""" + self.harness = Harness(PLACharm) + 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.config_changed.emit() + + +if __name__ == "__main__": + unittest.main() diff --git a/installers/charm/pla/tox.ini b/installers/charm/pla/tox.ini index 678a5ec5..32d78d3b 100644 --- a/installers/charm/pla/tox.ini +++ b/installers/charm/pla/tox.ini @@ -1,67 +1,81 @@ -# Copyright 2020 Canonical Ltd. +# Copyright 2020 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 +## [tox] -envlist = pep8 skipsdist = True +envlist = unit, lint +sitepackages = False +skip_missing_interpreters = False [testenv] -setenv = VIRTUAL_ENV={envdir} - PYTHONHASHSEED=0 -whitelist_externals = juju -passenv = HOME TERM CS_API_* OS_* AMULET_* -install_command = - pip install {opts} {packages} - +basepython = python3.8 +setenv = + PYTHONHASHSEED=0 + PYTHONPATH = {toxinidir}/src + CHARM_NAME = pla [testenv:build] -basepython = python3 passenv=HTTP_PROXY HTTPS_PROXY NO_PROXY -whitelist_externals = charmcraft - rm - unzip +whitelist_externals = + charmcraft + rm + unzip commands = - rm -rf release - charmcraft build - unzip pla.charm -d release + rm -rf release pla.charm + charmcraft build + unzip pla.charm -d release -[testenv:black] -basepython = python3 -deps = - black - yamllint +[testenv:unit] commands = - black --check --diff src - yamllint . - -[testenv:pep8] -basepython = python3 -deps=charm-tools -commands = charm-proof + 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 -[testenv:func-noop] -basepython = python3 +[testenv:lint] +deps = + black + yamllint + flake8 commands = - true - -[testenv:func] -basepython = python3 -commands = functest-run-suite - -[testenv:func-smoke] -basepython = python3 -commands = functest-run-suite --keep-model --smoke + 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/" -[testenv:venv] -commands = {posargs} +[coverage:run] +branch = True +concurrency = multiprocessing +parallel = True +source = + . +omit = + .tox/* + tests/* diff --git a/installers/charm/pol/tox.ini b/installers/charm/pol/tox.ini index 25bf8306..ae324f82 100644 --- a/installers/charm/pol/tox.ini +++ b/installers/charm/pol/tox.ini @@ -26,7 +26,7 @@ sitepackages = False skip_missing_interpreters = False [testenv] -basepython = python3 +basepython = python3.8 setenv = PYTHONHASHSEED=0 PYTHONPATH = {toxinidir}/src diff --git a/installers/charm/prometheus/tox.ini b/installers/charm/prometheus/tox.ini index a7625a6a..5491c07a 100644 --- a/installers/charm/prometheus/tox.ini +++ b/installers/charm/prometheus/tox.ini @@ -26,7 +26,7 @@ sitepackages = False skip_missing_interpreters = False [testenv] -basepython = python3 +basepython = python3.8 setenv = PYTHONHASHSEED=0 PYTHONPATH = {toxinidir}/src diff --git a/installers/charm/ro/tox.ini b/installers/charm/ro/tox.ini index 44461453..8fd07d3d 100644 --- a/installers/charm/ro/tox.ini +++ b/installers/charm/ro/tox.ini @@ -26,7 +26,7 @@ sitepackages = False skip_missing_interpreters = False [testenv] -basepython = python3 +basepython = python3.8 setenv = PYTHONHASHSEED=0 PYTHONPATH = {toxinidir}/src -- 2.25.1