def test_errors_on_apt_helper_process_timeout(self, m_exists, m_subp, m_unlink): """Raise the appropriate user facing error from apt-helper timeout.""" # Failure apt-helper response m_subp.side_effect = subprocess.TimeoutExpired("something timed out", timeout=1000000) with pytest.raises(exceptions.UserFacingError) as excinfo: assert_valid_apt_credentials(repo_url="http://fakerepo", username="******", password="******") error_msg = ("Cannot validate credentials for APT repo. Timeout" " after {} seconds trying to reach fakerepo.".format( apt.APT_HELPER_TIMEOUT)) assert error_msg == excinfo.value.msg exists_calls = [ mock.call("/usr/lib/apt/apt-helper"), mock.call("/tmp/uaclient-apt-test"), ] assert exists_calls == m_exists.call_args_list apt_helper_call = mock.call( [ "/usr/lib/apt/apt-helper", "download-file", "http://*****:*****@fakerepo/ubuntu/pool/", "/tmp/uaclient-apt-test", ], timeout=apt.APT_HELPER_TIMEOUT, ) assert [apt_helper_call] == m_subp.call_args_list assert [mock.call("/tmp/uaclient-apt-test")] == m_unlink.call_args_list
def test_errors_on_process_execution_errors(self, m_exists, m_subp, m_unlink, exit_code, stderr, error_msg): """Raise the appropriate user facing error from apt-helper failure.""" # Failure apt-helper response m_subp.side_effect = util.ProcessExecutionError( cmd="apt-helper ", exit_code=exit_code, stdout="Err:1...", stderr=stderr, ) with pytest.raises(exceptions.UserFacingError) as excinfo: assert_valid_apt_credentials(repo_url="http://fakerepo", username="******", password="******") assert error_msg == str(excinfo.value) exists_calls = [ mock.call("/usr/lib/apt/apt-helper"), mock.call("/tmp/uaclient-apt-test"), ] assert exists_calls == m_exists.call_args_list apt_helper_call = mock.call( [ "/usr/lib/apt/apt-helper", "download-file", "http://*****:*****@fakerepo/ubuntu/pool/", "/tmp/uaclient-apt-test", ], timeout=20, ) assert [apt_helper_call] == m_subp.call_args_list assert [mock.call("/tmp/uaclient-apt-test")] == m_unlink.call_args_list
def test_errors_on_apt_helper_process_timeout(self, m_exists, m_subp, m_temporary_directory): """Raise the appropriate user facing error from apt-helper timeout.""" m_temporary_directory.return_value.__enter__.return_value = ( "/does/not/exist") # Failure apt-helper response m_subp.side_effect = subprocess.TimeoutExpired("something timed out", timeout=1000000) with pytest.raises(exceptions.UserFacingError) as excinfo: assert_valid_apt_credentials(repo_url="http://fakerepo", username="******", password="******") error_msg = ("Cannot validate credentials for APT repo. Timeout" " after {} seconds trying to reach fakerepo.".format( apt.APT_HELPER_TIMEOUT)) assert error_msg == excinfo.value.msg exists_calls = [mock.call("/usr/lib/apt/apt-helper")] assert exists_calls == m_exists.call_args_list expected_path = os.path.join( m_temporary_directory.return_value.__enter__.return_value, "apt-helper-output", ) apt_helper_call = mock.call( [ "/usr/lib/apt/apt-helper", "download-file", "http://*****:*****@fakerepo/ubuntu/pool/", expected_path, ], timeout=apt.APT_HELPER_TIMEOUT, ) assert [apt_helper_call] == m_subp.call_args_list
def test_passes_when_missing_apt_helper(self, m_exists, m_subp): """When apt-helper tool is absent perform no validation.""" assert None is assert_valid_apt_credentials( repo_url="http://fakerepo", username="******", password="******") expected_calls = [mock.call("/usr/lib/apt/apt-helper")] assert expected_calls == m_exists.call_args_list assert 0 == m_subp.call_count
def test_passes_on_valid_creds(self, m_exists, m_subp, m_temporary_directory): """Succeed when apt-helper succeeds in authenticating to repo.""" m_temporary_directory.return_value.__enter__.return_value = ( "/does/not/exist") # Success apt-helper response m_subp.return_value = "Get:1 https://fakerepo\nFetched 285 B in 1s", "" assert None is assert_valid_apt_credentials( repo_url="http://fakerepo", username="******", password="******") exists_calls = [mock.call("/usr/lib/apt/apt-helper")] assert exists_calls == m_exists.call_args_list expected_path = os.path.join( m_temporary_directory.return_value.__enter__.return_value, "apt-helper-output", ) apt_helper_call = mock.call( [ "/usr/lib/apt/apt-helper", "download-file", "http://*****:*****@fakerepo/ubuntu/pool/", expected_path, ], timeout=20, ) assert [apt_helper_call] == m_subp.call_args_list
def test_errors_on_process_execution_errors( self, m_exists, m_subp, m_temporary_directory, exit_code, stderr, error_msg, ): """Raise the appropriate user facing error from apt-helper failure.""" m_temporary_directory.return_value.__enter__.return_value = ( "/does/not/exist" ) # Failure apt-helper response m_subp.side_effect = util.ProcessExecutionError( cmd="apt-helper ", exit_code=exit_code, stdout="Err:1...", stderr=stderr, ) with pytest.raises(exceptions.UserFacingError) as excinfo: assert_valid_apt_credentials( repo_url="http://fakerepo", username="******", password="******" ) assert error_msg == str(excinfo.value) exists_calls = [mock.call("/usr/lib/apt/apt-helper")] assert exists_calls == m_exists.call_args_list expected_path = os.path.join( m_temporary_directory.return_value.__enter__.return_value, "apt-helper-output", ) apt_helper_call = mock.call( [ "/usr/lib/apt/apt-helper", "download-file", "http://*****:*****@fakerepo/ubuntu/pool/", expected_path, ], timeout=20, ) assert [apt_helper_call] == m_subp.call_args_list
def test_passes_on_valid_creds(self, m_exists, m_subp, m_unlink): """Succeed when apt-helper succeeds in authenticating to repo.""" # Success apt-helper response m_subp.return_value = "Get:1 https://fakerepo\nFetched 285 B in 1s", "" assert None is assert_valid_apt_credentials( repo_url="http://fakerepo", username="******", password="******") exists_calls = [ mock.call("/usr/lib/apt/apt-helper"), mock.call("/tmp/uaclient-apt-test"), ] assert exists_calls == m_exists.call_args_list apt_helper_call = mock.call( [ "/usr/lib/apt/apt-helper", "download-file", "http://*****:*****@fakerepo/ubuntu/pool/", "/tmp/uaclient-apt-test", ], timeout=20, ) assert [apt_helper_call] == m_subp.call_args_list assert [mock.call("/tmp/uaclient-apt-test")] == m_unlink.call_args_list