class SchemaFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = Schema name = factory.LazyAttribute(lambda o: utils.random_string()) service_type = 'secret.credentials' schema = { 'type': 'object', 'properties': { 'domain': { 'title': 'Email Domain', 'type': 'string', 'minLength': 4, 'examples': ['Ex) gmail.com'] }, 'client_id': { 'title': 'Client ID', 'type': 'string', 'minLength': 4, 'examples': ['OAuth 2.0 Client ID'] } }, 'required': ['domain', 'client_id'] } labels = ['aa', 'bb'] tags = [{'key': 'tag_key', 'value': 'tag_value'}] repository = factory.SubFactory(RepositoryFactory) repository_id = factory.LazyAttribute(lambda o: utils.generate_id('repo')) project_id = None domain_id = utils.generate_id('domain') created_at = factory.Faker('date_time') updated_at = factory.Faker('date_time')
def test_update_plugin(self, mock_parse_request, *args): params = { 'storage_id': utils.generate_id('storage'), 'name': 'storage-plugin-update', 'plugin_info': { 'plugin_id': utils.generate_id('storage'), 'version': '3.0', 'options': {}, }, 'tags': { 'update_key': 'update_value' }, 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) storage_servicer = Storage() storage_info = storage_servicer.update_plugin(params, {}) storage_data = MessageToDict(storage_info, preserving_proto_field_name=True) print_message(storage_info, 'test_update_storage_plugin') self.assertIsInstance(storage_info, storage_pb2.StorageInfo) self.assertEqual(storage_info.name, params['name']) self.assertDictEqual(storage_data['tags'], params['tags']) self.assertEqual(storage_info.plugin_info.version, params['plugin_info']['version']) self.assertIsNotNone(storage_info.plugin_info) print(f'[TEST UPDATE STORAGE PLUGIN] {storage_data}')
def test_create_secret(self, mock_parse_request, *args): params = { 'name': utils.random_string(), 'secret_type': 'CREDENTIALS', 'data': {}, 'tags': { 'key': 'value' }, 'service_account_id': utils.generate_id('svc-account'), 'provider': 'aws', 'schema': 'aws_access_key', 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) secret_servicer = Secret() secret_info = secret_servicer.create({}, {}) print_message(secret_info, 'test_create_secret') self.assertEqual(secret_info.name, params['name']) self.assertEqual(secret_info.domain_id, params['domain_id']) self.assertEqual(secret_info.schema, params.get('schema', '')) self.assertEqual(secret_info.provider, params.get('provider', '')) self.assertEqual(secret_info.service_account_id, params.get('service_account_id', '')) self.assertEqual(secret_info.project_id, params.get('project_id', '')) self.assertDictEqual(MessageToDict(secret_info.tags), params['tags']) self.assertIsNone(getattr(secret_info, 'data', None))
class PluginInfoFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = PluginInfo plugin_id = factory.LazyAttribute(lambda o: utils.generate_id('plugin')) version = '1.0' options = {} metadata = { 'data_type': 'PLAIN_TEXT', 'data': { 'schema': { 'properties': { 'phone_number': { 'minLength': 8, 'title': 'Phone Number', 'type': 'string', 'pattern': '^01(?:0|1|[6-9])(\\d{3}|\\d{4})(\\d{4})$' # 'pattern': '^[0-9.\-]{8,15}$' } }, 'required': ['phone_number'], 'type': 'object' } } } secret_id = utils.generate_id('secret')
def test_pin_server_data(self, name=None): """ Create Server by Collector """ self.test_create_server(name) self.server = self.inventory_v1.Server.pin_data( { 'server_id': self.server.server_id, 'keys': [ 'os_type', 'data.hardware', ], 'domain_id': self.domain.domain_id }, metadata=( ('token', self.owner_token), )) self._print_data(self.server, 'test_pin_server_data_1') self._create_region() self.server = self.inventory_v1.Server.update( { 'server_id': self.server.server_id, 'os_type': 'WINDOWS', 'data': { 'hardware': { 'core': 8, 'manufacturer': 'HP', 'memory': 16 }, 'os': { 'os_distro': 'windows2012', # 'os_details': 'Windows 2012 ENT SP2' } }, 'nics': [{ 'ip_addresses': ['192.168.0.3'], 'cidr': '192.168.0.0/24', 'mac_address': 'aa:bb:cc:dd:ee:ff', 'device': 'eth0', 'device_index': 0, 'public_ip_address': '1.1.1.1' }], 'domain_id': self.domain.domain_id }, metadata=( ('token', self.owner_token), ('collector_id', utils.generate_id('collector')), ('service_account_id', utils.generate_id('sa')), ('secret_id', utils.generate_id('secret')), ('job_id', utils.generate_id('job')), )) server_data = MessageToDict(self.server, preserving_proto_field_name=True) self._print_data(self.server, 'test_pin_server_data_2') self.assertEqual(server_data['os_type'], 'LINUX') self.assertEqual(server_data['data']['hardware']['core'], 4) self.assertEqual(server_data['data']['os']['os_distro'], 'windows2012')
class ProtocolFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = Protocol protocol_id = factory.LazyAttribute(lambda o: utils.generate_id('protocol')) name = factory.LazyAttribute(lambda o: utils.random_string()) state = 'ENABLED' protocol_type = 'EXTERNAL' resource_type = 'identity.User' capability = { 'data_type': 'PLAIN_TEXT', 'supported_schema': [ 'slack_webhook' ] } plugin_info = factory.SubFactory(PluginInfoFactory) tags = { 'xxx': 'yy' } domain_id = utils.generate_id('domain') created_at = factory.Faker('date_time')
def test_register_metric_data_source_with_provider(self, mock_plugin_init, mock_list_secrets, mock_get_plugin, *args): plugin_id = utils.generate_id('plugin') plugin_version = '1.0' mock_plugin_init.return_value = { 'metadata': { 'supported_resource_type': ['inventory.Server', 'inventory.CloudService'], 'supported_stat': ['AVERAGE', 'MAX', 'MIN'], 'required_keys': ['reference.resource_id'] } } mock_list_secrets.return_value = { 'results': [{ 'secret_id': utils.generate_id('secret'), 'schema': 'aws_access_key' }], 'total_count': 1 } mock_get_plugin.return_value = { 'capability': { 'use_resource_secret': True, 'supported_schema': ['aws_access_key', 'aws_assume_role'], 'monitoring_type': 'METRIC' }, 'provider': 'aws' } params = { 'name': 'AWS CloudWatch', 'plugin_info': { 'plugin_id': plugin_id, 'version': plugin_version, 'options': {}, 'provider': 'aws' }, 'tags': { 'tag_key': 'tag_value' }, 'domain_id': self.domain_id } self.transaction.method = 'register' data_source_svc = DataSourceService(transaction=self.transaction) data_source_vo = data_source_svc.register(params.copy()) print_data(data_source_vo.to_dict(), 'test_register_metric_data_source_with_provider') DataSourceInfo(data_source_vo) self.assertIsInstance(data_source_vo, DataSource) self.assertEqual(params['name'], data_source_vo.name) self.assertEqual(params['tags'], utils.tags_to_dict(data_source_vo.tags)) self.assertEqual(params['domain_id'], data_source_vo.domain_id)
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)
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'])
class MetricDataFactory(factory.DictFactory): labels = factory.List([ int(time.time()) for _ in range(10) ]) resource_values = { utils.generate_id('server'): [random.randint(0, 20) for _ in range(10)], utils.generate_id('server'): [random.randint(0, 20) for _ in range(10)], utils.generate_id('server'): [random.randint(0, 20) for _ in range(10)] } domain_id = utils.generate_id('domain')
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'])
class NotificationFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = Notification notification_id = factory.LazyAttribute( lambda o: utils.generate_id('notification')) topic = 'topic-a' message = {'message': 'This is TC..'} notification_type = 'ERROR' notification_level = 'ALL' user_id = factory.LazyAttribute(lambda o: utils.generate_id('user')) domain_id = utils.generate_id('domain') created_at = factory.Faker('date_time')
class ProjectFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = Project project_id = factory.LazyAttribute(lambda o: utils.generate_id('project')) name = factory.LazyAttribute(lambda o: utils.random_string()) state = 'ACTIVE' project_group = factory.SubFactory(ProjectGroupFactory) tags = {'key': 'value'} domain_id = utils.generate_id('domain') created_by = factory.Faker('name') created_at = factory.Faker('date_time') deleted_at = None
def test_get_versions(self, mock_parse_request, *args): params = { 'plugin_id': utils.generate_id('plugin'), 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) plugin_servicer = Plugin() versions_info = plugin_servicer.get_versions({}, {}) print_message(versions_info, 'test_get_versions') self.assertIsInstance(versions_info, plugin_pb2.VersionsInfo)
class ServiceAccountFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = ServiceAccount service_account_id = factory.LazyAttribute( lambda o: utils.generate_id('sa')) name = factory.LazyAttribute(lambda o: utils.random_string()) data = {'account_id': '00123456'} provider = 'aws' project = factory.SubFactory(ProjectFactory) project_id = factory.LazyAttribute(lambda o: utils.generate_id('project')) tags = [{'key': 'tag_key', 'value': 'tag_value'}] domain_id = utils.generate_id('domain') created_at = factory.Faker('date_time')
def test_verify_plugin(self, mock_parse_request, *args): params = { 'data_source_id': utils.generate_id('data_source'), 'secret_id': utils.generate_id('secret'), 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) data_source_servicer = DataSource() verify_info = data_source_servicer.verify_plugin({}, {}) print_message(verify_info, 'test_verify_plugin') self.assertIsInstance(verify_info, data_source_pb2.VerifyInfo)
def test_create_server_by_collector(self, name=None): """ Create Server by Collector """ self._create_project_group() self._create_project(project_group_id=self.project_group.project_group_id) self.test_create_server(name, meta=( ('job_id', utils.generate_id('job')), ('collector_id', utils.generate_id('collector')), ('plugin_id', utils.generate_id('plugin')), ('secret.secret_id', utils.generate_id('secret')), ('secret.service_account_id', utils.generate_id('sa')), ('secret.project_id', self.project.project_id), ('secret.provider', 'aws') ))
class ProjectGroupFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = ProjectGroup project_group_id = factory.LazyAttribute(lambda o: utils.generate_id('pg')) name = factory.LazyAttribute(lambda o: utils.random_string()) parent_project_group = None tags = { 'key': 'value' } domain_id = utils.generate_id('domain') created_by = factory.Faker('name') created_at = factory.Faker('date_time')
def test_disable_schedule(self, mock_parse_request, *args): params = { 'schedule_id': utils.generate_id('schedule'), 'state': 'DISABLED', 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) schedule_servicer = Schedule() schedule_info = schedule_servicer.disable({}, {}) print_message(schedule_info, 'test_disable_schedule') self.assertIsInstance(schedule_info, schedule_pb2.ScheduleInfo) self.assertEqual(schedule_info.state, schedule_pb2.ScheduleInfo.State.DISABLED)
def test_verify_plugin(self, mock_parse_request, *args): params = { 'storage_id': utils.generate_id('storage'), 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) storage_servicer = Storage() storage_info = storage_servicer.verify_plugin(params, {}) storage_data = MessageToDict(storage_info, preserving_proto_field_name=True) print_message(storage_info, 'test_deregister_storage_plugin') self.assertIsInstance(storage_info, Empty) print(f'[TEST VERIFY STORAGE PLUGIN] {storage_data}')
class SecretFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = Secret secret_id = utils.generate_id('secret') name = 'AWS Access Key' secret_type = 'credentials' tags = {} schema = None provider = None service_account_id = None project_id = None domain_id = utils.generate_id('domain') created_at = factory.Faker('date_time')
def test_get_storage(self, mock_parse_request, *args): mock_parse_request.return_value = ({}, {}) params = { 'domain_id': utils.generate_id('domain'), 'storage_id': utils.generate_id('storage') } storage_servicer = Storage() storage_info = storage_servicer.get(params, {}) storage_data = MessageToDict(storage_info, preserving_proto_field_name=True) print_message(storage_info, 'test_get_schedule') self.assertIsInstance(storage_info, storage_pb2.StorageInfo) print(f'[TEST GET STORAGE] {storage_data}')
def test_create_service_account(self, mock_parse_request, *args): params = { 'name': utils.random_string(), 'data': { 'account_id': '000123456' }, 'provider': 'aws', 'tags': { 'key': 'value' }, 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) service_account_servicer = ServiceAccount() service_account_info = service_account_servicer.create({}, {}) print_message(service_account_info, 'test_create_service_account') self.assertIsInstance(service_account_info, service_account_pb2.ServiceAccountInfo) self.assertEqual(service_account_info.name, params['name']) self.assertDictEqual(MessageToDict(service_account_info.data), params['data']) self.assertDictEqual(MessageToDict(service_account_info.tags), params['tags']) self.assertEqual(service_account_info.domain_id, params['domain_id']) self.assertIsNotNone(getattr(service_account_info, 'created_at', None))
def stat(self, params): return { 'results': [{ 'project_id': utils.generate_id('project'), 'server_count': 100 }] }
def create(cls, data): create_data = {} unique_fields = [] for name, field in cls._fields.items(): if field.unique: unique_fields.append(name) if name in data: create_data[name] = data[name] else: generate_id = getattr(field, 'generate_id', None) if generate_id: create_data[name] = utils.generate_id(generate_id) if getattr(field, 'auto_now', False): create_data[name] = datetime.utcnow() elif getattr(field, 'auto_now_add', False): create_data[name] = datetime.utcnow() try: new_vo = cls(**create_data).save() except NotUniqueError as e: _raise_unique_error(str(e), unique_fields) raise ERROR_DB_QUERY(reason=e) except DuplicateKeyError as e: _raise_unique_error(str(e), unique_fields) raise ERROR_DB_QUERY(reason=e) except Exception as e: raise ERROR_DB_QUERY(reason=e) return new_vo
def test_create_user_config(self, mock_parse_request, *args): params = { 'name': utils.random_string(), 'data': { 'config_key': 'config_value' }, 'tags': { utils.random_string(): utils.random_string() }, 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) user_config_servicer = UserConfig() user_config_info = user_config_servicer.create({}, {}) print_message(user_config_info, 'test_create_user_config') user_config_data = MessageToDict(user_config_info, preserving_proto_field_name=True) self.assertIsInstance(user_config_info, user_config_pb2.UserConfigInfo) self.assertEqual(user_config_info.name, params['name']) self.assertDictEqual(MessageToDict(user_config_info.data), params['data']) self.assertDictEqual(user_config_data['tags'], params['tags']) self.assertEqual(user_config_info.domain_id, params['domain_id']) self.assertIsNotNone(getattr(user_config_info, 'created_at', None))
def test_update_domain_config(self, mock_parse_request, *args): params = { 'name': utils.random_string(), 'data': { 'update_config_key': 'update_config_value' }, 'schema': utils.random_string(), 'tags': { 'update_key': 'update_value' }, 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) domain_config_servicer = DomainConfig() domain_config_info = domain_config_servicer.update({}, {}) print_message(domain_config_info, 'test_update_domain_config') domain_config_data = MessageToDict(domain_config_info, preserving_proto_field_name=True) self.assertIsInstance(domain_config_info, domain_config_pb2.DomainConfigInfo) self.assertDictEqual(MessageToDict(domain_config_info.data), params['data']) self.assertDictEqual(domain_config_data['tags'], params['tags'])
def test_disable_data_source(self, mock_parse_request, *args): params = { 'data_source_id': utils.generate_id('data_source'), 'state': 'DISABLED', 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) data_source_servicer = DataSource() data_source_info = data_source_servicer.disable({}, {}) print_message(data_source_info, 'test_disable_data_source') self.assertIsInstance(data_source_info, data_source_pb2.DataSourceInfo) self.assertEqual(data_source_info.state, data_source_pb2.DataSourceInfo.State.DISABLED)
def test_update_plugin(self, mock_parse_request, *args): params = { 'data_source_id': utils.generate_id('data_source'), 'domain_id': utils.generate_id('domain'), 'version': "1.1", 'options': {} } mock_parse_request.return_value = (params, {}) data_source_servicer = DataSource() data_source_info = data_source_servicer.update_plugin({}, {}) print_message(data_source_info, 'test_update_data_source') data_source_data = MessageToDict(data_source_info, preserving_proto_field_name=True) self.assertIsInstance(data_source_info, data_source_pb2.DataSourceInfo) self.assertEqual(data_source_info.plugin_info.version, "1.1")
def test_create_policy(self, mock_parse_request, *args): params = { 'name': utils.random_string(), 'permissions': [ 'inventory.Region.*', 'inventory.Server.*', 'inventory.CloudServiceType.*', 'inventory.CloudService.*', 'inventory.Collector.get', 'inventory.Collector.list', ], 'labels': ['cc', 'dd'], 'tags': { utils.random_string(): utils.random_string() }, 'domain_id': utils.generate_id('domain') } mock_parse_request.return_value = (params, {}) policy_servicer = Policy() policy_info = policy_servicer.create({}, {}) print_message(policy_info, 'test_create_policy') policy_data = MessageToDict(policy_info, preserving_proto_field_name=True) self.assertIsInstance(policy_info, policy_pb2.PolicyInfo) self.assertEqual(policy_info.name, params['name']) self.assertEqual(list(policy_info.permissions), params['permissions']) self.assertListEqual(list(policy_info.labels), params['labels']) self.assertDictEqual(policy_data['tags'], params['tags']) self.assertEqual(policy_info.domain_id, params['domain_id']) self.assertIsNotNone(getattr(policy_info, 'created_at', None))