Esempio n. 1
0
    def test_enabled_metrics(self):
        """Test streamlit.metrics.toggle_metrics enabled."""
        config.set_option("global.metrics", True)
        client = streamlit.metrics.Client.get_current()
        client._metrics = {}

        # yapf: disable
        client._raw_metrics = [
            ('Counter', 'unittest_counter', 'Unittest counter', []),
            ('Counter', 'unittest_counter_labels', 'Unittest counter labels', ['label']),
            ('Gauge', 'unittest_gauge', 'Unittest gauge', []),
        ]
        # yapf: enable

        client.toggle_metrics()

        client.get("unittest_counter").inc()
        client.get("unittest_counter_labels").labels("some_label")
        client.get("unittest_gauge").set(42)

        truth = [
            "unittest_counter_total 1.0",
            'unittest_counter_labels_total{label="some_label"} 0.0',
            "unittest_gauge 42.0",
        ]
        lines = client.generate_latest().splitlines()
        metrics = [
            x.decode("utf-8") for x in lines
            if x.decode("utf-8").startswith("unit")
        ]
        metrics = [str(x) for x in metrics if "_created" not in x]
        self.assertEqual(sorted(truth), sorted(metrics))
Esempio n. 2
0
    def test_get_set_and_complex_config_options(self):
        """Verify that changing one option changes another, dependent one.

        This also implicitly tests simple and complex ConfigOptions as well as
        get_option() and set_option().
        """
        # Some useful variables.
        DUMMY_VAL_1, DUMMY_VAL_2, DUMMY_VAL_3 = "Steven", "Vincent", "Buscemi"

        # Set up both options.
        config._create_option(
            "_test.independentOption",
            description="This option can change at will",
            default_val=DUMMY_VAL_1,
        )

        @config._create_option("_test.dependentOption")
        def _test_dependent_option():
            """Depend on the value of _test.independentOption."""
            return config.get_option("_test.independentOption")

        config.get_config_options(force_reparse=True)

        # Check that the default values are good.
        self.assertEqual(config.get_option("_test.independentOption"),
                         DUMMY_VAL_1)
        self.assertEqual(config.get_option("_test.dependentOption"),
                         DUMMY_VAL_1)
        self.assertEqual(
            config.get_where_defined("_test.independentOption"),
            ConfigOption.DEFAULT_DEFINITION,
        )
        self.assertEqual(
            config.get_where_defined("_test.dependentOption"),
            ConfigOption.DEFAULT_DEFINITION,
        )

        # Override the independent option. Both update!
        config.set_option("_test.independentOption", DUMMY_VAL_2)
        self.assertEqual(config.get_option("_test.independentOption"),
                         DUMMY_VAL_2)
        self.assertEqual(config.get_option("_test.dependentOption"),
                         DUMMY_VAL_2)
        self.assertEqual(config.get_where_defined("_test.independentOption"),
                         config._USER_DEFINED)
        self.assertEqual(
            config.get_where_defined("_test.dependentOption"),
            ConfigOption.DEFAULT_DEFINITION,
        )

        # Override the dependent option. Only that updates!
        config.set_option("_test.dependentOption", DUMMY_VAL_3)
        self.assertEqual(config.get_option("_test.independentOption"),
                         DUMMY_VAL_2)
        self.assertEqual(config.get_option("_test.dependentOption"),
                         DUMMY_VAL_3)
        self.assertEqual(config.get_where_defined("_test.independentOption"),
                         config._USER_DEFINED)
        self.assertEqual(config.get_where_defined("_test.dependentOption"),
                         config._USER_DEFINED)
Esempio n. 3
0
def set_option(key, value):
    """Set config option.

    Currently, only the following config options can be set within the script itself:
        * client.caching
        * client.displayEnabled
        * deprecation.*

    Calling with any other options will raise StreamlitAPIException.

    Run `streamlit config show` in the terminal to see all available options.

    Parameters
    ----------
    key : str
        The config option key of the form "section.optionName". To see all
        available options, run `streamlit config show` on a terminal.

    value
        The new value to assign to this config option.

    """
    opt = _config._config_options_template[key]
    if opt.scriptable:
        _config.set_option(key, value)
        return

    raise StreamlitAPIException(
        "{key} cannot be set on the fly. Set as command line option, e.g. streamlit run script.py --{key}, or in config.toml instead.".format(
            key=key
        )
    )
    def test_auto_blacklist(self, _):
        prev_blacklist = config.get_option("server.folderWatchBlacklist")
        config.set_option("server.folderWatchBlacklist", [])

        lso = LocalSourcesWatcher.LocalSourcesWatcher(REPORT, NOOP_CALLBACK)

        def is_blacklisted(filepath):
            return any(
                _file_is_in_folder(filepath, blacklisted_folder)
                for blacklisted_folder in lso._folder_blacklist
            )

        # miniconda, anaconda, and .*/ folders should be blacklisted
        self.assertTrue(is_blacklisted("/foo/miniconda2/script.py"))
        self.assertTrue(is_blacklisted("/foo/miniconda3/script.py"))
        self.assertTrue(is_blacklisted("/foo/anaconda2/script.py"))
        self.assertTrue(is_blacklisted("/foo/anaconda3/script.py"))
        self.assertTrue(is_blacklisted("/foo/.virtualenv/script.py"))
        self.assertTrue(is_blacklisted("/foo/.venv/script.py"))
        self.assertTrue(is_blacklisted("/foo/.random_hidden_folder/script.py"))

        # Ensure we're not accidentally blacklisting things we shouldn't be
        self.assertFalse(is_blacklisted("/foo/not_blacklisted/script.py"))
        self.assertFalse(is_blacklisted("/foo/not_blacklisted/.hidden_script.py"))

        # Reset the config object.
        config.set_option("server.folderWatchBlacklist", prev_blacklist)
Esempio n. 5
0
    def test_server_headless_via_atom_plugin(self):
        os.environ["IS_RUNNING_IN_STREAMLIT_EDITOR_PLUGIN"] = "True"

        config.set_option("server.liveSave", False)
        self.assertEqual(True, config.get_option("server.headless"))

        del os.environ["IS_RUNNING_IN_STREAMLIT_EDITOR_PLUGIN"]
Esempio n. 6
0
    def test_server_headless_via_atom_plugin(self):
        os.environ['IS_RUNNING_IN_STREAMLIT_EDITOR_PLUGIN'] = 'True'

        config.set_option('server.liveSave', False)
        self.assertEqual(True, config.get_option('server.headless'))

        del os.environ['IS_RUNNING_IN_STREAMLIT_EDITOR_PLUGIN']
Esempio n. 7
0
    def test_s3(self):
        self.assertEqual(None, config.get_option('s3.secretAccessKey'))
        self.assertEqual(None, config.get_option('s3.accessKeyId'))
        self.assertEqual(None, config.get_option('s3.url'))
        self.assertEqual(None, config.get_option('s3.bucket'))
        with patch('streamlit.config._get_public_credentials') as p:
            p.return_value = {}
            config.set_option('global.sharingMode', 'streamlit-public')
            self.assertEqual(None, config.get_option('s3.secretAccessKey'))
            self.assertEqual(None, config.get_option('s3.accessKeyId'))
            self.assertEqual(None, config.get_option('s3.url'))
            self.assertEqual(None, config.get_option('s3.bucket'))

        with patch('streamlit.config._get_public_credentials') as p:
            p.return_value = {
                'secretAccessKey': 'sekrit',
                'accessKeyId': 'sekrit2',
                'url': 'http://test.url',
                'bucket': 'some.bucket',
            }
            config.set_option('global.sharingMode', 'streamlit-public')
            self.assertEqual('sekrit', config.get_option('s3.secretAccessKey'))
            self.assertEqual('sekrit2', config.get_option('s3.accessKeyId'))
            self.assertEqual('http://test.url', config.get_option('s3.url'))
            self.assertEqual('some.bucket', config.get_option('s3.bucket'))
Esempio n. 8
0
    def test_metrics(self):
        config.set_option("global.metrics", False)
        response = self.fetch("/metrics")
        self.assertEqual(404, response.code)

        config.set_option("global.metrics", True)
        response = self.fetch("/metrics")
        self.assertEqual(200, response.code)
Esempio n. 9
0
    def test_delete_option(self):
        config.set_option("s3.bucket", "some.bucket")
        config._delete_option("s3.bucket")
        with pytest.raises(RuntimeError) as e:
            config.get_option("s3.bucket")
        self.assertEqual(str(e.value), 'Config key "s3.bucket" not defined.')

        config._delete_option("s3.bucket")
Esempio n. 10
0
 def test_public_url(self, creds, static_files):
     creds.return_value = {
         'bucket': 'share.streamlit.io',
         'url': 'https://share.streamlit.io/',
         'accessKeyId': 'ACCESS_KEY_ID',
         'secretAccessKey': 'SECRERT_ACCESS_KEY',
     }
     static_files.return_value = [('index.html', 'some data')], hashlib.md5()
     set_option('global.sharingMode', 'streamlit-public')
     s3 = S3Storage()
     self.assertEqual(s3._url, 'https://share.streamlit.io/')
Esempio n. 11
0
    def test_enabled_metrics_no_prometheus(self):
        """Test streamlit.metrics.Client.toggle_metrics no prometheus."""
        config.set_option("global.metrics", True)

        client = streamlit.metrics.Client.get_current()
        builtin_import = "builtins.__import__"

        with pytest.raises(ImportError) as e:
            with patch(builtin_import, side_effect=ImportError):
                client.toggle_metrics()
        msg = "prometheus-client is not installed. pip install prometheus-client"
        self.assertEqual(msg, str(e.value))
Esempio n. 12
0
    def test_config_watcherType(self, _):
        """Test server.fileWatcherType"""

        config.set_option("server.fileWatcherType", "none")
        self.assertIsNone(local_sources_watcher.get_default_file_watcher_class())

        config.set_option("server.fileWatcherType", "poll")
        if local_sources_watcher.get_default_file_watcher_class() is not None:
            self.assertEqual(
                local_sources_watcher.get_default_file_watcher_class().__name__,
                "PollingFileWatcher",
            )

        config.set_option("server.fileWatcherType", "watchdog")
        if local_sources_watcher.get_default_file_watcher_class() is not None:
            self.assertEqual(
                local_sources_watcher.get_default_file_watcher_class().__name__,
                "EventBasedFileWatcher",
            )

        config.set_option("server.fileWatcherType", "auto")
        self.assertIsNotNone(local_sources_watcher.get_default_file_watcher_class())

        if sys.modules["streamlit.watcher.event_based_file_watcher"] is not None:
            self.assertEqual(
                local_sources_watcher.get_default_file_watcher_class().__name__,
                "EventBasedFileWatcher",
            )
        else:
            self.assertEqual(
                local_sources_watcher.get_default_file_watcher_class().__name__,
                "PollingFileWatcher",
            )
Esempio n. 13
0
    def test_enabled_metrics_no_prometheus(self):
        """Test streamlit.metrics.Client.toggle_metrics no prometheus."""
        config.set_option('global.metrics', True)

        client = streamlit.metrics.Client.get_current()
        if sys.version_info <= (3, 0):
            builtin_import = '__builtin__.__import__'
        else:
            builtin_import = 'builtins.__import__'

        with pytest.raises(ImportError) as e:
            with patch(builtin_import, side_effect=ImportError):
                client.toggle_metrics()
        msg = 'prometheus-client is not installed. pip install prometheus-client'
        self.assertEqual(msg, str(e.value))
Esempio n. 14
0
    def test_credentials_headless_no_config(self):
        """If headless mode and no config is present,
        activation should be None."""
        from streamlit import config

        config.set_option("server.headless", True)

        with patch("validators.url", return_value=False), patch(
            "streamlit.bootstrap.run"
        ), patch("os.path.exists", return_value=True), patch(
            "streamlit.credentials._check_credential_file_exists", return_value=False
        ):
            result = self.runner.invoke(cli, ["run", "some script.py"])
        from streamlit.credentials import Credentials

        credentials = Credentials.get_current()
        self.assertIsNone(credentials.activation)
        self.assertEqual(0, result.exit_code)
Esempio n. 15
0
    def test_private_url(self, static_files):
        static_files.return_value = [("index.html", "some data")], hashlib.md5()

        set_option("global.sharingMode", "s3")
        set_option("s3.bucket", "buckets")
        set_option("s3.accessKeyId", "ACCESS_KEY_ID")
        set_option("s3.secretAccessKey", "SECRET_ACCESS_KEY")
        s3 = S3Storage()
        self.assertEqual(s3._url, None)
        idx = s3._web_app_url.index("/", 8)
        self.assertEqual(s3._web_app_url[0:idx], "https://buckets.s3.amazonaws.com")
Esempio n. 16
0
    def test_private_url(self, static_files):
        static_files.return_value = [('index.html', 'some data')], hashlib.md5()

        set_option('global.sharingMode', 's3')
        set_option('s3.bucket', 'buckets')
        set_option('s3.accessKeyId', 'ACCESS_KEY_ID')
        set_option('s3.secretAccessKey', 'SECRET_ACCESS_KEY')
        s3 = S3Storage()
        self.assertEqual(s3._url, None)
        idx = s3._web_app_url.index('/', 8)
        self.assertEqual(s3._web_app_url[0:idx],
                         'https://buckets.s3.amazonaws.com')
    def test_blacklist(self, fob):
        prev_blacklist = config.get_option('server.folderWatchBlacklist')

        config.set_option('server.folderWatchBlacklist',
                          [os.path.dirname(DUMMY_MODULE_1.__file__)])

        lso = LocalSourcesWatcher.LocalSourcesWatcher(REPORT, CALLBACK)

        fob.assert_called_once()

        sys.modules['DUMMY_MODULE_1'] = DUMMY_MODULE_1
        fob.reset_mock()

        lso.update_watched_modules()

        fob.assert_not_called()

        # Reset the config object.
        config.set_option('server.folderWatchBlacklist', prev_blacklist)
Esempio n. 18
0
    def test_unix_socket(self):
        app = mock.MagicMock()

        config.set_option("server.address", "unix://~/fancy-test/testasd")
        some_socket = object()

        mock_server = self.get_httpserver()
        with patch(
                "streamlit.server.server.HTTPServer",
                return_value=mock_server), patch.object(
                    tornado.netutil,
                    "bind_unix_socket",
                    return_value=some_socket) as bind_unix_socket, patch.dict(
                        os.environ, {"HOME": "/home/superfakehomedir"}):
            start_listening(app)

            bind_unix_socket.assert_called_with(
                "/home/superfakehomedir/fancy-test/testasd")
            mock_server.add_socket.assert_called_with(some_socket)
Esempio n. 19
0
    def test_credentials_headless_with_config(self, headless_mode):
        """If headless, but a config file is present, activation should be
        defined.
        So we call `_check_activated`.
        """
        from streamlit import config

        config.set_option("server.headless", headless_mode)

        with patch("validators.url", return_value=False), patch(
            "streamlit.bootstrap.run"
        ), patch("os.path.exists", side_effect=[True, True]), mock.patch(
            "streamlit.credentials.Credentials._check_activated"
        ) as mock_check, patch(
            "streamlit.credentials._check_credential_file_exists", return_value=True
        ):
            result = self.runner.invoke(cli, ["run", "some script.py"])
        self.assertTrue(mock_check.called)
        self.assertEqual(0, result.exit_code)
Esempio n. 20
0
    def test_disabled_metrics(self):
        """Test streamlit.metrics.Client.toggle_metrics disabled."""
        config.set_option("global.metrics", False)
        client = streamlit.metrics.Client.get_current()
        client._metrics = {}

        # yapf: disable
        client._raw_metrics = [
            ('Counter', 'unittest_counter', 'Unittest counter', []),
            ('Counter', 'unittest_counter_labels', 'Unittest counter labels', ['label']),
            ('Gauge', 'unittest_gauge', 'Unittest gauge', []),
        ]
        # yapf: enable

        client.toggle_metrics()

        client.get("unittest_counter").inc()
        client.get("unittest_counter_labels").labels("some_label")
        client.get("unittest_gauge").set(42)
        client.get("unittest_gauge").dec()
Esempio n. 21
0
    def test_config_blacklist(self, fob, _):
        """Test server.folderWatchBlacklist"""
        prev_blacklist = config.get_option("server.folderWatchBlacklist")

        config.set_option("server.folderWatchBlacklist",
                          [os.path.dirname(DUMMY_MODULE_1.__file__)])

        lso = local_sources_watcher.LocalSourcesWatcher(REPORT, NOOP_CALLBACK)

        fob.assert_called_once()

        sys.modules["DUMMY_MODULE_1"] = DUMMY_MODULE_1
        fob.reset_mock()

        lso.update_watched_modules()

        fob.assert_not_called()

        # Reset the config object.
        config.set_option("server.folderWatchBlacklist", prev_blacklist)
Esempio n. 22
0
def start_listening_tcp_socket(http_server):
    call_count = 0

    while call_count < MAX_PORT_SEARCH_RETRIES:
        address = config.get_option("server.address")
        port = config.get_option("server.port")

        try:
            http_server.listen(port, address)
            break  # It worked! So let's break out of the loop.

        except (OSError, socket.error) as e:
            if e.errno == errno.EADDRINUSE:
                if server_port_is_manually_set():
                    LOGGER.error("Port %s is already in use", port)
                    sys.exit(1)
                else:
                    LOGGER.debug(
                        "Port %s already in use, trying to use the next one.", port
                    )
                    port += 1
                    # Save port 3000 because it is used for the development
                    # server in the front end.
                    if port == 3000:
                        port += 1

                    config.set_option(
                        "server.port", port, ConfigOption.STREAMLIT_DEFINITION
                    )
                    call_count += 1
            else:
                raise

    if call_count >= MAX_PORT_SEARCH_RETRIES:
        raise RetriesExceeded(
            "Cannot start Streamlit server. Port %s is already in use, and "
            "Streamlit was unable to find a free port after %s attempts.",
            port,
            MAX_PORT_SEARCH_RETRIES,
        )
Esempio n. 23
0
    def test_Credentials_activate(self, headless_mode):
        """Test Credentials.activate()"""
        c = Credentials.get_current()
        c.activation = None

        config.set_option("server.headless", headless_mode)

        with patch.object(
                c, "load",
                side_effect=RuntimeError("Some error")), patch.object(
                    c,
                    "save") as patched_save, patch(PROMPT) as patched_prompt:

            patched_prompt.side_effect = ["*****@*****.**"]
            c.activate()
            patched_save.assert_called_once()

            if headless_mode:
                self.assertEqual(c.activation.email, "")
            else:
                self.assertEqual(c.activation.email, "*****@*****.**")

            self.assertEqual(c.activation.is_valid, True)
Esempio n. 24
0
    def test_disabled_metrics_check_value(self):
        """Test streamlit.metrics.Client.toggle_metrics disabled check value."""
        with patch("streamlit.metrics.MockMetric", spec=True) as mock_metric:
            config.set_option("global.metrics", False)
            client = streamlit.metrics.Client.get_current()
            client._metrics = {}

            # yapf: disable
            client._raw_metrics = [
                ('Counter', 'unittest_counter', 'Unittest counter', []),
                ('Counter', 'unittest_counter_labels', 'Unittest counter labels', ['label']),
                ('Gauge', 'unittest_gauge', 'Unittest gauge', []),
            ]
            # yapf: enable

            client.toggle_metrics()

            # Test that handler in Server.py will return nothing.
            self.assertEqual(client.generate_latest(), "")

            client.get("unittest_counter").inc()
            client.get("unittest_counter_labels").labels("some_label")
            client.get("unittest_gauge").set(42)
            client.get("unittest_gauge").dec()

            calls = [
                call(),  # Constructor
                call(),  # unittest_counter
                call(),  # unittest_counter_labels
                call(),  # unittest_gauge
                call().inc(),
                call().labels("some_label"),
                call().set(42),
                call().dec(),
            ]
            self.assertEqual(calls, mock_metric.mock_calls)
Esempio n. 25
0
 def test_global_log_level(self):
     config.set_option("global.developmentMode", False)
     self.assertEqual("info", config.get_option("logger.level"))
Esempio n. 26
0
 def test_global_log_level_debug(self):
     config.set_option("global.developmentMode", True)
     self.assertEqual("debug", config.get_option("logger.level"))
Esempio n. 27
0
 def test_global_dev_mode(self):
     config.set_option("global.developmentMode", True)
     self.assertEqual(True, config.get_option("global.developmentMode"))
Esempio n. 28
0
 def test_server_headless_via_liveSave(self):
     config.set_option("server.liveSave", True)
     self.assertEqual(True, config.get_option("server.headless"))
Esempio n. 29
0
 def test_browser_server_port(self):
     # developmentMode must be False for server.port to be modified
     config.set_option("global.developmentMode", False)
     config.set_option("server.port", 1234)
     self.assertEqual(1234, config.get_option("browser.serverPort"))
Esempio n. 30
0
 def tearDown(self):
     set_option("global.sharingMode", "off")