Added basic unit tests

Change-Id: I51aad25c9b2930744974cf7619c0636600203b66
This commit is contained in:
Mohammed Naser 2020-04-16 22:52:25 -04:00
parent 0d326d36a5
commit 66b6315d09
9 changed files with 84 additions and 6 deletions

1
.gitignore vendored
View File

@ -27,3 +27,4 @@ doc/build
.tox .tox
__pycache__ __pycache__
*.egg* *.egg*
.stestr

3
.stestr.conf Normal file
View File

@ -0,0 +1,3 @@
[DEFAULT]
test_path=./openstack_operator/tests/unit
top_dir=./

View File

@ -63,11 +63,11 @@ def deployment_event(namespace, meta, spec, **_):
Deployments for Memcached to both update and synchronize the Mcrouter. Deployments for Memcached to both update and synchronize the Mcrouter.
""" """
name = meta['labels']['app.kubernetes.io/instance'] name = meta.get('labels', {}).get('app.kubernetes.io/instance')
selector = spec['selector']['matchLabels'] selector = spec.get('selector', {}).get('matchLabels', {})
servers = utils.get_ready_pod_ips(namespace, selector) servers = utils.get_ready_pod_ips(namespace, selector)
memcacheds = ["%s:11211" % s for s in servers] memcacheds = ["%s:11211" % s for s in servers]
utils.create_or_update('memcached/mcrouter.yml.j2', utils.create_or_update('memcached/mcrouter.yml.j2',
name=name, servers=memcacheds, name=name, servers=memcacheds,
spec=spec['template']['spec']) spec=spec.get('template', {}).get('spec', {}))

View File

View File

@ -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={})

View File

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

10
tox.ini
View File

@ -1,8 +1,15 @@
[tox] [tox]
minversion = 3.1.1 minversion = 3.1.1
envlist = py37
[testenv] [testenv]
usedevelop = True usedevelop = True
deps =
-rtest-requirements.txt
-rrequirements.txt
commands =
stestr run {posargs}
stestr slowest
[testenv:update-zuul-jobs] [testenv:update-zuul-jobs]
deps = deps =
@ -12,9 +19,6 @@ commands =
[testenv:linters] [testenv:linters]
basepython = python3.7 basepython = python3.7
deps =
-rtest-requirements.txt
-rrequirements.txt
commands = commands =
pylint openstack_operator pylint openstack_operator
flake8 openstack_operator flake8 openstack_operator

View File

@ -18,8 +18,10 @@
- golang-go-test - golang-go-test
- openstack-operator:linters:chart - openstack-operator:linters:chart
- openstack-operator:linters:tox - openstack-operator:linters:tox
- tox-py37
gate: gate:
jobs: jobs:
- golang-go-test - golang-go-test
- openstack-operator:linters:chart - openstack-operator:linters:chart
- openstack-operator:linters:tox - openstack-operator:linters:tox
- tox-py37