summaryrefslogtreecommitdiff
path: root/buildscripts/tests/resmokelib/logging/test_buildlogger.py
blob: 7bc705948dbbb834952a04387903bc583e589c7d (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
57
58
59
60
61
62
63
64
65
66
67
"""Unit tests for the buildscripts.resmokelib.logging.buildlogger module."""

from __future__ import absolute_import

import json
import unittest

from buildscripts.resmokelib.logging import buildlogger


class TestLogsSplitter(unittest.TestCase):
    """Unit tests for the _LogsSplitter class."""

    def test_split_no_logs(self):
        logs = []
        max_size = 10
        self.assertEqual([[]], buildlogger._LogsSplitter.split_logs(logs, max_size))

    def test_split_no_max_size(self):
        logs = self.__generate_logs(size=30)
        max_size = None
        self.assertEqual([logs], buildlogger._LogsSplitter.split_logs(logs, max_size))

    def test_split_max_size_smaller(self):
        logs = self.__generate_logs(size=20)
        max_size = 30
        self.assertEqual([logs], buildlogger._LogsSplitter.split_logs(logs, max_size))

    def test_split_max_size_equal(self):
        logs = self.__generate_logs(size=30)
        max_size = 30
        self.assertEqual([logs], buildlogger._LogsSplitter.split_logs(logs, max_size))

    def test_split_max_size_larger(self):
        logs = self.__generate_logs(size=31)
        max_size = 30
        self.assertEqual(
            [logs[0:-1], logs[-1:]],
            buildlogger._LogsSplitter.split_logs(logs, max_size))

        logs = self.__generate_logs(size=149)
        max_size = 19
        self.assertEqual(
            [logs[0:3], logs[3:6], logs[6:9], logs[9:12], logs[12:15],
             logs[15:18], logs[18:21], logs[21:24], logs[24:27], logs[27:]],
            buildlogger._LogsSplitter.split_logs(logs, max_size))

    def check_split_sizes(self, splits, max_size):
        for split in splits:
            self.assertTrue(TestLogsSplitter.size(split) <= max_size)

    def __generate_logs(self, size):
        # The size of [ "x" ] is 5. This is the minimum size we generate.
        self.assertTrue(size >= 5)
        # Each new "x" adds 5 to the size.
        nb_lines = size / 5
        # Each additional "x" on a line adds 1 to the size.
        last_line_extra = size % 5
        logs = ["x"] * nb_lines
        logs[-1] += "x" * last_line_extra
        self.assertEqual(size, TestLogsSplitter.size(logs))
        return logs

    @staticmethod
    def size(logs):
        """Returns the size of the log lines when represented in JSON."""
        return len(json.dumps(logs, encoding="utf-8"))