async def test_add_update_delete_get_list(expander: TemplateExpander) -> None:
    await expander.put_template(Template("albs", "is(aws_alb) and age>{{older_than}}"))
    result = await expander.get_template("albs")
    assert result and result.name == "albs" and result.template == "is(aws_alb) and age>{{older_than}}"
    assert len(await expander.list_templates()) == 1
    await expander.put_template(Template("albs", "is(aws_alb) and age>{{old}} limit 3"))
    assert (await expander.get_template("albs")).template == "is(aws_alb) and age>{{old}} limit 3"  # type: ignore
    await expander.delete_template("albs")
    assert len(await expander.list_templates()) == 0
示例#2
0
async def test_search_source(cli: CLI) -> None:
    result = await cli.execute_cli_command(
        'search is("foo") and some_int==0 --> identifier=~"9_"', stream.list)
    assert len(result[0]) == 10
    await cli.dependencies.template_expander.put_template(
        Template("test", 'is(foo) and some_int==0 --> identifier=~"{{fid}}"'))
    result2 = await cli.execute_cli_command('search expand(test, fid="9_")',
                                            stream.list)
    assert len(result2[0]) == 10

    result3 = await cli.execute_cli_command(
        "search --with-edges is(graph_root) -[0:1]->", stream.list)
    # node: graph_root
    # node: collector
    # edge: graph_root -> collector
    # -----------------------------
    # = 3 elements
    assert len(result3[0]) == 3

    result4 = await cli.execute_cli_command(
        "search --explain --with-edges is(graph_root) -[0:1]->", stream.list)
    assert result4[0][0]["rating"] == "simple"

    # use absolute path syntax
    result5 = await cli.execute_cli_command(
        "search aggregate(/reported.kind: sum(/reported.some_int) as si): "
        "is(foo) and not(/reported.some_int!=0) "
        "{child: --> /metadata!=null} some_int==0 "
        "with(any, --> /metadata!=null) sort /reported.name asc limit 1",
        stream.list,
    )
    assert result5 == [[{"group": {"kind": "foo"}, "si": 0}]]
示例#3
0
async def test_expand(expander: TemplateExpander) -> None:
    await expander.put_template(
        Template("albs", "is(aws_alb) and age>{{older_than}}"))
    result, expands = await expander.expand("query expand(albs, older_than=7d)"
                                            )
    assert result == "query is(aws_alb) and age>7d"
    with pytest.raises(NoSuchTemplateError, match="does_not_exist"):
        await expander.expand("query expand(does_not_exist)")
示例#4
0
async def test_simple_expand(expander: InMemoryTemplateExpander) -> None:
    templates = [
        Template("foo", "Hey {{name}} - this is {{noun}}"),
        Template("bla", "One, two, {{t3}}"),
        Template("bar", "Heureka"),
    ]
    for t in templates:
        expander.templates[t.name] = t
    result, expands = await expander.expand(
        "Test: expand(foo, name=bart, noun=crazy). expand(bla, t3=jiffy). expand(bar)"
    )
    assert result == "Test: Hey bart - this is crazy. One, two, jiffy. Heureka"
    assert len(expands) == 3
    assert expands[0].template == "foo"
    assert expands[0].props == dict(name="bart", noun="crazy")
    assert expands[1].template == "bla"
    assert expands[1].props == dict(t3="jiffy")
    assert expands[2].template == "bar"
    assert expands[2].props == {}
示例#5
0
def templates() -> List[Template]:
    return [Template(f"tpl_{a}", "is({{a}})") for a in range(0, 10)]