def read_config(dirname):
     local_state = LocalStateFile.load_for_directory(dirname)
     requirement = _redis_requirement()
     provider = RedisProvider()
     config = provider.read_config(requirement, dict(), local_state, 'default', UserConfigOverrides())
     assert 6380 == config['lower_port']
     assert 6449 == config['upper_port']
 def read_config(dirname):
     local_state = LocalStateFile.load_for_directory(dirname)
     requirement = _redis_requirement()
     provider = RedisProvider()
     config = provider.read_config(requirement, dict(), local_state, 'default', UserConfigOverrides())
     # revert to defaults
     assert 6380 == config['lower_port']
     assert 6449 == config['upper_port']
     # should have printed an error
     out, err = capsys.readouterr()
     assert ("Invalid port_range '%s', should be like '6380-6449'\n" % (port_range)) == err
    def prepare_after_setting_scope(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        requirement = _redis_requirement()
        provider = RedisProvider()
        environ = minimal_environ()
        config = provider.read_config(requirement, environ, local_state, 'default', UserConfigOverrides())
        assert config['source'] == 'find_all'
        provider.set_config_values_as_strings(requirement,
                                              environ,
                                              local_state,
                                              'default',
                                              UserConfigOverrides(),
                                              dict(source='find_project'))
        config = provider.read_config(requirement, environ, local_state, 'default', UserConfigOverrides())
        assert config['source'] == 'find_project'
        provider.set_config_values_as_strings(requirement,
                                              environ,
                                              local_state,
                                              'default',
                                              UserConfigOverrides(),
                                              dict(source='find_all'))
        config = provider.read_config(requirement, environ, local_state, 'default', UserConfigOverrides())
        assert config['source'] == 'find_all'
        provider.set_config_values_as_strings(requirement,
                                              environ,
                                              local_state,
                                              'default',
                                              UserConfigOverrides(),
                                              dict(source='environ'))
        config = provider.read_config(requirement, environ, local_state, 'default', UserConfigOverrides())
        assert config['source'] == 'find_all'  # default if no env var set
        provider.set_config_values_as_strings(requirement,
                                              environ,
                                              local_state,
                                              'default',
                                              UserConfigOverrides(),
                                              dict(source='environ'))
        environ_with_redis_url = environ.copy()
        environ_with_redis_url['REDIS_URL'] = 'blah'
        config = provider.read_config(requirement, environ_with_redis_url, local_state, 'default',
                                      UserConfigOverrides())
        assert config['source'] == 'environ'  # default when the env var IS set

        # use local variable when env var not set
        provider.set_config_values_as_strings(requirement,
                                              environ,
                                              local_state,
                                              'default',
                                              UserConfigOverrides(),
                                              dict(source='variables',
                                                   value='foo'))
        config = provider.read_config(requirement, environ, local_state, 'default', UserConfigOverrides())
        assert config['source'] == 'variables'
        assert config['value'] == 'foo'

        # use local variable when env var _is_ set
        provider.set_config_values_as_strings(requirement,
                                              environ_with_redis_url,
                                              local_state,
                                              'default',
                                              UserConfigOverrides(),
                                              dict(source='variables',
                                                   value='foo'))
        config = provider.read_config(requirement, environ, local_state, 'default', UserConfigOverrides())
        assert config['source'] == 'variables'
        assert config['value'] == 'foo'

        # set to use system, which should override using the local state
        provider.set_config_values_as_strings(requirement,
                                              environ,
                                              local_state,
                                              'default',
                                              UserConfigOverrides(),
                                              dict(source='find_system'))
        config = provider.read_config(requirement, environ, local_state, 'default', UserConfigOverrides())
        assert config['source'] == 'find_system'

        project = project_no_dedicated_env(dirname)
        result = _prepare_printing_errors(project, environ=minimal_environ())
        assert result
        assert dict(REDIS_URL="redis://localhost:6379",
                    PROJECT_DIR=project.directory_path) == strip_environ(result.environ)
        assert dict(host='localhost', port=6379, timeout_seconds=0.5) == can_connect_args
    def set_config(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        requirement = _redis_requirement()
        provider = RedisProvider()
        provider.set_config_values_as_strings(requirement,
                                              dict(),
                                              local_state,
                                              'default',
                                              UserConfigOverrides(),
                                              dict(lower_port="6001"))
        config = provider.read_config(requirement, dict(), local_state, 'default', UserConfigOverrides())
        assert config['lower_port'] == 6001
        assert config['upper_port'] == 6449

        provider.set_config_values_as_strings(requirement,
                                              dict(),
                                              local_state,
                                              'default',
                                              UserConfigOverrides(),
                                              dict(upper_port="6700"))
        config2 = provider.read_config(requirement, dict(), local_state, 'default', UserConfigOverrides())
        assert config2['lower_port'] == 6001
        assert config2['upper_port'] == 6700

        provider.set_config_values_as_strings(requirement,
                                              dict(),
                                              local_state,
                                              'default',
                                              UserConfigOverrides(),
                                              dict(lower_port="5500",
                                                   upper_port="6800"))
        config2 = provider.read_config(requirement, dict(), local_state, 'default', UserConfigOverrides())
        assert config2['lower_port'] == 5500
        assert config2['upper_port'] == 6800