def test_requirement_default_as_string(): no_default = dict() string_default = dict(default="foo") int_default = dict(default=42) float_default = dict(default=3.14) req = EnvVarRequirement(registry=PluginRegistry(), env_var='FOO', options=no_default) assert req.default_as_string is None req = EnvVarRequirement(registry=PluginRegistry(), env_var='FOO', options=string_default) assert req.default_as_string == "foo" req = EnvVarRequirement(registry=PluginRegistry(), env_var='FOO', options=int_default) assert req.default_as_string == "42" req = EnvVarRequirement(registry=PluginRegistry(), env_var='FOO', options=float_default) assert req.default_as_string == "3.14"
def test_find_by_env_var_unknown(): registry = PluginRegistry() found = registry.find_requirement_by_env_var(env_var='FOO', options=None) assert found is not None assert isinstance(found, EnvVarRequirement) assert found.env_var == 'FOO' assert "EnvVarRequirement(env_var='FOO')" == repr(found)
def test_find_by_service_type_redis(): registry = PluginRegistry() found = registry.find_requirement_by_service_type(service_type='redis', env_var='MYREDIS', options=dict()) assert found is not None assert isinstance(found, RedisRequirement) assert found.env_var == 'MYREDIS' assert found.service_type == 'redis'
def check_not_set(dirname): local_state = LocalStateFile.load_for_directory(dirname) requirement = RedisRequirement(registry=PluginRegistry(), env_var="REDIS_URL") status = requirement.check_status(dict(), local_state, 'default', UserConfigOverrides()) assert not status assert "Environment variable REDIS_URL is not set." == status.status_description
def test_empty_variable_treated_as_unset(): requirement = EnvVarRequirement(registry=PluginRegistry(), env_var='FOO') status = requirement.check_status(dict(FOO=''), tmp_local_state_file(), 'default', UserConfigOverrides()) assert not status assert "Environment variable FOO is not set." == status.status_description assert [] == status.logs assert [] == status.errors
def check_bad_scheme(dirname): local_state = LocalStateFile.load_for_directory(dirname) requirement = RedisRequirement(registry=PluginRegistry(), env_var="REDIS_URL") status = requirement.check_status( dict(REDIS_URL="http://example.com/"), local_state, 'default', UserConfigOverrides()) assert not status assert "REDIS_URL value 'http://example.com/' does not have 'redis:' scheme." == status.status_description
def test_checksum_is_not_a_string(): problems = [] requirements = [] DownloadRequirement._parse(PluginRegistry(), varname='FOO', item=dict(url='http://example.com/', md5=[]), problems=problems, requirements=requirements) assert ['Checksum value for FOO should be a string not [].'] == problems assert len(requirements) == 0
def test_description_is_not_a_string(): problems = [] requirements = [] DownloadRequirement._parse(PluginRegistry(), varname='FOO', item=dict(url='http://example.com/', description=[]), problems=problems, requirements=requirements) assert ["'description' field for download item FOO is not a string" ] == problems assert len(requirements) == 0
def check_missing_package(dirname): requirement = CondaEnvRequirement( registry=PluginRegistry(), env_specs=dict(default=EnvSpec( 'default', ['boguspackage', 'boguspackage2'], []))) project_dir_disable_dedicated_env(dirname) local_state = LocalStateFile.load_for_directory(dirname) environ = minimal_environ(PROJECT_DIR=dirname) status = requirement.check_status( environ, local_state, 'default', UserConfigOverrides(inherited_env=environ.get(conda_env_var))) assert "Conda environment is missing packages: boguspackage, boguspackage2" == status.status_description
def test_download_item_is_none_not_a_string_or_dict(): problems = [] requirements = [] DownloadRequirement._parse(PluginRegistry(), varname='FOO', item=None, problems=problems, requirements=requirements) assert [ "Download name FOO should be followed by a URL string or a dictionary describing the download." ] == problems assert len(requirements) == 0
def test_unzip_is_not_a_bool(): problems = [] requirements = [] DownloadRequirement._parse(PluginRegistry(), varname='FOO', item=dict(url='http://example.com/', unzip=[]), problems=problems, requirements=requirements) assert [ "Value of 'unzip' for download item FOO should be a boolean, not []." ] == problems assert len(requirements) == 0
def check_not_set(dirname): local_state = LocalStateFile.load_for_directory(dirname) requirement = DownloadRequirement(registry=PluginRegistry(), env_var=ENV_VAR, url='http://example.com', filename=ENV_VAR) status = requirement.check_status(dict(PROJECT_DIR=dirname), local_state, 'default', UserConfigOverrides()) assert not status assert "Environment variable {} is not set.".format( ENV_VAR) == status.status_description
def test_use_variable_name_for_filename(): problems = [] requirements = [] DownloadRequirement._parse(PluginRegistry(), varname='FOO', item='http://example.com/', problems=problems, requirements=requirements) assert [] == problems assert len(requirements) == 1 assert requirements[0].filename == 'FOO' assert requirements[0].url == 'http://example.com/' assert not requirements[0].unzip
def test_description_property(): problems = [] requirements = [] DownloadRequirement._parse(PluginRegistry(), varname='FOO', item=dict(url='http://example.com/', description="hi"), problems=problems, requirements=requirements) assert [] == problems assert len(requirements) == 1 assert requirements[0].title == 'FOO' assert requirements[0].description == 'hi'
def check_provide_contents(dirname): environ = dict(foo='bar') local_state_file = LocalStateFile.load_for_directory(dirname) requirement = EnvVarRequirement(PluginRegistry(), env_var="FOO") status = requirement.check_status(environ, local_state_file, 'default', UserConfigOverrides()) context = ProvideContext(environ=environ, local_state_file=local_state_file, default_env_spec_name='default', status=status, mode=PROVIDE_MODE_DEVELOPMENT) assert dict(foo='bar') == context.environ assert context.status is status
def test_use_unzip_if_url_ends_in_zip(): problems = [] requirements = [] DownloadRequirement._parse(PluginRegistry(), varname='FOO', item='http://example.com/bar.zip', problems=problems, requirements=requirements) assert [] == problems assert len(requirements) == 1 assert requirements[0].filename == 'bar' assert requirements[0].url == 'http://example.com/bar.zip' assert requirements[0].unzip
def test_no_unzip_if_url_ends_in_zip_and_filename_also_does(): problems = [] requirements = [] DownloadRequirement._parse(PluginRegistry(), varname='FOO', item=dict(url='http://example.com/bar.zip', filename='something.zip'), problems=problems, requirements=requirements) assert [] == problems assert len(requirements) == 1 assert requirements[0].filename == 'something.zip' assert requirements[0].url == 'http://example.com/bar.zip' assert not requirements[0].unzip
def test_allow_manual_override_of_use_unzip_if_url_ends_in_zip(): problems = [] requirements = [] DownloadRequirement._parse(PluginRegistry(), varname='FOO', item=dict(url='http://example.com/bar.zip', unzip=False), problems=problems, requirements=requirements) assert [] == problems assert len(requirements) == 1 assert requirements[0].filename == 'bar.zip' assert requirements[0].url == 'http://example.com/bar.zip' assert not requirements[0].unzip
def check_provide_contents(dirname): environ = dict() local_state_file = LocalStateFile.load_for_directory(dirname) requirement = EnvVarRequirement(PluginRegistry(), env_var="FOO") status = requirement.check_status(environ, local_state_file, 'default', UserConfigOverrides()) context = ProvideContext(environ=environ, local_state_file=local_state_file, default_env_spec_name='default', status=status, mode=PROVIDE_MODE_DEVELOPMENT) with pytest.raises(IOError) as excinfo: context.ensure_service_directory("foo") assert "this is not EEXIST" in repr(excinfo.value)
def downloaded_file_valid(dirname): local_state = LocalStateFile.load_for_directory(dirname) filename = os.path.join(dirname, 'data.zip') requirement = DownloadRequirement(registry=PluginRegistry(), env_var=ENV_VAR, url='http://localhost/data.zip', filename='data.zip') status = requirement.check_status( { ENV_VAR: filename, 'PROJECT_DIR': dirname }, local_state, 'default', UserConfigOverrides()) assert status assert 'File downloaded to {}'.format( filename) == status.status_description
def check_missing_filename(dirname): local_state = LocalStateFile.load_for_directory(dirname) filename = '/data.zip' requirement = DownloadRequirement(registry=PluginRegistry(), env_var=ENV_VAR, url='http://localhost/data.zip', filename='data.zip') status = requirement.check_status( { ENV_VAR: filename, 'PROJECT_DIR': dirname }, local_state, 'default', UserConfigOverrides()) assert not status assert 'File not found: {}'.format( filename) == status.status_description
def check_cannot_connect(dirname): local_state = LocalStateFile.load_for_directory(dirname) requirement = RedisRequirement(registry=PluginRegistry(), env_var="REDIS_URL") can_connect_args_list = _monkeypatch_can_connect_to_socket_fails( monkeypatch) status = requirement.check_status( dict(REDIS_URL="redis://example.com:1234/"), local_state, 'default', UserConfigOverrides()) assert dict(host='example.com', port=1234, timeout_seconds=0.5) == can_connect_args_list[0] assert dict(host='localhost', port=6379, timeout_seconds=0.5) == can_connect_args_list[1] assert not status expected = "Cannot connect to Redis at redis://example.com:1234/." assert expected == status.status_description
def check(dirname): _monkeypatch_pwd(monkeypatch, dirname) status = SimpleStatus(success=True, description='Service added.') status.requirement = RedisRequirement(PluginRegistry(), env_var='REDIS_URL', options=dict(type='redis')) _monkeypatch_add_service(monkeypatch, status) code = _parse_args_and_run_subcommand( ['anaconda-project', 'add-service', 'redis']) assert code == 0 out, err = capsys.readouterr() assert ( 'Service added.\n' + 'Added service redis to the project file, its address will be in REDIS_URL.\n' ) == out assert '' == err
def check_fails_while_listing_installed(dirname): def sabotaged_installed_command(prefix): from anaconda_project.internal import conda_api raise conda_api.CondaError("sabotage!") monkeypatch.setattr('anaconda_project.internal.conda_api.installed', sabotaged_installed_command) project_dir_disable_dedicated_env(dirname) local_state = LocalStateFile.load_for_directory(dirname) requirement = CondaEnvRequirement( registry=PluginRegistry(), env_specs=dict( default=EnvSpec('default', ['not_a_real_package'], []))) environ = minimal_environ(PROJECT_DIR=dirname) status = requirement.check_status( environ, local_state, 'default', UserConfigOverrides(inherited_env=environ.get(conda_env_var))) assert status.status_description.startswith( "Conda failed while listing installed packages in ") assert status.status_description.endswith(": sabotage!")
def check_provide_contents(dirname): environ = dict() local_state_file = LocalStateFile.load_for_directory(dirname) requirement = EnvVarRequirement(PluginRegistry(), env_var="FOO") status = requirement.check_status(environ, local_state_file, 'default', UserConfigOverrides()) context = ProvideContext(environ=environ, local_state_file=local_state_file, default_env_spec_name='default', status=status, mode=PROVIDE_MODE_DEVELOPMENT) workpath = context.ensure_service_directory("foo") assert os.path.isdir(workpath) assert workpath.endswith("foo") parent = os.path.dirname(workpath) assert parent.endswith("services") parent = os.path.dirname(parent) assert parent == dirname # be sure we can create if it already exists workpath2 = context.ensure_service_directory("foo") assert os.path.isdir(workpath2) assert workpath == workpath2
def check_provide_contents(dirname): environ = dict() local_state_file = LocalStateFile.load_for_directory(dirname) local_state_file.set_service_run_state("myservice", dict(port=42)) requirement = EnvVarRequirement(PluginRegistry(), env_var="FOO") status = requirement.check_status(environ, local_state_file, 'default', UserConfigOverrides()) context = ProvideContext(environ=environ, local_state_file=local_state_file, default_env_spec_name='default', status=status, mode=PROVIDE_MODE_DEVELOPMENT) def transform_it(state): assert 42 == state['port'] state['port'] = 43 state['foo'] = 'bar' return 1234 result = context.transform_service_run_state("myservice", transform_it) assert 1234 == result assert dict( port=43, foo='bar') == local_state_file.get_service_run_state("myservice")
def test_find_by_service_type_unknown(): registry = PluginRegistry() found = registry.find_requirement_by_service_type(service_type='blah', env_var='FOO', options=dict()) assert found is None
def req(env_var, options=None): return EnvVarRequirement(registry=PluginRegistry(), env_var=env_var, options=options)
def test_requirement_repr(): requirement = EnvVarRequirement(registry=PluginRegistry(), env_var='FOO') assert "EnvVarRequirement(env_var='FOO')" == repr(requirement)
def test_requirement_status_repr(): requirement = EnvVarRequirement(registry=PluginRegistry(), env_var='FOO') status = requirement.check_status(dict(FOO=''), tmp_local_state_file(), 'default', UserConfigOverrides()) assert "RequirementStatus(False,'Environment variable FOO is not set.',EnvVarRequirement(env_var='FOO'))" == repr( status)