RETIRED, Tricircle is to provide networking automation across Neutron.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.7 KiB

# Copyright 2015 Huawei Technologies Co., Ltd.
# All Rights Reserved
# 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
# 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 mock import patch
import unittest
import pecan
import tricircle.api.controllers.root as root_controller
from tricircle.common import context
from tricircle.db import client
from tricircle.db import core
from tricircle.db import models
class ControllerTest(unittest.TestCase):
def setUp(self):
self.context = context.Context()
self.context.is_admin = True
root_controller._get_environment = mock.Mock(return_value={})
root_controller._extract_context_from_environ = mock.Mock(
pecan.abort = mock.Mock()
pecan.response = mock.Mock()
def tearDown(self):
class SitesControllerTest(ControllerTest):
def setUp(self):
super(SitesControllerTest, self).setUp()
self.controller = root_controller.SitesController()
def test_post_top_site(self):
kw = {'name': 'TopSite', 'top': True}
site_id =**kw)['site']['site_id']
site = models.get_site(self.context, site_id)
self.assertEqual(site['site_name'], 'TopSite')
self.assertEqual(site['az_id'], '')
@patch.object(client.Client, 'create_resources')
def test_post_bottom_site(self, mock_method):
kw = {'name': 'BottomSite'}
site_id =**kw)['site']['site_id']
site = models.get_site(self.context, site_id)
self.assertEqual(site['site_name'], 'BottomSite')
self.assertEqual(site['az_id'], 'az_BottomSite')
mock_method.assert_called_once_with('aggregate', self.context,
'ag_BottomSite', 'az_BottomSite')
def test_post_site_name_missing(self):
kw = {'top': True}**kw)
pecan.abort.assert_called_once_with(400, 'Name of site required')
def test_post_conflict(self):
kw = {'name': 'TopSite', 'top': True}**kw)**kw)
'Site with name TopSite exists')
def test_post_not_admin(self):
self.context.is_admin = False
kw = {'name': 'TopSite', 'top': True}**kw)
400, 'Admin role required to create sites')
@patch.object(client.Client, 'create_resources')
def test_post_decide_top(self, mock_method):
# 'top' default to False
# top site
kw = {'name': 'Site1', 'top': True}**kw)
# bottom site
kw = {'name': 'Site2', 'top': False}**kw)
kw = {'name': 'Site3'}**kw)
calls = ['aggregate', self.context, 'ag_Site%d' % i,
'az_Site%d' % i) for i in xrange(2, 4)]
@patch.object(models, 'create_site')
def test_post_create_site_exception(self, mock_method):
mock_method.side_effect = Exception
kw = {'name': 'BottomSite'}**kw)
pecan.abort.assert_called_once_with(500, 'Fail to create site')
@patch.object(client.Client, 'create_resources')
def test_post_create_aggregate_exception(self, mock_method):
mock_method.side_effect = Exception
kw = {'name': 'BottomSite'}**kw)
pecan.abort.assert_called_once_with(500, 'Fail to create aggregate')
# make sure site is deleted
site_filter = [{'key': 'site_name',
'comparator': 'eq',
'value': 'BottomSite'}]
sites = models.list_sites(self.context, site_filter)
self.assertEqual(len(sites), 0)
def test_get_one(self):
kw = {'name': 'TopSite', 'top': True}
site_id =**kw)['site']['site_id']
return_site = self.controller.get_one(site_id)['site']
self.assertEqual(return_site, {'site_id': site_id,
'site_name': 'TopSite',
'az_id': ''})
def test_get_one_not_found(self):
'Site with id fake_id not found')
@patch.object(client.Client, 'create_resources', new=mock.Mock)
def test_get_all(self):
kw1 = {'name': 'TopSite', 'top': True}
kw2 = {'name': 'BottomSite'}**kw1)**kw2)
sites = self.controller.get_all()
actual_result = [(site['site_name'],
site['az_id']) for site in sites['sites']]
expect_result = [('BottomSite', 'az_BottomSite'), ('TopSite', '')]
self.assertItemsEqual(actual_result, expect_result)
def tearDown(self):