summaryrefslogtreecommitdiff
path: root/buildscripts/tests/resmokelib/testing/fixtures/test_interface.py
blob: e7685ece4c4ef24a76a0b183699b33c49526b4c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Unit tests for the resmokelib.testing.fixtures.interface module."""
import logging
import unittest

from buildscripts.resmokelib import errors
from buildscripts.resmokelib.testing.fixtures import interface

# pylint: disable=missing-docstring,protected-access


class TestFixture(unittest.TestCase):
    def test_teardown_ok(self):  # pylint: disable=no-self-use
        raising_fixture = UnitTestFixture(should_raise=False)
        raising_fixture.teardown()

    def test_teardown_raise(self):
        raising_fixture = UnitTestFixture(should_raise=True)
        with self.assertRaises(errors.ServerFailure):
            raising_fixture.teardown()


class TestFixtureTeardownHandler(unittest.TestCase):
    def test_teardown_ok(self):
        handler = interface.FixtureTeardownHandler(logging.getLogger("handler_unittests"))
        # Before any teardown.
        self.assertTrue(handler.was_successful())
        self.assertIsNone(handler.get_error_message())
        # Successful teardown.
        ok_fixture = UnitTestFixture(should_raise=False)
        handler.teardown(ok_fixture, "ok")
        # After successful teardown.
        self.assertTrue(handler.was_successful())
        self.assertIsNone(handler.get_error_message())

    def test_teardown_error(self):
        handler = interface.FixtureTeardownHandler(logging.getLogger("handler_unittests"))
        # Failing teardown.
        ko_fixture = UnitTestFixture(should_raise=True)
        handler.teardown(ko_fixture, "ko")
        # After failed teardown.
        self.assertFalse(handler.was_successful())
        expected_msg = "Error while stopping ko: " + UnitTestFixture.ERROR_MESSAGE
        self.assertEqual(expected_msg, handler.get_error_message())


class UnitTestFixture(interface.Fixture):  # pylint: disable=abstract-method
    ERROR_MESSAGE = "Failed"

    def __init__(self, should_raise=False):
        logger = logging.getLogger("fixture_unittests")
        interface.Fixture.__init__(self, logger, 99)
        self._should_raise = should_raise

    def _do_teardown(self, kill=False):
        if self._should_raise:
            raise errors.ServerFailure(self.ERROR_MESSAGE)