def test_arg_flag_supercedes_config_file(fs_fast): mock_argv = ["sg", "--namespace", "namespace-from-arg"] fake_config_file = _write_config_file( fs_fast, ["[defaults]", "SG_NAMESPACE=namespace-from-config-file"]) with patch_os_environ({"SG_CONFIG_FILE": fake_config_file}): config = create_config_dict() assert config["SG_NAMESPACE"] == "namespace-from-config-file" with patch.object(sys, "argv", mock_argv): config = create_config_dict() assert config["SG_NAMESPACE"] == "namespace-from-arg"
def test_env_var_supercedes_config_file(fs_fast): fake_config_file = _write_config_file( fs_fast, ["[defaults]", "SG_NAMESPACE=namespace-from-config-file"]) with patch_os_environ({"SG_CONFIG_FILE": fake_config_file}): config = create_config_dict() assert config["SG_NAMESPACE"] == "namespace-from-config-file" with patch_os_environ({ "SG_NAMESPACE": "pass-env-namespace-test", "SG_CONFIG_FILE": fake_config_file }): config = create_config_dict() assert config["SG_NAMESPACE"] == "pass-env-namespace-test"
def test_key_set_in_arg_flag(): # --namespace mapped to SG_NAMESPACE in config/keys.py mock_argv = ["sg", "--namespace", "pass-namespace-test"] with patch.object(sys, "argv", mock_argv): config = create_config_dict() assert config["SG_NAMESPACE"] == "pass-namespace-test"
def test_key_set_in_config_file(fs_fast): fake_config_file = _write_config_file( fs_fast, ["[defaults]", "SG_NAMESPACE=pass-the-test"]) with patch_os_environ({"SG_CONFIG_FILE": fake_config_file}): config = create_config_dict() assert config["SG_NAMESPACE"] == "pass-the-test"
def _make_dummy_config_dict(): # Sanitize the test config so that there isn't a ton of spam source_config = create_config_dict() source_config["SG_CONFIG_FILE"] = ".sgconfig" source_config["remotes"] = {_REMOTE: source_config["remotes"][_REMOTE]} del source_config["data_sources"] del source_config["commands"] del source_config["external_handlers"] return source_config
def test_arg_flag_supercedes_env_var(fs_fast): mock_environ = {"SG_NAMESPACE": "namespace-from-env-var"} mock_argv = ["sg", "--namespace", "namespace-from-arg"] with patch.object(sys, "argv", mock_argv): with patch_os_environ(mock_environ): assert os.environ.get("SG_NAMESPACE", None) == "namespace-from-env-var" assert sys.argv[2] == "namespace-from-arg" config = create_config_dict() assert config["SG_NAMESPACE"] == "namespace-from-arg"
def get_latest_version(self) -> Optional[str]: # Do a version check to see if updates are available. If the user is logged # into the registry, also send the user ID for metrics. # The user can opt out by setting "SG_UPDATE_FREQUENCY" to 0 or opt out of # sending user ID by setting SG_UPDATE_ANONYMOUS to true. config = create_config_dict() frequency = int(get_singleton(config, "SG_UPDATE_FREQUENCY")) if frequency == 0: return None last_check = int(get_singleton(config, "SG_UPDATE_LAST")) now = int(time.time()) if last_check + frequency > now: return None headers = get_headers() if get_singleton(config, "SG_UPDATE_ANONYMOUS").lower() == "false": try: headers.update( {"Authorization": "Bearer " + self.access_token}) except AuthAPIError: pass try: logging.debug("Running update check") response = requests.post( self.endpoint + "/update_check", verify=self.verify, headers=headers, ) response.raise_for_status() latest_version = str(response.json()["latest_version"]) except requests.RequestException as e: logging.debug("Error running the update check", exc_info=e) return None except KeyError: logging.debug("Malformed response from the update service") return None try: patch_and_save_config(config, {"SG_UPDATE_LAST": str(now)}) except Exception as e: logging.debug("Error patching the config", exc_info=e) return latest_version return latest_version
def test_config_file_accumulation(fs_fast): fake_config_file = _write_config_file( fs_fast, [ "[remote: blah]", "SG_ENGINE_HOST=pass-the-test", "[remote: foo]", "SG_ENGINE_HOST=foo-pass", ], ) with patch_os_environ({"SG_CONFIG_FILE": fake_config_file}): config = create_config_dict() assert config["remotes"]["blah"]["SG_ENGINE_HOST"] == "pass-the-test" assert config["remotes"]["foo"]["SG_ENGINE_HOST"] == "foo-pass"
def access_token(self) -> str: """ Will return an up-to-date access token by either getting it from the configuration file or contacting the auth service for a new one. Will write the new access token into the configuration file. :return: Access token. """ config = create_config_dict() try: current_access_token = get_from_subsection( config, "remotes", self.remote, "SG_CLOUD_ACCESS_TOKEN") exp = get_token_claim(current_access_token, "exp") now = time.time() if now < exp - self.access_token_expiry_tolerance: return current_access_token except KeyError: pass # Token expired or non-existent, get a new one. try: api_key = get_from_subsection(config, "remotes", self.remote, "SG_ENGINE_USER") api_secret = get_from_subsection(config, "remotes", self.remote, "SG_ENGINE_PWD") new_access_token = cast( str, self.get_access_token_from_api(api_key, api_secret)) except KeyError as e: try: refresh_token = get_from_subsection(config, "remotes", self.remote, "SG_CLOUD_REFRESH_TOKEN") new_access_token = cast(str, self.get_access_token(refresh_token)) except KeyError: raise AuthAPIError(( "No refresh token or API keys found in the config for remote %s! " % self.remote ) + "Log into the registry using sgr cloud login.") from e set_in_subsection(config, "remotes", self.remote, "SG_CLOUD_ACCESS_TOKEN", new_access_token) overwrite_config(config, get_singleton(config, "SG_CONFIG_FILE")) return new_access_token
def test_key_set_in_env_var(): mock_environ = {"SG_NAMESPACE": "pass-env-namespace-test"} with patch_os_environ(mock_environ): config = create_config_dict() assert config["SG_NAMESPACE"] == "pass-env-namespace-test"
def test_config_exports_dict(): """ The CONFIG object exported from config should be the same as the result of calling create_config_dict() """ assert json.dumps(CONFIG) == json.dumps(create_config_dict())