From 0e7bc74b7941df12640ffb49db7b925f4009cb49 Mon Sep 17 00:00:00 2001 From: Mohammed Naser Date: Sat, 18 Apr 2020 23:39:28 -0400 Subject: [PATCH] tests: added unit tests for all templates Change-Id: Icf3ca3aa151d063cc21c45f5a949695c83399a25 --- openstack_operator/tests/unit/base.py | 74 +++++++++++++++++++ .../tests/unit/test_mcrouter.py | 27 +++++++ .../tests/unit/test_memcached.py | 12 ++- openstack_operator/utils.py | 15 +++- test-requirements.txt | 1 - 5 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 openstack_operator/tests/unit/base.py create mode 100644 openstack_operator/tests/unit/test_mcrouter.py diff --git a/openstack_operator/tests/unit/base.py b/openstack_operator/tests/unit/base.py new file mode 100644 index 00000000..6f554e26 --- /dev/null +++ b/openstack_operator/tests/unit/base.py @@ -0,0 +1,74 @@ +# Copyright 2020 VEXXHOST, Inc. +# +# 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. + +"""Base test classes + +This module contains the base test classes. +""" + +import testtools + +import yaml + +from openstack_operator import utils + + +class BaseTestCase(testtools.TestCase): + """Base test class for the OpenStack operator.""" + + +class KubernetesObjectTestCase(testtools.TestCase): + """Base class for Kubernetes object tests.""" + + SAMPLES_PATH = 'config/samples' + + @classmethod + def setUpClass(cls): + sample_path = "%s/%s" % (cls.SAMPLES_PATH, cls.SAMPLE_FILE) + with open(sample_path) as sample_fd: + sample = yaml.load(sample_fd, Loader=yaml.FullLoader) + name = sample['metadata']['name'] + spec = sample['spec'] + + cls.object = utils.render_template(cls.TEMPLATE_FILE, + name=name, spec=spec) + + +class KubernetesAppTestCaseMixin: + """Mix-in to be used for tests that involve apps and containers.""" + + def test_containers_have_liveness_probe(self): + """Ensure that all containers have liveness probes.""" + for container in self.object['spec']['template']['spec']['containers']: + self.assertIn('livenessProbe', container) + + def test_containers_have_readiness_probe(self): + """Ensure that all containers have readiness probes.""" + for container in self.object['spec']['template']['spec']['containers']: + self.assertIn('readinessProbe', container) + + def test_containers_have_resource_limits(self): + """Ensure that all containers have resource limits.""" + for container in self.object['spec']['template']['spec']['containers']: + self.assertIn('resources', container) + + +class DeploymentTestCase(KubernetesObjectTestCase, + KubernetesAppTestCaseMixin): + """Basic tests for Kubernetes Deployments.""" + + +class StatefulSetTestCase(KubernetesObjectTestCase, + KubernetesAppTestCaseMixin): + """Basic tests for Kubernetes StatefulSets.""" diff --git a/openstack_operator/tests/unit/test_mcrouter.py b/openstack_operator/tests/unit/test_mcrouter.py new file mode 100644 index 00000000..5223a5a4 --- /dev/null +++ b/openstack_operator/tests/unit/test_mcrouter.py @@ -0,0 +1,27 @@ +# Copyright 2020 VEXXHOST, Inc. +# +# 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. + +"""Tests for Mcrouter Operator + +This module contains all the tests for the Mcrouter operator. +""" + +from openstack_operator.tests.unit import base + + +class McrouterDeploymentTestCase(base.StatefulSetTestCase): + """Basic tests for the Deployment.""" + + SAMPLE_FILE = 'infrastructure_v1alpha1_mcrouter.yaml' + TEMPLATE_FILE = 'mcrouter/deployment.yml.j2' diff --git a/openstack_operator/tests/unit/test_memcached.py b/openstack_operator/tests/unit/test_memcached.py index 97e6b845..5ff96c12 100644 --- a/openstack_operator/tests/unit/test_memcached.py +++ b/openstack_operator/tests/unit/test_memcached.py @@ -20,11 +20,10 @@ This module contains all the tests for the Memcached operator. # Disable no-self-use # pylint: disable=R0201 -import mock - -from oslotest import base +from unittest import mock from openstack_operator import memcached +from openstack_operator.tests.unit import base class MemcachedOperatorTestCase(base.BaseTestCase): @@ -37,3 +36,10 @@ class MemcachedOperatorTestCase(base.BaseTestCase): memcached.create_or_resume("foo", {}) mock_ensure_absent.assert_called_once_with( 'memcached/deployment.yml.j2', name="foo", spec={}) + + +class MemcachedStatefulSetTestCase(base.StatefulSetTestCase): + """Basic tests for the StatefulSet.""" + + SAMPLE_FILE = 'infrastructure_v1alpha1_memcached.yaml' + TEMPLATE_FILE = 'memcached/statefulset.yml.j2' diff --git a/openstack_operator/utils.py b/openstack_operator/utils.py index ccd94194..96362b58 100644 --- a/openstack_operator/utils.py +++ b/openstack_operator/utils.py @@ -88,6 +88,17 @@ def ensure_absent(template, **kwargs): resource.delete() +def render_template(template, **kwargs): + """Render template from YAML files. + + This function renders a template based on provided keyword arguments. + """ + + template = ENV.get_template(template) + yamldoc = template.render(**kwargs) + return yaml.safe_load(yamldoc) + + def generate_yaml(template, **kwargs): """Generate dictionary from YAML template. @@ -96,9 +107,7 @@ def generate_yaml(template, **kwargs): cluster. """ - template = ENV.get_template(template) - yamldoc = template.render(**kwargs) - doc = yaml.safe_load(yamldoc) + doc = render_template(template, **kwargs) kopf.adopt(doc) return doc diff --git a/test-requirements.txt b/test-requirements.txt index 11eb75da..4c609974 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,4 +1,3 @@ flake8 pylint -oslotest stestr