def _get_credentials(): """Finds the credentials for a specific test and options. Returns: Credentials: set of credentials Raises: Exception: when the credential could not be set and they are needed for that set of options """ if os.getenv('USE_ALTERNATE_ENV_CREDENTIALS', ''): # Special case: instead of using the standard credentials mechanism, # load them from different environment variables. This assumes they # will always be in place, as is used by the Travis setup. return Credentials(os.getenv('IBMQ_TOKEN'), os.getenv('IBMQ_URL')) # Attempt to read the standard credentials. discovered_credentials = discover_credentials() if discovered_credentials: # Decide which credentials to use for testing. if len(discovered_credentials) > 1: try: # Attempt to use QE credentials. return discovered_credentials[(None, None, None)] except KeyError: pass # Use the first available credentials. return list(discovered_credentials.values())[0] raise Exception('Could not locate valid credentials.') from None
def _get_credentials(test_object, test_options): """Finds the credentials for a specific test and options. Args: test_object (QiskitTestCase): The test object asking for credentials test_options (dict): Options after QISKIT_TESTS was parsed by get_test_options. Returns: Credentials: set of credentials Raises: ImportError: if the Exception: when the credential could not be set and they are needed for that set of options """ try: from qiskit.providers.ibmq.credentials import (Credentials, discover_credentials) except ImportError: raise ImportError('qiskit-ibmq-provider could not be found, and is ' 'required for mocking or executing online tests.') dummy_credentials = Credentials( 'dummyapiusersloginWithTokenid01', 'https://quantumexperience.ng.bluemix.net/api') if test_options['mock_online']: return dummy_credentials if os.getenv('USE_ALTERNATE_ENV_CREDENTIALS', ''): # Special case: instead of using the standard credentials mechanism, # load them from different environment variables. This assumes they # will always be in place, as is used by the Travis setup. return Credentials(os.getenv('IBMQ_TOKEN'), os.getenv('IBMQ_URL')) else: # Attempt to read the standard credentials. discovered_credentials = discover_credentials() if discovered_credentials: # Decide which credentials to use for testing. if len(discovered_credentials) > 1: try: # Attempt to use QE credentials. return discovered_credentials[dummy_credentials.unique_id()] except KeyError: pass # Use the first available credentials. return list(discovered_credentials.values())[0] # No user credentials were found. if test_options['rec']: raise Exception('Could not locate valid credentials. You need them for ' 'recording tests against the remote API.') test_object.log.warning('No user credentials were detected. ' 'Running with mocked data.') test_options['mock_online'] = True return dummy_credentials
def _get_credentials(): """Finds the credentials for a specific test and options. Returns: Credentials: set of credentials Raises: SkipTest: when credentials can't be found """ try: from qiskit.providers.ibmq.credentials import (Credentials, discover_credentials) except ImportError: raise unittest.SkipTest('qiskit-ibmq-provider could not be found, ' 'and is required for executing online tests.') if os.getenv('IBMQ_TOKEN') and os.getenv('IBMQ_URL'): return Credentials(os.getenv('IBMQ_TOKEN'), os.getenv('IBMQ_URL')) elif os.getenv('QISKIT_TESTS_USE_CREDENTIALS_FILE'): # Attempt to read the standard credentials. discovered_credentials = discover_credentials() if discovered_credentials: # Decide which credentials to use for testing. if len(discovered_credentials) > 1: raise unittest.SkipTest( "More than 1 credential set found, use: " "IBMQ_TOKEN and IBMQ_URL env variables to " "set credentials explicitly") # Use the first available credentials. return list(discovered_credentials.values())[0] raise unittest.SkipTest( 'No IBMQ credentials found for running the test. This is required for ' 'running online tests.')
def _get_credentials(): """Finds the credentials for a specific test and options. Returns: Credentials: set of credentials Raises: Exception: When the credential could not be set and they are needed for that set of options. """ if os.getenv("QISKIT_IBM_USE_STAGING_CREDENTIALS", ""): # Special case: instead of using the standard credentials mechanism, # load them from different environment variables. This assumes they # will always be in place, as is used by the Travis setup. return Credentials(os.getenv("QISKIT_IBM_STAGING_API_TOKEN"), os.getenv("QISKIT_IBM_STAGING_API_URL")) # Attempt to read the standard credentials. discovered_credentials, _ = discover_credentials() if discovered_credentials: # Decide which credentials to use for testing. if len(discovered_credentials) > 1: try: # Attempt to use QE credentials. return discovered_credentials[(None, None, None)] except KeyError: pass # Use the first available credentials. return list(discovered_credentials.values())[0] raise Exception("Unable to locate valid credentials.")
def test_qconfig_over_all(self): """Test order, with qconfig""" credentials = Credentials('QISKITRC_TOKEN', url=QE2_AUTH_URL) with custom_qiskitrc(): # Prepare the credentials: qconfig, env and qiskitrc present store_credentials(credentials) with custom_qconfig(b"APItoken='QCONFIG_TOKEN'"),\ custom_envs({'QE_TOKEN': 'ENVIRON_TOKEN'}): credentials, _ = discover_credentials() self.assertEqual(len(credentials), 1) self.assertEqual(list(credentials.values())[0].token, 'QCONFIG_TOKEN')
def test_environ_over_qiskitrc(self): """Test order, without qconfig""" credentials = Credentials('QISKITRC_TOKEN', url=QE_URL) with custom_qiskitrc(): # Prepare the credentials: both env and qiskitrc present store_credentials(credentials) with no_file('Qconfig.py'), custom_envs({'QE_TOKEN': 'ENVIRON_TOKEN', 'QE_URL': 'ENVIRON_URL'}): credentials = discover_credentials() self.assertEqual(len(credentials), 1) self.assertEqual(list(credentials.values())[0].token, 'ENVIRON_TOKEN')
def test_environ_over_qiskitrc(self) -> None: """Test credential discovery order.""" credentials = Credentials('QISKITRC_TOKEN', url=QE2_AUTH_URL) with custom_qiskitrc(): # Prepare the credentials: both env and qiskitrc present store_credentials(credentials) with custom_envs({ 'QE_TOKEN': 'ENVIRON_TOKEN', 'QE_URL': 'ENVIRON_URL' }): credentials, _ = discover_credentials() self.assertEqual(len(credentials), 1) self.assertEqual(list(credentials.values())[0].token, 'ENVIRON_TOKEN')