updates to gacks tests
[sfa.git] / tests / testGacksHandle.py
1 import unittest
2 import xmlrpclib
3 from gackshandle import *
4
5 class TestGacksHandle(unittest.TestCase):
6    def setUp(self):
7        pass
8
9    def testCreate(self):
10        h = GacksHandle("cpu", 10, 15, 20, 25)
11        self.assertEqual(h.id, "cpu")
12        self.assertEqual(h.unitStart, 10)
13        self.assertEqual(h.unitStop, 15)
14        self.assertEqual(h.timeStart, 20)
15        self.assertEqual(h.timeStop, 25)
16
17    def testAsString(self):
18        h = GacksHandle("cpu", 10, 15, 20, 25)
19        s = h.as_string()
20        self.assertEqual(s, "cpu#10-15#20-25")
21
22    def testLoadFromString(self):
23        h = GacksHandle("cpu", 10, 15, 20, 25)
24        s = h.as_string()
25        h2 = GacksHandle(string=s)
26        self.assertEqual(h.id, h2.id)
27        self.assertEqual(h.unitStart, h2.unitStart)
28        self.assertEqual(h.unitStop, h2.unitStop)
29        self.assertEqual(h.timeStart, h2.timeStart)
30        self.assertEqual(h.timeStop, h2.timeStop)
31
32    def testGetQuantity(self):
33        h = GacksHandle("cpu", 10, 15, 20, 25)
34        self.assertEqual(h.get_quantity(), 5)
35
36    def testGetDuration(self):
37        h = GacksHandle("cpu", 10, 15, 20, 25)
38        self.assertEqual(h.get_duration(), 5)
39
40    def testClone(self):
41        h = GacksHandle("cpu", 10, 15, 20, 25)
42        h2 = h.clone()
43        self.assertEqual(h.id, h2.id)
44        self.assertEqual(h.unitStart, h2.unitStart)
45        self.assertEqual(h.unitStop, h2.unitStop)
46        self.assertEqual(h.timeStart, h2.timeStart)
47        self.assertEqual(h.timeStop, h2.timeStop)
48
49    def testSplitUnit(self):
50        h = GacksHandle("cpu", 10, 15, 20, 25)
51        parts = h.split_unit(12)
52        h1 = parts[0]
53        h2 = parts[1]
54
55        self.assertEqual(h1.id, "cpu")
56        self.assertEqual(h1.unitStart, 10)
57        self.assertEqual(h1.unitStop, 12)
58        self.assertEqual(h1.timeStart, 20)
59        self.assertEqual(h1.timeStop, 25)
60
61        self.assertEqual(h2.id, "cpu")
62        self.assertEqual(h2.unitStart, 12)
63        self.assertEqual(h2.unitStop, 15)
64        self.assertEqual(h2.timeStart, 20)
65        self.assertEqual(h2.timeStop, 25)
66
67    def testSplitTime(self):
68        h = GacksHandle("cpu", 10, 15, 20, 25)
69        parts = h.split_time(23)
70        h1 = parts[0]
71        h2 = parts[1]
72
73        self.assertEqual(h1.id, "cpu")
74        self.assertEqual(h1.unitStart, 10)
75        self.assertEqual(h1.unitStop, 15)
76        self.assertEqual(h1.timeStart, 20)
77        self.assertEqual(h1.timeStop, 23)
78
79        self.assertEqual(h2.id, "cpu")
80        self.assertEqual(h2.unitStart, 10)
81        self.assertEqual(h2.unitStop, 15)
82        self.assertEqual(h2.timeStart, 23)
83        self.assertEqual(h2.timeStop, 25)
84
85    def testSplitSubset(self):
86        h = GacksHandle("cpu", 10, 15, 20, 25)
87
88        # split out a subset right in the middle
89        parts = h.clone().split_subset(12, 13, 22, 23)
90
91        self.assertEqual(len(parts), 5)
92        self.assert_(find_handle_in_list(parts, 10, 12, 20, 25)) # h1
93        self.assert_(find_handle_in_list(parts, 12, 13, 20, 22)) # h2
94        self.assert_(find_handle_in_list(parts, 12, 13, 23, 25)) # h3
95        self.assert_(find_handle_in_list(parts, 13, 15, 20, 25)) # h4
96        self.assert_(find_handle_in_list(parts, 12, 13, 22, 23)) # s
97
98        # split out a subset in the top left corner
99        parts = h.clone().split_subset(10, 13, 20, 23)
100
101        self.assertEqual(len(parts), 3)
102        self.assert_(find_handle_in_list(parts, 10, 13, 23, 25)) # h3
103        self.assert_(find_handle_in_list(parts, 13, 15, 20, 25)) # h4
104        self.assert_(find_handle_in_list(parts, 10, 13, 20, 23)) # s
105
106        # split out a subset in the bottom right corner
107        parts = h.clone().split_subset(12, 15, 22, 25)
108
109        self.assertEqual(len(parts), 3)
110        self.assert_(find_handle_in_list(parts, 10, 12, 20, 25)) # h1
111        self.assert_(find_handle_in_list(parts, 12, 15, 20, 22)) # h2
112        self.assert_(find_handle_in_list(parts, 12, 15, 22, 25)) # s
113
114 class TestGacksRecord(unittest.TestCase):
115    def setUp(self):
116        pass
117
118    def testCreate(self):
119        r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
120        self.assertEqual(r.id, "cpu")
121        self.assertEqual(r.unitStart, 10)
122        self.assertEqual(r.unitStop, 15)
123        self.assertEqual(r.timeStart, 20)
124        self.assertEqual(r.timeStop, 25)
125        self.assertEqual(r.allocatorHRNs, ["foo", "bar"])
126        self.assertEqual(r.consumerHRN, "slice1")
127
128    def testClone(self):
129        r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
130        r2 = r.clone()
131
132        self.assertEqual(r.id, r2.id)
133        self.assertEqual(r.unitStart, r2.unitStart)
134        self.assertEqual(r.unitStop, r2.unitStop)
135        self.assertEqual(r.timeStart, r2.timeStart)
136        self.assertEqual(r.timeStop, r2.timeStop)
137        self.assertEqual(r.allocatorHRNs, r2.allocatorHRNs)
138        self.assertEqual(r.consumerHRN, r2.consumerHRN)
139
140    def testSetAllocator(self):
141        r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
142        r.set_allocator("bar", "bob", -1, 1)
143        self.assertEqual(r.allocatorHRNs, ["foo", "bar", "bob"])
144
145        r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
146        r.set_allocator("bar", "bob", -1, 0)
147        self.assertEqual(r.allocatorHRNs, ["foo", "bob"])
148
149        r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar","foo","bar"], "slice1")
150        r.set_allocator("bar", "bob", 0, 0)
151        self.assertEqual(r.allocatorHRNs, ["foo", "bob"])
152
153        r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar","foo","bar"], "slice1")
154        r.set_allocator("bar", "bob", 1, 0)
155        self.assertEqual(r.allocatorHRNs, ["foo", "bar", "foo", "bob"])
156
157    def testGetAllocator(self):
158        r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
159        self.assertEqual(r.get_allocators(), ["foo", "bar"])
160
161    def testSetConsumer(self):
162        r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
163        r.set_consumer("slice2")
164        self.assertEqual(r.get_consumer(), "slice2")
165
166    def testGetConsumer(self):
167        r = GacksRecord("cpu", 10, 15, 20, 25, ["foo","bar"], "slice1")
168        self.assertEqual(r.get_consumer(), "slice1")
169
170
171
172 if __name__ == "__main__":
173     unittest.main()