summaryrefslogtreecommitdiffstats
path: root/testing/talos/tests/test_xrestop.py
blob: 1e0ed32aa153b98f39ebb44222c29e264703db49 (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
#!/usr/bin/env python

"""
Tests for talos.xrestop
"""

import os
import subprocess
import unittest
from talos.cmanager_linux import xrestop

here = os.path.dirname(os.path.abspath(__file__))
xrestop_output = os.path.join(here, 'xrestop_output.txt')


class TestXrestop(unittest.TestCase):

    def test_parsing(self):
        """test parsing xrestop output from xrestop_output.txt"""

        class MockPopen(object):
            """
            stub class for subprocess.Popen
            We mock this to return a local static copy of xrestop output
            This has the unfortunate nature of depending on implementation
            details.
            """
            def __init__(self, *args, **kwargs):
                self.returncode = 0

            def communicate(self):
                stdout = open(xrestop_output).read()
                return stdout, ''

        # monkey-patch subprocess.Popen
        Popen = subprocess.Popen
        subprocess.Popen = MockPopen

        # get the output
        output = xrestop()

        # ensure that the parsed output is equal to what is in
        # xrestop_output.txt
        self.assertEqual(len(output), 7)  # seven windows with PIDs

        # the first window is Thunderbird
        pid = 2035  # thundrbird's pid
        self.assertTrue(pid in output)
        thunderbird = output[pid]
        self.assertEqual(thunderbird['index'], 0)
        self.assertEqual(thunderbird['total bytes'], '~4728761')

        # PID=1668 is a Terminal
        pid = 1668
        self.assertTrue(pid in output)
        terminal = output[pid]
        self.assertEqual(terminal['pixmap bytes'], '1943716')

        # cleanup: set subprocess.Popen back
        subprocess.Popen = Popen

if __name__ == '__main__':
    unittest.main()