Browse Source

tests: added unit tests for all templates

Change-Id: Icf3ca3aa151d063cc21c45f5a949695c83399a25
changes/49/721049/4
Mohammed Naser 12 months ago
committed by okozachenko
parent
commit
0e7bc74b79
5 changed files with 122 additions and 7 deletions
  1. +74
    -0
      openstack_operator/tests/unit/base.py
  2. +27
    -0
      openstack_operator/tests/unit/test_mcrouter.py
  3. +9
    -3
      openstack_operator/tests/unit/test_memcached.py
  4. +12
    -3
      openstack_operator/utils.py
  5. +0
    -1
      test-requirements.txt

+ 74
- 0
openstack_operator/tests/unit/base.py 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."""

+ 27
- 0
openstack_operator/tests/unit/test_mcrouter.py 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'

+ 9
- 3
openstack_operator/tests/unit/test_memcached.py View File

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

+ 12
- 3
openstack_operator/utils.py View File

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


+ 0
- 1
test-requirements.txt View File

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

Loading…
Cancel
Save