def test_template_filter_route(self): output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None) se.template("tag", "hash_length", output_dir, local=True, template_filter=["Route"]) data = anymarkup.parse_file(os.path.join(output_dir, "hash_length.yaml")) for item in data["items"]: if item["kind"] == "Route": assert False
def test_template_parameters(self): image = "some_image" se = SaasHerder(temp_path, None) se.template("tag", "redirector", output_dir, local=True) data = anymarkup.parse_file(os.path.join(output_dir, "redirector.yaml")) for item in data["items"]: if item["kind"] == "DeploymentConfig": assert item["spec"]["template"]["spec"]["containers"][0]["image"].startswith(image)
def test_template_hash_length(self): hash = "abcdef7" se = SaasHerder(temp_path, None) se.template("tag", "hash_length", output_dir, local=True) data = anymarkup.parse_file(os.path.join(output_dir, "hash_length.yaml")) for item in data["items"]: if item["kind"] == "DeploymentConfig": assert item["spec"]["template"]["spec"]["containers"][0]["image"].endswith(hash)
def test_template_environment_no_parameters(self): environment = "no_params" output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None, environment) se.template("tag", "hash_length", output_dir, local=True) data = anymarkup.parse_file(os.path.join(output_dir, "hash_length.yaml")) for item in data["items"]: if item["kind"] == "DeploymentConfig": assert item["spec"]["replicas"] == 200
def test_sh_update(self): sh = SaasHerder(temp_path, None) output_file = os.path.join(temp_dir, "multiple_services.yaml") sh.update("hash", "multiple_services", "master", output_file=output_file) data = anymarkup.parse_file(output_file) assert len(data["services"]) == 2 assert data["services"][1]["hash"] == "master"
def test_template_filter_types(self): output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None) se.template("tag", "hash_length", output_dir, local=True, template_filter=["Route"]) with open(os.path.join(output_dir, "hash_length.yaml"), "r") as fp: for line in fp: if re.search(" - name: '8080'", line): return True assert False
def test_label_selectors(self): output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None) label_selectors = se.label("all", templates_dir, output_dir) label_selector_fixture = os.path.join(fixtures_dir, label_selectors_fixture) with open(label_selector_fixture) as f: lines = f.readlines() lines = [x.strip() for x in lines] for i in range(len(lines)): assert lines[i] == label_selectors[i]
def test_label_processed_files(self): output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None) se.label("all", templates_dir, output_dir) for root, _, files in os.walk(output_dir): for f in files: if not f.endswith("yaml"): continue processed = os.path.join(root, f) fixture = os.path.join(fixtures_dir, f) assert filecmp.cmp(processed, fixture)
def test_label_selectors_with_saas_repo_url(self): output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None) label_selectors = se.label( "all", templates_dir, output_dir, saas_repo_url='https://github.com/app-sre/saas-test') label_selector_fixture = os.path.join(fixtures_dir_with_repo_url, label_selectors_fixture) with open(label_selector_fixture) as f: lines = f.readlines() lines = [x.strip() for x in lines] for i in range(len(lines)): assert lines[i] == label_selectors[i]
def test_label_processed_files_with_saas_repo_url(self): output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None) se.label("all", templates_dir, output_dir, saas_repo_url='https://github.com/app-sre/saas-test') for root, _, files in os.walk(output_dir): for f in files: if not f.endswith("yaml"): continue processed = os.path.join(root, f) fixture = os.path.join(fixtures_dir_with_repo_url, f) assert filecmp.cmp(processed, fixture)
def test_template_processed_files(self): output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None) se.template("tag", "all", output_dir, local=True, template_filter=["Route"]) for root, _, files in os.walk(output_dir): for f in files: if not f.endswith("yaml"): continue processed = os.path.join(root, f) fixture = os.path.join(fixtures_dir, f) assert filecmp.cmp(processed, fixture)
def test_template_apply_dry_run(self): if os.getenv("SKIP_OC", False): return output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None) se.template("tag", "all", output_dir, local=True, template_filter=["Route"]) assert subprocess.call(["oc", "cluster", "up"]) == 0 failed = False for root, _, files in os.walk(output_dir): for f in files: if not f.endswith("yaml"): continue print(f) if subprocess.call(["oc", "apply", "--dry-run", "-f", os.path.join(root, f)]) == 1: failed = True subprocess.call(["oc", "cluster", "down"]) assert not failed
subparser_template = subparsers.add_parser("get") #subparser_template.add_argument('--all', default=False, action='store_true', # help='Perform the action on all services') subparser_template.add_argument( "type", choices=["path", "url", "hash", "template-url"], help="Update image tag with commit hash") subparser_template.add_argument( "services", nargs="*", default="all", help="Service which template should be updated") args = parser.parse_args() se = SaasHerder(args.service_dir, args.templates_dir) if args.command == "pull": if args.service: se.collect_services(args.service) elif args.command == "update": se.update(args.type, args.service, args.value, output_file=args.output_file) elif args.command == "template": se.template(args.type, args.services, args.output_dir, force=args.force) elif args.command == "get": se.get(args.type, args.services)
def test_template_environment_skip(self): environment = "test" output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None, environment) se.template("tag", "redirector", output_dir, local=True) assert not os.path.isfile(os.path.join(output_dir, "redirector.yaml"))
def test_sh_load(self): sh = SaasHerder(temp_path, None) assert os.path.basename(sh.output_dir) == "saas-processed"
def test_sh_get_services(self): sh = SaasHerder(temp_path, None) assert len(sh.get_services("redirector")) > 0
def test_sh_get_services_all(self): sh = SaasHerder(temp_path, None) assert len(sh.get_services("all")) == len(sh.services)
def test_sh_services_num(self): sh = SaasHerder(temp_path, None) assert len(sh.services) == 2
def test_sh_load(self): sh = SaasHerder(temp_path, None) assert sh.output_dir == "saas-processed"
def main(): parser = argparse.ArgumentParser(description='') parser.add_argument('-d', '--debug', default=False, action='store_true', help='Run in debug mode') parser.add_argument('-c', '--config', default="config.yaml", help='Config file for saas herder') parser.add_argument('--context', default=None, help='Context to use') parser.add_argument( '--environment', default=None, help='Environment to use to override service defined values') subparsers = parser.add_subparsers(dest="command") subparser_pull = subparsers.add_parser("pull") subparser_pull.add_argument('service', nargs="*", default="all") subparser_pull.add_argument( '--token', default=None, help="Token to use when pulling from private repo") subparser_update = subparsers.add_parser("update") subparser_update.add_argument( '-o', '--output-file', default=None, help='Name of the output file for updated services yaml') subparser_update.add_argument('type', choices=["hash", "path"], help="What to update") subparser_update.add_argument('service', nargs="?", default=None, help="Service to be updated") subparser_update.add_argument('value', help="Value to be used in services yaml") subparser_template = subparsers.add_parser("template") #subparser_template.add_argument('--all', default=False, action='store_true', # help='Perform the action on all services') subparser_template.add_argument( '-f', '--force', default=False, action='store_true', help='Force processing of all templates (i.e. those with skip: True)') subparser_template.add_argument( '--local', default=False, action='store_true', help= 'Use --local option for oc process - processing happen locally instead on server' ) subparser_template.add_argument( '--output-dir', default=None, help='Output directory where the updated templates will be stored') subparser_template.add_argument("type", choices=["tag"], help="Update image tag with commit hash") subparser_template.add_argument( "services", nargs="*", default="all", help="Service which template should be updated") subparser_template = subparsers.add_parser("get") #subparser_template.add_argument('--all', default=False, action='store_true', # help='Perform the action on all services') subparser_template.add_argument( "type", choices=["path", "url", "hash", "template-url"], help="Update image tag with commit hash") subparser_template.add_argument( "services", nargs="*", default="all", help="Service which template should be updated") subparser_template = subparsers.add_parser("config") subparser_template.add_argument("type", choices=["get-contexts"], help="Subcommands for config command") args = parser.parse_args() se = SaasHerder(args.config, args.context, args.environment) if args.command == "pull": if args.service: se.collect_services(args.service, args.token) elif args.command == "update": se.update(args.type, args.service, args.value, output_file=args.output_file) elif args.command == "template": se.template(args.type, args.services, args.output_dir, force=args.force, local=args.local) elif args.command == "get": se.get(args.type, args.services) elif args.command == "config": sc = SaasConfig(args.config) if args.type == "get-contexts": sc.print_contexts()
def test_sh_get_hash_lenght_default(self): sh = SaasHerder(temp_path, None) assert sh.get("hash_length", ["redirector"])[0] == 6
def test_template_environment_url(self): environment = "url" output_dir = tempfile.mkdtemp() se = SaasHerder(temp_path, None, environment) assert se.get_services("redirector")[0]["url"] != "some_url"
def main(): parser = argparse.ArgumentParser(description='') parser.add_argument('-d', '--debug', default=False, action='store_true', help='Run in debug mode') parser.add_argument('-c', '--config', default="config.yaml", help='Config file for saas herder') parser.add_argument('--context', default=None, help='Context to use') parser.add_argument( '--environment', default=None, help='Environment to use to override service defined values') subparsers = parser.add_subparsers(dest="command") # subcommand: pull subparser_pull = subparsers.add_parser( "pull", help="Download templates from repositories and store them locally") subparser_pull.add_argument('service', nargs="*", default="all") subparser_pull.add_argument( '--token', default=None, help="Token to use when pulling from private repo") # subcommand: update subparser_update = subparsers.add_parser( "update", help="Replace parameters in a service template and write it to a file") subparser_update.add_argument( '-o', '--output-file', default=None, help='Name of the output file for updated services yaml') subparser_update.add_argument('type', choices=["hash", "path"], help="What to update") subparser_update.add_argument('service', nargs="?", default=None, help="Service to be updated") subparser_update.add_argument('value', help="Value to be used in services yaml") # subcommand: template subparser_template = subparsers.add_parser( "template", help= "Runs oc process to generate the templates. Requires running pull first" ) subparser_template.add_argument( '-f', '--force', default=False, action='store_true', help='Force processing of all templates (i.e. those with skip: True)') subparser_template.add_argument( '--local', default=False, action='store_true', help= 'Use --local option for oc process - processing happen locally instead on server' ) subparser_template.add_argument( '--output-dir', default=None, help='Output directory where the updated templates will be stored') subparser_template.add_argument( '--filter', default=None, help='Comma separated list of kinds you want to filter out') subparser_template.add_argument("type", choices=["tag"], help="Update image tag with commit hash") subparser_template.add_argument( "services", nargs="*", default="all", help="Service which template should be updated") # subcommand: get subparser_get = subparsers.add_parser("get", help="Extracts info from a service") subparser_get.add_argument( "type", choices=["path", "url", "hash", "hash_length", "template-url"], help="Update image tag with commit hash") subparser_get.add_argument("services", nargs="*", default="all", help="Services to query") # subcommand: get-services subparser_get_services = subparsers.add_parser("get-services", help="Get list of services") subparser_get_services.add_argument("--context", action="store") # subcommand: config subparser_config = subparsers.add_parser( "config", help="Extracts info from the configuration file") subparser_config.add_argument( "type", choices=["get-contexts"], help="Prints the list of contexts in the configuration file") # subcommand: changelog subparser_changelog = subparsers.add_parser("changelog") subparser_changelog.add_argument("--context", action="store") subparser_changelog.add_argument("--format", choices=['markdown', 'plain', 'html'], default='plain') subparser_changelog.add_argument( "old", action="store", help="Commit or a date (parsed by dateutil.parser)") subparser_changelog.add_argument( "new", action="store", help="Commit or a date (parsed by dateutil.parser)") # Execute command args = parser.parse_args() se = SaasHerder(args.config, args.context, args.environment) if args.command == "pull": if args.service: se.collect_services(args.service, args.token) elif args.command == "update": se.update(args.type, args.service, args.value, output_file=args.output_file) elif args.command == "template": filters = args.filter.split(",") if args.filter else None se.template(args.type, args.services, args.output_dir, filters, force=args.force, local=args.local) elif args.command == "get": for val in se.get(args.type, args.services): print val elif args.command == "get-services": if args.context: sc = SaasConfig(args.config) sc.switch_context(args.context) for service in se.get_services("all"): print service['name'] elif args.command == "config": sc = SaasConfig(args.config) if args.type == "get-contexts": for context in sc.get_contexts(): print context elif args.command == "changelog": se.changelog.generate(args.context, args.old, args.new, args.format)
def test_sh_get_hash_lenght(self): sh = SaasHerder(temp_path, None) assert sh.get("hash_length", ["hash_length"])[0] == 7