ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python
 
import unittest
import common
from autotest_lib.client.common_lib import profiler_manager
 
 
# simple job stub for using in tests
class stub_job(object):
    tmpdir = "/home/autotest/tmp"
    autodir = "/home/autotest"
 
 
# simple profiler stub for using in tests
class stub_profiler(object):
    started = 0
    def __init__(self, name):
        self.name = name
    @classmethod
    def start(cls, test):
        cls.started += 1
    @classmethod
    def stop(cls, test):
        cls.started -= 1
 
 
# replace profiler_manager.load_profiler with a simple stub
class stub_manager(profiler_manager.profiler_manager):
    def load_profiler(self, profiler, args, dargs):
        return stub_profiler(profiler)
 
 
class TestProfilerManager(unittest.TestCase):
    def test_starts_with_no_profilers(self):
        p = stub_manager(stub_job)
        self.assertEqual(set(), p.current_profilers())
 
 
    def test_single_add(self):
        p = stub_manager(stub_job)
        p.add("prof1")
        self.assertEqual(set(["prof1"]), p.current_profilers())
 
 
    def test_duplicate_adds(self):
        p = stub_manager(stub_job)
        p.add("prof1")
        p.add("prof1")
        self.assertEqual(set(["prof1"]), p.current_profilers())
 
 
    def test_multiple_adds(self):
        p = stub_manager(stub_job)
        p.add("prof1")
        p.add("prof2")
        self.assertEqual(set(["prof1", "prof2"]), p.current_profilers())
 
 
    def test_add_and_delete(self):
        p = stub_manager(stub_job)
        p.add("prof1")
        p.add("prof2")
        p.delete("prof1")
        self.assertEqual(set(["prof2"]), p.current_profilers())
 
 
    def test_present_with_no_profilers(self):
        p = stub_manager(stub_job)
        self.assertEqual(False, p.present())
 
 
    def test_present_after_add(self):
        p = stub_manager(stub_job)
        p.add("prof1")
        self.assertEqual(True, p.present())
 
 
    def test_present_after_add_and_remove(self):
        p = stub_manager(stub_job)
        p.add("prof1")
        p.delete("prof1")
        self.assertEqual(False, p.present())
 
 
    def test_started(self):
        p = stub_manager(stub_job)
        p.add("prof1")
        p.add("prof2")
        started = stub_profiler.started
        self.assertEqual(False, p.active())
        p.start(object())
        self.assertEqual(started + 2, stub_profiler.started)
        self.assertEqual(True, p.active())
 
 
    def test_stop(self):
        p = stub_manager(stub_job)
        p.add("prof1")
        p.add("prof2")
        started = stub_profiler.started
        self.assertEqual(False, p.active())
        test = object()
        p.start(test)
        p.stop(test)
        self.assertEqual(started, stub_profiler.started)
        self.assertEqual(False, p.active())
 
 
 
if __name__ == "__main__":
    unittest.main()