Пример #1
0
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
Пример #2
0
 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
Пример #3
0
    def do_test(dirname):
        io_loop = IOLoop()
        io_loop.make_current()

        events = []

        def event_handler(event):
            events.append(event)

        project = Project(dirname)
        local_state_file = LocalStateFile.load_for_directory(dirname)

        requirement = EnvVarRequirement(registry=project.plugin_registry,
                                        env_var="FOO")
        status = requirement.check_status(dict(), local_state_file, 'default',
                                          UserConfigOverrides())
        context = ConfigurePrepareContext(dict(), local_state_file, 'default',
                                          UserConfigOverrides(), [status])
        server = UIServer(project, _no_op_prepare(context), event_handler,
                          io_loop)

        # do a get so that _requirements_by_id below exists
        get_response = http_get(io_loop, server.url)
        assert 200 == get_response.code

        req_id = list(server._application._requirements_by_id.keys())[0]
        if '%s' in name_template:
            name = name_template % req_id
        else:
            name = name_template

        encoder = MultipartEncoder({name: 'bloop'})
        body = encoder.to_string()
        headers = {'Content-Type': encoder.content_type}

        post_response = http_post(io_loop,
                                  server.url,
                                  body=body,
                                  headers=headers)
        # we just ignore bad form names, because they are assumed
        # to be some sort of hostile thing. we shouldn't ever
        # generate them on purpose.
        assert 200 == post_response.code

        server.unlisten()

        assert len(events) == 1
        assert isinstance(events[0], UIServerDoneEvent)

        out, err = capsys.readouterr()
        assert out == ""
        assert err == expected_err
Пример #4
0
 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)
Пример #5
0
    def do_test(dirname):
        io_loop = IOLoop()
        io_loop.make_current()

        events = []

        def event_handler(event):
            events.append(event)

        local_state_file = LocalStateFile.load_for_directory(dirname)

        value = local_state_file.get_value(['variables', 'FOO'])
        assert value is None

        project = Project(dirname)
        requirement = EnvVarRequirement(registry=project.plugin_registry,
                                        env_var="FOO")
        status = requirement.check_status(dict(), local_state_file, 'default',
                                          UserConfigOverrides())
        context = ConfigurePrepareContext(dict(), local_state_file, 'default',
                                          UserConfigOverrides(), [status])
        server = UIServer(project, _no_op_prepare(context), event_handler,
                          io_loop)

        get_response = http_get(io_loop, server.url)
        print(repr(get_response))

        soup = BeautifulSoup(get_response.body, _BEAUTIFUL_SOUP_BACKEND)
        field = soup.find_all("input", attrs={'type': 'text'})[0]

        assert 'name' in field.attrs

        encoder = MultipartEncoder({field['name']: 'bloop'})
        body = encoder.to_string()
        headers = {'Content-Type': encoder.content_type}

        post_response = http_post(io_loop,
                                  server.url,
                                  body=body,
                                  headers=headers)
        print(repr(post_response))

        server.unlisten()

        assert len(events) == 1
        assert isinstance(events[0], UIServerDoneEvent)

        value = local_state_file.get_value(['variables', 'FOO'])
        assert 'bloop' == value
Пример #6
0
    def _parse(cls, registry, varname, item, problems, requirements):
        """Parse an item from the services: section."""
        service_type = None
        if is_string(item):
            service_type = item
            options = dict(type=service_type)
        elif isinstance(item, dict):
            service_type = item.get('type', None)
            if service_type is None:
                problems.append(
                    "Service {} doesn't contain a 'type' field.".format(
                        varname))
                return
            options = deepcopy(item)
        else:
            problems.append(
                "Service {} should have a service type string or a dictionary as its value."
                .format(varname))
            return

        if not EnvVarRequirement._parse_default(options, varname, problems):
            return

        requirement = registry.find_requirement_by_service_type(
            service_type=service_type, env_var=varname, options=options)
        if requirement is None:
            problems.append("Service {} has an unknown type '{}'.".format(
                varname, service_type))
        else:
            assert isinstance(requirement, ServiceRequirement)
            assert 'type' in requirement.options
            requirements.append(requirement)
Пример #7
0
    def _parse(cls, registry, varname, item, problems, requirements):
        """Parse an item from the services: section."""
        service_type = None
        if is_string(item):
            service_type = item
            options = dict(type=service_type)
        elif isinstance(item, dict):
            service_type = item.get('type', None)
            if service_type is None:
                problems.append("Service {} doesn't contain a 'type' field.".format(varname))
                return
            options = deepcopy(item)
        else:
            problems.append("Service {} should have a service type string or a dictionary as its value.".format(
                varname))
            return

        if not EnvVarRequirement._parse_default(options, varname, problems):
            return

        requirement = registry.find_requirement_by_service_type(service_type=service_type,
                                                                env_var=varname,
                                                                options=options)
        if requirement is None:
            problems.append("Service {} has an unknown type '{}'.".format(varname, service_type))
        else:
            assert isinstance(requirement, ServiceRequirement)
            assert 'type' in requirement.options
            requirements.append(requirement)
Пример #8
0
    def do_test(dirname):
        io_loop = IOLoop()
        io_loop.make_current()

        events = []

        def event_handler(event):
            events.append(event)

        project = Project(dirname)
        local_state_file = LocalStateFile.load_for_directory(dirname)

        requirement = EnvVarRequirement(registry=project.plugin_registry, env_var="FOO")
        status = requirement.check_status(dict(), local_state_file, 'default', UserConfigOverrides())
        context = ConfigurePrepareContext(dict(), local_state_file, 'default', UserConfigOverrides(), [status])
        server = UIServer(project, _no_op_prepare(context), event_handler, io_loop)

        # do a get so that _requirements_by_id below exists
        get_response = http_get(io_loop, server.url)
        assert 200 == get_response.code

        req_id = list(server._application._requirements_by_id.keys())[0]
        if '%s' in name_template:
            name = name_template % req_id
        else:
            name = name_template

        encoder = MultipartEncoder({name: 'bloop'})
        body = encoder.to_string()
        headers = {'Content-Type': encoder.content_type}

        post_response = http_post(io_loop, server.url, body=body, headers=headers)
        # we just ignore bad form names, because they are assumed
        # to be some sort of hostile thing. we shouldn't ever
        # generate them on purpose.
        assert 200 == post_response.code

        server.unlisten()

        assert len(events) == 1
        assert isinstance(events[0], UIServerDoneEvent)

        out, err = capsys.readouterr()
        assert out == ""
        assert err == expected_err
Пример #9
0
    def _update_variables(self, requirements, problems, project_file):
        variables = project_file.get_value("variables")

        def check_conda_reserved(key):
            if key in ('CONDA_DEFAULT_ENV', 'CONDA_ENV_PATH', 'CONDA_PREFIX'):
                problems.append(("Environment variable %s is reserved for Conda's use, " +
                                 "so it can't appear in the variables section.") % key)
                return True
            else:
                return False

        # variables: section can contain a list of var names or a dict from
        # var names to options OR default values. it can also be missing
        # entirely which is the same as empty.
        if variables is None:
            pass
        elif isinstance(variables, dict):
            for key in variables.keys():
                if check_conda_reserved(key):
                    continue
                if key.strip() == '':
                    problems.append("Variable name cannot be empty string, found: '{}' as name".format(key))
                    continue
                raw_options = variables[key]

                if raw_options is None:
                    options = {}
                elif isinstance(raw_options, dict):
                    options = deepcopy(raw_options)  # so we can modify it below
                else:
                    options = dict(default=raw_options)

                assert (isinstance(options, dict))

                if EnvVarRequirement._parse_default(options, key, problems):
                    requirement = self.registry.find_requirement_by_env_var(key, options)
                    requirements.append(requirement)
        elif isinstance(variables, list):
            for item in variables:
                if is_string(item):
                    if item.strip() == '':
                        problems.append("Variable name cannot be empty string, found: '{}' as name".format(item))
                        continue
                    if check_conda_reserved(item):
                        continue
                    requirement = self.registry.find_requirement_by_env_var(item, options=dict())
                    requirements.append(requirement)
                else:
                    problems.append(
                        "variables section should contain environment variable names, {item} is not a string".format(
                            item=item))
        else:
            problems.append(
                "variables section contains wrong value type {value}, should be dict or list of requirements".format(
                    value=variables))
Пример #10
0
    def do_test(dirname):
        io_loop = IOLoop()
        io_loop.make_current()

        events = []

        def event_handler(event):
            events.append(event)

        local_state_file = LocalStateFile.load_for_directory(dirname)

        value = local_state_file.get_value(['variables', 'FOO'])
        assert value is None

        project = Project(dirname)
        requirement = EnvVarRequirement(registry=project.plugin_registry, env_var="FOO")
        status = requirement.check_status(dict(), local_state_file, 'default', UserConfigOverrides())
        context = ConfigurePrepareContext(dict(), local_state_file, 'default', UserConfigOverrides(), [status])
        server = UIServer(project, _no_op_prepare(context), event_handler, io_loop)

        get_response = http_get(io_loop, server.url)
        print(repr(get_response))

        soup = BeautifulSoup(get_response.body, _BEAUTIFUL_SOUP_BACKEND)
        field = soup.find_all("input", attrs={'type': 'text'})[0]

        assert 'name' in field.attrs

        encoder = MultipartEncoder({field['name']: 'bloop'})
        body = encoder.to_string()
        headers = {'Content-Type': encoder.content_type}

        post_response = http_post(io_loop, server.url, body=body, headers=headers)
        print(repr(post_response))

        server.unlisten()

        assert len(events) == 1
        assert isinstance(events[0], UIServerDoneEvent)

        value = local_state_file.get_value(['variables', 'FOO'])
        assert 'bloop' == value
Пример #11
0
    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")
Пример #12
0
    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
Пример #13
0
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)
Пример #14
0
def test_requirement_repr():
    requirement = EnvVarRequirement(registry=PluginRegistry(), env_var='FOO')
    assert "EnvVarRequirement(env_var='FOO')" == repr(requirement)
Пример #15
0
 def req(env_var, options=None):
     return EnvVarRequirement(registry=PluginRegistry(), env_var=env_var, options=options)
Пример #16
0
    def _update_variables(self, requirements, problems, project_file):
        variables = project_file.get_value("variables")

        def check_conda_reserved(key):
            if key in ('CONDA_DEFAULT_ENV', 'CONDA_ENV_PATH', 'CONDA_PREFIX'):
                problems.append(
                    ("Environment variable %s is reserved for Conda's use, " +
                     "so it can't appear in the variables section.") % key)
                return True
            else:
                return False

        # variables: section can contain a list of var names or a dict from
        # var names to options OR default values. it can also be missing
        # entirely which is the same as empty.
        if variables is None:
            pass
        elif isinstance(variables, dict):
            for key in variables.keys():
                if check_conda_reserved(key):
                    continue
                if key.strip() == '':
                    problems.append(
                        "Variable name cannot be empty string, found: '{}' as name"
                        .format(key))
                    continue
                raw_options = variables[key]

                if raw_options is None:
                    options = {}
                elif isinstance(raw_options, dict):
                    options = deepcopy(
                        raw_options)  # so we can modify it below
                else:
                    options = dict(default=raw_options)

                assert (isinstance(options, dict))

                if EnvVarRequirement._parse_default(options, key, problems):
                    requirement = self.registry.find_requirement_by_env_var(
                        key, options)
                    requirements.append(requirement)
        elif isinstance(variables, list):
            for item in variables:
                if is_string(item):
                    if item.strip() == '':
                        problems.append(
                            "Variable name cannot be empty string, found: '{}' as name"
                            .format(item))
                        continue
                    if check_conda_reserved(item):
                        continue
                    requirement = self.registry.find_requirement_by_env_var(
                        item, options=dict())
                    requirements.append(requirement)
                else:
                    problems.append(
                        "variables section should contain environment variable names, {item} is not a string"
                        .format(item=item))
        else:
            problems.append(
                "variables section contains wrong value type {value}, should be dict or list of requirements"
                .format(value=variables))