1#!/usr/bin/python 2 3import unittest 4import common 5from autotest_lib.client.common_lib import profiler_manager 6 7 8# simple job stub for using in tests 9class stub_job(object): 10 tmpdir = "/home/autotest/tmp" 11 autodir = "/home/autotest" 12 13 14# simple profiler stub for using in tests 15class stub_profiler(object): 16 started = 0 17 def __init__(self, name): 18 self.name = name 19 @classmethod 20 def start(cls, test): 21 cls.started += 1 22 @classmethod 23 def stop(cls, test): 24 cls.started -= 1 25 26 27# replace profiler_manager.load_profiler with a simple stub 28class stub_manager(profiler_manager.profiler_manager): 29 def load_profiler(self, profiler, args, dargs): 30 return stub_profiler(profiler) 31 32 33class TestProfilerManager(unittest.TestCase): 34 def test_starts_with_no_profilers(self): 35 p = stub_manager(stub_job) 36 self.assertEqual(set(), p.current_profilers()) 37 38 39 def test_single_add(self): 40 p = stub_manager(stub_job) 41 p.add("prof1") 42 self.assertEqual(set(["prof1"]), p.current_profilers()) 43 44 45 def test_duplicate_adds(self): 46 p = stub_manager(stub_job) 47 p.add("prof1") 48 p.add("prof1") 49 self.assertEqual(set(["prof1"]), p.current_profilers()) 50 51 52 def test_multiple_adds(self): 53 p = stub_manager(stub_job) 54 p.add("prof1") 55 p.add("prof2") 56 self.assertEqual(set(["prof1", "prof2"]), p.current_profilers()) 57 58 59 def test_add_and_delete(self): 60 p = stub_manager(stub_job) 61 p.add("prof1") 62 p.add("prof2") 63 p.delete("prof1") 64 self.assertEqual(set(["prof2"]), p.current_profilers()) 65 66 67 def test_present_with_no_profilers(self): 68 p = stub_manager(stub_job) 69 self.assertEqual(False, p.present()) 70 71 72 def test_present_after_add(self): 73 p = stub_manager(stub_job) 74 p.add("prof1") 75 self.assertEqual(True, p.present()) 76 77 78 def test_present_after_add_and_remove(self): 79 p = stub_manager(stub_job) 80 p.add("prof1") 81 p.delete("prof1") 82 self.assertEqual(False, p.present()) 83 84 85 def test_started(self): 86 p = stub_manager(stub_job) 87 p.add("prof1") 88 p.add("prof2") 89 started = stub_profiler.started 90 self.assertEqual(False, p.active()) 91 p.start(object()) 92 self.assertEqual(started + 2, stub_profiler.started) 93 self.assertEqual(True, p.active()) 94 95 96 def test_stop(self): 97 p = stub_manager(stub_job) 98 p.add("prof1") 99 p.add("prof2") 100 started = stub_profiler.started 101 self.assertEqual(False, p.active()) 102 test = object() 103 p.start(test) 104 p.stop(test) 105 self.assertEqual(started, stub_profiler.started) 106 self.assertEqual(False, p.active()) 107 108 109 110if __name__ == "__main__": 111 unittest.main() 112