From 66b6315d0980d19bcad2d52142ea6c7e4ad6c1ad Mon Sep 17 00:00:00 2001 From: Mohammed Naser Date: Thu, 16 Apr 2020 22:52:25 -0400 Subject: [PATCH] Added basic unit tests Change-Id: I51aad25c9b2930744974cf7619c0636600203b66 --- .gitignore | 1 + .stestr.conf | 3 + openstack_operator/memcached.py | 6 +- openstack_operator/tests/__init__.py | 0 openstack_operator/tests/unit/__init__.py | 0 .../tests/unit/test_memcached.py | 66 +++++++++++++++++++ test-requirements.txt | 2 + tox.ini | 10 ++- zuul.d/misc-jobs.yaml | 2 + 9 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 .stestr.conf create mode 100644 openstack_operator/tests/__init__.py create mode 100644 openstack_operator/tests/unit/__init__.py create mode 100644 openstack_operator/tests/unit/test_memcached.py diff --git a/.gitignore b/.gitignore index 7f9fe117..109fa9c3 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ doc/build .tox __pycache__ *.egg* +.stestr diff --git a/.stestr.conf b/.stestr.conf new file mode 100644 index 00000000..348dffbb --- /dev/null +++ b/.stestr.conf @@ -0,0 +1,3 @@ +[DEFAULT] +test_path=./openstack_operator/tests/unit +top_dir=./ diff --git a/openstack_operator/memcached.py b/openstack_operator/memcached.py index 6d01397c..fe0c7dff 100644 --- a/openstack_operator/memcached.py +++ b/openstack_operator/memcached.py @@ -63,11 +63,11 @@ def deployment_event(namespace, meta, spec, **_): Deployments for Memcached to both update and synchronize the Mcrouter. """ - name = meta['labels']['app.kubernetes.io/instance'] - selector = spec['selector']['matchLabels'] + name = meta.get('labels', {}).get('app.kubernetes.io/instance') + selector = spec.get('selector', {}).get('matchLabels', {}) servers = utils.get_ready_pod_ips(namespace, selector) memcacheds = ["%s:11211" % s for s in servers] utils.create_or_update('memcached/mcrouter.yml.j2', name=name, servers=memcacheds, - spec=spec['template']['spec']) + spec=spec.get('template', {}).get('spec', {})) diff --git a/openstack_operator/tests/__init__.py b/openstack_operator/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/openstack_operator/tests/unit/__init__.py b/openstack_operator/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/openstack_operator/tests/unit/test_memcached.py b/openstack_operator/tests/unit/test_memcached.py new file mode 100644 index 00000000..ce26f5d7 --- /dev/null +++ b/openstack_operator/tests/unit/test_memcached.py @@ -0,0 +1,66 @@ +# 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 Memcached Operator + +This module contains all the tests for the Memcached operator. +""" + +# Disable no-self-use +# pylint: disable=R0201 + +import mock + +from oslotest import base + +from openstack_operator import memcached + + +class MemcachedListTestCase(base.BaseTestCase): + """Tests for determining server list.""" + + @mock.patch.object(memcached.utils, 'get_ready_pod_ips') + @mock.patch.object(memcached.utils, 'create_or_update') + def test_with_no_ips(self, mock_create, mock_get_ready_pods): + """Test a deployment with no ready pods.""" + + mock_get_ready_pods.return_value = [] + memcached.deployment_event("default", {}, {}) + + mock_create.assert_called_once_with('memcached/mcrouter.yml.j2', + name=None, servers=[], spec={}) + + @mock.patch.object(memcached.utils, 'get_ready_pod_ips') + @mock.patch.object(memcached.utils, 'create_or_update') + def test_with_single_ip(self, mock_create, mock_get_ready_pods): + """Test a deployment with a single ready pod.""" + + mock_get_ready_pods.return_value = ['1.1.1.1'] + memcached.deployment_event("default", {}, {}) + + mock_create.assert_called_once_with( + 'memcached/mcrouter.yml.j2', name=None, + servers=['1.1.1.1:11211'], spec={}) + + @mock.patch.object(memcached.utils, 'get_ready_pod_ips') + @mock.patch.object(memcached.utils, 'create_or_update') + def test_multiple_ips(self, mock_create, mock_get_ready_pods): + """Test a deployment with a multiple ready pods.""" + + mock_get_ready_pods.return_value = ['1.1.1.1', '2.2.2.2'] + memcached.deployment_event("default", {}, {}) + + mock_create.assert_called_once_with( + 'memcached/mcrouter.yml.j2', name=None, + servers=['1.1.1.1:11211', '2.2.2.2:11211'], spec={}) diff --git a/test-requirements.txt b/test-requirements.txt index 897e59f5..11eb75da 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,2 +1,4 @@ flake8 pylint +oslotest +stestr diff --git a/tox.ini b/tox.ini index a2bb08ae..ff39b7e3 100644 --- a/tox.ini +++ b/tox.ini @@ -1,8 +1,15 @@ [tox] minversion = 3.1.1 +envlist = py37 [testenv] usedevelop = True +deps = + -rtest-requirements.txt + -rrequirements.txt +commands = + stestr run {posargs} + stestr slowest [testenv:update-zuul-jobs] deps = @@ -12,9 +19,6 @@ commands = [testenv:linters] basepython = python3.7 -deps = - -rtest-requirements.txt - -rrequirements.txt commands = pylint openstack_operator flake8 openstack_operator diff --git a/zuul.d/misc-jobs.yaml b/zuul.d/misc-jobs.yaml index 26927f04..d7ef6982 100644 --- a/zuul.d/misc-jobs.yaml +++ b/zuul.d/misc-jobs.yaml @@ -18,8 +18,10 @@ - golang-go-test - openstack-operator:linters:chart - openstack-operator:linters:tox + - tox-py37 gate: jobs: - golang-go-test - openstack-operator:linters:chart - openstack-operator:linters:tox + - tox-py37