"Fossies" - the Fresh Open Source Software Archive

Member "senlin-8.0.0/senlin/tests/unit/engine/receivers/test_receiver.py" (16 Oct 2019, 16507 Bytes) of package /linux/misc/openstack/senlin-8.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 last Fossies "Diffs" side-by-side code changes report for "test_receiver.py": 6.0.0_vs_7.0.0.

    1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 # not use this file except in compliance with the License. You may obtain
    3 # a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 # License for the specific language governing permissions and limitations
   11 # under the License.
   12 
   13 import mock
   14 from oslo_config import cfg
   15 from oslo_context import context as oslo_ctx
   16 from oslo_utils import timeutils
   17 import six
   18 
   19 from senlin.common import context
   20 from senlin.common import exception
   21 from senlin.common import utils as common_utils
   22 from senlin.drivers import base as driver_base
   23 from senlin.engine.receivers import base as rb
   24 from senlin.engine.receivers import message as rm
   25 from senlin.engine.receivers import webhook as rw
   26 from senlin.objects import credential as co
   27 from senlin.objects import receiver as ro
   28 from senlin.tests.unit.common import base
   29 from senlin.tests.unit.common import utils
   30 
   31 CLUSTER_ID = '2c5139a6-24ba-4a6f-bd53-a268f61536de'
   32 UUID1 = 'aa5f86b8-e52b-4f2b-828a-4c14c770938d'
   33 UUID2 = '60efdaa1-06c2-4fcf-ae44-17a2d85ff3ea'
   34 
   35 
   36 class TestReceiver(base.SenlinTestCase):
   37 
   38     def setUp(self):
   39         super(TestReceiver, self).setUp()
   40         self.context = utils.dummy_context()
   41         self.actor = {
   42             'auth_url': 'TEST_URL',
   43             'user_id': '123',
   44             'password': 'abc'
   45         }
   46         self.params = {
   47             'key1': 'value1',
   48             'key2': 'value2',
   49         }
   50 
   51     def _create_receiver(self, receiver_name, receiver_id=None):
   52         values = {
   53             'id': receiver_id,
   54             'name': receiver_name,
   55             'type': 'webhook',
   56             'cluster_id': CLUSTER_ID,
   57             'action': 'test-action',
   58             'user': self.context.user_id,
   59             'project': self.context.project_id,
   60             'domain': self.context.domain_id,
   61             'created_at': timeutils.utcnow(True),
   62             'updated_at': None,
   63             'actor': self.actor,
   64             'params': self.params,
   65             'channel': None,
   66         }
   67 
   68         return ro.Receiver.create(self.context, values)
   69 
   70     def test_receiver_init(self):
   71         kwargs = {
   72             'id': UUID1,
   73             'name': 'test-receiver',
   74             'user': 'test-user',
   75             'project': 'test-project',
   76             'domain': 'test-domain',
   77             'created_at': timeutils.utcnow(True),
   78             'updated_at': None,
   79             'actor': self.actor,
   80             'params': self.params,
   81             'channel': {'alarm_url': 'http://url1'},
   82         }
   83 
   84         receiver = rb.Receiver('webhook', CLUSTER_ID, 'test-action', **kwargs)
   85 
   86         self.assertEqual(kwargs['id'], receiver.id)
   87         self.assertEqual(kwargs['name'], receiver.name)
   88         self.assertEqual('webhook', receiver.type)
   89         self.assertEqual('test-action', receiver.action)
   90         self.assertEqual(kwargs['user'], receiver.user)
   91         self.assertEqual(kwargs['project'], receiver.project)
   92         self.assertEqual(kwargs['domain'], receiver.domain)
   93 
   94         self.assertEqual(kwargs['created_at'], receiver.created_at)
   95         self.assertEqual(kwargs['updated_at'], receiver.updated_at)
   96 
   97         self.assertEqual(CLUSTER_ID, receiver.cluster_id)
   98         self.assertEqual('test-action', receiver.action)
   99         self.assertEqual(self.actor, receiver.actor)
  100         self.assertEqual(self.params, receiver.params)
  101         self.assertEqual(kwargs['channel'], receiver.channel)
  102 
  103     def test_receiver_init_default_value(self):
  104         receiver = rb.Receiver('webhook', CLUSTER_ID, 'test-action')
  105         self.assertIsNone(receiver.id)
  106         self.assertIsNone(receiver.name)
  107         self.assertEqual('webhook', receiver.type)
  108         self.assertEqual('', receiver.user)
  109         self.assertEqual('', receiver.project)
  110         self.assertEqual('', receiver.domain)
  111 
  112         self.assertIsNone(receiver.created_at)
  113         self.assertIsNone(receiver.updated_at)
  114 
  115         self.assertEqual(CLUSTER_ID, receiver.cluster_id)
  116         self.assertEqual('test-action', receiver.action)
  117         self.assertEqual({}, receiver.actor)
  118         self.assertEqual({}, receiver.params)
  119         self.assertEqual({}, receiver.channel)
  120 
  121     def test_receiver_store(self):
  122         receiver = rb.Receiver('webhook', CLUSTER_ID, 'test-action',
  123                                name='test_receiver_123456',
  124                                project=self.context.project_id)
  125         self.assertIsNone(receiver.id)
  126 
  127         receiver_id = receiver.store(self.context)
  128 
  129         self.assertIsNotNone(receiver_id)
  130         self.assertEqual(receiver_id, receiver.id)
  131 
  132         result = ro.Receiver.get(self.context, receiver_id)
  133 
  134         self.assertIsNotNone(result)
  135         self.assertEqual(receiver_id, result.id)
  136         self.assertEqual(receiver.type, result.type)
  137         self.assertEqual(receiver.name, result.name)
  138         self.assertEqual(receiver.user, result.user)
  139         self.assertEqual(receiver.project, result.project)
  140         self.assertEqual(receiver.domain, result.domain)
  141         self.assertEqual(common_utils.isotime(receiver.created_at),
  142                          common_utils.isotime(result.created_at)),
  143         self.assertEqual(receiver.updated_at, result.updated_at)
  144         self.assertEqual(receiver.action, result.action)
  145         self.assertEqual(receiver.actor, result.actor)
  146         self.assertEqual(receiver.params, result.params)
  147         self.assertEqual(receiver.channel, result.channel)
  148 
  149     @mock.patch.object(co.Credential, 'get')
  150     @mock.patch.object(rw.Webhook, 'initialize_channel')
  151     def test_receiver_create_webhook_admin(self, mock_initialize_channel,
  152                                            mock_c_get):
  153         mock_c_get.return_value = {
  154             'cred': {'openstack': {'trust': '123abc'}}
  155         }
  156         ctx = utils.dummy_context(is_admin=True)
  157         cluster = mock.Mock()
  158         cluster.id = CLUSTER_ID
  159         cluster.user = 'user1'
  160         cluster.project = 'project1'
  161         receiver = rb.Receiver.create(ctx, 'webhook', cluster,
  162                                       'FAKE_ACTION',
  163                                       name='test_receiver_2234')
  164 
  165         self.assertEqual(ctx.user_id, receiver.user)
  166         self.assertEqual(ctx.project_id, receiver.project)
  167         self.assertEqual(ctx.domain_id, receiver.domain)
  168         self.assertEqual('123abc', receiver.actor['trust_id'])
  169         mock_c_get.assert_called_once_with(ctx, 'user1', 'project1')
  170 
  171     @mock.patch.object(rw.Webhook, 'initialize_channel')
  172     def test_receiver_create_webhook_non_admin(self, mock_initialize_channel):
  173         ctx = utils.dummy_context(is_admin=False)
  174         cluster = mock.Mock()
  175         cluster.id = CLUSTER_ID
  176         receiver = rb.Receiver.create(ctx, 'webhook', cluster,
  177                                       'FAKE_ACTION',
  178                                       name='test_receiver_2234')
  179 
  180         self.assertEqual(ctx.user_id, receiver.user)
  181         self.assertEqual(ctx.project_id, receiver.project)
  182         self.assertEqual(ctx.domain_id, receiver.domain)
  183         self.assertIsNone(receiver.actor['trust_id'])
  184 
  185     @mock.patch.object(rm.Message, 'initialize_channel')
  186     def test_receiver_create_message(self, mock_initialize_channel):
  187         receiver = rb.Receiver.create(self.context, 'message', None,
  188                                       None, name='test_receiver_2234')
  189 
  190         self.assertEqual(self.context.user_id, receiver.user)
  191         self.assertEqual(self.context.project_id, receiver.project)
  192         self.assertEqual(self.context.domain_id, receiver.domain)
  193 
  194     def _verify_receiver(self, receiver, result):
  195         self.assertEqual(receiver.id, result.id)
  196         self.assertEqual(receiver.name, result.name)
  197         self.assertEqual(receiver.type, result.type)
  198         self.assertEqual(receiver.user, result.user)
  199         self.assertEqual(receiver.project, result.project)
  200         self.assertEqual(receiver.domain, result.domain)
  201 
  202         self.assertEqual(receiver.created_at, result.created_at)
  203         self.assertEqual(receiver.updated_at, result.updated_at)
  204 
  205         self.assertEqual(receiver.cluster_id, result.cluster_id)
  206         self.assertEqual(receiver.actor, result.actor)
  207         self.assertEqual(receiver.action, result.action)
  208         self.assertEqual(receiver.params, result.params)
  209         self.assertEqual(receiver.channel, result.channel)
  210 
  211     def test_receiver_load_with_id(self):
  212         receiver = self._create_receiver('receiver-1', UUID1)
  213         result = rb.Receiver.load(self.context, receiver_id=receiver.id)
  214         self._verify_receiver(receiver, result)
  215 
  216     def test_receiver_load_with_object(self):
  217         receiver = self._create_receiver('receiver-1', UUID1)
  218         result = rb.Receiver.load(self.context, receiver_obj=receiver)
  219         self._verify_receiver(receiver, result)
  220 
  221     def test_receiver_load_not_found(self):
  222         ex = self.assertRaises(exception.ResourceNotFound,
  223                                rb.Receiver.load,
  224                                self.context, 'fake-receiver', None)
  225         self.assertEqual("The receiver 'fake-receiver' could not "
  226                          "be found.", six.text_type(ex))
  227 
  228     def test_receiver_load_diff_project(self):
  229         receiver = self._create_receiver('receiver-1', UUID1)
  230 
  231         new_context = utils.dummy_context(project='a-different-project')
  232         ex = self.assertRaises(exception.ResourceNotFound,
  233                                rb.Receiver.load,
  234                                new_context, UUID1, None)
  235         self.assertEqual("The receiver '%s' could not be found." % UUID1,
  236                          six.text_type(ex))
  237 
  238         res = rb.Receiver.load(new_context, receiver.id, project_safe=False)
  239         self.assertIsNotNone(res)
  240         self.assertEqual(receiver.id, res.id)
  241 
  242     def test_receiver_to_dict(self):
  243         receiver = self._create_receiver('test-receiver', UUID1)
  244         self.assertIsNotNone(receiver.id)
  245         expected = {
  246             'id': receiver.id,
  247             'name': receiver.name,
  248             'type': receiver.type,
  249             'user': receiver.user,
  250             'project': receiver.project,
  251             'domain': receiver.domain,
  252             'cluster_id': receiver.cluster_id,
  253             'action': receiver.action,
  254             'actor': receiver.actor,
  255             'params': receiver.params,
  256             'created_at': common_utils.isotime(receiver.created_at),
  257             'updated_at': common_utils.isotime(receiver.updated_at),
  258             'channel': None,
  259         }
  260 
  261         result = rb.Receiver.load(self.context, receiver_id=receiver.id)
  262         self.assertEqual(expected, result.to_dict())
  263 
  264     def test_release_channel(self):
  265         receiver = self._create_receiver('test-receiver', UUID1)
  266         receiver = rb.Receiver.load(self.context, UUID1)
  267         res = receiver.release_channel(self.context)
  268         self.assertIsNone(res)
  269 
  270     def test_notify(self):
  271         receiver = self._create_receiver('test-receiver', UUID1)
  272         receiver = rb.Receiver.load(self.context, UUID1)
  273         res = receiver.notify(self.context)
  274         self.assertIsNone(res)
  275 
  276     @mock.patch.object(ro.Receiver, 'delete')
  277     @mock.patch.object(rb.Receiver, 'load')
  278     def test_receiver_delete(self, mock_load, mock_delete):
  279         mock_receiver = mock.Mock()
  280         mock_receiver.id = 'test-receiver-id'
  281         mock_load.return_value = mock_receiver
  282 
  283         rb.Receiver.delete(self.context, 'test-receiver-id')
  284 
  285         mock_load.assert_called_once_with(self.context,
  286                                           receiver_id='test-receiver-id')
  287         mock_receiver.release_channel.assert_called_once_with(self.context)
  288         mock_delete.assert_called_once_with(self.context, 'test-receiver-id')
  289 
  290     @mock.patch.object(context, "get_service_credentials")
  291     @mock.patch.object(driver_base, "SenlinDriver")
  292     def test_get_base_url_succeeded(self, mock_senlin_driver,
  293                                     mock_get_service_creds):
  294         cfg.CONF.set_override('default_region_name', 'RegionOne')
  295         fake_driver = mock.Mock()
  296         fake_kc = mock.Mock()
  297         fake_cred = mock.Mock()
  298         mock_senlin_driver.return_value = fake_driver
  299         fake_driver.identity.return_value = fake_kc
  300         mock_get_service_creds.return_value = fake_cred
  301 
  302         fake_kc.get_senlin_endpoint.return_value = "http://web.com:1234/v1"
  303 
  304         receiver = rb.Receiver(
  305             'webhook', CLUSTER_ID, 'FAKE_ACTION',
  306             id=UUID1, params={'KEY': 884, 'FOO': 'BAR'})
  307 
  308         res = receiver._get_base_url()
  309         self.assertEqual("http://web.com:1234/v1", res)
  310         mock_get_service_creds.assert_called_once_with()
  311         fake_kc.get_senlin_endpoint.assert_called_once_with()
  312 
  313     @mock.patch.object(context, "get_service_credentials")
  314     @mock.patch.object(driver_base, "SenlinDriver")
  315     def test_get_base_url_failed_get_endpoint_exception(
  316             self, mock_senlin_driver, mock_get_service_creds):
  317         cfg.CONF.set_override('default_region_name', 'RegionOne')
  318         fake_driver = mock.Mock()
  319         fake_kc = mock.Mock()
  320         fake_cred = mock.Mock()
  321         mock_senlin_driver.return_value = fake_driver
  322         fake_driver.identity.return_value = fake_kc
  323         mock_get_service_creds.return_value = fake_cred
  324 
  325         fake_kc.get_senlin_endpoint.side_effect = exception.InternalError(
  326             message='Error!')
  327 
  328         receiver = rb.Receiver(
  329             'webhook', CLUSTER_ID, 'FAKE_ACTION',
  330             id=UUID1, params={'KEY': 884, 'FOO': 'BAR'})
  331 
  332         res = receiver._get_base_url()
  333         self.assertIsNone(res)
  334         mock_get_service_creds.assert_called_once_with()
  335         fake_kc.get_senlin_endpoint.assert_called_once_with()
  336 
  337     @mock.patch.object(co.Credential, 'get')
  338     @mock.patch.object(context, 'get_service_credentials')
  339     @mock.patch.object(oslo_ctx, 'get_current')
  340     def test_build_conn_params(self, mock_get_current, mock_get_service_creds,
  341                                mock_cred_get):
  342         user = 'user1'
  343         project = 'project1'
  344         service_cred = {
  345             'auth_url': 'AUTH_URL',
  346             'username': 'senlin',
  347             'user_domain_name': 'default',
  348             'password': '123'
  349         }
  350         current_ctx = {
  351             'auth_url': 'auth_url',
  352             'user_name': user,
  353             'user_domain_name': 'default',
  354             'password': '456'
  355         }
  356         cred_info = {
  357             'openstack': {
  358                 'trust': 'TRUST_ID',
  359             }
  360         }
  361 
  362         cred = mock.Mock()
  363         cred.cred = cred_info
  364         mock_get_service_creds.return_value = service_cred
  365         mock_get_current.return_value = current_ctx
  366         mock_cred_get.return_value = cred
  367 
  368         receiver = self._create_receiver('receiver-1', UUID1)
  369         receiver = rb.Receiver.load(self.context, receiver_obj=receiver)
  370         expected_result = {
  371             'auth_url': 'AUTH_URL',
  372             'username': 'senlin',
  373             'user_domain_name': 'default',
  374             'password': '123',
  375             'trust_id': 'TRUST_ID'
  376         }
  377         res = receiver._build_conn_params(user, project)
  378         self.assertEqual(expected_result, res)
  379         mock_get_service_creds.assert_called_once_with()
  380         mock_cred_get.assert_called_once_with(current_ctx, user, project)
  381 
  382     @mock.patch.object(co.Credential, 'get')
  383     @mock.patch.object(context, 'get_service_credentials')
  384     @mock.patch.object(oslo_ctx, 'get_current')
  385     def test_build_conn_params_trust_not_found(
  386             self, mock_get_current, mock_get_service_creds, mock_cred_get):
  387 
  388         user = 'user1'
  389         project = 'project1'
  390         service_cred = {
  391             'auth_url': 'AUTH_URL',
  392             'username': 'senlin',
  393             'user_domain_name': 'default',
  394             'password': '123'
  395         }
  396 
  397         mock_get_service_creds.return_value = service_cred
  398         mock_cred_get.return_value = None
  399 
  400         receiver = self._create_receiver('receiver-1', UUID1)
  401         receiver = rb.Receiver.load(self.context, receiver_obj=receiver)
  402         ex = self.assertRaises(exception.TrustNotFound,
  403                                receiver._build_conn_params, user, project)
  404         msg = "The trust for trustor 'user1' could not be found."
  405         self.assertEqual(msg, six.text_type(ex))