Python six.moves.reload_module() Examples

The following are 30 code examples of six.moves.reload_module(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six.moves , or try the search function .
Example #1
Source File: test_objects.py    From rtv with MIT License 6 votes vote down vote up
def test_patch_webbrowser(*_):

    # Make sure that webbrowser re-generates the browser list using the
    # mocked environment
    import webbrowser
    webbrowser = reload_module(webbrowser)

    # By default, we expect that BROWSER will be loaded as a generic browser
    # This is because "safari" is not a valid script in the system PATH
    assert isinstance(webbrowser.get(), webbrowser.GenericBrowser)

    # After patching, the default webbrowser should now be interpreted as an
    # OSAScript browser
    patch_webbrowser()
    assert isinstance(webbrowser.get(), webbrowser.MacOSXOSAScript)
    assert webbrowser._tryorder[0] == 'safari' 
Example #2
Source File: test_swift_store.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def test_delete_with_reference_params(self):
        """
        Test we can delete an existing image in the swift store
        """
        conf = copy.deepcopy(SWIFT_CONF)
        self.config(**conf)
        moves.reload_module(swift)
        # mock client because v3 uses it to receive auth_info
        self.mock_keystone_client()
        self.store = Store(self.conf)
        self.store.configure()

        uri = "swift+config://ref1/glance/%s" % (FAKE_UUID)
        loc = location.get_location_from_uri(uri, conf=self.conf)
        self.store.delete(loc)

        self.assertRaises(exceptions.NotFound, self.store.get, loc) 
Example #3
Source File: test_swift_store.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def test_delete_nonslo_not_deleted_as_slo(self, mock_del_obj):
        """
        Test that non-SLOs are not being deleted the SLO way
        """
        conf = copy.deepcopy(SWIFT_CONF)
        self.config(**conf)
        moves.reload_module(swift)
        self.mock_keystone_client()
        self.store = Store(self.conf)
        self.store.configure()

        uri = "swift://%s:key@authurl/glance/%s" % (self.swift_store_user,
                                                    FAKE_UUID)
        loc = location.get_location_from_uri(uri, conf=self.conf)
        self.store.delete(loc)

        self.assertEqual(1, mock_del_obj.call_count)
        _, kwargs = mock_del_obj.call_args
        self.assertIsNone(kwargs.get('query_string')) 
Example #4
Source File: test_swift_store.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def test_delete(self):
        """
        Test we can delete an existing image in the swift store
        """
        conf = copy.deepcopy(SWIFT_CONF)
        self.config(**conf)
        moves.reload_module(swift)
        self.mock_keystone_client()
        self.store = Store(self.conf)
        self.store.configure()

        uri = "swift://%s:key@authurl/glance/%s" % (
            self.swift_store_user, FAKE_UUID)
        loc = location.get_location_from_uri(uri, conf=self.conf)
        self.store.delete(loc)

        self.assertRaises(exceptions.NotFound, self.store.get, loc) 
Example #5
Source File: test_pip.py    From stash with MIT License 6 votes vote down vote up
def test_update(self):
        """test 'pip update <pypi_package>'."""
        output = self.run_command("pip --verbose install rsa==3.2.3", exitcode=0)
        self.assertIn("Downloading package", output)
        self.assert_did_run_setup(output)
        self.assertIn("Package installed: rsa", output)
        try:
            import rsa
            self.reload_module(rsa)
        except ImportError as e:
            self.logger.info("sys.path = " + str(sys.path))
            raise AssertionError("Could not import installed module: " + repr(e))
        else:
            self.assertEqual(rsa.__version__, "3.2.3")
            del rsa
        output = self.run_command("pip --verbose update rsa", exitcode=0)
        try:
            import rsa
            self.reload_module(rsa)
        except ImportError as e:
            self.logger.info("sys.path = " + str(sys.path))
            raise AssertionError("Could not import installed module: " + repr(e))
        else:
            self.assertNotEqual(rsa.__version__, "3.2.3")
            del rsa 
Example #6
Source File: test_swift_store.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def test_add_multi_store(self):

        conf = copy.deepcopy(SWIFT_CONF)
        conf['default_swift_reference'] = 'store_2'
        self.config(**conf)
        moves.reload_module(swift)
        self.mock_keystone_client()
        self.store = Store(self.conf)
        self.store.configure()

        expected_swift_size = FIVE_KB
        expected_swift_contents = b"*" * expected_swift_size
        expected_image_id = str(uuid.uuid4())
        image_swift = six.BytesIO(expected_swift_contents)
        global SWIFT_PUT_OBJECT_CALLS
        SWIFT_PUT_OBJECT_CALLS = 0
        loc = 'swift+config://store_2/glance/%s'

        expected_location = loc % (expected_image_id)

        location, size, checksum, multihash, arg = self.store.add(
            expected_image_id, image_swift, expected_swift_size, HASH_ALGO)
        self.assertEqual(expected_location, location) 
Example #7
Source File: test_swift_store_multibackend.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def test_single_tenant_location(self):
        conf = copy.deepcopy(SWIFT_CONF)
        conf['swift_store_container'] = 'container'
        conf_file = "glance-swift.conf"
        self.swift_config_file = self.copy_data_file(conf_file, self.test_dir)
        conf.update({'swift_store_config_file': self.swift_config_file})
        conf['default_swift_reference'] = 'ref1'
        self.config(group="swift1", **conf)
        moves.reload_module(swift)

        store = swift.SingleTenantStore(self.conf, backend="swift1")
        store.configure()
        location = store.create_location('image-id')
        self.assertEqual('swift+https', location.scheme)
        self.assertEqual('https://example.com', location.swift_url)
        self.assertEqual('container', location.container)
        self.assertEqual('image-id', location.obj)
        self.assertEqual('tenant:user1', location.user)
        self.assertEqual('key1', location.key) 
Example #8
Source File: test_altair_import_hook.py    From colabtools with Apache License 2.0 6 votes vote down vote up
def testRunsInitCodeOnImportWithFailure(self):
    if six.PY2:
      self.skipTest('Altair not available in Python 2')
    _altair._register_hook()

    altair = importlib.import_module('altair')

    self.assertNotIn('COLAB_ALTAIR_IMPORT_HOOK_EXCEPTION', os.environ)
    self.assertIn('altair', sys.modules)
    self.assertEqual('colab', altair.renderers.active)

    # Reload of the module should not re-execute code.
    # Modify the active renderer and ensure that a reload doesn't reset it to
    # colab.
    altair.renderers.enable('default')
    self.assertEqual('default', altair.renderers.active)

    altair = reload_module(altair)
    self.assertNotIn('COLAB_ALTAIR_IMPORT_HOOK_EXCEPTION', os.environ)
    self.assertIn('altair', sys.modules)
    self.assertEqual('default', altair.renderers.active) 
Example #9
Source File: __init__.py    From cloudkitty with Apache License 2.0 6 votes vote down vote up
def refresh_stevedore(namespace=None):
    """Trigger reload of entry points.

    Useful to have dynamic loading/unloading of stevedore modules.
    """
    # NOTE(sheeprine): pkg_resources doesn't support reload on python3 due to
    # defining basestring which is still there on reload hence executing
    # python2 related code.
    try:
        del sys.modules['pkg_resources'].basestring
    except AttributeError:
        # python2, do nothing
        pass
    # Force working_set reload
    moves.reload_module(sys.modules['pkg_resources'])
    # Clear stevedore cache
    cache = extension.ExtensionManager.ENTRY_POINT_CACHE
    if namespace:
        if namespace in cache:
            del cache[namespace]
    else:
        cache.clear() 
Example #10
Source File: test_swift_store_multibackend.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def test_delete_with_reference_params(self):
        """
        Test we can delete an existing image in the swift store
        """
        conf = copy.deepcopy(SWIFT_CONF)
        self.config(group="swift1", **conf)
        moves.reload_module(swift)
        # mock client because v3 uses it to receive auth_info
        self.mock_keystone_client()
        self.store = Store(self.conf, backend="swift1")
        self.store.configure()

        uri = "swift+config://ref1/glance/%s" % (FAKE_UUID)
        loc = location.get_location_from_uri_and_backend(
            uri, "swift1", conf=self.conf)
        self.store.delete(loc)

        self.assertRaises(exceptions.NotFound, self.store.get, loc) 
Example #11
Source File: test_swift_store_multibackend.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def test_delete_nonslo_not_deleted_as_slo(self, mock_del_obj):
        """
        Test that non-SLOs are not being deleted the SLO way
        """
        conf = copy.deepcopy(SWIFT_CONF)
        self.config(group="swift1", **conf)
        moves.reload_module(swift)
        self.mock_keystone_client()
        self.store = Store(self.conf, backend="swift1")
        self.store.configure()

        uri = "swift://%s:key@authurl/glance/%s" % (self.swift_store_user,
                                                    FAKE_UUID)
        loc = location.get_location_from_uri_and_backend(
            uri, "swift1", conf=self.conf)
        self.store.delete(loc)

        self.assertEqual(1, mock_del_obj.call_count)
        _, kwargs = mock_del_obj.call_args
        self.assertIsNone(kwargs.get('query_string')) 
Example #12
Source File: test_swift_store_multibackend.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def test_delete_slo(self, mock_del_obj):
        """
        Test we can delete an existing image stored as SLO, static large object
        """
        conf = copy.deepcopy(SWIFT_CONF)
        self.config(group="swift1", **conf)
        moves.reload_module(swift)
        self.store = Store(self.conf, backend="swift1")
        self.store.configure()

        uri = "swift://%s:key@authurl/glance/%s" % (self.swift_store_user,
                                                    FAKE_UUID2)
        loc = location.get_location_from_uri_and_backend(
            uri, "swift1", conf=self.conf)
        self.store.delete(loc)

        self.assertEqual(1, mock_del_obj.call_count)
        _, kwargs = mock_del_obj.call_args
        self.assertEqual('multipart-manifest=delete',
                         kwargs.get('query_string')) 
Example #13
Source File: test_swift_store_multibackend.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def test_delete(self):
        """
        Test we can delete an existing image in the swift store
        """
        conf = copy.deepcopy(SWIFT_CONF)
        self.config(group="swift1", **conf)
        moves.reload_module(swift)
        self.mock_keystone_client()
        self.store = Store(self.conf, backend="swift1")
        self.store.configure()

        uri = "swift://%s:key@authurl/glance/%s" % (
            self.swift_store_user, FAKE_UUID)
        loc = location.get_location_from_uri_and_backend(
            uri, "swift1", conf=self.conf)
        self.store.delete(loc)

        self.assertRaises(exceptions.NotFound, self.store.get, loc) 
Example #14
Source File: test_registry.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def test_registry_instance_loads_entrypoints():

    class MockRunContext(object):
        pass

    mock_entrypoint = mock.Mock()
    mock_entrypoint.load.return_value = MockRunContext

    with mock.patch(
        "entrypoints.get_group_all", return_value=[mock_entrypoint]
    ) as mock_get_group_all:
        # Entrypoints are registered at import time, so we need to reload the module to register th
        # entrypoint given by the mocked extrypoints.get_group_all
        reload(mlflow.tracking.context.registry)

    assert MockRunContext in _currently_registered_run_context_provider_classes()
    mock_get_group_all.assert_called_once_with("mlflow.run_context_provider") 
Example #15
Source File: test_swift_store_multibackend.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def test_add_multi_store(self):

        conf = copy.deepcopy(SWIFT_CONF)
        conf['default_swift_reference'] = 'store_2'
        self.config(group="swift1", **conf)
        moves.reload_module(swift)
        self.mock_keystone_client()
        self.store = Store(self.conf, backend="swift1")
        self.store.configure()

        expected_swift_size = FIVE_KB
        expected_swift_contents = b"*" * expected_swift_size
        expected_image_id = str(uuid.uuid4())
        image_swift = six.BytesIO(expected_swift_contents)
        global SWIFT_PUT_OBJECT_CALLS
        SWIFT_PUT_OBJECT_CALLS = 0
        loc = 'swift+config://store_2/glance/%s'

        expected_location = loc % (expected_image_id)

        location, size, checksum, arg = self.store.add(expected_image_id,
                                                       image_swift,
                                                       expected_swift_size)
        self.assertEqual("swift1", arg['store'])
        self.assertEqual(expected_location, location) 
Example #16
Source File: test_utils.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def test_standard_store_registry_with_installed_plugin(tmp_wkdir):
    """This test requires the package in tests/resources/mlflow-test-plugin to be installed"""

    reload(mlflow.tracking._tracking_service.utils)
    assert "file-plugin" in \
           mlflow.tracking._tracking_service.utils._tracking_store_registry._registry.keys()

    from mlflow_test_plugin.file_store import PluginFileStore

    env = {
        _TRACKING_URI_ENV_VAR: "file-plugin:test-path",
    }
    with mock.patch.dict(os.environ, env):
        plugin_file_store = mlflow.tracking._tracking_service.utils._get_store()
        assert isinstance(plugin_file_store, PluginFileStore)
        assert plugin_file_store.is_plugin 
Example #17
Source File: lib_test.py    From tensorboard with Apache License 2.0 5 votes vote down vote up
def test_functional_after_reload(self):
        self.assertNotIn("tensorboard", sys.modules)
        import tensorboard as tensorboard  # it makes the Google sync happy

        submodules = ["notebook", "program", "summary"]
        dirs_before = {
            module_name: dir(getattr(tensorboard, module_name))
            for module_name in submodules
        }
        tensorboard = moves.reload_module(tensorboard)
        dirs_after = {
            module_name: dir(getattr(tensorboard, module_name))
            for module_name in submodules
        }
        self.assertEqual(dirs_before, dirs_after) 
Example #18
Source File: test_registry.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def test_run_context_provider_registry_with_installed_plugin(tmp_wkdir):
    """This test requires the package in tests/resources/mlflow-test-plugin to be installed"""

    reload(mlflow.tracking.context.registry)

    from mlflow_test_plugin.run_context_provider import PluginRunContextProvider
    assert PluginRunContextProvider in _currently_registered_run_context_provider_classes()

    # The test plugin's context provider always returns False from in_context
    # to avoid polluting tags in developers' environments. The following mock overrides this to
    # perform the integration test.
    with mock.patch.object(PluginRunContextProvider, "in_context", return_value=True):
        assert resolve_tags()["test"] == "tag" 
Example #19
Source File: test_profiler.py    From magnum with Apache License 2.0 5 votes vote down vote up
def test_all_public_methods_are_traced(self):
        profiler_opts.set_defaults(conf.CONF)
        self.config(enabled=True,
                    group='profiler')

        classes = [
            'magnum.conductor.api.API',
            'magnum.conductor.api.ListenerAPI',
            'magnum.conductor.handlers.ca_conductor.Handler',
            'magnum.conductor.handlers.cluster_conductor.Handler',
            'magnum.conductor.handlers.conductor_listener.Handler',
            'magnum.conductor.handlers.indirection_api.Handler',
            'magnum.service.periodic.MagnumPeriodicTasks',
        ]
        for clsname in classes:
            # give the metaclass and trace_cls() decorator a chance to patch
            # methods of the classes above
            six.reload_module(
                importutils.import_module(clsname.rsplit('.', 1)[0]))
            cls = importutils.import_class(clsname)

            for attr, obj in cls.__dict__.items():
                # only public methods are traced
                if attr.startswith('_'):
                    continue
                # only checks callables
                if not (inspect.ismethod(obj) or inspect.isfunction(obj)):
                    continue
                # osprofiler skips static methods
                if isinstance(obj, staticmethod):
                    continue

                self.assertTrue(getattr(obj, '__traced__', False), obj) 
Example #20
Source File: test_utils.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def test_standard_store_registry_with_mocked_entrypoint():
    mock_entrypoint = mock.Mock()
    mock_entrypoint.name = "mock-scheme"

    with mock.patch(
        "entrypoints.get_group_all", return_value=[mock_entrypoint]
    ):
        # Entrypoints are registered at import time, so we need to reload the
        # module to register the entrypoint given by the mocked
        # extrypoints.get_group_all
        reload(mlflow.tracking._tracking_service.utils)

        expected_standard_registry = {
            '',
            'file',
            'http',
            'https',
            'postgresql',
            'mysql',
            'sqlite',
            'mssql',
            'databricks',
            'mock-scheme'
        }
        assert expected_standard_registry.issubset(
            mlflow.tracking._tracking_service.utils._tracking_store_registry._registry.keys()
        ) 
Example #21
Source File: tcollector.py    From scalyr-agent-2 with Apache License 2.0 5 votes vote down vote up
def load_config_module(name, options, tags):
    """Imports the config module of the given name

    The 'name' argument can be a string, in which case the module will be
    loaded by name, or it can be a module object, in which case the module
    will get reloaded.

    If the module has an 'onload' function, calls it.
    Returns: the reference to the module loaded.
    """

    if isinstance(name, six.text_type):
        LOG.info("Loading %s", name)
        d = {}
        # Strip the trailing .py
        module = __import__(name[:-3], d, d)
    else:
        module = reload_module(name)
    onload = module.__dict__.get("onload")
    if callable(onload):
        try:
            onload(options, tags)
        except:
            # Scalyr edit:  Add this line to disable the fatal log.
            if "no_fatal_on_error" not in options:
                LOG.fatal("Exception while loading %s", name)
            raise
    return module 
Example #22
Source File: test_pip.py    From stash with MIT License 5 votes vote down vote up
def test_install_pypi_version_2(self):
        """test 'pip install <pypi_package>==<specific_version_2>' (Test 2)."""
        output = self.run_command("pip --verbose install rsa==3.2.2", exitcode=0)
        self.assertIn("Downloading package", output)
        self.assert_did_run_setup(output)
        self.assertIn("Package installed: rsa", output)
        try:
            import rsa
            self.reload_module(rsa)
        except ImportError as e:
            self.logger.info("sys.path = " + str(sys.path))
            raise AssertionError("Could not import installed module: " + repr(e))
        else:
            self.assertEqual(rsa.__version__, "3.2.2") 
Example #23
Source File: test_pip.py    From stash with MIT License 5 votes vote down vote up
def test_install_pypi_version_1(self):
        """test 'pip install <pypi_package>==<specific_version_1>' (Test 1)."""
        output = self.run_command("pip --verbose install rsa==3.4.2", exitcode=0)
        self.assertIn("Downloading package", output)
        self.assert_did_run_setup(output)
        self.assertIn("Package installed: rsa", output)
        try:
            import rsa
            self.reload_module(rsa)
        except ImportError as e:
            self.logger.info("sys.path = " + str(sys.path))
            raise AssertionError("Could not import installed module: " + repr(e))
        else:
            self.assertEqual(rsa.__version__, "3.4.2") 
Example #24
Source File: test_artifact_repository_registry.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def test_standard_artifact_registry():
    mock_entrypoint = mock.Mock()
    mock_entrypoint.name = "mock-scheme"

    with mock.patch(
        "entrypoints.get_group_all", return_value=[mock_entrypoint]
    ):
        # Entrypoints are registered at import time, so we need to reload the
        # module to register the entrypoint given by the mocked
        # extrypoints.get_group_all
        reload(artifact_repository_registry)

        expected_artifact_repository_registry = {
            '',
            's3',
            'gs',
            'wasbs',
            'ftp',
            'sftp',
            'dbfs',
            'mock-scheme'
        }

    assert expected_artifact_repository_registry.issubset(
        artifact_repository_registry._artifact_repository_registry._registry.keys()
    ) 
Example #25
Source File: reloader.py    From testplan with Apache License 2.0 5 votes vote down vote up
def reload(self):
        """Reload this module from file."""
        self._native_mod = reload_module(self._native_mod) 
Example #26
Source File: test_pip.py    From stash with MIT License 5 votes vote down vote up
def reload_module(self, m):
        """reload a module."""
        reload_module(m) 
Example #27
Source File: test_swift_store.py    From glance_store with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        super(TestCreatingLocations, self).setUp()
        conf = copy.deepcopy(SWIFT_CONF)
        self.store = Store(self.conf)
        self.config(**conf)
        moves.reload_module(swift)
        self.addCleanup(self.conf.reset)

        service_catalog = [
            {
                'endpoint_links': [],
                'endpoints': [
                    {
                        'adminURL': 'https://some_admin_endpoint',
                        'region': 'RegionOne',
                        'internalURL': 'https://some_internal_endpoint',
                        'publicURL': 'https://some_endpoint',
                    },
                ],
                'type': 'object-store',
                'name': 'Object Storage Service',
            }
        ]
        self.ctxt = mock.MagicMock(user='user', tenant='tenant',
                                   auth_token='123',
                                   service_catalog=service_catalog) 
Example #28
Source File: test_swift_store.py    From glance_store with Apache License 2.0 5 votes vote down vote up
def test_delete_non_existing(self):
        """
        Test that trying to delete a swift that doesn't exist
        raises an error
        """
        conf = copy.deepcopy(SWIFT_CONF)
        self.config(**conf)
        moves.reload_module(swift)
        self.store = Store(self.conf)
        self.store.configure()

        loc = location.get_location_from_uri(
            "swift://%s:key@authurl/glance/noexist" % (self.swift_store_user),
            conf=self.conf)
        self.assertRaises(exceptions.NotFound, self.store.delete, loc) 
Example #29
Source File: test_swift_store.py    From glance_store with Apache License 2.0 5 votes vote down vote up
def test_add_no_container_and_multiple_containers_no_create(self):
        """
        Tests that adding an image with a non-existing container while using
        multiple containers raises an appropriate exception
        """
        conf = copy.deepcopy(SWIFT_CONF)
        conf['swift_store_user'] = 'tenant:user'
        conf['swift_store_create_container_on_put'] = False
        conf['swift_store_container'] = 'randomname'
        conf['swift_store_multiple_containers_seed'] = 2
        self.config(**conf)
        moves.reload_module(swift)
        self.mock_keystone_client()

        expected_image_id = str(uuid.uuid4())
        expected_container = 'randomname_' + expected_image_id[:2]

        self.store = Store(self.conf)
        self.store.configure()

        image_swift = six.BytesIO(b"nevergonnamakeit")

        global SWIFT_PUT_OBJECT_CALLS
        SWIFT_PUT_OBJECT_CALLS = 0

        # We check the exception text to ensure the container
        # missing text is found in it, otherwise, we would have
        # simply used self.assertRaises here
        exception_caught = False
        try:
            self.store.add(expected_image_id, image_swift, 0, HASH_ALGO)
        except exceptions.BackendException as e:
            exception_caught = True
            expected_msg = "container %s does not exist in Swift"
            expected_msg = expected_msg % expected_container
            self.assertIn(expected_msg, encodeutils.exception_to_unicode(e))
        self.assertTrue(exception_caught)
        self.assertEqual(0, SWIFT_PUT_OBJECT_CALLS) 
Example #30
Source File: lazylinker_c.py    From D-VAE with MIT License 5 votes vote down vote up
def try_reload():
    sys.path[0:0] = [config.compiledir]
    reload(lazylinker_ext)
    del sys.path[0]