def test_print_table_with_mocked_terminal(capsys): (flexmock(osbs.cli.render).should_receive('get_terminal_size').and_return( 25, 80).once()) short_data = [{ 'x': 'Header1', 'y': 'Header2', 'z': 'Header3' }, { 'x': 'x' * 8, 'y': 'y' * 20, 'z': 'z' * 4 }] p = TablePrinter(short_data, ["x", "y", "z"]) p.render() out, err = capsys.readouterr() expected_header = """ Header1 | Header2 | Header3 -----------------------+----------------------------------+--------------------- """.lstrip('\n') # noqa:W291 expected_data = """ xxxxxxxx | yyyyyyyyyyyyyyyyyyyy | zzzz """.lstrip('\n') # noqa:W291 assert err == expected_header assert out == expected_data
def cmd_list_builds(args, osbs): kwargs = {} if args.running: kwargs['running'] = args.running if args.from_json: with open(args.from_json) as fp: builds = [BuildResponse(build) for build in json.load(fp)] else: builds = osbs.list_builds(**kwargs) if args.output == 'json': json_output = [] for build in builds: json_output.append(build.json) print_json_nicely(json_output) elif args.output == 'text': if args.columns: cols_to_display = args.columns.split(",") else: cols_to_display = CLI_LIST_BUILDS_DEFAULT_COLS data = [{ "base_image": "BASE IMAGE NAME", "base_image_id": "BASE IMAGE ID", "commit": "COMMIT", "image": "IMAGE NAME", "unique_image": "UNIQUE IMAGE NAME", "image_id": "IMAGE ID", "koji_build_id": "KOJI BUILD ID", "name": "BUILD ID", "status": "STATUS", "time_created": "TIME CREATED", }] for build in sorted(builds, key=lambda x: x.get_time_created_in_seconds()): unique_image = build.get_image_tag() try: image = strip_registry_from_image( build.get_repositories()["primary"][0]) except (TypeError, KeyError, IndexError): image = "" # "" or unique_image? failed builds don't have that ^ if args.FILTER and args.FILTER not in image: continue if args.running and not build.is_in_progress(): continue b = { "base_image": build.get_base_image_name() or '', "base_image_id": build.get_base_image_id() or '', "commit": build.get_commit_id(), "image": image, "unique_image": unique_image, "image_id": build.get_image_id() or '', "koji_build_id": build.get_koji_build_id() or '', "name": build.get_build_name(), "status": build.status, "time_created": build.get_time_created(), } data.append(b) tp = TablePrinter(data, cols_to_display) tp.render()
def cmd_list_builds(args, osbs): kwargs = {} if args.running: field_selector = ",".join(["status!={status}".format(status=status.capitalize()) for status in BUILD_FINISHED_STATES]) kwargs['field_selector'] = field_selector if args.from_json: with open(args.from_json) as fp: builds = [BuildResponse(build) for build in json.load(fp)] else: builds = osbs.list_builds(**kwargs) if args.output == 'json': json_output = [] for build in builds: json_output.append(build.json) print_json_nicely(json_output) elif args.output == 'text': if args.columns: cols_to_display = args.columns.split(",") else: cols_to_display = CLI_LIST_BUILDS_DEFAULT_COLS data = [{ "base_image": "BASE IMAGE NAME", "base_image_id": "BASE IMAGE ID", "commit": "COMMIT", "image": "IMAGE NAME", "unique_image": "UNIQUE IMAGE NAME", "image_id": "IMAGE ID", "name": "BUILD ID", "status": "STATUS", "time_created": "TIME CREATED", }] for build in sorted(builds, key=lambda x: x.get_time_created_in_seconds()): unique_image = build.get_image_tag() try: image = strip_registry_from_image(build.get_repositories()["primary"][0]) except (TypeError, KeyError, IndexError): image = "" # "" or unique_image? failed builds don't have that ^ if args.FILTER: if args.FILTER not in image: continue if args.running and not build.is_in_progress(): continue b = { "base_image": build.get_base_image_name() or '', "base_image_id": build.get_base_image_id() or '', "commit": build.get_commit_id(), "image": image, "unique_image": unique_image, "image_id": build.get_image_id() or '', "name": build.get_build_name(), "status": build.status, "time_created": build.get_time_created(), } data.append(b) tp = TablePrinter(data, cols_to_display) tp.render()
def cmd_watch_builds(args, osbs): field_selector = ",".join([ "status!={status}".format(status=status.capitalize()) for status in BUILD_FINISHED_STATES ]) cols_to_display = CLI_WATCH_BUILDS_DEFAULT_COLS if args.columns: cols_to_display = args.columns.split(",") data = [{ "changetype": "CHANGE", "status": "STATUS", "created": "CREATED", "name": "NAME", }] for changetype, obj in osbs.watch_builds(field_selector=field_selector): try: name = obj['metadata']['name'] except KeyError: logger.error("'object' doesn't have any name") continue else: try: status = obj['status']['phase'] except KeyError: status = '(not reported)' try: timestamp = obj['metadata']['creationTimestamp'] except KeyError: created = '(not reported)' else: created = time.ctime(get_time_from_rfc3339(timestamp)) b = { "changetype": changetype, "name": name or '', "status": status, "created": created, } data.append(b) if args.output == 'json': print(json.dumps(b)) sys.stdout.flush() elif args.output == 'text': tp = TablePrinter(data, cols_to_display) tp.render()
def cmd_watch_builds(args, osbs): field_selector = ",".join(["status!={status}".format(status=status.capitalize()) for status in BUILD_FINISHED_STATES]) cols_to_display = CLI_WATCH_BUILDS_DEFAULT_COLS if args.columns: cols_to_display = args.columns.split(",") data = [{ "changetype": "CHANGE", "status": "STATUS", "created": "CREATED", "name": "NAME", }] for changetype, obj in osbs.watch_builds(field_selector=field_selector): try: name = obj['metadata']['name'] except KeyError: logger.error("'object' doesn't have any name") continue else: try: status = obj['status']['phase'] except KeyError: status = '(not reported)' try: timestamp = obj['metadata']['creationTimestamp'] except KeyError: created = '(not reported)' else: created = time.ctime(get_time_from_rfc3339(timestamp)) b = { "changetype": changetype, "name": name or '', "status": status, "created": created, } data.append(b) if args.output == 'json': print(json.dumps(b)) sys.stdout.flush() elif args.output == 'text': tp = TablePrinter(data, cols_to_display) tp.render()
def test_print_table_with_mocked_terminal(capsys): (flexmock(osbs.cli.render) .should_receive('get_terminal_size') .and_return(25, 80) .once()) short_data = [{'x': 'Header1', 'y': 'Header2', 'z': 'Header3'}, {'x': 'x' * 8, 'y': 'y' * 20, 'z': 'z' * 4}] p = TablePrinter(short_data, ["x", "y", "z"]) p.render() out, err = capsys.readouterr() expected_header = """ Header1 | Header2 | Header3 -----------------------+----------------------------------+--------------------- """.lstrip('\n') expected_data = """ xxxxxxxx | yyyyyyyyyyyyyyyyyyyy | zzzz """.lstrip('\n') assert err == expected_header assert out == expected_data
def test_print_table(): p = TablePrinter(SAMPLE_DATA, ["x", "y", "z"]) p.render()