tests: added unit tests for all templates

Change-Id: Icf3ca3aa151d063cc21c45f5a949695c83399a25
This commit is contained in:
Mohammed Naser 2020-04-18 23:39:28 -04:00 committed by okozachenko
parent f283298d32
commit 0e7bc74b79
5 changed files with 122 additions and 7 deletions

View File

@ -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."""

View File

@ -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'

View File

@ -20,11 +20,10 @@ This module contains all the tests for the Memcached operator.
# Disable no-self-use # Disable no-self-use
# pylint: disable=R0201 # pylint: disable=R0201
import mock from unittest import mock
from oslotest import base
from openstack_operator import memcached from openstack_operator import memcached
from openstack_operator.tests.unit import base
class MemcachedOperatorTestCase(base.BaseTestCase): class MemcachedOperatorTestCase(base.BaseTestCase):
@ -37,3 +36,10 @@ class MemcachedOperatorTestCase(base.BaseTestCase):
memcached.create_or_resume("foo", {}) memcached.create_or_resume("foo", {})
mock_ensure_absent.assert_called_once_with( mock_ensure_absent.assert_called_once_with(
'memcached/deployment.yml.j2', name="foo", spec={}) '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'

View File

@ -88,6 +88,17 @@ def ensure_absent(template, **kwargs):
resource.delete() 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): def generate_yaml(template, **kwargs):
"""Generate dictionary from YAML template. """Generate dictionary from YAML template.
@ -96,9 +107,7 @@ def generate_yaml(template, **kwargs):
cluster. cluster.
""" """
template = ENV.get_template(template) doc = render_template(template, **kwargs)
yamldoc = template.render(**kwargs)
doc = yaml.safe_load(yamldoc)
kopf.adopt(doc) kopf.adopt(doc)
return doc return doc

View File

@ -1,4 +1,3 @@
flake8 flake8
pylint pylint
oslotest
stestr stestr