Ejemplo n.º 1
0
def test_put_template_named_no_meta():
    mpt = MockTemplateApis()
    es = MockElasticsearch(mpt)
    mappings = {'pbench-run': {'meta': {"version": 0}}}
    body = dict(one=1, two=2, mappings=mappings)
    with pytest.raises(TemplateException):
        put_template(
            es, name="mytemplate", mapping_name="prefix-mapname0", body=body
        )
Ejemplo n.º 2
0
def test_put_template_not_retried():
    # Assert that a 500 error is raised as a TransportError exception
    mpt = MockTemplateApis(behavior=["501"])
    es = MockElasticsearch(mpt)
    mappings = {'prefix-mapname0': {'_meta': {'version': 0}}}
    body = dict(one=1, two=2, mappings=mappings)
    with pytest.raises(es_excs.TransportError):
        put_template(
            es, name="mytemplate", mapping_name="prefix-mapname0", body=body
        )
    assert mpt.name == "mytemplate"
    assert mpt.body == body
Ejemplo n.º 3
0
def test_put_template_exc():
    # Assert that if es.indices.put_template() raises an unknown exception
    # it is passed along to the caller.
    mpt = MockTemplateApis(behavior=["exception"])
    es = MockElasticsearch(mpt)
    mappings = {'prefix-mapname0': {'_meta': {'version': 0}}}
    body = dict(one=1, two=2, mappings=mappings)
    with pytest.raises(MockException):
        put_template(
            es, name="mytemplate", mapping_name="prefix-mapname0", body=body
        )
    assert mpt.name == "mytemplate"
    assert mpt.body == body
Ejemplo n.º 4
0
def test_put_template():
    # Assert that a one-n-done call works as expected
    mpt = MockTemplateApis()
    es = MockElasticsearch(mpt)
    mappings = {'prefix-mapname0': {'_meta': {'version': 0}}}
    body = dict(one=1, two=2, mappings=mappings)
    res = put_template(
        es, name="mytemplate", mapping_name="prefix-mapname0", body=body
    )
    beg, end, retry_count, note = res
    assert beg == 0
    assert end == 1
    assert retry_count == 0
    assert note == "original-no-version"
    assert mpt.name == "mytemplate"
    assert mpt.body == body
Ejemplo n.º 5
0
def test_put_template_noname():
    # Assert that there's nothing requiring a doc type
    mpt = MockTemplateApis()
    es = MockElasticsearch(mpt)
    mappings = {'_meta': {'version': 0}}
    body = dict(one=1, two=2, mappings=mappings)
    res = put_template(
        es, name="mytemplate", mapping_name="prefix-mapname0", body=body
    )
    beg, end, retry_count, note = res
    assert beg == 0
    assert end == 1
    assert retry_count == 0
    assert note == "original-no-version"
    assert mpt.name == "mytemplate"
    assert mpt.body == body
Ejemplo n.º 6
0
    def update_templates(self, es, target_name=None):
        """
        Register with Elasticsearch the set of index templates used by the
        Pbench server.

        Args
            es:             Elasticsearch object to connect to server
            target_name:    Optional template name to update just one template

        Raises
            TemplateError   Problem updating the template to server
        """
        template_names = {t.template_name: t for t in self.templates.values()}
        successes = retries = 0
        beg = end = None
        for name in sorted(template_names.keys()):
            template = template_names[name]
            if target_name is not None and target_name != template.idxname:
                # If we were asked to only load a given template name, skip
                # all non-matching templates.
                continue
            try:
                _beg, _end, _retries, _stat = pyesbulk.put_template(
                    es,
                    name,
                    "pbench-{}".format(template.name),
                    template.body(),
                )
            except Exception as e:
                self.counters["put_template_failures"] += 1
                raise TemplateError(f"Tool {name} update failed: {e}")
            else:
                successes += 1
                if beg is None:
                    beg = _beg
                end = _end
                retries += _retries
        log_action = self.logger.warning if retries > 0 else self.logger.debug
        log_action(
            "done templates (start ts: {}, end ts: {}, duration: {:.2f}s,"
            " successes: {:d}, retries: {:d})",
            tstos(beg),
            tstos(end),
            end - beg,
            successes,
            retries,
        )
Ejemplo n.º 7
0
def test_put_template_retries():
    # Assert that ConnectionErrors and TransportErrors are properly retried
    # until successful.
    mpt = MockTemplateApis(behavior=["ce", "ce", "ce", "500", "503", "504"])
    es = MockElasticsearch(mpt)
    mappings = {'prefix-mapname0': {'_meta': {'version': 0}}}
    body = dict(one=1, two=2, mappings=mappings)
    res = put_template(
        es, name="mytemplate", mapping_name="prefix-mapname0", body=body
    )
    beg, end, retry_count, note = res
    assert beg == 0
    assert end == 1
    assert retry_count == 6
    assert note == "original-no-version"
    assert mpt.name == "mytemplate"
    assert mpt.body == body
Ejemplo n.º 8
0
 def update_templates(self, es, target_name=None):
     """Push the various Elasticsearch index templates required by pbench.
     """
     if target_name is not None:
         idxname = self.index_patterns[target_name]["idxname"]
     else:
         idxname = None
     template_names = [name for name in self.templates]
     template_names.sort()
     successes = retries = 0
     beg = end = None
     for name in template_names:
         if idxname is not None and not name.endswith(idxname):
             # If we were asked to only load a given template name, skip
             # all non-matching templates.
             continue
         try:
             _beg, _end, _retries, _stat = pyesbulk.put_template(
                 es,
                 name,
                 "pbench-{}".format(name.split(".")[2]),
                 self.templates[name],
             )
         except Exception as e:
             self.counters["put_template_failures"] += 1
             raise TemplateError(e)
         else:
             successes += 1
             if beg is None:
                 beg = _beg
             end = _end
             retries += _retries
     log_action = self.logger.warning if retries > 0 else self.logger.debug
     log_action(
         "done templates (start ts: {}, end ts: {}, duration: {:.2f}s,"
         " successes: {:d}, retries: {:d})",
         tstos(beg),
         tstos(end),
         end - beg,
         successes,
         retries,
     )