Debian Codegraph @

tests/test_pb2.py

raw symbols SCIP
1 from labgrid.remote.common import Place, ResourceMatch, Reservation, set_map_from_dict, build_dict_from_map
2 import labgrid.remote.generated.labgrid_coordinator_pb2 as labgrid_coordinator_pb2
3
4 def test_place_as_pb2():
5 place = Place(name="testing-place")
6 pb2 = place.as_pb2()
7 assert pb2.name == "testing-place"
8 assert pb2.created == place.created
9 assert pb2.changed == place.changed
10
11 def test_place_from_pb2():
12 place_start = Place(name="testing-place", comment="such-comment")
13 pb2 = place_start.as_pb2()
14 assert pb2.name == "testing-place"
15 place_new = Place.from_pb2(pb2)
16 assert place_new.name == "testing-place"
17 assert place_new.name == place_start.name
18 assert place_new.comment == place_start.comment
19 assert place_new.tags == place_start.tags
20 assert place_new.matches == place_start.matches
21 assert place_new.acquired == place_start.acquired
22 assert place_new.acquired_resources == place_start.acquired_resources
23 assert place_new.allowed == place_start.allowed
24 assert place_new.created == place_start.created
25 assert place_new.changed == place_start.changed
26 assert place_new.reservation == place_start.reservation
27
28 def test_from_pb2_tags():
29 tags = {"some": "test", "more": "values"}
30 place_start = Place(name="testing-place", tags=tags)
31 pb2 = place_start.as_pb2()
32 assert pb2.name == "testing-place", f"PB2 has wrong name: {pb2}"
33 assert pb2.tags is not None, f"PB2 has no tags field: {pb2}"
34 place_new = Place.from_pb2(pb2)
35 assert place_new.name == "testing-place"
36 assert place_new.tags == place_start.tags
37 assert place_new.tags == tags
38
39 def test_from_pb2_matches():
40 rm = ResourceMatch("such", "test", "match")
41 place_start = Place(name="testing-place", matches=[rm])
42 pb2 = place_start.as_pb2()
43 assert pb2.name == "testing-place", f"PB2 has wrong name: {pb2}"
44 assert pb2.tags is not None, f"PB2 has no tags field: {pb2}"
45 place_new = Place.from_pb2(pb2)
46 assert place_new.name == "testing-place"
47 assert place_new.tags == place_start.tags
48 assert place_new.matches == place_start.matches
49
50 def test_from_pb2_tags_deepcopy():
51 # Used by the RemotePlaceManager
52 tags = {"some": "test", "more": "values"}
53 place_start = Place(name="testing-place", tags=tags)
54 pb2 = place_start.as_pb2()
55 place_new = Place.from_pb2(pb2)
56 import copy
57 tags_copy = copy.deepcopy(place_new.tags)
58
59 def test_place_as_pb2_copy_with_match():
60 tags = {"some": "test", "more": "values"}
61 # Used by the RemotePlaceManager
62 place_start = Place(name="testing-place", tags=tags, comment="Hello", aliases={"some": "alias"}, matches=[ResourceMatch("testporter","somegroup","someclass")])
63 out = labgrid_coordinator_pb2.ClientOutMessage()
64
65 update_response = labgrid_coordinator_pb2.UpdateResponse()
66 update_response.place.CopyFrom(place_start.as_pb2())
67
68 out.updates.append(update_response)
69
70 def test_match_as_from_pb2():
71 rms = ResourceMatch("*", "somegroup", "someclass")
72 pb2 = rms.as_pb2()
73 assert pb2
74 rme = ResourceMatch.from_pb2(pb2)
75 assert rms == rme
76
77 def test_reservation_as_pb2():
78 reservation = Reservation(
79 owner="test",
80 filters={
81 "main": {"some": "filter"},
82 },
83 allocations={
84 "main": ["the-place"],
85 },
86 )
87 pb2 = reservation.as_pb2()
88 assert pb2.owner == "test"
89 assert pb2.token == reservation.token
90 assert pb2.state == reservation.state.value
91 assert pb2.filters["main"].filter == {"some": "filter"}
92 assert pb2.created == reservation.created
93 assert pb2.timeout == reservation.timeout
94
95 def test_reservation_as_from_pb2():
96 resold = Reservation(
97 owner="test",
98 filters={
99 "main": {"some": "filter"},
100 },
101 allocations={
102 "main": ["the-place"],
103 },
104 )
105 pb2 = resold.as_pb2()
106 assert pb2.owner == resold.owner
107 assert pb2.token == resold.token
108 assert pb2.state == resold.state.value
109 assert pb2.filters["main"].filter == {"some": "filter"}
110 assert pb2.created == resold.created
111 assert pb2.timeout == resold.timeout
112
113 resnew = Reservation.from_pb2(pb2)
114
115 assert resnew.owner == resold.owner
116 assert resnew.token == resold.token
117 assert resnew.state == resold.state
118 assert resnew.filters["main"] == resold.filters["main"]
119 assert resnew.created == resold.created
120 assert resnew.timeout == resold.timeout
121
122 def test_resource_dict():
123 params = {
124 'host': 'foo',
125 'model_id': 2277,
126 'vendor_id': 1133,
127 'path': None,
128 }
129
130 resource = labgrid_coordinator_pb2.Resource()
131 set_map_from_dict(resource.params, params)
132 bm = resource.SerializeToString()
133
134 resource = labgrid_coordinator_pb2.Resource()
135 resource.ParseFromString(bm)
136 decoded = build_dict_from_map(resource.params)
137
138 assert params == decoded
139
140 def test_map_serialize(benchmark):
141 params = {
142 'host': 'foo',
143 'model_id': 2277,
144 'vendor_id': 1133,
145 'path': None,
146 }
147
148 def run():
149 resource = labgrid_coordinator_pb2.Resource()
150 set_map_from_dict(resource.params, params)
151 bm = resource.SerializeToString()
152
153 benchmark(run)
154
155 def test_map_deser(benchmark):
156 params = {
157 'host': 'foo',
158 'model_id': 2277,
159 'vendor_id': 1133,
160 'path': None,
161 }
162
163 resource = labgrid_coordinator_pb2.Resource()
164 set_map_from_dict(resource.params, params)
165 bm = resource.SerializeToString()
166
167 def run():
168 resource = labgrid_coordinator_pb2.Resource()
169 resource.ParseFromString(bm)
170 decoded = build_dict_from_map(resource.params)
171
172 benchmark(run)