Пример #1
0
    def test_config_load_multiple_explicit_configfiles(self):
        """Test more specific config overrides less specific one,
        on a key by key basis, in a list of explicitly given files."""

        file1 = """
            [alpha]
            endpoint = alpha
            solver = DW_2000Q_1
        """
        file2 = """
            [alpha]
            solver = DW_2000Q_2
        """

        with mock.patch('dwave.cloud.config.open', create=True) as m:
            m.side_effect = [
                iterable_mock_open(file1)(),
                iterable_mock_open(file2)()
            ]
            section = load_config(config_file=['file1', 'file2'],
                                  profile='alpha')
            m.assert_has_calls(
                [mock.call('file1', 'r'),
                 mock.call('file2', 'r')])
            self.assertEqual(section['endpoint'], 'alpha')
            self.assertEqual(section['solver'], 'DW_2000Q_2')
Пример #2
0
    def test_config_load_multiple_configfiles(self):
        """Test more specific config overrides less specific one,
        on a key by key basis."""

        config_system = u"""
            [alpha]
            endpoint = alpha
            solver = DW_2000Q_1
        """
        config_user = u"""
            [alpha]
            solver = DW_2000Q_2
            [beta]
            endpoint = beta
        """

        with mock.patch("dwave.cloud.config.get_configfile_paths",
                        lambda: ['config_system', 'config_user']):

            # test per-key override
            with mock.patch('dwave.cloud.config.open', create=True) as m:
                m.side_effect=[iterable_mock_open(config_system)(),
                               iterable_mock_open(config_user)()]
                section = load_config(profile='alpha')
                self.assertEqual(section['endpoint'], 'alpha')
                self.assertEqual(section['solver'], 'DW_2000Q_2')

            # test per-section override (section addition)
            with mock.patch('dwave.cloud.config.open', create=True) as m:
                m.side_effect=[iterable_mock_open(config_system)(),
                               iterable_mock_open(config_user)()]
                section = load_config(profile='beta')
                self.assertEqual(section['endpoint'], 'beta')
    def test_custom_options(self):
        """Test custom options (request_timeout, polling_timeout, permissive_ssl) are propagated to Client."""
        request_timeout = 15
        polling_timeout = 180

        with mock.patch("dwave.cloud.config.open",
                        iterable_mock_open(config_body),
                        create=True):
            with Client.from_config('config_file', profile='custom') as client:
                # check permissive_ssl and timeouts custom params passed-thru
                self.assertFalse(client.session.verify)
                self.assertEqual(client.request_timeout, request_timeout)
                self.assertEqual(client.polling_timeout, polling_timeout)

                # verify client uses those properly
                def mock_send(*args, **kwargs):
                    self.assertEqual(kwargs.get('timeout'), request_timeout)
                    response = requests.Response()
                    response.status_code = 200
                    response._content = b'{}'
                    return response

                with mock.patch("requests.adapters.HTTPAdapter.send",
                                mock_send):
                    client.get_solvers()
Пример #4
0
 def test_config_load_from_file(self):
     with mock.patch('dwave.cloud.config.open',
                     iterable_mock_open(self.config_body),
                     create=True):
         config = load_config_from_files(filenames=["filename"])
         self.assertEqual(config.sections(),
                          ['dw2000', 'software', 'alpha'])
         self.assertEqual(config['dw2000']['client'], 'qpu')
         self.assertEqual(config['software']['client'], 'sw')
 def test_only_file(self):
     """With no arguments or environment variables, the default connection from the config file should be used."""
     with mock.patch("dwave.cloud.config.open", iterable_mock_open(config_body), create=True):
         with Client.from_config('config_file') as client:
             try:
                 client.get_solver('arg-solver')
             except RequestEvent as event:
                 self.assertTrue(event.url.startswith('http://file-prod.url/'))
                 return
             self.fail()
 def test_explicit_with_file(self):
     """With arguments and a config file, the config file should be ignored."""
     with mock.patch("dwave.cloud.config.open", iterable_mock_open(config_body), create=True):
         with Client.from_config(endpoint=self.endpoint, token='arg-token') as client:
             try:
                 client.get_solver('arg-solver')
             except RequestEvent as event:
                 self.assertTrue(event.url.startswith(self.endpoint))
                 return
             self.fail()
 def test_env_with_file_set(self):
     """With environment variables and a config file, the config file should be ignored."""
     with mock.patch("dwave.cloud.config.open", iterable_mock_open(legacy_config_body), create=True):
         with mock.patch.dict(os.environ, {'DW_INTERNAL__HTTPLINK': 'http://env.url', 'DW_INTERNAL__TOKEN': 'env-token'}):
             with Client.from_config(config_file=False, legacy_config_fallback=True) as client:
                 try:
                     client.get_solver('arg-solver')
                 except RequestEvent as event:
                     self.assertTrue(event.url.startswith('http://env.url/'))
                     return
                 self.fail()
 def test_only_file_key(self):
     """If give a name from the config file the proper URL should be loaded."""
     with mock.patch("dwave.cloud.config.open", iterable_mock_open(config_body), create=True):
         with mock.patch("dwave.cloud.config.get_configfile_paths", lambda *x: ['file']):
             with Client.from_config(profile='alpha') as client:
                 try:
                     client.get_solver('arg-solver')
                 except RequestEvent as event:
                     self.assertTrue(event.url.startswith('http://file-alpha.url/'))
                     return
                 self.fail()
Пример #9
0
 def test_config_load_from_file__invalid_format__duplicate_sections(self):
     """Config loading should fail with ``ConfigFileParseError`` for invalid
     config files."""
     myconfig = u"""
         [section]
         key = val
         [section]
         key = val
     """
     with mock.patch('dwave.cloud.config.open', iterable_mock_open(myconfig), create=True):
         self.assertRaises(ConfigFileParseError, load_config_from_files, filenames=["filename"])
         self.assertRaises(ConfigFileParseError, load_config, config_file="filename", profile="section")
Пример #10
0
    def test_iterable_mock_open(self):
        data = '1\n2\n3'
        namespaced_open = '{}.open'.format(__name__)

        # first, verify `mock.mock_open` fails for iteration
        with self.assertRaises(AttributeError):
            with mock.patch(namespaced_open, mock.mock_open(data),
                            create=True):
                for line in open('filename'):
                    pass

        # then verify our `iterable_mock_open` works
        lines = []
        with mock.patch(namespaced_open, iterable_mock_open(data),
                        create=True):
            for line in open('filename'):
                lines.append(line)
        self.assertEqual(''.join(lines), data)
Пример #11
0
 def mock_open(filename, *pa, **kw):
     self.assertEqual(filename, expected_path)
     return iterable_mock_open(conf_content)()