1#
2# Copyright (c) ZeroC, Inc. All rights reserved.
3#
4
5import Ice, Test, sys, TestI
6
7def test(b):
8    if not b:
9        raise RuntimeError('test assertion failed')
10
11def testFacets(com, builtInFacets = True):
12
13    if builtInFacets:
14        test(com.findAdminFacet("Properties") != None)
15        test(com.findAdminFacet("Process") != None)
16        test(com.findAdminFacet("Logger") != None)
17        test(com.findAdminFacet("Metrics") != None)
18
19    f1 = TestI.TestFacetI()
20    f2 = TestI.TestFacetI()
21    f3 = TestI.TestFacetI()
22
23    com.addAdminFacet(f1, "Facet1")
24    com.addAdminFacet(f2, "Facet2")
25    com.addAdminFacet(f3, "Facet3")
26
27    test(com.findAdminFacet("Facet1") == f1)
28    test(com.findAdminFacet("Facet2") == f2)
29    test(com.findAdminFacet("Facet3") == f3)
30    test(com.findAdminFacet("Bogus") == None)
31
32    facetMap = com.findAllAdminFacets()
33    if builtInFacets:
34        test(len(facetMap) == 7)
35        test("Properties" in facetMap)
36        test(isinstance(facetMap["Properties"], Ice.NativePropertiesAdmin))
37        test("Process" in facetMap)
38        test("Logger" in facetMap)
39        test("Metrics" in facetMap)
40
41    test(len(facetMap) >=3)
42
43    test("Facet1" in facetMap)
44    test("Facet2" in facetMap)
45    test("Facet3" in facetMap)
46
47    try:
48        com.addAdminFacet(f1, "Facet1")
49        test(False)
50    except Ice.AlreadyRegisteredException:
51        pass # Expected
52
53    try:
54        com.removeAdminFacet("Bogus")
55        test(False)
56    except Ice.NotRegisteredException:
57        pass # Expected
58
59    com.removeAdminFacet("Facet1")
60    com.removeAdminFacet("Facet2")
61    com.removeAdminFacet("Facet3")
62
63    try:
64        com.removeAdminFacet("Facet1")
65        test(False)
66    except Ice.NotRegisteredException:
67        pass # Expected
68
69def allTests(helper, communicator):
70    sys.stdout.write("testing communicator operations... ")
71    sys.stdout.flush()
72
73    #
74    # Test: Exercise addAdminFacet, findAdminFacet, removeAdminFacet with a typical configuration.
75    #
76    init = Ice.InitializationData()
77    init.properties = Ice.createProperties()
78    init.properties.setProperty("Ice.Admin.Endpoints", "tcp -h 127.0.0.1")
79    init.properties.setProperty("Ice.Admin.InstanceName", "Test")
80    init.properties.setProperty("Ice.ProgramName", "MyTestProgram")
81    com = Ice.initialize(init)
82    testFacets(com)
83    test(com.getLogger().getPrefix() == "MyTestProgram")
84    com.destroy()
85
86    #
87    # Test: Verify that the operations work correctly in the presence of facet filters.
88    #
89    init = Ice.InitializationData()
90    init.properties = Ice.createProperties()
91    init.properties.setProperty("Ice.Admin.Endpoints", "tcp -h 127.0.0.1")
92    init.properties.setProperty("Ice.Admin.InstanceName", "Test")
93    init.properties.setProperty("Ice.Admin.Facets", "Properties")
94    com = Ice.initialize(init)
95    testFacets(com, False)
96    com.destroy()
97
98    #
99    # Test: Verify that the operations work correctly with the Admin object disabled.
100    #
101    com = Ice.initialize()
102    testFacets(com, False)
103    com.destroy()
104
105    #
106    # Test: Verify that the operations work correctly when Ice.Admin is enabled.
107    #
108    init = Ice.InitializationData()
109    init.properties = Ice.createProperties()
110    init.properties.setProperty("Ice.Admin.Enabled", "1")
111    com = Ice.initialize(init)
112    test(com.getAdmin() == None)
113    identity = Ice.stringToIdentity("test-admin")
114    try:
115        com.createAdmin(None, identity)
116        test(False)
117    except Ice.InitializationException:
118        pass
119
120    adapter = com.createObjectAdapter("")
121    test(com.createAdmin(adapter, identity) != None)
122    test(com.getAdmin() != None)
123
124    testFacets(com)
125    com.destroy()
126
127    #
128    # Test: Verify that the operations work correctly when creation of the Admin object is delayed.
129    #
130    init = Ice.InitializationData()
131    init.properties = Ice.createProperties()
132    init.properties.setProperty("Ice.Admin.Endpoints", "tcp -h 127.0.0.1")
133    init.properties.setProperty("Ice.Admin.InstanceName", "Test")
134    init.properties.setProperty("Ice.Admin.DelayCreation", "1")
135    com = Ice.initialize(init)
136    testFacets(com)
137    com.getAdmin()
138    testFacets(com)
139    com.destroy()
140    print("ok")
141
142    ref = "factory:{0} -t 10000".format(helper.getTestEndpoint())
143    factory = Test.RemoteCommunicatorFactoryPrx.uncheckedCast(communicator.stringToProxy(ref))
144
145    sys.stdout.write("testing process facet... ")
146    sys.stdout.flush()
147
148    #
149    # Test: Verify that Process::shutdown() operation shuts down the communicator.
150    #
151    props = {}
152    props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1"
153    props["Ice.Admin.InstanceName"] = "Test"
154    com = factory.createCommunicator(props)
155    obj = com.getAdmin()
156    proc = Ice.ProcessPrx.checkedCast(obj, "Process")
157    proc.shutdown()
158    com.waitForShutdown()
159    com.destroy()
160
161    print("ok")
162
163    sys.stdout.write("testing properties facet... ")
164    sys.stdout.flush()
165
166    props = {}
167    props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1"
168    props["Ice.Admin.InstanceName"] = "Test"
169    props["Prop1"] = "1"
170    props["Prop2"] = "2"
171    props["Prop3"] = "3"
172    com = factory.createCommunicator(props)
173    obj = com.getAdmin()
174    pa = Ice.PropertiesAdminPrx.checkedCast(obj, "Properties")
175
176    #
177    # Test: PropertiesAdmin::getProperty()
178    #
179    test(pa.getProperty("Prop2") == "2")
180    test(pa.getProperty("Bogus") == "")
181
182    #
183    # Test: PropertiesAdmin::getProperties()
184    #
185    pd = pa.getPropertiesForPrefix("")
186    test(len(pd) == 5)
187    test(pd["Ice.Admin.Endpoints"] == "tcp -h 127.0.0.1")
188    test(pd["Ice.Admin.InstanceName"] == "Test")
189    test(pd["Prop1"] == "1")
190    test(pd["Prop2"] == "2")
191    test(pd["Prop3"] == "3")
192
193    changes = {}
194
195    #
196    # Test: PropertiesAdmin::setProperties()
197    #
198    setProps = {}
199    setProps["Prop1"] = "10" # Changed
200    setProps["Prop2"] = "20" # Changed
201    setProps["Prop3"] = "" # Removed
202    setProps["Prop4"] = "4" # Added
203    setProps["Prop5"] = "5" # Added
204    pa.setProperties(setProps)
205    test(pa.getProperty("Prop1") == "10")
206    test(pa.getProperty("Prop2") == "20")
207    test(pa.getProperty("Prop3") == "")
208    test(pa.getProperty("Prop4") == "4")
209    test(pa.getProperty("Prop5") == "5")
210    changes = com.getChanges()
211    test(len(changes) == 5)
212    test(changes["Prop1"] == "10")
213    test(changes["Prop2"] == "20")
214    test(changes["Prop3"] == "")
215    test(changes["Prop4"] == "4")
216    test(changes["Prop5"] == "5")
217    pa.setProperties(setProps)
218    changes = com.getChanges()
219    test(len(changes) == 0)
220
221    com.destroy()
222
223    print("ok")
224
225    sys.stdout.write("testing custom facet... ")
226    sys.stdout.flush()
227
228    #
229    # Test: Verify that the custom facet is present.
230    #
231    props = {}
232    props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1"
233    props["Ice.Admin.InstanceName"] = "Test"
234    com = factory.createCommunicator(props)
235    obj = com.getAdmin()
236    tf = Test.TestFacetPrx.checkedCast(obj, "TestFacet")
237    tf.op()
238    com.destroy()
239
240    print("ok")
241
242    sys.stdout.write("testing facet filtering... ")
243    sys.stdout.flush()
244
245    #
246    # Test: Set Ice.Admin.Facets to expose only the Properties facet,
247    # meaning no other facet is available.
248    #
249    props = {}
250    props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1"
251    props["Ice.Admin.InstanceName"] = "Test"
252    props["Ice.Admin.Facets"] = "Properties"
253    com = factory.createCommunicator(props)
254    obj = com.getAdmin()
255
256    proc = Ice.ProcessPrx.checkedCast(obj, "Process")
257    test(proc == None)
258    tf = Test.TestFacetPrx.checkedCast(obj, "TestFacet")
259    test(tf == None)
260    com.destroy()
261
262    #
263    # Test: Set Ice.Admin.Facets to expose only the Process facet,
264    # meaning no other facet is available.
265    #
266    props = {}
267    props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1"
268    props["Ice.Admin.InstanceName"] = "Test"
269    props["Ice.Admin.Facets"] = "Process"
270    com = factory.createCommunicator(props)
271    obj = com.getAdmin()
272
273    pa = Ice.PropertiesAdminPrx.checkedCast(obj, "Properties")
274    test(pa == None)
275    tf = Test.TestFacetPrx.checkedCast(obj, "TestFacet")
276    test(tf == None)
277
278    com.destroy()
279
280    #
281    # Test: Set Ice.Admin.Facets to expose only the TestFacet facet,
282    # meaning no other facet is available.
283    #
284    props = {}
285    props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1"
286    props["Ice.Admin.InstanceName"] = "Test"
287    props["Ice.Admin.Facets"] = "TestFacet"
288    com = factory.createCommunicator(props)
289    obj = com.getAdmin()
290
291    pa = Ice.PropertiesAdminPrx.checkedCast(obj, "Properties")
292    test(pa == None)
293
294    proc = Ice.ProcessPrx.checkedCast(obj, "Process")
295    test(proc == None)
296
297    com.destroy()
298
299    #
300    # Test: Set Ice.Admin.Facets to expose two facets. Use whitespace to separate the
301    # facet names.
302    #
303    props = {}
304    props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1"
305    props["Ice.Admin.InstanceName"] = "Test"
306    props["Ice.Admin.Facets"] = "Properties TestFacet"
307    com = factory.createCommunicator(props)
308    obj = com.getAdmin()
309    pa = Ice.PropertiesAdminPrx.checkedCast(obj, "Properties")
310    test(pa.getProperty("Ice.Admin.InstanceName") == "Test")
311    tf = Test.TestFacetPrx.checkedCast(obj, "TestFacet")
312    tf.op()
313
314    proc = Ice.ProcessPrx.checkedCast(obj, "Process")
315    test(proc == None)
316    com.destroy()
317
318    #
319    # Test: Set Ice.Admin.Facets to expose two facets. Use a comma to separate the
320    # facet names.
321    #
322    props = {}
323    props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1"
324    props["Ice.Admin.InstanceName"] = "Test"
325    props["Ice.Admin.Facets"] = "TestFacet, Process"
326    com = factory.createCommunicator(props)
327    obj = com.getAdmin()
328
329    pa = Ice.PropertiesAdminPrx.checkedCast(obj, "Properties")
330    test(pa == None)
331
332    tf = Test.TestFacetPrx.checkedCast(obj, "TestFacet")
333    tf.op()
334    proc = Ice.ProcessPrx.checkedCast(obj, "Process")
335    proc.shutdown()
336    com.waitForShutdown()
337    com.destroy()
338
339    print("ok")
340
341    factory.shutdown()
342