"Fossies" - the Fresh Open Source Software Archive

Member "masakari-9.0.0/masakari/tests/unit/objects/test_segments.py" (13 May 2020, 12312 Bytes) of package /linux/misc/openstack/masakari-9.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_segments.py": 8.0.0_vs_9.0.0.

    1 #    Copyright 2016 NTT DATA
    2 #    All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 
   16 import copy
   17 from unittest import mock
   18 
   19 from oslo_utils import timeutils
   20 
   21 from masakari.api import utils as api_utils
   22 from masakari import exception
   23 from masakari.objects import fields
   24 from masakari.objects import segment
   25 from masakari.tests.unit.objects import test_objects
   26 from masakari.tests import uuidsentinel
   27 
   28 
   29 NOW = timeutils.utcnow().replace(microsecond=0)
   30 
   31 fake_segment = {
   32     'created_at': NOW,
   33     'updated_at': None,
   34     'deleted_at': None,
   35     'deleted': False,
   36     'id': 123,
   37     'uuid': uuidsentinel.fake_segment,
   38     'name': 'foo-segment',
   39     'service_type': 'COMPUTE',
   40     'description': 'fake-description',
   41     'recovery_method': 'auto'
   42     }
   43 
   44 
   45 class TestFailoverSegmentObject(test_objects._LocalTest):
   46 
   47     @mock.patch('masakari.db.failover_segment_get_by_name')
   48     def test_get_by_name(self, mock_api_get):
   49 
   50         mock_api_get.return_value = fake_segment
   51 
   52         segment_obj = segment.FailoverSegment.get_by_name(self.context,
   53                                                           'foo-segment')
   54         self.compare_obj(segment_obj, fake_segment)
   55 
   56         mock_api_get.assert_called_once_with(self.context, 'foo-segment')
   57 
   58     @mock.patch('masakari.db.failover_segment_get_by_uuid')
   59     def test_get_by_uuid(self, mock_api_get):
   60 
   61         mock_api_get.return_value = fake_segment
   62 
   63         segment_obj = (segment.FailoverSegment.
   64                        get_by_uuid(self.context, uuidsentinel.fake_segment))
   65         self.compare_obj(segment_obj, fake_segment)
   66 
   67         mock_api_get.assert_called_once_with(self.context,
   68                                              uuidsentinel.fake_segment)
   69 
   70     @mock.patch('masakari.db.failover_segment_get_by_id')
   71     def test_get_by_id(self, mock_api_get):
   72 
   73         mock_api_get.return_value = fake_segment
   74         fake_id = 123
   75         segment_obj = segment.FailoverSegment.get_by_id(self.context, fake_id)
   76         self.compare_obj(segment_obj, fake_segment)
   77 
   78         mock_api_get.assert_called_once_with(self.context, fake_id)
   79 
   80     def _segment_create_attribute(self):
   81 
   82         segment_obj = segment.FailoverSegment(context=self.context)
   83         segment_obj.name = 'foo-segment'
   84         segment_obj.description = 'keydata'
   85         segment_obj.service_type = 'fake-user'
   86         segment_obj.recovery_method = 'auto'
   87         segment_obj.uuid = uuidsentinel.fake_segment
   88 
   89         return segment_obj
   90 
   91     @mock.patch.object(api_utils, 'notify_about_segment_api')
   92     @mock.patch('masakari.db.failover_segment_create')
   93     def test_create(self, mock_segment_create, mock_notify_about_segment_api):
   94         mock_segment_create.return_value = fake_segment
   95 
   96         segment_obj = self._segment_create_attribute()
   97         segment_obj.create()
   98         self.compare_obj(segment_obj, fake_segment)
   99 
  100         mock_segment_create.assert_called_once_with(self.context, {
  101             'uuid': uuidsentinel.fake_segment, 'name': 'foo-segment',
  102             'description': 'keydata', 'service_type': 'fake-user',
  103             'recovery_method': 'auto'})
  104         action = fields.EventNotificationAction.SEGMENT_CREATE
  105         phase_start = fields.EventNotificationPhase.START
  106         phase_end = fields.EventNotificationPhase.END
  107         notify_calls = [
  108             mock.call(self.context, segment_obj, action=action,
  109                       phase=phase_start),
  110             mock.call(self.context, segment_obj, action=action,
  111                       phase=phase_end)]
  112         mock_notify_about_segment_api.assert_has_calls(notify_calls)
  113 
  114     @mock.patch.object(api_utils, 'notify_about_segment_api')
  115     @mock.patch('masakari.db.failover_segment_create')
  116     def test_recreate_fails(self, mock_segment_create,
  117                             mock_notify_about_segment_api):
  118         mock_segment_create.return_value = fake_segment
  119 
  120         segment_obj = self._segment_create_attribute()
  121         segment_obj.create()
  122         self.assertRaises(exception.ObjectActionError, segment_obj.create)
  123 
  124         mock_segment_create.assert_called_once_with(self.context, {
  125             'uuid': uuidsentinel.fake_segment, 'name': 'foo-segment',
  126             'description': 'keydata', 'service_type': 'fake-user',
  127             'recovery_method': 'auto'})
  128         action = fields.EventNotificationAction.SEGMENT_CREATE
  129         phase_start = fields.EventNotificationPhase.START
  130         phase_end = fields.EventNotificationPhase.END
  131         notify_calls = [
  132             mock.call(self.context, segment_obj, action=action,
  133                       phase=phase_start),
  134             mock.call(self.context, segment_obj, action=action,
  135                       phase=phase_end)]
  136         mock_notify_about_segment_api.assert_has_calls(notify_calls)
  137 
  138     @mock.patch.object(api_utils, 'notify_about_segment_api')
  139     @mock.patch('masakari.db.failover_segment_delete')
  140     def test_destroy(self, mock_segment_destroy,
  141                      mock_notify_about_segment_api):
  142         segment_obj = self._segment_create_attribute()
  143         segment_obj.id = 123
  144         segment_obj.destroy()
  145 
  146         mock_segment_destroy.assert_called_once_with(
  147             self.context, uuidsentinel.fake_segment)
  148         action = fields.EventNotificationAction.SEGMENT_DELETE
  149         phase_start = fields.EventNotificationPhase.START
  150         phase_end = fields.EventNotificationPhase.END
  151         notify_calls = [
  152             mock.call(self.context, segment_obj, action=action,
  153                       phase=phase_start),
  154             mock.call(self.context, segment_obj, action=action,
  155                       phase=phase_end)]
  156         mock_notify_about_segment_api.assert_has_calls(notify_calls)
  157 
  158     @mock.patch.object(api_utils, 'notify_about_segment_api')
  159     @mock.patch('masakari.db.failover_segment_delete')
  160     def test_destroy_failover_segment_found(self, mock_segment_destroy,
  161                                             mock_notify_about_segment_api):
  162         mock_segment_destroy.side_effect = exception.FailoverSegmentNotFound(
  163             id=123)
  164         segment_obj = self._segment_create_attribute()
  165         segment_obj.id = 123
  166         self.assertRaises(exception.FailoverSegmentNotFound,
  167                           segment_obj.destroy)
  168         action = fields.EventNotificationAction.SEGMENT_DELETE
  169         phase_start = fields.EventNotificationPhase.START
  170         notify_calls = [
  171             mock.call(self.context, segment_obj, action=action,
  172                       phase=phase_start)]
  173         mock_notify_about_segment_api.assert_has_calls(notify_calls)
  174 
  175     @mock.patch('masakari.db.failover_segment_get_all_by_filters')
  176     def test_get_segment_by_recovery_method(self, mock_api_get):
  177         fake_segment2 = copy.deepcopy(fake_segment)
  178         fake_segment2['name'] = 'fake_segment2'
  179 
  180         mock_api_get.return_value = [fake_segment2, fake_segment]
  181 
  182         segment_result = (segment.FailoverSegmentList.
  183                           get_all(self.context,
  184                                   filters={'recovery_method': 'auto'}))
  185         self.assertEqual(2, len(segment_result))
  186         self.compare_obj(segment_result[0], fake_segment2)
  187         self.compare_obj(segment_result[1], fake_segment)
  188         mock_api_get.assert_called_once_with(self.context, filters={
  189             'recovery_method': 'auto'
  190         }, limit=None, marker=None, sort_dirs=None, sort_keys=None)
  191 
  192     @mock.patch('masakari.db.failover_segment_get_all_by_filters')
  193     def test_get_segment_by_service_type(self, mock_api_get):
  194         fake_segment2 = copy.deepcopy(fake_segment)
  195         fake_segment2['name'] = 'fake_segment'
  196 
  197         mock_api_get.return_value = [fake_segment2, fake_segment]
  198 
  199         segment_result = (segment.FailoverSegmentList.
  200                           get_all(self.context,
  201                                   filters={'service_type': 'COMPUTE'}))
  202         self.assertEqual(2, len(segment_result))
  203         self.compare_obj(segment_result[0], fake_segment2)
  204         self.compare_obj(segment_result[1], fake_segment)
  205         mock_api_get.assert_called_once_with(self.context, filters={
  206             'service_type': 'COMPUTE'
  207         }, limit=None, marker=None, sort_dirs=None, sort_keys=None)
  208 
  209     @mock.patch('masakari.db.failover_segment_get_all_by_filters')
  210     def test_get_limit_and_marker_invalid_marker(self, mock_api_get):
  211         segment_name = 'unknown_segment'
  212         mock_api_get.side_effect = exception.MarkerNotFound(marker=segment_name
  213                                                             )
  214 
  215         self.assertRaises(exception.MarkerNotFound,
  216                           segment.FailoverSegmentList.get_all,
  217                           self.context, limit=5, marker=segment_name)
  218 
  219     @mock.patch.object(api_utils, 'notify_about_segment_api')
  220     @mock.patch('masakari.db.failover_segment_update')
  221     def test_save(self, mock_segment_update, mock_notify_about_segment_api):
  222 
  223         mock_segment_update.return_value = fake_segment
  224 
  225         segment_object = segment.FailoverSegment(context=self.context)
  226         segment_object.name = "foo-segment"
  227         segment_object.id = 123
  228         segment_object.uuid = uuidsentinel.fake_segment
  229         segment_object.save()
  230 
  231         self.compare_obj(segment_object, fake_segment)
  232         self.assertTrue(mock_segment_update.called)
  233         action = fields.EventNotificationAction.SEGMENT_UPDATE
  234         phase_start = fields.EventNotificationPhase.START
  235         phase_end = fields.EventNotificationPhase.END
  236         notify_calls = [
  237             mock.call(self.context, segment_object, action=action,
  238                       phase=phase_start),
  239             mock.call(self.context, segment_object, action=action,
  240                       phase=phase_end)]
  241         mock_notify_about_segment_api.assert_has_calls(notify_calls)
  242 
  243     @mock.patch.object(api_utils, 'notify_about_segment_api')
  244     @mock.patch('masakari.db.failover_segment_update')
  245     def test_save_failover_segment_not_found(self, mock_segment_update,
  246                                              mock_notify_about_segment_api):
  247 
  248         mock_segment_update.side_effect = (
  249             exception.FailoverSegmentNotFound(id=uuidsentinel.fake_segment))
  250 
  251         segment_object = segment.FailoverSegment(context=self.context)
  252         segment_object.name = "foo-segment"
  253         segment_object.id = 123
  254         segment_object.uuid = uuidsentinel.fake_segment
  255 
  256         self.assertRaises(exception.FailoverSegmentNotFound,
  257                           segment_object.save)
  258         action = fields.EventNotificationAction.SEGMENT_UPDATE
  259         phase_start = fields.EventNotificationPhase.START
  260         notify_calls = [
  261             mock.call(self.context, segment_object, action=action,
  262                       phase=phase_start)]
  263         mock_notify_about_segment_api.assert_has_calls(notify_calls)
  264 
  265     @mock.patch.object(api_utils, 'notify_about_segment_api')
  266     @mock.patch('masakari.db.failover_segment_update')
  267     def test_save_failover_segment_already_exists(self, mock_segment_update,
  268                                             mock_notify_about_segment_api):
  269 
  270         mock_segment_update.side_effect = (
  271             exception.FailoverSegmentExists(name="foo-segment"))
  272 
  273         segment_object = segment.FailoverSegment(context=self.context)
  274         segment_object.name = "foo-segment"
  275         segment_object.id = 123
  276         segment_object.uuid = uuidsentinel.fake_segment
  277 
  278         self.assertRaises(exception.FailoverSegmentExists, segment_object.save)
  279         action = fields.EventNotificationAction.SEGMENT_UPDATE
  280         phase_start = fields.EventNotificationPhase.START
  281         notify_calls = [
  282             mock.call(self.context, segment_object, action=action,
  283                       phase=phase_start)]
  284         mock_notify_about_segment_api.assert_has_calls(notify_calls)