Browse Source

Added basic unit tests

Change-Id: I51aad25c9b2930744974cf7619c0636600203b66
changes/33/720633/2
Mohammed Naser 1 year ago
parent
commit
66b6315d09
9 changed files with 84 additions and 6 deletions
  1. +1
    -0
      .gitignore
  2. +3
    -0
      .stestr.conf
  3. +3
    -3
      openstack_operator/memcached.py
  4. +0
    -0
      openstack_operator/tests/__init__.py
  5. +0
    -0
      openstack_operator/tests/unit/__init__.py
  6. +66
    -0
      openstack_operator/tests/unit/test_memcached.py
  7. +2
    -0
      test-requirements.txt
  8. +7
    -3
      tox.ini
  9. +2
    -0
      zuul.d/misc-jobs.yaml

+ 1
- 0
.gitignore View File

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

+ 3
- 0
.stestr.conf View File

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

+ 3
- 3
openstack_operator/memcached.py View File

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

+ 0
- 0
openstack_operator/tests/__init__.py View File


+ 0
- 0
openstack_operator/tests/unit/__init__.py View File


+ 66
- 0
openstack_operator/tests/unit/test_memcached.py 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={})

+ 2
- 0
test-requirements.txt View File

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

+ 7
- 3
tox.ini View File

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


+ 2
- 0
zuul.d/misc-jobs.yaml View File

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

Loading…
Cancel
Save