-
Notifications
You must be signed in to change notification settings - Fork 26
/
run_cim_operations.py
executable file
·1007 lines (749 loc) · 31.7 KB
/
run_cim_operations.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
Test CIM operations against a real WBEM server that is specified on the
command line. It executes either a specific test or the full test
suite depending on user input.
The return codes here may be specific to OpenPegasus.
"""
# pylint: disable=missing-docstring,superfluous-parens,no-self-use
import sys
from datetime import timedelta
import unittest
from getpass import getpass
import six
from pywbem.cim_constants import *
from pywbem import CIMInstance, CIMInstanceName, CIMClass, CIMClassName, \
CIMProperty, CIMQualifier, CIMQualifierDeclaration, \
CIMMethod, WBEMConnection, CIMError, \
Uint8, Uint16, Uint32, Uint64, \
Sint8, Sint16, Sint32, Sint64, \
Real32, Real64, CIMDateTime
from pywbem.cim_operations import CIMError
# Test for decorator for unimplemented tests
# decorator is @unittest.skip(UNIMPLEMENTED)
UNIMPLEMENTED = "test not implemented"
# A class that should be implemented in a wbem server and is used
# for testing
TEST_CLASS = 'CIM_ComputerSystem'
TEST_CLASS_PROPERTY1 = 'Name'
TEST_CLASS_PROPERTY2 = 'CreationClassName'
class ClientTest(unittest.TestCase):
"""Base class that creates a pywbem.WBEMConnection for
subclasses to use."""
def setUp(self):
"""Create a connection."""
#pylint: disable=global-variable-not-assigned
global args # pylint: disable=invalid-name
self.system_url = args['url']
self.namespace = args['namespace']
self.verbose = args['verbose']
self.debug = args['debug']
self.log('setup connection {} ns {}'.format(self.system_url,
self.namespace))
self.conn = WBEMConnection(
self.system_url,
(args['username'], args['password']),
self.namespace,
timeout=args['timeout'])
# enable saving of xml for display
self.conn.debug = args['verbose']
self.log('Connected {}, ns {}'.format(self.system_url,
args['namespace']))
def cimcall(self, fn, *pargs, **kwargs):
"""Make a PyWBEM call, catch any exceptions, and log the
request and response XML.
Logs the call parameters, request/response xml,
and response status
Returns result of request to caller
"""
self.log('cimcall fn {} args *pargs {} **kwargs {}'.
format(fn, pargs, kwargs))
try:
result = fn(*pargs, **kwargs)
except Exception as exc:
self.log('Operation %s failed with %s: %s\n' % \
(fn.__name__, exc.__class__.__name__, str(exc)))
last_request = self.conn.last_request or self.conn.last_raw_request
self.log('Request:\n\n%s\n' % last_request)
last_reply = self.conn.last_reply or self.conn.last_raw_reply
self.log('Reply:\n\n%s\n' % last_reply)
raise
self.log('Operation %s succeeded\n' % fn.__name__)
last_request = self.conn.last_request or self.conn.last_raw_request
self.log('Request:\n\n%s\n' % last_request)
last_reply = self.conn.last_reply or self.conn.last_raw_reply
self.log('Reply:\n\n%s\n' % last_reply)
return result
def log(self, data_):
"""Display log entry if verbose"""
if self.verbose:
print('{}'.format(data_))
## TODO this is where we should disconnect the client
def tearDown(self):
self.log('FUTURE: disconnect this connection')
#################################################################
# Instance provider interface tests
#################################################################
class EnumerateInstanceNames(ClientTest):
def test_all(self):
# Single arg call
names = self.cimcall(self.conn.EnumerateInstanceNames,
TEST_CLASS)
self.assertTrue(len(names) >= 1)
for name in names:
self.assertTrue(isinstance(name, CIMInstanceName))
self.assertTrue(len(name.namespace) > 0)
self.assertTrue(name.namespace == self.namespace)
# Call with explicit CIM namespace that exists
self.cimcall(self.conn.EnumerateInstanceNames,
TEST_CLASS,
namespace=self.conn.default_namespace)
# Call with explicit CIM namespace that does not exist
try:
self.cimcall(self.conn.EnumerateInstanceNames,
TEST_CLASS,
namespace='root/blah')
except CIMError as ce:
if ce.args[0] != CIM_ERR_INVALID_NAMESPACE:
raise
class EnumerateInstances(ClientTest):
def test_all(self):
# Simplest invocation
instances = self.cimcall(self.conn.EnumerateInstances,
TEST_CLASS)
self.assertTrue(len(instances) >= 1)
for i in instances:
self.assertTrue(isinstance(i, CIMInstance))
self.assertTrue(isinstance(i.path, CIMInstanceName))
self.assertTrue(len(i.path.namespace) > 0)
self.assertTrue(i.path.namespace == self.namespace)
# Call with explicit CIM namespace that exists
self.cimcall(self.conn.EnumerateInstances,
TEST_CLASS,
namespace=self.conn.default_namespace)
# Call with property list and localonly
instances = self.cimcall(self.conn.EnumerateInstances,
TEST_CLASS,
PropertyList=[TEST_CLASS_PROPERTY1, \
TEST_CLASS_PROPERTY2],
LocalOnly=False)
self.assertTrue(len(instances) >= 1)
for i in instances:
self.assertTrue(isinstance(i, CIMInstance))
self.assertTrue(isinstance(i.path, CIMInstanceName))
self.assertTrue(len(i.path.namespace) > 0)
self.assertTrue(i.path.namespace == self.namespace)
self.assertTrue(len(i.properties) == 2)
# Call with explicit CIM namespace that exists
self.cimcall(self.conn.EnumerateInstances,
TEST_CLASS,
namespace=self.conn.default_namespace)
# Call with explicit CIM namespace that does not exist
try:
self.cimcall(self.conn.EnumerateInstances,
TEST_CLASS,
namespace='root/blah')
except CIMError as ce:
if ce.args[0] != CIM_ERR_INVALID_NAMESPACE:
raise
class ExecQuery(ClientTest):
def test_all(self):
try:
# Simplest invocation
instances = self.cimcall(self.conn.ExecQuery,
'WQL',
'Select * from %s' % TEST_CLASS)
self.assertTrue(len(instances) >= 1)
for i in instances:
self.assertTrue(isinstance(i, CIMInstance))
self.assertTrue(isinstance(i.path, CIMInstanceName))
self.assertTrue(len(i.path.namespace) > 0)
except CIMError as ce:
if ce.args[0] == CIM_ERR_NOT_SUPPORTED:
raise AssertionError(
"CIM_ERR_NOT_SUPPORTED: The WBEM server doesn't" \
" support ExecQuery for this query")
if ce.args[0] == CIM_ERR_QUERY_LANGUAGE_NOT_SUPPORTED:
raise AssertionError(
"CIM_ERR_QUERY_LANGUAGE_NOT_SUPPORTED: The WBEM" \
" server doesn't support WQL for ExecQuery")
else:
raise
# Call with explicit CIM namespace that does not exist
try:
self.cimcall(self.conn.ExecQuery,
'WQL',
'Select * from %s' % TEST_CLASS,
namespace='root/blah')
except CIMError as ce:
if ce.args[0] != CIM_ERR_INVALID_NAMESPACE:
raise
# Call with invalid query lang
try:
self.cimcall(self.conn.ExecQuery,
'wql',
'Select * from %s' % TEST_CLASS,
namespace='root/cimv2')
except CIMError as ce:
if ce.args[0] != CIM_ERR_QUERY_LANGUAGE_NOT_SUPPORTED:
raise
# Call with invalid query
try:
self.cimcall(self.conn.ExecQuery,
'WQL',
'SelectSLOP * from %s' % TEST_CLASS,
namespace='root/cimv2')
except CIMError as ce:
if ce.args[0] != CIM_ERR_INVALID_QUERY:
raise
class GetInstance(ClientTest):
def test_all(self):
inst_names = self.cimcall(self.conn.EnumerateInstanceNames,
TEST_CLASS)
self.assertTrue(len(inst_names) >= 1)
name = inst_names[0] # Pick the first returned instance
# Simplest invocation
obj = self.cimcall(self.conn.GetInstance,
name)
self.assertTrue(isinstance(obj, CIMInstance))
self.assertTrue(isinstance(obj.path, CIMInstanceName))
# Call with property list and localonly
obj = self.cimcall(self.conn.GetInstance,
name, \
PropertyList=[TEST_CLASS_PROPERTY1], \
LocalOnly=False)
self.assertTrue(isinstance(obj, CIMInstance))
self.assertTrue(isinstance(obj.path, CIMInstanceName))
self.assertTrue(obj.path.namespace == self.namespace)
self.assertTrue(len(obj.properties) == 1)
# Call with property list empty
obj = self.cimcall(self.conn.GetInstance,
name, \
PropertyList=[], \
LocalOnly=False)
self.assertTrue(isinstance(obj, CIMInstance))
self.assertTrue(isinstance(obj.path, CIMInstanceName))
self.assertTrue(obj.path.namespace == self.namespace)
self.assertTrue(len(obj.properties) == 0)
# Call with invalid namespace path
invalid_name = name.copy()
invalid_name.namespace = 'blahblahblah'
try:
self.cimcall(self.conn.GetInstance, invalid_name)
except CIMError as ce:
if ce.args[0] != CIM_ERR_INVALID_NAMESPACE:
raise
class CreateInstance(ClientTest):
def test_all(self):
# Test instance
instance = CIMInstance(
'PyWBEM_Person',
{'CreationClassName': 'PyWBEM_Person',
'Name': 'Test'},
path=CIMInstanceName('PyWBEM_Person',
{'CreationClassName': 'PyWBEM_Person',
'Name': 'Test'}))
# Delete if already exists (previous test incomplete)
try:
self.cimcall(self.conn.DeleteInstance, instance.path)
except CIMError as ce:
if ce.args[0] == CIM_ERR_NOT_FOUND:
pass
# Simple create and delete
try:
result = self.cimcall(self.conn.CreateInstance, instance)
except CIMError as ce:
if ce.args[0] == CIM_ERR_INVALID_CLASS:
# does not support creation
pass
else:
self.assertTrue(isinstance(result, CIMInstanceName))
self.assertTrue(len(result.namespace) > 0)
result = self.cimcall(self.conn.DeleteInstance, instance.path)
self.assertTrue(result is None)
try:
self.cimcall(self.conn.GetInstance(instance.path))
except CIMError as arg:
if arg == CIM_ERR_NOT_FOUND:
pass
class ModifyInstance(ClientTest):
def test_all(self):
# Test instance
instance = CIMInstance(
'PyWBEM_Person',
{'CreationClassName': 'PyWBEM_Person',
'Name': 'Test'},
path=CIMInstanceName('PyWBEM_Person',
{'CreationClassName': 'PyWBEM_Person',
'Name': 'Test'}))
# Delete if already exists
try:
self.cimcall(self.conn.DeleteInstance, instance.path)
except CIMError as ce:
if ce.args[0] == CIM_ERR_NOT_FOUND:
pass
# Create instance
try:
self.cimcall(self.conn.CreateInstance, instance)
except CIMError as ce:
if ce.args[0] == CIM_ERR_INVALID_CLASS:
# does not support creation
pass
else:
# Modify instance
instance['Title'] = 'Sir'
instance.path.namespace = 'root/cimv2'
result = self.cimcall(self.conn.ModifyInstance, instance)
self.assertTrue(result is None)
# TODO add get and test for change.
# Clean up
self.cimcall(self.conn.DeleteInstance, instance.path)
#################################################################
# Method provider interface tests
#################################################################
class InvokeMethod(ClientTest):
def test_all(self):
# Invoke on classname
try:
self.cimcall(
self.conn.InvokeMethod,
'FooMethod',
TEST_CLASS)
except CIMError as ce:
if ce.args[0] != CIM_ERR_METHOD_NOT_AVAILABLE:
raise
# Invoke on an InstanceName
inst_names = self.cimcall(self.conn.EnumerateInstanceNames,
TEST_CLASS)
self.assertTrue(len(inst_names) >= 1)
name = inst_names[0] # Pick the first returned instance
try:
self.cimcall(self.conn.InvokeMethod,
'FooMethod',
name)
except CIMError as ce:
if ce.args[0] not in (CIM_ERR_METHOD_NOT_AVAILABLE,
CIM_ERR_METHOD_NOT_FOUND):
raise
# Test remote instance name
name2 = name.copy()
name2.host = 'woot.com'
name2.namespace = 'root/cimv2'
try:
self.cimcall(self.conn.InvokeMethod,
'FooMethod',
name)
except CIMError as ce:
if ce.args[0] not in (CIM_ERR_METHOD_NOT_AVAILABLE,
CIM_ERR_METHOD_NOT_FOUND):
raise
# Call with all possible parameter types
try:
self.cimcall(self.conn.InvokeMethod,
'FooMethod',
TEST_CLASS,
String='Spotty',
Uint8=Uint8(1),
Sint8=Sint8(2),
Uint16=Uint16(3),
Sint16=Sint16(3),
Uint32=Uint32(4),
Sint32=Sint32(5),
Uint64=Uint64(6),
Sint64=Sint64(7),
Real32=Real32(8),
Real64=Real64(9),
Bool=True,
Date1=CIMDateTime.now(),
Date2=timedelta(60),
Ref=name)
except CIMError as ce:
if ce.args[0] != CIM_ERR_METHOD_NOT_AVAILABLE:
raise
# Call with non-empty arrays
try:
self.cimcall(self.conn.InvokeMethod,
'FooMethod',
TEST_CLASS,
StringArray='Spotty',
Uint8Array=[Uint8(1)],
Sint8Array=[Sint8(2)],
Uint16Array=[Uint16(3)],
Sint16Array=[Sint16(3)],
Uint32Array=[Uint32(4)],
Sint32Array=[Sint32(5)],
Uint64Array=[Uint64(6)],
Sint64Array=[Sint64(7)],
Real32Array=[Real32(8)],
Real64Array=[Real64(9)],
BoolArray=[False, True],
Date1Array=[CIMDateTime.now(), CIMDateTime.now()],
Date2Array=[timedelta(0), timedelta(60)],
RefArray=[name, name])
except CIMError as ce:
if ce.args[0] != CIM_ERR_METHOD_NOT_AVAILABLE:
raise
# Call with new Params arg
try:
self.cimcall(self.conn.InvokeMethod,
'FooMethod',
TEST_CLASS,
[('Spam', Uint16(1)), ('Ham', Uint16(2))], # Params
Drink=Uint16(3), # begin of **params
Beer=Uint16(4))
except CIMError as ce:
if ce.args[0] != CIM_ERR_METHOD_NOT_AVAILABLE:
raise
# TODO: Call with empty arrays
# TODO: Call with weird VALUE.REFERENCE child types:
# (CLASSPATH|LOCALCLASSPATH|CLASSNAME|INSTANCEPATH|LOCALINSTANCEPATH|
# INSTANCENAME)
#################################################################
# Association provider interface tests
#################################################################
class Associators(ClientTest):
def test_all(self):
# Call on named instance
inst_names = self.cimcall(self.conn.EnumerateInstanceNames, TEST_CLASS)
self.assertTrue(len(inst_names) >= 1)
inst_name = inst_names[0] # Pick the first returned instance
instances = self.cimcall(self.conn.Associators, inst_name)
for i in instances:
self.assertTrue(isinstance(i, CIMInstance))
self.assertTrue(isinstance(i.path, CIMInstanceName))
# TODO: For now, disabled test for class name of associated insts.
# self.assertTrue(i.classname == 'TBD')
self.assertTrue(i.path.namespace is not None)
self.assertTrue(i.path.host is not None)
# Call on class name
classes = self.cimcall(self.conn.Associators, TEST_CLASS)
self.assertTrue(len(classes) > 0)
for cl in classes:
self.assertTrue(isinstance(cl, tuple))
self.assertTrue(isinstance(cl[0], CIMClassName))
self.assertTrue(isinstance(cl[1], CIMClass))
# TODO: check return values
class AssociatorNames(ClientTest):
def test_all(self):
# Call on named instance
inst_names = self.cimcall(self.conn.EnumerateInstanceNames, TEST_CLASS)
self.assertTrue(len(inst_names) >= 1)
inst_name = inst_names[0] # Pick the first returned instance
names = self.cimcall(self.conn.AssociatorNames, inst_name)
for n in names:
self.assertTrue(isinstance(n, CIMInstanceName))
# TODO: For now, disabled test for class name of associated insts.
# self.assertTrue(n.classname == 'TBD')
self.assertTrue(n.namespace is not None)
self.assertTrue(n.host is not None)
# Call on class name. Returns CIMClassName
names = self.cimcall(self.conn.AssociatorNames, TEST_CLASS)
for n in names:
self.assertTrue(isinstance(n, CIMClassName))
# TODO: check return values, NS, etc.
class References(ClientTest):
def test_all(self):
# Call on named instance
inst_names = self.cimcall(self.conn.EnumerateInstanceNames, TEST_CLASS)
self.assertTrue(len(inst_names) >= 1)
inst_name = inst_names[0] # Pick the first returned instance
instances = self.cimcall(self.conn.References, inst_name)
for i in instances:
self.assertTrue(isinstance(i, CIMInstance))
self.assertTrue(isinstance(i.path, CIMInstanceName))
# TODO: For now, disabled test for class name of referencing insts.
# self.assertTrue(i.classname == 'TBD')
self.assertTrue(i.path.namespace is not None)
self.assertTrue(i.path.host is not None)
# Call on class name
classes = self.cimcall(self.conn.References, TEST_CLASS)
self.assertTrue(len(classes) > 0)
for cl in classes:
self.assertTrue(isinstance(cl, tuple))
self.assertTrue(isinstance(cl[0], CIMClassName))
self.assertTrue(isinstance(cl[1], CIMClass))
# TODO: check return values
class ReferenceNames(ClientTest):
def test_all(self):
# Call on instance name
inst_names = self.cimcall(self.conn.EnumerateInstanceNames, TEST_CLASS)
self.assertTrue(len(inst_names) >= 1)
inst_name = inst_names[0] # Pick the first returned instance
names = self.cimcall(self.conn.ReferenceNames, inst_name)
for n in names:
self.assertTrue(isinstance(n, CIMInstanceName))
# TODO: For now, disabled test for class name of referencing insts.
# self.assertTrue(n.classname == 'TBD')
self.assertTrue(n.namespace is not None)
self.assertTrue(n.host is not None)
# Call on class name
names = self.cimcall(self.conn.ReferenceNames, TEST_CLASS)
for n in names:
self.assertTrue(isinstance(n, CIMClassName))
# TODO: further check return values
#################################################################
# Schema manipulation interface tests
#################################################################
class ClientClassTest(ClientTest):
"""Intermediate class for testing CIMClass instances.
Class operations subclass from this class"""
def verify_property(self, prop):
"""Verify that this a cim property and verify attributes"""
self.assertTrue(isinstance(prop, CIMProperty))
def verify_qualifier(self, qualifier):
"""Verify qualifier attributes"""
self.assertTrue(isinstance(qualifier, CIMQualifier))
self.assertTrue(qualifier.name)
self.assertTrue(qualifier.value)
def verify_method(self, method):
"""Verify method attributes"""
self.assertTrue(isinstance(method, CIMMethod))
# TODO add these tests
##pass
def verify_class(self, cl):
"""Verify simple class attributes"""
self.assertTrue(isinstance(cl, CIMClass))
self.assertTrue(cl.classname)
if cl.superclass is not None:
self.assertTrue(cl.superclass)
# Verify properties, qualifiers and methods
for p in cl.properties.values():
self.verify_property(p)
# TODO validate qualifiers in properties. here or elsewhere
for q in cl.qualifiers.values():
self.verify_qualifier(q)
for m in cl.methods.values():
self.verify_method(m)
# TODO validate parameters in methods
class EnumerateClassNames(ClientTest):
def test_all(self):
# Enumerate all classes
names = self.cimcall(self.conn.EnumerateClassNames)
for n in names:
self.assertTrue(isinstance(n, six.string_types))
# Enumerate with classname arg
names = self.cimcall(self.conn.EnumerateClassNames,
ClassName='CIM_ManagedElement')
for n in names:
self.assertTrue(isinstance(n, six.string_types))
class EnumerateClasses(ClientClassTest):
def test_all(self):
# Enumerate all classes
classes = self.cimcall(self.conn.EnumerateClasses)
for c in classes:
self.assertTrue(isinstance(c, CIMClass))
self.verify_class(c)
# Enumerate with classname arg
classes = self.cimcall(self.conn.EnumerateClasses,
ClassName='CIM_ManagedElement')
for cl in classes:
self.assertTrue(isinstance(cl, CIMClass))
self.verify_class(cl)
class GetClass(ClientClassTest):
def test_all(self):
"""Get a classname from server and then get class"""
name = self.cimcall(self.conn.EnumerateClassNames)[0]
cl = self.cimcall(self.conn.GetClass, name)
if self.debug:
print('GetClass gets name %s' % name)
self.verify_class(cl)
class CreateClass(ClientClassTest):
@unittest.skip(UNIMPLEMENTED)
def test_all(self):
raise AssertionError("test not implemented")
class DeleteClass(ClientClassTest):
@unittest.skip(UNIMPLEMENTED)
def test_all(self):
raise AssertionError("test not implemented")
class ModifyClass(ClientClassTest):
@unittest.skip(UNIMPLEMENTED)
def test_all(self):
raise AssertionError("test not implemented")
#################################################################
# Qualifier Declaration provider interface tests
#################################################################
class QualifierDeclClientTest(ClientTest):
"""Base class for QualifierDeclaration tests. Adds specific
tests
"""
def verify_qual_decl(self, ql, test_name=None):
"""Verify simple class attributes"""
self.assertTrue(isinstance(ql, CIMQualifierDeclaration))
if test_name is not None:
self.assertTrue(ql.name == test_name)
# TODO expand the verification of qual decls
class EnumerateQualifiers(QualifierDeclClientTest):
def test_all(self):
qual_decls = self.cimcall(self.conn.EnumerateQualifiers)
self.assertTrue(len(qual_decls) > 0)
for qual_decl in qual_decls:
self.verify_qual_decl(qual_decl)
class GetQualifier(QualifierDeclClientTest):
def test_all(self):
qual_decl = self.cimcall(self.conn.GetQualifier, 'Abstract')
self.verify_qual_decl(qual_decl, test_name='Abstract')
# test with name that is not found
try:
qual_decl = self.cimcall(self.conn.GetQualifier, 'blahblah')
except CIMError as ce:
if ce.args[0] != CIM_ERR_NOT_FOUND:
raise
class SetQualifier(QualifierDeclClientTest):
@unittest.skip(UNIMPLEMENTED)
def test_all(self):
raise AssertionError("test not implemented")
class DeleteQualifier(QualifierDeclClientTest):
@unittest.skip(UNIMPLEMENTED)
def test_all(self):
raise AssertionError("test not implemented")
#################################################################
# Query provider interface
#################################################################
class ExecuteQuery(ClientTest):
@unittest.skip(UNIMPLEMENTED)
def test_all(self):
raise AssertionError("test not implemented")
#################################################################
# Main function
#################################################################
TEST_LIST = [
# Instance provider interface tests
'EnumerateInstanceNames',
'EnumerateInstances',
'GetInstance',
'CreateInstance',
'ModifyInstance',
# Method provider interface tests
'InvokeMethod',
# Association provider interface tests
'Associators',
'AssociatorNames',
'References',
'ReferenceNames',
# Schema manipulation interface tests
'EnumerateClassNames',
'EnumerateClasses',
'GetClass',
'CreateClass',
'DeleteClass',
'ModifyClass',
# Qualifier provider interface tests
'EnumerateQualifiers',
'GetQualifier',
'SetQualifier',
'DeleteQualifier',
# Query provider interface tests
'ExecQuery',
'ExecuteQuery',
]
def parse_args(argv_):
argv = list(argv_)
if len(argv) <= 1:
print('Error: No arguments specified; Call with --help or -h for '\
'usage.')
sys.exit(2)
elif argv[1] == '--help' or argv[1] == '-h':
print('')
print('Test program for CIM operations.')
print('')
print('Usage:')
print(' %s [GEN_OPTS] URL [USERNAME%%PASSWORD [UT_OPTS '\
'[UT_CLASS ...]]] ' % argv[0])
print('')
print('Where:')
print(' GEN_OPTS General options (see below).')
print(' URL URL of the target WBEM server.\n'\
' http:// or https:// prefix'\
' defines ssl usage')
print(' USERNAME Userid used to log into '\
'WBEM server.\n' \
' Requests user input if not supplied')
print(' PASSWORD Password used to log into '\
'WBEM server.\n' \
' Requests user input if not supplier')
print(' UT_OPTS Unittest options (see below).')
print(' UT_CLASS Name of testcase class (e.g. '\
'EnumerateInstances).')
print('')
print('General options[GEN_OPTS]:')
print(' --help, -h Display this help text.')
print(' -n NAMESPACE Use this CIM namespace instead of '\
'default: %s' % DEFAULT_NAMESPACE)
print(' -t TIMEOUT Use this timeout (in seconds)'\
' instead of no timeout')
print(' -v Verbose output which includes:\n' \
' - xml input and output,\n' \
' - connection details,\n'
' - details of tests')
print(' -d Debug flag for extra displays')
print(' -hl List of indifidual tests')
print('')
print('Examples:')
print(' %s https://9.10.11.12 username%%password' % argv[0])
print(' %s https://myhost -v username%%password' % argv[0])
print(' %s http://localhost -v username%%password' \
' GetQualifier' % argv[0])
print('------------------------')
print('Unittest arguments[UT_OPTS]:')
print('')
sys.argv[1:] = ['--help']
unittest.main()
sys.exit(2)
args_ = {}
# set argument defaults
args_['namespace'] = DEFAULT_NAMESPACE
args_['timeout'] = None
args_['debug'] = False
args_['verbose'] = False
args_['username'] = None
args_['password'] = None
# options must proceed arguments
while True:
if argv[1][0] != '-':
# Stop at first non-option
break
if argv[1] == '-n':
args_['namespace'] = argv[2]
del argv[1:3]
elif argv[1] == '-t':
args_['timeout'] = int(argv[2])
del argv[1:3]
elif argv[1] == '-v':
args_['verbose'] = True
del argv[1:2]
elif argv[1] == '-d':
args_['debug'] = True
del argv[1:2]
elif argv[1] == '-hl':
args_['debug'] = True
del argv[1:3]
print('List of tests: %s' % ", ".join(TEST_LIST))
sys.exit(2)
else:
print("Error: Unknown option: %s" % argv[1])
sys.exit(1)
args_['url'] = argv[1]
del argv[1:2]
if len(argv) >= 2:
args_['username'], args_['password'] = argv[1].split('%')
del argv[1:2]
else:
# Get user name and pw from console
sys.stdout.write('Username: ')
sys.stdout.flush()
args_['username'] = sys.stdin.readline().strip()
args_['password'] = getpass()
return args_, argv
if __name__ == '__main__':
args, sys.argv = parse_args(sys.argv) # pylint: disable=invalid-name
print("Using WBEM Server:")
print(" server url: %s" % args['url'])
print(" namespace: %s" % args['namespace'])
print(" username: %s" % args['username'])
print(" password: %s" % ("*"*len(args['password'])))
print(" timeout: %s" % args['timeout'])