Ejemplo n.º 1
0
    def test_stat_protocol(self, *args):
        protocol_vos = ProtocolFactory.build_batch(10, domain_id=self.domain_id)
        list(map(lambda vo: vo.save(), protocol_vos))

        params = {
            'domain_id': self.domain_id,
            'query': {
                'aggregate': [{
                    'group': {
                        'keys': [{
                            'key': 'protocol_id',
                            'name': 'Id'
                        }],
                        'fields': [{
                            'operator': 'count',
                            'name': 'Count'
                        }]
                    }
                }, {
                    'sort': {
                        'key': 'Count',
                        'desc': True
                    }
                }]
            }
        }

        self.transaction.method = 'stat'
        protocol_svc = ProtocolService(transaction=self.transaction)
        values = protocol_svc.stat(params)
        StatisticsInfo(values)

        print_data(values, 'test_stat_protocol')
Ejemplo n.º 2
0
    def test_create_protocol(self, mock_plugin_verify, mock_list_secrets,
                             mock_get_plugin, *args):
        secret_id = utils.generate_id('secret')
        plugin_id = utils.generate_id('plugin')
        plugin_version = '1.0'

        mock_plugin_verify.return_value = {
            'metadata': {
                'supported_schema': ['slack_webhook', 'spaceone_user'],
                'data_type': 'PLAIN_TEXT'
            }
        }

        mock_list_secrets.return_value = {
            'results': [{
                'secret_id': secret_id,
                'schema': 'aws_access_key'
            }],
            'total_count': 1
        }

        mock_get_plugin.return_value = {
            'name': 'notification-slack-protocol',
            'service_type': 'notification.Protocol',
            'image': 'pyengine/notification-slack-protocol',
            'capability': {
                'supported_schema': ['slack_webhook', 'spaceone_user'],
                'data_type': 'PLAIN_TEXT'
            },
            'tags': {
                'description': 'Notification Slack Protocol',
                'spaceone:plugin_name': 'notification-slack-protocol'
            }
        }

        params = {
            'name': 'Slack Notification',
            'plugin_info': {
                'plugin_id': plugin_id,
                'version': plugin_version,
                'options': {},
            },
            'tags': {
                utils.random_string(): utils.random_string()
            },
            'domain_id': self.domain_id
        }

        self.transaction.method = 'create'
        protocol_svc = ProtocolService(transaction=self.transaction)
        protocol_vo = protocol_svc.create(params.copy())

        print_data(protocol_vo.to_dict(), 'test_create_protocol')
        ProtocolInfo(protocol_vo)

        self.assertIsInstance(protocol_vo, Protocol)
        self.assertEqual(params['name'], protocol_vo.name)
        self.assertEqual(params['tags'], protocol_vo.tags)
        self.assertEqual(params['domain_id'], protocol_vo.domain_id)
Ejemplo n.º 3
0
    def test_update_protocol_plugin(self, mock_plugin_init, mock_list_secrets, *args):
        plugin_version = '1.2'
        update_options = {
            'test': 'xxxxx'
        }

        mock_plugin_init.return_value = {
            'metadata': {
                'data_type': 'PLAIN_TEXT',
                'data': {
                    'schema': {
                        'properties': {
                            'phone_number': {
                                'minLength': 10,
                                'title': 'Phone Number',
                                'type': 'string',
                                'pattern': '^01(?:0|1|[6-9])[.-]?(\\d{3}|\\d{4}[.-]?(\\d{4})$'
                            }
                        },
                        'required': [
                            'phone_number'
                        ],
                        'type': 'object'
                    }
                }
            }
        }

        mock_list_secrets.return_value = {
            'results': [{
                'secret_id': utils.generate_id('secret')
            }],
            'total_count': 1
        }

        protocol_vo = ProtocolFactory(domain_id=self.domain_id)
        params = {
            'protocol_id': protocol_vo.protocol_id,
            'version': plugin_version,
            'options': update_options,
            'domain_id': self.domain_id
        }

        self.transaction.method = 'update_plugin'
        protocol_svc = ProtocolService(transaction=self.transaction)
        new_protocol_vo = protocol_svc.update_plugin(params.copy())

        print_data(new_protocol_vo.to_dict(), 'test_update_protocol_plugin')
        ProtocolInfo(new_protocol_vo)

        self.assertIsInstance(new_protocol_vo, Protocol)
        self.assertEqual(new_protocol_vo.protocol_id, protocol_vo.protocol_id)
        self.assertEqual(params['version'], new_protocol_vo.plugin_info.version)
        self.assertEqual(params['options'], new_protocol_vo.plugin_info.options)
Ejemplo n.º 4
0
    def test_delete_protocol(self, *args):
        protocol_vo = ProtocolFactory(domain_id=self.domain_id)
        params = {
            'protocol_id': protocol_vo.protocol_id,
            'domain_id': self.domain_id
        }

        self.transaction.method = 'delete'
        protocol_svc = ProtocolService(transaction=self.transaction)
        result = protocol_svc.delete(params)

        self.assertIsNone(result)
Ejemplo n.º 5
0
    def test_get_protocol(self, *args):
        protocol_vo = ProtocolFactory(domain_id=self.domain_id)
        params = {
            'protocol_id': protocol_vo.protocol_id,
            'domain_id': self.domain_id
        }

        self.transaction.method = 'get'
        protocol_svc = ProtocolService(transaction=self.transaction)
        protocol_vo = protocol_svc.get(params)

        print_data(protocol_vo.to_dict(), 'test_get_protocol')
        ProtocolInfo(protocol_vo)

        self.assertIsInstance(protocol_vo, Protocol)
Ejemplo n.º 6
0
    def test_list_protocols_by_name(self, *args):
        protocol_vos = ProtocolFactory.build_batch(10,
                                                   domain_id=self.domain_id)
        list(map(lambda vo: vo.save(), protocol_vos))

        params = {'name': protocol_vos[0].name, 'domain_id': self.domain_id}

        self.transaction.method = 'list'
        protocol_svc = ProtocolService(transaction=self.transaction)
        protocol_vos, total_count = protocol_svc.list(params)
        ProtocolsInfo(protocol_vos, total_count)

        self.assertEqual(len(protocol_vos), 1)
        self.assertIsInstance(protocol_vos[0], Protocol)
        self.assertEqual(total_count, 1)
Ejemplo n.º 7
0
    def test_disable_protocol(self, *args):
        protocol_vo = ProtocolFactory(domain_id=self.domain_id, state='ENABLED')
        params = {
            'protocol_id': protocol_vo.protocol_id,
            'domain_id': self.domain_id
        }

        self.transaction.method = 'disable'
        protocol_svc = ProtocolService(transaction=self.transaction)
        updated_protocol_vo = protocol_svc.disable(params.copy())

        print_data(updated_protocol_vo.to_dict(), 'test_disable_protocol')
        ProtocolInfo(updated_protocol_vo)

        self.assertIsInstance(updated_protocol_vo, Protocol)
        self.assertEqual(updated_protocol_vo.protocol_id, protocol_vo.protocol_id)
        self.assertEqual('DISABLED', updated_protocol_vo.state)
Ejemplo n.º 8
0
    def test_list_protocols_by_protocol_type(self, *args):
        internal_protocol_vos = ProtocolFactory.build_batch(
            5, protocol_type='INTERNAL', domain_id=self.domain_id)
        external_protocol_vos = ProtocolFactory.build_batch(
            10, protocol_type='EXTERNAL', domain_id=self.domain_id)
        list(map(lambda vo: vo.save(), external_protocol_vos))

        params = {'protocol_type': 'EXTERNAL', 'domain_id': self.domain_id}

        self.transaction.method = 'list'
        protocol_svc = ProtocolService(transaction=self.transaction)
        protocol_vos, total_count = protocol_svc.list(params)
        ProtocolsInfo(protocol_vos, total_count)

        self.assertEqual(len(protocol_vos), 10)
        self.assertIsInstance(protocol_vos[0], Protocol)
        self.assertEqual(total_count, 10)
Ejemplo n.º 9
0
    def test_update_protocol_plugin(self, mock_plugin_init, mock_list_secrets,
                                    *args):
        plugin_version = '1.2'
        update_options = {'test': 'xxxxx'}

        mock_plugin_init.return_value = {
            'metadata': {
                'supported_resource_type': ['inventory.Server'],
                'supported_stat': ['AVERAGE', 'MAX', 'MIN'],
                'required_keys': ['reference.resource_id']
            }
        }

        mock_list_secrets.return_value = {
            'results': [{
                'secret_id': utils.generate_id('secret')
            }],
            'total_count': 1
        }

        protocol_vo = ProtocolFactory(domain_id=self.domain_id)
        params = {
            'protocol_id': protocol_vo.protocol_id,
            'version': plugin_version,
            'options': update_options,
            'domain_id': self.domain_id
        }

        self.transaction.method = 'update_plugin'
        protocol_svc = ProtocolService(transaction=self.transaction)
        new_protocol_vo = protocol_svc.update_plugin(params.copy())

        print_data(new_protocol_vo.to_dict(), 'test_update_protocol_plugin')
        ProtocolInfo(new_protocol_vo)

        self.assertIsInstance(new_protocol_vo, Protocol)
        self.assertEqual(new_protocol_vo.protocol_id, protocol_vo.protocol_id)
        self.assertEqual(params['version'],
                         new_protocol_vo.plugin_info.version)
        self.assertEqual(params['options'],
                         new_protocol_vo.plugin_info.options)
Ejemplo n.º 10
0
    def test_stat_protocol_distinct(self, *args):
        protocol_vos = ProtocolFactory.build_batch(10, domain_id=self.domain_id)
        list(map(lambda vo: vo.save(), protocol_vos))

        params = {
            'domain_id': self.domain_id,
            'query': {
                'distinct': 'protocol_id',
                'page': {
                    'start': 2,
                    'limit': 3
                }
            }
        }

        self.transaction.method = 'stat'
        protocol_svc = ProtocolService(transaction=self.transaction)
        values = protocol_svc.stat(params)
        StatisticsInfo(values)

        print_data(values, 'test_stat_protocol_distinct')
Ejemplo n.º 11
0
    def test_update_protocol(self, *args):
        protocol_vo = ProtocolFactory(domain_id=self.domain_id)
        params = {
            'protocol_id': protocol_vo.protocol_id,
            'name': 'Update New Protocol',
            'tags': {
                'update_key': 'update_value'
            },
            'domain_id': self.domain_id
        }

        self.transaction.method = 'update'
        protocol_svc = ProtocolService(transaction=self.transaction)
        update_protocol_vo = protocol_svc.update(params.copy())

        print_data(update_protocol_vo.to_dict(), 'test_update_protocol')
        ProtocolInfo(update_protocol_vo)

        self.assertIsInstance(update_protocol_vo, Protocol)
        self.assertEqual(update_protocol_vo.protocol_id, protocol_vo.protocol_id)
        self.assertEqual(params['name'], update_protocol_vo.name)
        self.assertEqual(params['tags'], update_protocol_vo.tags)