Exemple #1
0
def test_run_raise_subprocesserror(monkeypatch):
    def mock_check_output_raise_subprocesserror(args, stderr, input, timeout):
        raise OSError

    monkeypatch.setattr(subprocess, "check_output",
                        mock_check_output_raise_subprocesserror)
    m = Matcher(rule_1)
    m.run("org.reactobus.test", "uuid", "0", "lavaserver",
          {"something": "myself"})
Exemple #2
0
def test_run_raise_timeout(monkeypatch):
    def mock_check_output_raise_oserror(args, stderr, input, timeout):
        import subprocess

        raise subprocess.TimeoutExpired(args[0], timeout)

    monkeypatch.setattr(subprocess, "check_output",
                        mock_check_output_raise_oserror)
    m = Matcher(rule_1)
    m.run("org.reactobus.test", "uuid", "0", "lavaserver",
          {"something": "myself"})
Exemple #3
0
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": "******"})
Exemple #4
0
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
Exemple #5
0
def test_run(monkeypatch):
    m_args = []
    m_input = ""
    m_timeout = 0

    def mock_check_output(args, stderr, input, timeout):
        nonlocal m_args
        nonlocal m_input
        nonlocal m_timeout
        m_args = args
        m_input = input
        m_timeout = timeout
        for arg in args:
            assert isinstance(arg, bytes)
        assert isinstance(input, bytes)
        return ""

    monkeypatch.setattr(subprocess, "check_output", mock_check_output)
    m = Matcher(rule_1)

    m.run("org.reactobus", "uuid", "0", "lavauser", {})
    assert m_args == [
        b(m.binary), b"topic", b"org.reactobus", b"username", b"lavauser"
    ]
    assert m_input == b""
    assert m_timeout == 1

    m.run("org.reactobus.test", "uuid", "0", "kernel", {})
    assert m_args == [
        b(m.binary),
        b"topic",
        b"org.reactobus.test",
        b"username",
        b"kernel",
    ]
    assert m_input == b""
    assert m_timeout == 1

    m = Matcher(rule_7)
    m.run("org.reactobus.test", "uuid", "0", "lavaserver",
          {"submitter": "myself"})
    assert m_args == [b(m.binary), b"stdin", b"myself"]
    assert m_input == b"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
Exemple #6
0
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"})
Exemple #7
0
def test_data_matching():
    m = Matcher(rule_3)

    assert m.match({}, {"submitter": "kernel-ci"}) is True
    assert m.match({}, {"submitter": "kernel"}) is False
Exemple #8
0
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
Exemple #9
0
def test_worker_and_matchers(monkeypatch):
    # Replace zmq.Context.instance()
    zmq_instance = mock.ZMQContextInstance()
    monkeypatch.setattr(zmq.Context, "instance", zmq_instance)

    m_args = []
    m_input = ""
    m_timeout = 0

    def mock_check_output(args, stderr, input, timeout):
        nonlocal m_args
        nonlocal m_input
        nonlocal m_timeout
        m_args = args
        m_input = input
        m_timeout = timeout
        for arg in args:
            assert isinstance(arg, bytes)
        assert isinstance(input, bytes)
        return ""

    monkeypatch.setattr(subprocess, "check_output", mock_check_output)

    rule_1 = {
        "name": "first test",
        "match": {
            "field": "topic",
            "patterns": "^org.reactobus.lava"
        },
        "exec": {
            "path": "/bin/true",
            "args": ["topic", "$topic", "username", "$username"],
            "timeout": 1,
        },
    }
    rule_2 = {
        "name": "second test",
        "match": {
            "field": "username",
            "patterns": ".*kernel.*"
        },
        "exec": {
            "path": "/bin/true",
            "args": ["topic", "$topic", "username", "$username"],
            "timeout": 1,
        },
    }
    rule_3 = {
        "name": "data matching",
        "match": {
            "field": "data.submitter",
            "patterns": "kernel-ci"
        },
        "exec": {
            "path": "/bin/true",
            "args": ["topic", "$topic", "submitter", "$data.submitter"],
            "timeout": 1,
        },
    }

    # Create the matchers
    matchers = [Matcher(rule_1), Matcher(rule_2), Matcher(rule_3)]
    w = Worker(matchers)

    # Run for nothing to create the sockets
    with pytest.raises(IndexError):
        w.run()

    dealer = zmq_instance.socks[zmq.DEALER]
    assert dealer.connected and not dealer.bound

    # Create some work for rule_1
    data = [[b"0", b"org.reactobus.lava", b"", b"", b"lavauser", b"{}"]]
    dealer.recv = data

    with pytest.raises(IndexError):
        w.run()

    assert m_args == [
        b"/bin/true",
        b"topic",
        b"org.reactobus.lava",
        b"username",
        b"lavauser",
    ]

    # Create some work for rule_2
    data = [[b"1", b"org.kernel.git", b"", b"", b"kernelci", b"{}"]]
    dealer.recv = data

    with pytest.raises(IndexError):
        w.run()

    assert m_args == [
        b"/bin/true",
        b"topic",
        b"org.kernel.git",
        b"username",
        b"kernelci",
    ]

    # Create some work for rule_3
    data = [[
        b"2",
        b"org.linaro.validation",
        b"",
        b"",
        b"lavauser",
        b'{"submitter": "kernel-ci"}',
    ]]
    dealer.recv = data

    with pytest.raises(IndexError):
        w.run()

    assert m_args == [
        b"/bin/true",
        b"topic",
        b"org.linaro.validation",
        b"submitter",
        b"kernel-ci",
    ]

    m_args = []
    # Create some invalid work for rule_3
    data = [[
        b"2",
        b"org.linaro.validation",
        b"",
        b"",
        b"lavauser",
        b'{"submitter: "kernel-ci"}',
    ]]
    dealer.recv = data

    with pytest.raises(IndexError):
        w.run()

    assert m_args == []