def test_split_groups_read_on_root_group(self, group_dev_namespace_fixture): with application.test_request_context('/v2/groups/', method='GET') as ctx: response = FlaskResponse( response=json.dumps(group_dev_namespace_fixture), status=HTTPStatus.OK, headers={} ) with RequestsMock() as rsps: rsps.add(method='GET', url=conf.MARATHON_ADDRESSES[0] + '/v2/groups//dev/', body=json.dumps(deepcopy(group_dev_namespace_fixture)), status=200) rsps.add(method='GET', url=conf.MARATHON_ADDRESSES[0] + '/v2/groups//dev/a', body=json.dumps(deepcopy(group_dev_namespace_fixture['groups'][0])), status=200) rsps.add(method='GET', url=conf.MARATHON_ADDRESSES[0] + '/v2/groups//dev/group-b', body=json.dumps(deepcopy(group_dev_namespace_fixture['groups'][1])), status=200) rsps.add(method='GET', url=conf.MARATHON_ADDRESSES[0] + '/v2/groups//dev/group-b/group-b0', body=json.dumps(deepcopy(group_dev_namespace_fixture['groups'][1]['groups'][0])), status=200) rsps.add(method='GET', url=conf.MARATHON_ADDRESSES[0] + '/v2/groups//dev/group-c', body=json.dumps(deepcopy(group_dev_namespace_fixture['groups'][2])), status=200) ctx.request.user = self.user response = Response(ctx.request, response) groups_tuple = list(response.split()) self.assertEqual(5, len(groups_tuple)) expected_groups = [AsgardAppGroup(g) for g in AsgardAppGroup(MarathonGroup.from_json(group_dev_namespace_fixture)).iterate_groups()] # Compara com os groups originais self.assertEqual(expected_groups, [g[1] for g in groups_tuple])
def test_iterate_sub_groups_three_levels(self): """ Grupos: + / + /foo + /bla + /baz """ data = { "id": "/", "groups": [ { "id": "/foo", "apps": [], "groups": [ { "id": "/foo/bar", "apps": [], "groups": [{"id": "/foo/bar/baz"}] }, ] }, ], "apps": [] } group = AsgardAppGroup(MarathonGroup.from_json(data)) self.assertEqual(group.id, "/") expected_all_group_ids = ["/", "/foo", "/foo/bar", "/foo/bar/baz"] returned_groups = list(group.iterate_groups()) self.assertEqual(expected_all_group_ids, [g.id for g in returned_groups])
def split(self) -> Apps: if self.is_read_request(): response_content = json.loads(self.response.data) if self.is_list_apps_request(): for app in response_content['apps']: response_app = AsgardApp.from_json(app) app = self.marathon_client.get_app(self.object_id or response_app.id) yield response_app, app return elif self.is_group_request(): response_group = AsgardAppGroup( MarathonGroup.from_json(response_content)) for current_group in response_group.iterate_groups(): group_id = current_group.id group_id_without_namespace = self._remove_namespace_if_exists( self.request.user.current_account.namespace, group_id) original_group = self._get_original_group( self.request.user, group_id_without_namespace) yield current_group, original_group return elif self.is_tasks_request(): for task in response_content['tasks']: response_task = MarathonTask.from_json(task) yield response_task, response_task return elif self.is_deployment(): content = response_content deployments = (MarathonDeployment.from_json(deploy) for deploy in content) for deployment in deployments: yield deployment, deployment return elif self.is_queue_request(): queue_data = response_content queued_apps = (MarathonQueueItem.from_json(queue_item) for queue_item in queue_data['queue']) for queued_app in queued_apps: yield queued_app, queued_app return else: response_app = AsgardApp.from_json( response_content.get('app') or response_content) app = self.marathon_client.get_app(self.object_id) yield response_app, app return if self.is_write_request(): response_content = json.loads(self.response.data) if 'tasks' in response_content: for task in response_content['tasks']: response_task = MarathonTask.from_json(task) yield response_task, response_task return return yield AsgardApp(), self.marathon_client.get_app(self.app_id)
def split(self) -> Apps: if self.is_read_request(): response_content = json.loads(self.response.data) if self.is_list_apps_request(): all_apps = list( AsgardAppGroup.from_json(response_content).iterate_apps() ) for response_app in all_apps: yield response_app, response_app return elif self.is_group_request(): response_group = AsgardAppGroup( MarathonGroup.from_json(response_content) ) for current_group in response_group.iterate_groups(): yield current_group, current_group return elif self.is_tasks_request(): for task in response_content["tasks"]: response_task = MarathonTask.from_json(task) yield response_task, response_task return elif self.is_deployment(): content = response_content deployments = ( MarathonDeployment.from_json(deploy) for deploy in content ) for deployment in deployments: yield deployment, deployment return elif self.is_queue_request(): queue_data = response_content queued_apps = ( MarathonQueueItem.from_json(queue_item) for queue_item in queue_data["queue"] ) for queued_app in queued_apps: yield queued_app, queued_app return else: response_app = AsgardApp.from_json( response_content.get("app") or response_content ) app = self.marathon_client.get_app(self.object_id) yield response_app, app return if self.is_write_request(): response_content = json.loads(self.response.data) if "tasks" in response_content: for task in response_content["tasks"]: response_task = MarathonTask.from_json(task) yield response_task, response_task return return yield AsgardApp(), self.marathon_client.get_app(self.app_id)
def test_modify_some_apps(self): data = {"id": "/", "apps": [{"id": "/foo"}, {"id": "/bla"}]} group = AsgardAppGroup(MarathonGroup.from_json(data)) apps = list(group.iterate_apps()) apps[0].id = "/foo0" apps[1].id = "/bla0" apps_modified = list(group.iterate_apps()) self.assertEqual(["/foo0", "/bla0"], [app.id for app in apps_modified])
def _get_original_group(self, user, group_id): group_id_with_namespace = "/{}/{}".format( user.current_account.namespace, (group_id or "/").strip("/")) try: return AsgardAppGroup( self.marathon_client.get_group(group_id_with_namespace)) except NotFoundError as e: return AsgardAppGroup( MarathonGroup.from_json({"id": group_id_with_namespace}))
def test_iterate_sub_groups_one_level(self): data = { "id": "/", "groups": [ {"id": "/foo", "apps": []}, {"id": "/bla", "apps": []}, ], "apps": [] } group = AsgardAppGroup(MarathonGroup.from_json(data)) self.assertEqual(group.id, "/") all_group_ids = ["/", "/foo", "/bla"] self.assertEqual(all_group_ids, [g.id for g in group.iterate_groups()])
def test_iterate_group_apps(self): data = { "id": "/", "groups": [{ "id": "/foo", "apps": [{ "id": "/foo/app0" }, { "id": "/foo/app1" }], "groups": [{ "id": "/foo/bar", "apps": [], "groups": [{ "id": "/foo/bar/baz", "apps": [ { "id": "/foo/bar/baz/app0" }, { "id": "/foo/bar/baz/app1" }, ], }], }], }], "apps": [{ "id": "/app0" }, { "id": "/app1" }], } group = AsgardAppGroup(MarathonGroup.from_json(data)) self.assertEqual(group.id, "/") expected_all_apps_ids = [ "/app0", "/app1", "/foo/app0", "/foo/app1", "/foo/bar/baz/app0", "/foo/bar/baz/app1", ] returned_apps = list(group.iterate_apps()) self.assertEqual(expected_all_apps_ids, [g.id for g in returned_apps])
def test_from_json(self): data = { "id": "/", "apps": [], "groups": [], } group = AsgardAppGroup.from_json(data) self.assertEqual("/", group.id)
def test_split_groups_read_on_specific_group(self, group_dev_namespace_fixture): with application.test_request_context("/v2/groups/group-b", method="GET") as ctx: response = FlaskResponse( response=json.dumps(group_dev_namespace_fixture["groups"][1]), status=HTTPStatus.OK, headers={}, ) ctx.request.user = self.user response = Response(ctx.request, response) groups_tuple = list(response.split()) self.assertEqual(2, len(groups_tuple)) expected_groups = [ AsgardAppGroup(g) for g in AsgardAppGroup( MarathonGroup.from_json( group_dev_namespace_fixture["groups"] [1])).iterate_groups() ] # Compara com os groups originais self.assertEqual(expected_groups, [g[1] for g in groups_tuple])
def test_get_original_group_migrated(self): found_group = {"id": "/dev/foo", "apps": [], "groups": []} with application.test_request_context("/v2/groups//foo", method="GET") as ctx: with RequestsMock() as rsps: rsps.add( method="GET", url=conf.MARATHON_ADDRESSES[0] + "/v2/groups//dev/foo", body=json.dumps(found_group), status=200, ) ctx.request.user = self.user request = Request(ctx.request) group = request._get_original_group(self.user, "/foo") self.assertTrue(isinstance(group, AsgardAppGroup)) self.assertEqual(AsgardAppGroup.from_json(found_group), group)
def test_get_original_group_not_found(self): """ Tenta buscar um grupo que não existe. """ with application.test_request_context('/v2/groups//not-found', method='GET') as ctx: with RequestsMock() as rsps: rsps.add(method='GET', url=conf.MARATHON_ADDRESSES[0] + '/v2/groups//dev/not-found', status=404) ctx.request.user = self.user request = Request(ctx.request) group = request._get_original_group(self.user, "/not-found") self.assertEqual( AsgardAppGroup( MarathonGroup.from_json({"id": "/dev/not-found"})), group)
#!/usr/bin/env python # Script que percorre todas as apps e groups de um json (de Grupo) removendo: # app.version # app.fetch # group.version import sys import json from hollowman.marathon.group import AsgardAppGroup from marathon.models.group import MarathonGroup from marathon.util import MarathonJsonEncoder data = open(sys.argv[1]).read() _g = AsgardAppGroup(MarathonGroup.from_json(json.loads(data))) for group in _g.iterate_groups(): del group.version for app in group.apps: del app.version del app.fetch data_output = json.dumps(_g._marathon_group, cls=MarathonJsonEncoder) print(data_output)
def test_iterate_empty_group(self): group = AsgardAppGroup() apps = list(group.iterate_apps()) self.assertEqual(0, len(apps))
def test_eq(self): root_a = AsgardAppGroup.from_json({"id": "/"}) root_b = AsgardAppGroup.from_json({"id": "/"}) self.assertEqual(root_a, root_b)
import sys import json def _patch_docker_params(params): for p in params: for k, v in p.items(): if "=" in v: idx_ = v.index("=") parts = v.split("=") v = parts[0] + "=" + "/sieve" + parts[1] p[k] = v _group = AsgardAppGroup(MarathonGroup.from_json(json.loads(sys.stdin.read()))) for group in _group.iterate_groups(): group.id = "/sieve{}".format(group.id) del group.version print(group.id, file=sys.stderr) for app in group.apps: app.id = "/sieve{}".format(app.id) app.fetch = [] del app.version _patch_docker_params([ p for p in app.container.docker.parameters if p['value'].startswith("hollowman.appname") ]) print(" >", app.id, app.container.docker.parameters, file=sys.stderr)