def test_load_config(): with TemporaryDirectory() as td: subdirA = os.path.join(td, "A") subdirA1 = os.path.join(subdirA, "1") os.mkdir(subdirA) os.mkdir(subdirA1) sample_config = 'conf/sample-team-conf.yaml' subdirA_config = parse_ozy_conf(sample_config) subdirA_config['apps']['nomad']['version'] = '10.10.10.10' save_temp_ozy_conf(subdirA_config, os.path.join(subdirA, ".ozy.yaml")) loaded_config = load_config(config=parse_ozy_conf(sample_config), current_working_directory=subdirA) assert loaded_config['apps']['nomad']['version'] == '10.10.10.10' # lets try with three dirs! subdirA1_config = parse_ozy_conf(sample_config) subdirA1_config['apps']['nomad']['version'] = '11.11.11.11' save_temp_ozy_conf(subdirA1_config, os.path.join(subdirA1, ".ozy.yaml")) loaded_config = load_config(config=parse_ozy_conf(sample_config), current_working_directory=subdirA1) assert loaded_config['apps']['nomad']['version'] == '11.11.11.11'
def test_unsupported_installer(): sample_config_file = "conf/sample-team-conf.yaml" ozy_conf = parse_ozy_conf(sample_config_file) ozy_conf['templates']['hashicorp'][ 'type'] = 'triple_binary_star' # not a supported installer with pytest.raises(OzyError): App("nomad", ozy_conf)
def update(dry_run, url): """Update base configuration from the remote URL.""" user_conf = load_ozy_user_conf() if not url: if 'url' not in user_conf: raise OzyError('Missing url in configuration') url = user_conf['url'] ozy_conf_filename = f"{get_ozy_dir()}/ozy.yaml" tmp_filename = ozy_conf_filename + ".tmp" download_to(tmp_filename, url) new_conf_root = parse_ozy_conf(tmp_filename) old_conf_root = parse_ozy_conf(ozy_conf_filename) changed = False for app, new_conf in new_conf_root['apps'].items(): old_conf = old_conf_root['apps'].get(app, None) if not old_conf: _LOGGER.info('%s new app %s (%s)', "Would install" if dry_run else "Installing", app, new_conf['version']) changed = True elif old_conf['version'] != new_conf['version']: _LOGGER.info('%s %s from %s to %s', "Would upgrade" if dry_run else "Upgrading", app, old_conf['version'], new_conf['version']) changed = True if not dry_run: ozy_bin_dir = get_ozy_bin_dir() user_conf['url'] = url save_ozy_user_conf(user_conf) os.rename(tmp_filename, ozy_conf_filename) symlink_binaries(ozy_bin_dir, new_conf_root) if not changed: _LOGGER.info("No changes made") else: if changed: _LOGGER.info("Dry run only - no changes made") else: _LOGGER.info( "Dry run only - no changes would be made, even without --dry-run" ) os.unlink(tmp_filename)
def test_ensure_keys(): sample_config_file = "conf/sample-team-conf.yaml" ozy_conf = parse_ozy_conf(sample_config_file) tmp_ozy_conf = ozy_conf.copy() del tmp_ozy_conf['templates']['hashicorp']['type'] config = resolve(ozy_conf['apps']['nomad'], ozy_conf['templates']) assert config with pytest.raises(OzyError): ensure_keys('nomad', config, 'type')
def test_parse_ozy_conf(): sample_config_file = "conf/sample-team-conf.yaml" ozy_conf = parse_ozy_conf(sample_config_file) assert ozy_conf is not None assert 'name' in ozy_conf assert 'ozy_version' in ozy_conf assert 'templates' in ozy_conf assert 'apps' in ozy_conf templates = ozy_conf['templates'] assert 'hashicorp' in templates
def test_app(): sample_config_file = "conf/sample-team-conf.yaml" ozy_conf = parse_ozy_conf(sample_config_file) app = App("nomad", ozy_conf) assert app.name == "nomad" assert app.config['template'] == 'hashicorp' assert app.config['url'] assert app.config['type'] == 'single_binary_zip' assert app.config['sha256_gpg_key'] == ozy_conf['templates']['hashicorp'][ 'sha256_gpg_key'] assert app.config['sha256'] assert app.config['sha256_signature']
def init(url): """Initialise and install ozy, with configuration from the given URL.""" ensure_ozy_dirs() user_conf = load_ozy_user_conf() # TODO: make sure it isn't there already? upgrade/update instead? ozy_conf_filename = f"{get_ozy_dir()}/ozy.yaml" download_to(ozy_conf_filename, url) ozy_bin_dir = get_ozy_bin_dir() root_conf = parse_ozy_conf(ozy_conf_filename) ## TODO think how this interacts with local config files symlink_binaries(ozy_bin_dir, root_conf) user_conf['url'] = url save_ozy_user_conf(user_conf) if not check_path(ozy_bin_dir): _LOGGER.info("ozy is installed, but needs a little more setup work:") show_path_warning(ozy_bin_dir) else: _LOGGER.info("ozy is installed and is ready to run")
def test_resolve(): sample_config_file = "conf/sample-team-conf.yaml" ozy_conf = parse_ozy_conf(sample_config_file) templates = ozy_conf['templates'] config = ozy_conf['apps']['nomad'] with pytest.raises(OzyError): bad_templates = templates.copy() del bad_templates['hashicorp'] resolve(config, bad_templates) # with a version check turned on, the below 3 LOC will verify that flipping config and template is caught. # try intentionally switching them # with pytest.raises(OzyError): # resolve(config=templates, templates=config) good_resolved = resolve(config, templates) assert 'url' in good_resolved assert 'template' in good_resolved assert 'type' in good_resolved assert 'app_name' in good_resolved assert 'version' in good_resolved assert 'sha256' in good_resolved assert 'sha256_gpg_key' in good_resolved