Merge "Add unit tests for service modules"

This commit is contained in:
Jenkins 2017-06-07 06:44:37 +00:00 committed by Gerrit Code Review
commit f44c8d0931
2 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,31 @@
# Copyright (C) 2017 Catalyst IT Ltd
#
# 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.
import mock
from distil.service.api.v2 import products
from distil.tests.unit import base
class ProductsTest(base.DistilTestCase):
@mock.patch('stevedore.driver.DriverManager')
def test_get_products(self, mock_driver):
fake_region = 'nz-1'
driver_manager = mock_driver.return_value
driver = driver_manager.driver
products.get_products([fake_region])
driver.get_products.assert_called_once_with([fake_region])

View File

@ -0,0 +1,73 @@
# Copyright (C) 2017 Catalyst IT Ltd
#
# 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.
import json
import mock
from distil.service.api.v2 import quotations
from distil.tests.unit import base
class QuotationsTest(base.DistilWithDbTestCase):
@mock.patch('stevedore.driver.DriverManager')
@mock.patch('distil.db.api.project_get')
@mock.patch('distil.db.api.usage_get')
@mock.patch('distil.db.api.resource_get_by_ids')
def test_get_quotations(self, mock_get_resources, mock_get_usage,
mock_get_project, mock_driver):
self.override_config('keystone_authtoken', region_name='region-1')
class Project(object):
def __init__(self, id, name):
self.id = id
self.name = name
mock_get_project.return_value = Project('123', 'fake_project')
usage = [
{
'resource_id': '111',
'service': 'srv1',
'volume': 10,
'unit': 'byte',
},
{
'resource_id': '222',
'service': 'srv2',
'volume': 20,
'unit': 'byte',
}
]
mock_get_usage.return_value = usage
class Resource(object):
def __init__(self, id, info):
self.id = id
self.info = info
res1 = Resource('111', json.dumps({'name': 'resource1'}))
res2 = Resource('222', json.dumps({'name': 'resource2'}))
mock_get_resources.return_value = [res1, res2]
driver_manager = mock_driver.return_value
driver = driver_manager.driver
quotations.get_quotations('123', detailed=False)
driver.get_quotations.assert_called_once_with(
'region-1', '123',
measurements=usage,
resources=[res1, res2],
detailed=False
)