def test_meta_kernel_finder(): kf = discovery.KernelFinder(providers=[DummyKernelProvider()]) assert list(kf.find_kernels()) == \ [('dummy/sample', {'argv': ['dummy_kernel']})] conn_info, manager = kf.launch('dummy/sample') assert isinstance(manager, DummyKernelManager)
async def test_kernel_launch_params(caplog, setup_test): kf = discovery.KernelFinder(providers=[LaunchParamsKernelProvider()]) kspecs = list(kf.find_kernels()) count = 0 param_spec = None for name, spec in kspecs: if name == 'params_kspec/params_kspec': param_spec = spec count += 1 assert count == 1 assert param_spec['argv'] == [ 'tail', '{follow}', '-n {line_count}', '{connection_file}' ] # application gathers launch parameters here... Since this is full schema, application will likely # just access: param_spec['metadata']['launch_parameter_schema'] # line_count_schema = param_spec['metadata']['launch_parameter_schema'][ 'properties']['line_count'] follow_schema = param_spec['metadata']['launch_parameter_schema'][ 'properties']['follow'] cpus_schema = param_spec['metadata']['launch_parameter_schema'][ 'properties']['cpus'] memory_schema = param_spec['metadata']['launch_parameter_schema'][ 'properties']['memory'] # validate we have our metadata assert line_count_schema['minimum'] == 1 assert follow_schema['default'] == '-f' assert cpus_schema['maximum'] == 8.0 assert memory_schema[ 'description'] == "The number of GB to reserve for memory for this kernel" # Kernel provider would be responsible for validating values against the schema upon return from client. # This includes setting any default values for parameters that were not included, etc. The following # simulates the parameter gathering... launch_params = dict() launch_params['follow'] = follow_schema['enum'][0] launch_params['line_count'] = 8 launch_params['cpus'] = cpus_schema['default'] # add a "system-owned" parameter - connection_file - ensure this value is NOT substituted. launch_params['connection_file'] = 'bad_param' # capture DEBUG output in order to confirm argv substitutions with caplog.at_level(logging.DEBUG): conn_info, manager = await kf.launch('params_kspec/params_kspec', launch_params=launch_params) assert isinstance(manager, KernelManager) assert "Starting kernel cmd: ['tail', '-f', '-n 8'," in caplog.text # this actually starts a tail -f command, so let's make sure its terminated await manager.kill() await manager.cleanup()
def test_reinstantiate(): # If the kernel fails, a new manager should be instantiated kf = discovery.KernelFinder(providers=[DummyKernelProvider()]) _, manager = kf.launch('dummy/sample') manager.kill() restarter = KernelRestarterBase(manager, 'dummy/sample', kernel_finder=kf) assert restarter.kernel_manager is manager restarter.poll() assert restarter.kernel_manager is not manager assert restarter.kernel_manager.is_alive()
async def test_kernel_spec_provider_subclass(setup_test): kf = discovery.KernelFinder(providers=[DummyKernelSpecProvider()]) dummy_kspecs = list(kf.find_kernels()) assert len(dummy_kspecs) == 2 for name, spec in dummy_kspecs: assert name.startswith('dummy_kspec/dummy_kspec') assert spec['argv'] == ['cat', '{connection_file}'] conn_info, manager = await kf.launch('dummy_kspec/dummy_kspec1') assert isinstance(manager, DummyKernelManager) await manager.kill( ) # no process was started, so this is only for completeness
async def test_load_config(setup_test): # create fake application app = ProviderApplication() app.launch_instance(argv=["--ProviderConfig.my_argv=['xxx','yyy']"]) kf = discovery.KernelFinder(providers=[TestConfigKernelProvider()]) dummy_kspecs = list(kf.find_kernels()) count = 0 found_argv = [] for name, spec in dummy_kspecs: if name == 'config/sample': found_argv = spec['argv'] count += 1 assert count == 1 assert found_argv == ['xxx', 'yyy']
def test_relaunch(): # If the kernel fails after the first poll, its manager's relaunch() method # should be called. kf = discovery.KernelFinder(providers=[DummyKernelProvider()]) manager = kf.launch('dummy/sample') relaunch_count = [0] def relaunch(): relaunch_count[0] += 1 manager.relaunch = relaunch restarter = KernelRestarterBase(manager, 'dummy/sample', kernel_finder=kf) restarter.poll() assert relaunch_count[0] == 0 # Kernel dies after first poll manager.kill() restarter.poll() assert relaunch_count[0] == 1 assert restarter.kernel_manager is manager
async def test_kernel_spec_provider(setup_test): kf = discovery.KernelFinder(providers=[discovery.KernelSpecProvider()]) dummy_kspecs = list(kf.find_kernels()) count = 0 found_argv = [] for name, spec in dummy_kspecs: if name == 'spec/sample': found_argv = spec['argv'] count += 1 assert count == 1 assert found_argv == ['cat', '{connection_file}'] with pytest.raises(kernelspec.NoSuchKernel): await kf.launch('spec/dummy_kspec1') conn_info, manager = await kf.launch('spec/sample') assert isinstance(manager, KernelManager) # this actually starts a kernel, so let's make sure its terminated await manager.kill() await manager.cleanup()