def test_build_args_errors(): m = Matcher(rule_5) with pytest.raises(KeyError): m.build_args("org.reactobus.lava.hello", "uuid", "", "lavauser", {}) m = Matcher(rule_3) with pytest.raises(KeyError): m.build_args("org.reactobus", "uuid", "", "lavauser", {"username": "******"})
def test_simple_matching(): m = Matcher(rule_1) assert m.match({"topic": "org.reactobus.lava"}, {}) is True assert m.match({"topic": "org.reactobus.lava.job"}, {}) is True assert m.match({"topic": "reactobus.lava"}, {}) is False # Non existing field will return False assert m.match({"topi": "reactobus.lava"}, {}) is False
def test_data_matching(): m = Matcher(rule_3) assert m.match({}, {"submitter": "kernel-ci"}) is True assert m.match({}, {"submitter": "kernel"}) is False
def test_simple_matching_2(): m = Matcher(rule_2) assert m.match({"topic": "something", "username": "******"}, {}) is True # Non existing field will return False assert m.match({"topic": "something", "user": "******"}, {}) is False
def test_run(monkeypatch): m_args = [] m_input = "" m_timeout = 0 def mock_check_output(args, stderr, universal_newlines, input, timeout): nonlocal m_args nonlocal m_input nonlocal m_timeout m_args = args m_input = input m_timeout = timeout return "" monkeypatch.setattr(subprocess, "check_output", mock_check_output) m = Matcher(rule_1) m.run("org.reactobus", "uuid", "0", "lavauser", {}) assert m_args == [m.binary, "topic", "org.reactobus", "username", "lavauser"] assert m_input == "" assert m_timeout == 1 m.run("org.reactobus.test", "uuid", "0", "kernel", {}) assert m_args == [m.binary, "topic", "org.reactobus.test", "username", "kernel"] assert m_input == "" assert m_timeout == 1 m = Matcher(rule_7) m.run("org.reactobus.test", "uuid", "0", "lavaserver", {"submitter": "myself"}) assert m_args == [m.binary, "stdin", "myself"] assert m_input == "hello\norg.reactobus.test\nmyself" assert m_timeout == 4 m_args = None m.run("org.reactobus.test", "uuid", "0", "lavaserver", {"something": "myself"}) assert m_args is None
def test_build_args(): m = Matcher(rule_1) # Test for classical substitution (args, stdin) = m.build_args("org.reactobus.lava.hello", "uuid", "", "lavauser", {}) assert args == [m.binary, "topic", "org.reactobus.lava.hello", "username", "lavauser"] assert stdin == '' (args, stdin) = m.build_args("org.reactobus.lava.something", "uuid", "erty", "kernel-ci", {}) assert args == [m.binary, "topic", "org.reactobus.lava.something", "username", "kernel-ci"] assert stdin == '' # Test for data.* substitution m = Matcher(rule_3) (args, stdin) = m.build_args("org.reactobus", "uuid", "", "lavauser", {"submitter": "health"}) assert args == [m.binary, "topic", "org.reactobus", "submitter", "health"] assert stdin == '' # Without args m = Matcher(rule_6) (args, stdin) = m.build_args("org.reactobus", "uuid", "", "lavauser", {"submitter": "health"}) assert args == [m.binary] assert stdin == '' # With "stdin:" and "$data." m = Matcher(rule_7) (args, stdin) = m.build_args("org.reactobus", "uuid", "", "", {"submitter": "kernel-ci", "key": "value"}) assert args == [m.binary, "stdin", "kernel-ci"] assert stdin == "hello\norg.reactobus\nkernel-ci" with pytest.raises(KeyError): (args, stdin) = m.build_args("org.reactobus", "uuid", "", "", {"key": "value"})
def test_lookup(): assert Matcher.lookup("username", {"username": "******"}, {}) == "kernel" assert Matcher.lookup("msg", {"username": "******", "msg": "hello"}, {}) == "hello" assert Matcher.lookup("data", {"msg": "something"}, {}) == {} assert Matcher.lookup("data", {"msg": "something"}, {"hello": "world"}) == {"hello": "world"} assert Matcher.lookup("data.key", {"msg": "something"}, {"key": "value"}) == "value" assert Matcher.lookup("data.hello", {"msg": "something"}, {"hello": "world"}) == "world" with pytest.raises(KeyError): Matcher.lookup("msg", {}, {}) with pytest.raises(KeyError): Matcher.lookup("msg", {}, {"msg": "value"}) with pytest.raises(KeyError): Matcher.lookup("msg", {"username": "******"}, {}) with pytest.raises(KeyError): Matcher.lookup("data.username", {"username": "******"}, {})