Esempio n. 1
0
    def test_list_data_sources_by_tag(self, *args):
        DataSourceFactory(tags=[{
            'key': 'tag_key_1',
            'value': 'tag_value_1'
        }],
                          domain_id=self.domain_id)
        data_source_vos = DataSourceFactory.build_batch(
            9, domain_id=self.domain_id)
        list(map(lambda vo: vo.save(), data_source_vos))

        params = {
            'query': {
                'filter': [{
                    'k': 'tags.tag_key_1',
                    'v': 'tag_value_1',
                    'o': 'eq'
                }]
            },
            'domain_id': self.domain_id
        }

        self.transaction.method = 'list'
        data_source_svc = DataSourceService(transaction=self.transaction)
        data_sources_vos, total_count = data_source_svc.list(params)
        DataSourcesInfo(data_source_vos, total_count)

        self.assertEqual(len(data_sources_vos), 1)
        self.assertIsInstance(data_sources_vos[0], DataSource)
        self.assertEqual(total_count, 1)
Esempio n. 2
0
 def update_plugin(self, params):
     params['plugin_info'] = {}
     params['plugin_info']['version'] = params.get('version')
     params['plugin_info']['options'] = params.get('options')
     del params['version']
     del params['options']
     return DataSourceFactory(**params)
Esempio n. 3
0
    def test_stat_data_source(self, *args):
        data_source_vos = DataSourceFactory.build_batch(
            10, domain_id=self.domain_id)
        list(map(lambda vo: vo.save(), data_source_vos))

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

        self.transaction.method = 'stat'
        data_source_svc = DataSourceService(transaction=self.transaction)
        values = data_source_svc.stat(params)
        StatisticsInfo(values)

        print_data(values, 'test_stat_data_source')
Esempio n. 4
0
    def test_list_server_logs(self, mock_list_logs, mock_list_secrets,
                              mock_get_server, *args):
        server_id = utils.generate_id('server')
        end = datetime.utcnow()
        start = end - timedelta(days=1)

        mock_list_logs.return_value.__iter__ = lambda response: iter([{
            'resource_type':
            'monitoring.Log',
            'result': {
                'logs': [{
                    'key1': 'value1',
                    'key2': 'value2',
                    'key3': 'value3'
                }]
            }
        }])

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

        mock_get_server.return_value = {
            'server_id': server_id,
            'reference': {
                'resource_id':
                'arn:aws:ec2:ap-northeast-2:123456789012:instance/i-1234'
            },
            'collection_info': {
                'secrets': [utils.generate_id('secret')]
            }
        }

        new_data_source_vo = DataSourceFactory(domain_id=self.domain_id)
        params = {
            'data_source_id': new_data_source_vo.data_source_id,
            'resource_type': 'inventory.Server',
            'resource_id': server_id,
            'start': start.isoformat(),
            'end': end.isoformat(),
            'sort': {
                'key': 'EventId',
                'desc': False
            },
            'limit': 10,
            'domain_id': self.domain_id
        }

        self.transaction.method = 'list'
        log_svc = LogService(transaction=self.transaction)
        logs_data_info = log_svc.list(params.copy())

        print_data(logs_data_info, 'test_list_logs')
        LogDataInfo(logs_data_info)

        self.assertEqual(params['domain_id'], logs_data_info['domain_id'])
Esempio n. 5
0
    def test_deregister_data_source(self, *args):
        new_data_source_vo = DataSourceFactory(domain_id=self.domain_id)
        params = {
            'data_source_id': new_data_source_vo.data_source_id,
            'domain_id': self.domain_id
        }

        self.transaction.method = 'deregister'
        data_source_svc = DataSourceService(transaction=self.transaction)
        result = data_source_svc.deregister(params)

        self.assertIsNone(result)
Esempio n. 6
0
    def test_update_data_source(self, mock_plugin_verify, mock_list_secrets,
                                *args):
        mock_plugin_verify.return_value.__iter__ = lambda response: iter([{
            'resource_type':
            'monitoring.DataSource',
            'result': {
                'options': {
                    'supported_resource_type': ['inventory.Server'],
                    'supported_stat': ['AVERAGE', 'MAX', 'MIN'],
                    'reference_keys': [{
                        'resource_type': 'inventory.Server',
                        'reference_key': 'reference.resource_id'
                    }]
                }
            }
        }])

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

        new_data_source_vo = DataSourceFactory(domain_id=self.domain_id)
        params = {
            'data_source_id': new_data_source_vo.data_source_id,
            'name': 'Update AWS CloudWatch',
            'plugin_info': {
                'plugin_id': new_data_source_vo.plugin_info.plugin_id,
                'version': '2.0',
                'options': {},
                'provider': 'aws'
            },
            'tags': {
                'update_key': 'update_value'
            },
            'domain_id': self.domain_id
        }

        self.transaction.method = 'update'
        data_source_svc = DataSourceService(transaction=self.transaction)
        data_source_vo = data_source_svc.update(params.copy())

        print_data(data_source_vo.to_dict(), 'test_update_data_source')
        DataSourceInfo(data_source_vo)

        self.assertIsInstance(data_source_vo, DataSource)
        self.assertEqual(new_data_source_vo.data_source_id,
                         data_source_vo.data_source_id)
        self.assertEqual(params['name'], data_source_vo.name)
        self.assertEqual(params.get('tags', {}), data_source_vo.tags)
    def test_get_metric_data(self, mock_get_metric_data, mock_list_secrets, mock_list_servers, *args):
        server_id_1 = utils.generate_id('server')
        server_id_2 = utils.generate_id('server')
        end = datetime.utcnow()
        start = end - timedelta(days=1)
        start_ts = int(time.mktime(start.timetuple()))

        mock_get_metric_data.return_value = {
            'labels': [start_ts, start_ts + 3600, start_ts + (3600 * 2), start_ts + (3600 * 3),
                       start_ts + (3600 * 4)],
            'values': [1, 2, 3, 4, 5]
        }

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

        mock_list_servers.return_value = {
            'results': [{
                'server_id': server_id_1,
                'reference': {'resource_id': 'arn:aws:ec2:ap-northeast-2:123456789012:instance/i-1234'},
                'collection_info': {'secrets': [utils.generate_id('secret')]}
            }, {
                'server_id': server_id_2,
                'reference': {'resource_id': 'arn:aws:ec2:ap-northeast-2:123456789012:instance/i-4567'},
                'collection_info': {'secrets': [utils.generate_id('secret')]}
            }],
            'total_count': 2
        }

        new_data_source_vo = DataSourceFactory(domain_id=self.domain_id)
        params = {
            'data_source_id': new_data_source_vo.data_source_id,
            'resource_type': 'inventory.Server',
            'resources': [server_id_1, server_id_2],
            'metric': 'cpu',
            'start': start.isoformat(),
            'end': end.isoformat(),
            'domain_id': self.domain_id
        }

        self.transaction.method = 'get_data'
        metric_svc = MetricService(transaction=self.transaction)
        metrics_data_info = metric_svc.get_data(params.copy())

        print_data(metrics_data_info, 'test_get_metric_data')
        MetricDataInfo(metrics_data_info)

        self.assertEqual(params['domain_id'], metrics_data_info['domain_id'])
Esempio n. 8
0
    def test_list_data_sources_by_monitoring_type(self, *args):
        data_source_vos = DataSourceFactory.build_batch(
            10, monitoring_type='METRIC', domain_id=self.domain_id)
        list(map(lambda vo: vo.save(), data_source_vos))

        params = {'monitoring_type': 'METRIC', 'domain_id': self.domain_id}

        self.transaction.method = 'list'
        data_source_svc = DataSourceService(transaction=self.transaction)
        data_sources_vos, total_count = data_source_svc.list(params)
        DataSourcesInfo(data_source_vos, total_count)

        self.assertEqual(len(data_sources_vos), 10)
        self.assertIsInstance(data_sources_vos[0], DataSource)
        self.assertEqual(total_count, 10)
Esempio n. 9
0
    def test_get_data_source(self, *args):
        new_data_source_vo = DataSourceFactory(domain_id=self.domain_id)
        params = {
            'data_source_id': new_data_source_vo.data_source_id,
            'domain_id': self.domain_id
        }

        self.transaction.method = 'get'
        data_source_svc = DataSourceService(transaction=self.transaction)
        data_source_vo = data_source_svc.get(params)

        print_data(data_source_vo.to_dict(), 'test_get_data_source')
        DataSourceInfo(data_source_vo)

        self.assertIsInstance(data_source_vo, DataSource)
Esempio n. 10
0
    def test_disable_data_source(self, *args):
        new_data_source_vo = DataSourceFactory(domain_id=self.domain_id, state='ENABLED')
        params = {
            'data_source_id': new_data_source_vo.data_source_id,
            'domain_id': self.domain_id
        }

        self.transaction.method = 'disable'
        data_source_svc = DataSourceService(transaction=self.transaction)
        data_source_vo = data_source_svc.disable(params.copy())

        print_data(data_source_vo.to_dict(), 'test_disable_data_source')
        DataSourceInfo(data_source_vo)

        self.assertIsInstance(data_source_vo, DataSource)
        self.assertEqual(new_data_source_vo.data_source_id, data_source_vo.data_source_id)
        self.assertEqual('DISABLED', data_source_vo.state)
Esempio n. 11
0
    def test_stat_data_source_distinct(self, *args):
        data_source_vos = DataSourceFactory.build_batch(10, domain_id=self.domain_id)
        list(map(lambda vo: vo.save(), data_source_vos))

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

        self.transaction.method = 'stat'
        data_source_svc = DataSourceService(transaction=self.transaction)
        values = data_source_svc.stat(params)
        StatisticsInfo(values)

        print_data(values, 'test_stat_data_source_distinct')
Esempio n. 12
0
    def test_list_metrics(self, mock_list_metrics, mock_list_secrets,
                          mock_list_servers, *args):
        server_id_1 = utils.generate_id('server')
        server_id_2 = utils.generate_id('server')
        server_id_3 = utils.generate_id('server')

        mock_list_metrics.return_value = {
            'resource_type': 'monitoring.Metric',
            'result': {
                'metrics': [{
                    'key': 'cpu',
                    'name': 'CPU Utilization',
                    'unit': {
                        'x': 'Datetime',
                        'y': '%'
                    },
                    'chart_type': 'line'
                }, {
                    'key': 'memory',
                    'name': 'Memory Usage',
                    'unit': {
                        'x': 'Datetime',
                        'y': '%'
                    },
                    'chart_type': 'line'
                }]
            }
        }

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

        mock_list_servers.return_value = {
            'results': [{
                'server_id': server_id_1,
                'reference': {
                    'resource_id':
                    'arn:aws:ec2:ap-northeast-2:123456789012:instance/i-1234'
                },
                'collection_info': {
                    'secrets': [utils.generate_id('secret')]
                }
            }, {
                'server_id': server_id_2,
                'reference': {
                    'resource_id':
                    'arn:aws:ec2:ap-northeast-2:123456789012:instance/i-4567'
                },
                'collection_info': {
                    'secrets': [utils.generate_id('secret')]
                }
            }],
            'total_count':
            2
        }

        new_data_source_vo = DataSourceFactory(domain_id=self.domain_id)
        params = {
            'data_source_id': new_data_source_vo.data_source_id,
            'resource_type': 'inventory.Server',
            'resources': [server_id_1, server_id_2, server_id_3],
            'domain_id': self.domain_id
        }

        self.transaction.method = 'list'
        metric_svc = MetricService(transaction=self.transaction)
        metrics_info = metric_svc.list(params.copy())

        print_data(metrics_info, 'test_list_metrics')
        MetricsInfo(metrics_info)

        self.assertEqual(params['domain_id'], metrics_info['domain_id'])
Esempio n. 13
0
 def list(self, params):
     return DataSourceFactory.build_batch(10, **params), 10
Esempio n. 14
0
 def update(self, params):
     return DataSourceFactory(**params)
Esempio n. 15
0
 def disable(self, params):
     return DataSourceFactory(**params)
Esempio n. 16
0
    def update(self, params):
        params = copy.deepcopy(params)
        if 'tags' in params:
            params['tags'] = utils.dict_to_tags(params['tags'])

        return DataSourceFactory(**params)
Esempio n. 17
0
 def get(self, params):
     return DataSourceFactory(**params)
Esempio n. 18
0
 def register(self, params):
     return DataSourceFactory(**params)