async def test_setup_opp_safe_mode( mock_enable_logging, mock_is_virtual_env, mock_mount_local_lib_path, mock_ensure_config_exists, mock_process_op_config_upgrade, loop, ): """Test it works.""" with patch( "openpeerpower.components.browser.setup") as browser_setup, patch( "openpeerpower.config_entries.ConfigEntries.async_domains", return_value=["browser"], ): opp = await bootstrap.async_setup_opp( runner.RuntimeConfig( config_dir=get_test_config_dir(), verbose=False, log_rotate_days=10, log_file="", log_no_color=False, skip_pip=True, safe_mode=True, ), ) assert "safe_mode" in opp.config.components assert len(mock_mount_local_lib_path.mock_calls) == 0 # Validate we didn't try to set up config entry. assert "browser" not in opp.config.components assert len(browser_setup.mock_calls) == 0
async def test_setup_opp_invalid_core_config( mock_enable_logging, mock_is_virtual_env, mock_mount_local_lib_path, mock_ensure_config_exists, mock_process_op_config_upgrade, loop, ): """Test it works.""" with patch( "openpeerpower.config.async_opp_config_yaml", return_value={"openpeerpower": { "non-existing": 1 }}, ): opp = await bootstrap.async_setup_opp( runner.RuntimeConfig( config_dir=get_test_config_dir(), verbose=False, log_rotate_days=10, log_file="", log_no_color=False, skip_pip=True, safe_mode=False, ), ) assert "safe_mode" in opp.config.components
async def test_setup_and_run_opp(opp, tmpdir): """Test we can setup and run.""" test_dir = tmpdir.mkdir("config") default_config = runner.RuntimeConfig(test_dir) with patch("openpeerpower.bootstrap.async_setup_opp", return_value=opp), patch( "threading._shutdown" ), patch("openpeerpower.core.OpenPeerPower.async_run") as mock_run: await runner.setup_and_run_opp(default_config) assert threading._shutdown == thread.deadlock_safe_shutdown assert mock_run.called
async def test_setup_opp( mock_enable_logging, mock_is_virtual_env, mock_mount_local_lib_path, mock_ensure_config_exists, mock_process_op_config_upgrade, caplog, loop, ): """Test it works.""" verbose = Mock() log_rotate_days = Mock() log_file = Mock() log_no_color = Mock() with patch( "openpeerpower.config.async_opp_config_yaml", return_value={ "browser": {}, "frontend": {} }, ), patch.object(bootstrap, "LOG_SLOW_STARTUP_INTERVAL", 5000): opp = await bootstrap.async_setup_opp( runner.RuntimeConfig( config_dir=get_test_config_dir(), verbose=verbose, log_rotate_days=log_rotate_days, log_file=log_file, log_no_color=log_no_color, skip_pip=True, safe_mode=False, ), ) assert "Waiting on integrations to complete setup" not in caplog.text assert "browser" in opp.config.components assert "safe_mode" not in opp.config.components assert len(mock_enable_logging.mock_calls) == 1 assert mock_enable_logging.mock_calls[0][1] == ( opp, verbose, log_rotate_days, log_file, log_no_color, ) assert len(mock_mount_local_lib_path.mock_calls) == 1 assert len(mock_ensure_config_exists.mock_calls) == 1 assert len(mock_process_op_config_upgrade.mock_calls) == 1
async def test_setup_safe_mode_if_no_frontend( mock_enable_logging, mock_is_virtual_env, mock_mount_local_lib_path, mock_ensure_config_exists, mock_process_op_config_upgrade, loop, ): """Test we setup safe mode if frontend didn't load.""" verbose = Mock() log_rotate_days = Mock() log_file = Mock() log_no_color = Mock() with patch( "openpeerpower.config.async_opp_config_yaml", return_value={ "openpeerpower": { "internal_url": "http://192.168.1.100:8123", "external_url": "https://abcdef.ui.nabu.casa", }, "map": {}, "person": { "invalid": True }, }, ): opp = await bootstrap.async_setup_opp( runner.RuntimeConfig( config_dir=get_test_config_dir(), verbose=verbose, log_rotate_days=log_rotate_days, log_file=log_file, log_no_color=log_no_color, skip_pip=True, safe_mode=False, ), ) assert "safe_mode" in opp.config.components assert opp.config.config_dir == get_test_config_dir() assert opp.config.skip_pip assert opp.config.internal_url == "http://192.168.1.100:8123" assert opp.config.external_url == "https://abcdef.ui.nabu.casa"
async def test_setup_opp_takes_longer_than_log_slow_startup( mock_enable_logging, mock_is_virtual_env, mock_mount_local_lib_path, mock_ensure_config_exists, mock_process_op_config_upgrade, caplog, loop, ): """Test it works.""" verbose = Mock() log_rotate_days = Mock() log_file = Mock() log_no_color = Mock() async def _async_setup_that_blocks_startup(*args, **kwargs): await asyncio.sleep(0.6) return True with patch( "openpeerpower.config.async_opp_config_yaml", return_value={ "browser": {}, "frontend": {} }, ), patch.object(bootstrap, "LOG_SLOW_STARTUP_INTERVAL", 0.3), patch.object( bootstrap, "SLOW_STARTUP_CHECK_INTERVAL", 0.05), patch( "openpeerpower.components.frontend.async_setup", side_effect=_async_setup_that_blocks_startup, ): await bootstrap.async_setup_opp( runner.RuntimeConfig( config_dir=get_test_config_dir(), verbose=verbose, log_rotate_days=log_rotate_days, log_file=log_file, log_no_color=log_no_color, skip_pip=True, safe_mode=False, ), ) assert "Waiting on integrations to complete setup" in caplog.text
async def test_setup_opp_config_dir_nonexistent( mock_enable_logging, mock_is_virtual_env, mock_mount_local_lib_path, mock_ensure_config_exists, mock_process_op_config_upgrade, loop, ): """Test it works.""" mock_ensure_config_exists.return_value = False assert (await bootstrap.async_setup_opp( runner.RuntimeConfig( config_dir=get_test_config_dir(), verbose=False, log_rotate_days=10, log_file="", log_no_color=False, skip_pip=True, safe_mode=False, ), ) is None)
async def test_setup_opp_invalid_yaml( mock_enable_logging, mock_is_virtual_env, mock_mount_local_lib_path, mock_ensure_config_exists, mock_process_op_config_upgrade, loop, ): """Test it works.""" with patch("openpeerpower.config.async_opp_config_yaml", side_effect=OpenPeerPowerError): opp = await bootstrap.async_setup_opp( runner.RuntimeConfig( config_dir=get_test_config_dir(), verbose=False, log_rotate_days=10, log_file="", log_no_color=False, skip_pip=True, safe_mode=False, ), ) assert "safe_mode" in opp.config.components assert len(mock_mount_local_lib_path.mock_calls) == 0