more tests. fix errors
[tests.git] / qaapi / qa / tests / api_unit_test.py
1 #!/usr/bin/env /usr/share/plc_api/plcsh
2 #
3 # Test script example
4 #
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7 #
8
9 from pprint import pprint
10 from string import letters, digits, punctuation
11 import traceback
12 from traceback import print_exc
13 import base64
14 import os, sys
15 import socket
16 import struct
17 import xmlrpclib
18 import time
19
20 from Test import Test
21 from qa import utils
22 from qa.Config import Config
23 from qa.logger import Logfile, log, logfile
24 from random import Random
25
26 random = Random()
27
28 config = Config()
29 api = config.api
30 auth = api.auth  
31
32 try: boot_states = api.GetBootStates(auth)
33 except: boot_states = [u'boot', u'dbg', u'inst', u'new', u'rcnf', u'rins']
34
35 try: roles = [role['role_id'] for role in api.GetRoles(auth)]
36 except: roles = [10,20,30,40]
37
38 try: methods = api.GetNetworkMethods(auth)
39 except: methods = [u'static', u'dhcp', u'proxy', u'tap', u'ipmi', u'unknown']
40
41 try: key_types = api.GetKeyTypes(auth)
42 except: key_types = [u'ssh']
43
44 try:types = api.GetNetworkTypes(auth)
45 except: types = [u'ipv4']
46
47 try:
48     attribute_types = [a['attribute_type_id'] for a in api.GetSliceAttributeTypes(auth)]
49     attribute_types = filter(lambda x: x['name'] != 'initscript', attribute_types)
50         
51 except: 
52     attribute_types = range(6,20)
53
54 try:
55     sites = api.GetSites(auth, None, ['login_base'])
56     login_bases = [site['login_base'] for site in sites]
57 except: 
58     login_bases = ['pl']
59
60
61 def randfloat(min = 0.0, max = 1.0):
62     return float(min) + (random.random() * (float(max) - float(min)))
63
64 def randint(min = 0, max = 1):
65     return int(randfloat(min, max + 1))
66
67 def randbool():
68     return random.sample([True, False], 1)[0]   
69 # See "2.2 Characters" in the XML specification:
70 #
71 # #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
72 # avoiding
73 # [#x7F-#x84], [#x86-#x9F], [#xFDD0-#xFDDF]
74
75 ascii_xml_chars = map(unichr, [0x9, 0xA, 0xD])
76 ascii_xml_chars += map(unichr, xrange(0x20, 0x7F - 1))
77 low_xml_chars = list(ascii_xml_chars)
78 low_xml_chars += map(unichr, xrange(0x84 + 1, 0x86 - 1))
79 low_xml_chars += map(unichr, xrange(0x9F + 1, 0xFF))
80 valid_xml_chars = list(low_xml_chars)
81 valid_xml_chars += map(unichr, xrange(0xFF + 1, 0xD7FF))
82 valid_xml_chars += map(unichr, xrange(0xE000, 0xFDD0 - 1))
83 valid_xml_chars += map(unichr, xrange(0xFDDF + 1, 0xFFFD))
84
85 def randstr(length, pool = valid_xml_chars, encoding = "utf-8"):
86     sample = random.sample(pool, min(length, len(pool)))
87     while True:
88         s = u''.join(sample)
89         bytes = len(s.encode(encoding))
90         if bytes > length:
91             sample.pop()
92         elif bytes < length:
93             sample += random.sample(pool, min(length - bytes, len(pool)))
94             random.shuffle(sample)
95         else:
96             break
97     return s
98
99 def randhostname():
100     # 1. Each part begins and ends with a letter or number.
101     # 2. Each part except the last can contain letters, numbers, or hyphens.
102     # 3. Each part is between 1 and 64 characters, including the trailing dot.
103     # 4. At least two parts.
104     # 5. Last part can only contain between 2 and 6 letters.
105     hostname = 'a' + randstr(61, letters + digits + '-') + '1.' + \
106                'b' + randstr(61, letters + digits + '-') + '2.' + \
107                'c' + randstr(5, letters)
108     return hostname
109
110 def randpath(length):
111     parts = []
112     for i in range(randint(1, 10)):
113         parts.append(randstr(randint(1, 30), ascii_xml_chars))
114     return os.sep.join(parts)[0:length]
115
116 def randemail():
117     return (randstr(100, letters + digits) + "@" + randhostname()).lower()
118
119 def randkey(bits = 2048):
120     key_types = ["ssh-dss", "ssh-rsa"]
121     key_type = random.sample(key_types, 1)[0]
122     return ' '.join([key_type,
123                      base64.b64encode(''.join(randstr(bits / 8).encode("utf-8"))),
124                      randemail()])
125
126 def random_site():
127     return {
128         'name': randstr(254),
129         'abbreviated_name': randstr(50),
130         'login_base': randstr(20, letters).lower(),
131         'latitude': int(randfloat(-90.0, 90.0) * 1000) / 1000.0,
132         'longitude': int(randfloat(-180.0, 180.0) * 1000) / 1000.0,
133         'max_slices': 10
134         }
135
136 def random_peer():
137     bits = 2048 
138     return {
139         'peername': randstr(254),
140         'peer_url': "https://" + randhostname() + "/",
141         'key': base64.b64encode(''.join(randstr(bits / 8).encode("utf-8"))),
142         'cacert': base64.b64encode(''.join(randstr(bits / 8).encode("utf-8")))
143         }            
144 def random_address_type():
145     return {
146         'name': randstr(20),
147         'description': randstr(254),
148         }
149
150 def random_address():
151     return {
152         'line1': randstr(254),
153         'line2': randstr(254),
154         'line3': randstr(254),
155         'city': randstr(254),
156         'state': randstr(254),
157         'postalcode': randstr(64),
158         'country': randstr(128),
159         }
160
161 def random_person():
162     return {
163         'first_name': randstr(128),
164         'last_name': randstr(128),
165         'email': randemail(),
166         'bio': randstr(254),
167         # Accounts are disabled by default
168         'enabled': False,
169         'password': randstr(254),
170         }
171
172 def random_key():
173     return {
174         'key_type': random.sample(key_types, 1)[0],
175         'key': randkey()
176         }
177
178 def random_slice():
179     return {
180         'name': random.sample(login_bases, 1)[0] + "_" + randstr(11, letters).lower(),
181         'url': "http://" + randhostname() + "/",
182         'description': randstr(2048),
183         }
184
185 def random_nodegroup():
186     return {
187         'name': randstr(50),
188         'description': randstr(200),
189         }
190
191 def random_node():
192    return {
193        'hostname': randhostname(),
194        'boot_state': random.sample(boot_states, 1)[0],
195        'model': randstr(255),
196        'version': randstr(64),
197        }
198
199 def random_nodenetwork():
200     nodenetwork_fields = {
201         'method': random.sample(methods, 1)[0],
202         'type': random.sample(types, 1)[0],
203         'bwlimit': randint(500000, 10000000),
204         }
205
206     if nodenetwork_fields['method'] != 'dhcp':
207         ip = randint(0, 0xffffffff)
208         netmask = (0xffffffff << randint(2, 31)) & 0xffffffff
209         network = ip & netmask
210         broadcast = ((ip & netmask) | ~netmask) & 0xffffffff
211         gateway = randint(network + 1, broadcast - 1)
212         dns1 = randint(0, 0xffffffff)
213
214         for field in 'ip', 'netmask', 'network', 'broadcast', 'gateway', 'dns1':
215             nodenetwork_fields[field] = socket.inet_ntoa(struct.pack('>L', locals()[field]))
216
217     return nodenetwork_fields
218
219 def random_nodenetwork_setting():
220     return {
221         'value': randstr(20)
222         }
223
224 def random_nodenetwork_setting_type(): 
225     return {
226         'name': randstr(20),
227         'description': randstr(50),
228         'category': randstr(20),
229         'min_role_id': random.sample(roles, 1)[0]
230         }
231
232 def random_pcu():
233     return {
234         'hostname': randhostname(),
235         'ip': socket.inet_ntoa(struct.pack('>L', randint(0, 0xffffffff))),
236         'protocol': randstr(16),
237         'username': randstr(254),
238         'password': randstr(254),
239         'notes': randstr(254),
240         'model': randstr(32),
241         }
242
243 def random_conf_file():
244     return {
245         'enabled': bool(randint()),
246         'source': randpath(255),
247         'dest': randpath(255),
248         'file_permissions': "%#o" % randint(0, 512),
249         'file_owner': randstr(32, letters + '_' + digits),
250         'file_group': randstr(32, letters + '_' + digits),
251         'preinstall_cmd': randpath(100),
252         'postinstall_cmd': randpath(100),
253         'error_cmd': randpath(100),
254         'ignore_cmd_errors': bool(randint()),
255         'always_update': bool(randint()),
256         }
257
258 def random_attribute_type():
259     return {
260         'name': randstr(100),
261         'description': randstr(254),
262         'min_role_id': int(random.sample(roles, 1)[0]),
263         }
264
265 def random_pcu_type():
266     return {
267         'model': randstr(254),
268         'name': randstr(254),
269         }
270
271 def random_pcu_protocol_type():
272     return { 
273         'port': randint(0, 65535),
274         'protocol': randstr(254),
275         'supported': randbool()
276         }
277
278 def random_slice_instantiation():
279     return {
280         'instantiation': randstr(10)
281         }
282 def random_slice_attribute():
283     return {
284         'attribute_type_id': random.sample(attribute_types, 1)[0],
285         'name': randstr(50),
286         'value': randstr(20)
287         }
288
289 def random_initscript():
290     return {
291         'name': randstr(20),
292         'enabled': randbool(),
293         'script': randstr(200)
294         }
295
296 def random_role():
297     return {
298         'role_id': randint(1000),
299         'name': randstr(50)
300         }
301                 
302 class api_unit_test(Test):
303
304     logfile = Logfile("api-unittest-summary.log")
305    
306     def call(self,
307              boot_states = 2,
308              sites = 2,
309              peers = 2,
310              address_types = 3,
311              addresses = 2,
312              pcu_types = 2,
313              pcu_protocol_types = 2,
314              pcus = 2,
315              network_methods = 2,
316              network_types = 2,
317              nodegroups = 3,
318              nodes = 4,
319              conf_files = 3,
320              nodenetworks = 4,
321              nodenetwork_setting_types = 2,
322              nodenetwork_settings = 2,
323              slice_attribute_types = 2,
324              slice_instantiations = 2,
325              slices = 4,
326              slice_attributes = 4,           
327              initscripts = 4,   
328              roles = 2,
329              persons = 4, 
330              key_types = 3,     
331              keys = 3,
332              messages = 2
333             ):
334         # Filter out deprecated (Adm) and boot Methods
335         current_methods = lambda method: not method.startswith('Adm') and \
336                                          not method.startswith('Slice') and \
337                                          not method.startswith('Boot') and \
338                                          not method.startswith('Anon') and \
339                                          not method.startswith('system')
340         self.all_methods = set(filter(current_methods, api.system.listMethods())) 
341         self.methods_tested = set()
342         self.methods_failed = set()
343
344         # Begin testing methods
345         try:
346             try:
347                 if hasattr(self, 'BootStates'): self.boot_states = self.BootStates(boot_states)
348                 if hasattr(self, 'Sites'): self.site_ids = self.Sites(sites)
349                 if hasattr(self, 'Peers'): self.peer_ids = self.Peers(peers)
350                 if hasattr(self, 'AddressTypes'): self.address_type_ids = self.AddressTypes(address_types)
351                 if hasattr(self, 'Addresses'): self.address_ids = self.Addresses(addresses)
352                 if hasattr(self, 'PCUTypes'): self.pcu_type_ids = self.PCUTypes(pcu_types)
353                 if hasattr(self, 'PCUProtocolTypes'): self.pcu_protocol_type_ids = self.PCUProtocolTypes(pcu_protocol_types)
354                 if hasattr(self, 'PCUs'): self.pcu_ids = self.PCUs(pcus)                
355                 if hasattr(self, 'NetworkMethods'): self.network_methods = self.NetworkMethods()
356                 if hasattr(self, 'NetworkTypes'): self.network_types = self.NetworkTypes()
357                 if hasattr(self, 'NodeGroups'): self.nodegroup_ids = self.NodeGroups()
358                 if hasattr(self, 'Nodes'): self.node_ids = self.Nodes(nodes)
359                 if hasattr(self, 'ConfFiles'): self.conf_file_ids = self.ConfFiles(conf_files)
360                 if hasattr(self, 'NodeNetworks'): self.nodenetwork_ids = self.NodeNetworks(nodenetworks)
361                 if hasattr(self, 'NodeNetworkSettingTypes'): self.nodenetwork_setting_type_ids = self.NodeNetworkSettingTypes(nodenetwork_setting_types)
362                 if hasattr(self, 'NodeNetworkSettings'): self.nodenetwork_setting_ids = self.NodeNetworkSettings(nodenetwork_settings)
363                 if hasattr(self, 'SliceAttributeTypes'): self.slice_attribute_type_ids = self.SliceAttributeTypes(slice_attribute_types)
364                 if hasattr(self, 'SliceInstantiations'): self.slice_instantiations = self.SliceInstantiations(slice_instantiations)
365                 if hasattr(self, 'Slices'): self.slice_ids = self.Slices(slices)
366                 if hasattr(self, 'SliceAttributes'): self.slice_attribute_ids = self.SliceAttributes(slice_attributes)
367                 if hasattr(self, 'InitScripts'): self.initscript_ids = self.InitScripts(initscripts)
368                 if hasattr(self, 'Roles'): self.role_ids = self.Roles(roles)
369                 if hasattr(self, 'Persons'): self.person_ids = self.Persons(persons)
370                 if hasattr(self, 'KeyTypes'): self.key_types = self.KeyTypes(key_types)
371                 if hasattr(self, 'Keys'): self.key_ids = self.Keys(keys)
372                 if hasattr(self, 'Messages'):  self.message_ids = self.Messages(messages)       
373                 if hasattr(self, 'Sessions'): self.Sessions()
374                 
375                 # Test misc Get calls
376                 if hasattr(self, 'GenerateNodeConfFile'): self.GenerateNodeConfFile()
377                 if hasattr(self, 'GetBootMedium'): self.GetBootMedium() 
378                 if hasattr(self, 'GetEventObjects'): self.event_object_ids = self.GetEventObjects()
379                 if hasattr(self, 'GetEvents'): self.event_ids = self.GetEvents()
380                 if hasattr(self, 'GetPeerData'): self.GetPeerData()
381                 if hasattr(self, 'GetPeerName'): self.GetPeerName()
382                 if hasattr(self, 'GetPlcRelease'): self.GetPlcRelease()
383                 if hasattr(self, 'GetSliceKeys'): self.GetSliceKeys()
384                 if hasattr(self, 'GetSliceTicket'): self.GetSliceTicket()
385                 if hasattr(self, 'GetSlicesMD5'): self.GetSlicesMD5()
386                 if hasattr(self, 'GetSlivers'): self.GetSlivers()
387                 if hasattr(self, 'GetWhitelist'): self.GetWhitelist()
388
389                 # Test various administrative methods
390                 if hasattr(self, 'NotifyPersons'): self.NotifyPersons()
391                 if hasattr(self, 'NotifySupport'): self.NotifySupport()
392                 if hasattr(self, 'RebootNode'): self.RebootNode()
393                 if hasattr(self, 'RefrestPeer'): self.RefreshPeer()
394                 if hasattr(self, 'ResetPassword'): self.ResetPassword()
395                 if hasattr(self, 'SetPersonPrimarySite'): self.SetPersonPrimarySite()
396                 if hasattr(self, 'VerifyPerson'): self.VerifyPerson()
397                 
398                 
399                 
400                  
401             except:
402                 print_exc()
403         finally:
404             try:
405                 self.cleanup()
406             finally: 
407                 utils.header("writing api-unittest-summary.log") 
408                 methods_ok = list(self.methods_tested.difference(self.methods_failed))
409                 methods_failed = list(self.methods_failed)
410                 methods_untested = list(self.all_methods.difference(self.methods_tested))
411                 methods_ok.sort()
412                 methods_failed.sort()
413                 methods_untested.sort()
414                 print >> self.logfile, "\n".join([m+": [OK]" for m in  methods_ok])
415                 print >> self.logfile, "\n".join([m+": [FAILED]" for m in methods_failed])
416                 print >> self.logfile, "\n".join([m+": [Not Tested]" for m in  methods_untested])
417  
418     def isequal(self, object_fields, expected_fields, method_name):
419         try:
420             for field in expected_fields:
421                 assert field in object_fields
422                 assert object_fields[field] == expected_fields[field]
423         except:
424             self.methods_failed.update([method_name])    
425             return False
426         return True
427
428     def islistequal(self, list1, list2, method_name):
429         try: assert set(list1) == set(list2)
430         except:
431             self.methods_failed.update([method_name]) 
432             return False
433         return True
434
435     def isunique(self, id, id_list, method_name):
436         try: assert id not in id_list
437         except:
438             self.methods_failed.update([method_name])    
439             return False
440         return True
441     
442     def isinlist(self, item, item_list, method_name):
443         try: assert item in item_list
444         except:
445             self.methods_failed.update([method_name])
446             return False
447         return True 
448     def debug(self, method, method_name=None):
449         if method_name is None:
450              method_name = method.name
451
452         self.methods_tested.update([method_name])
453         def wrapper(*args, **kwds):
454             try:
455                 return method(*args, **kwds)
456             except:
457                 self.methods_failed.update([method_name])
458                 print >> logfile, "%s%s: %s\n" % (method_name, tuple(args[1:]), traceback.format_exc()) 
459                 return None
460
461         return wrapper
462  
463     def cleanup(self):
464         if hasattr(self, 'key_types'): self.DeleteKeyTypes()
465         if hasattr(self, 'key_ids'): self.DeleteKeys()
466         if hasattr(self, 'person_ids'): self.DeletePersons()
467         if hasattr(self, 'role_ids'): self.DeleteRoles()
468         if hasattr(self, 'initscript_ids'): self.DeleteInitScripts()
469         if hasattr(self, 'slice_attribute_ids'): self.DeleteSliceAttributes()
470         if hasattr(self, 'slice_ids'): self.DeleteSlices()
471         if hasattr(self, 'slice_instantiations'): self.DeleteSliceInstantiations()
472         if hasattr(self, 'slice_attribute_type_ids'): self.DeleteSliceAttributeTypes()
473         if hasattr(self, 'nodenetwork_setting_ids'): self.DeleteNodeNetworkSettings()
474         if hasattr(self, 'nodenetwork_setting_type_ids'): self.DeleteNodeNetworkSettingTypes()
475         if hasattr(self, 'nodenetwork_ids'): self.DeleteNodeNetworks()
476         if hasattr(self, 'conf_file_ids'): self.DeleteConfFiles()
477         if hasattr(self, 'node_ids'): self.DeleteNodes()
478         if hasattr(self, 'nodegroup_ids'): self.DeleteNodeGroups()
479         if hasattr(self, 'network_types'): self.DeleteNetworkTypes()
480         if hasattr(self, 'network_methods'): self.DeleteNetworkMethods()
481         if hasattr(self, 'pcu_ids'): self.DeletePCUs()
482         if hasattr(self, 'pcu_protocol_type_ids'): self.DeletePCUProtocolTypes()                
483         if hasattr(self, 'pcu_type_ids'): self.DeletePCUTypes()
484         if hasattr(self, 'address_ids'): self.DeleteAddresses()
485         if hasattr(self, 'address_type_ids'): self.DeleteAddressTypes()
486         if hasattr(self, 'peer_ids'): self.DeletePeers()
487         if hasattr(self, 'site_ids'): self.DeleteSites()
488         if hasattr(self, 'boot_states'): self.DeleteBootStates()
489         
490
491     def Sites(self, n=4):
492         site_ids = []
493         for i in range(n):
494             # Add Site
495             site_fields = random_site()
496             AddSite = self.debug(api.AddSite) 
497             site_id = AddSite(auth, site_fields)
498             if site_id is None: continue
499
500             # Should return a unique id
501             self.isunique(site_id, site_ids, 'AddSite - isunique')
502             site_ids.append(site_id)
503             GetSites = self.debug(api.GetSites)
504             sites = GetSites(auth, [site_id])
505             if sites is None: continue
506             site = sites[0]
507             self.isequal(site, site_fields, 'AddSite - isequal')
508         
509             # Update site
510             site_fields = random_site()
511             UpdateSite = self.debug(api.UpdateSite)
512             result = UpdateSite(auth, site_id, site_fields)
513
514             # Check again
515             sites = GetSites(auth, [site_id])
516             if sites is None: continue
517             site = sites[0]      
518             self.isequal(site, site_fields, 'UpdateSite - isequal')
519             
520         sites = GetSites(auth, site_ids)
521         if sites is not None: 
522             self.islistequal(site_ids, [site['site_id'] for site in sites], 'GetSites - isequal')
523         
524         if self.config.verbose:
525             utils.header("Added sites: %s" % site_ids)          
526
527         return site_ids
528
529
530     def DeleteSites(self):
531         # Delete all sites
532         DeleteSite = self.debug(api.DeleteSite)
533         for site_id in self.site_ids:
534             result = DeleteSite(auth, site_id)
535
536         # Check if sites are deleted
537         GetSites = self.debug(api.GetSites)
538         sites = GetSites(auth, self.site_ids) 
539         self.islistequal(sites, [], 'DeleteSite - check')       
540
541         if self.config.verbose:
542             utils.header("Deleted sites: %s" % self.site_ids)
543
544         self.site_ids = []
545
546     def NetworkMethods(self, n=2):
547         methods = []
548         AddNetworkMethod = self.debug(api.AddNetworkMethod)
549         GetNetworkMethods = self.debug(api.GetNetworkMethods)
550  
551         for i in range(n):
552             # Add Network Method
553             net_method = randstr(10)
554             AddNetworkMethod(auth, net_method)
555             if net_method is None: continue
556
557             # Should return a unique id
558             self.isunique(net_method, methods, 'AddNetworkMethod - isunique')
559             methods.append(net_method)
560             net_methods = GetNetworkMethods(auth)
561             if net_methods is None: continue
562             net_methods = filter(lambda x: x in [net_method], net_methods) 
563             method = net_methods[0]
564             self.isequal(method, net_method, 'AddNetworkMethod - isequal')
565
566
567         net_methods = GetNetworkMethods(auth)
568         if net_methods is not None:
569             net_methods = filter(lambda x: x in methods, net_methods)
570             self.islistequal(methods, net_methods, 'GetNetworkMethods - isequal')
571
572         if self.config.verbose:
573             utils.header("Added network methods: %s" % methods)
574
575         return methods
576
577     def DeleteNetworkMethods(self):
578         DeleteNetworkMethod = self.debug(api.DeleteNetworkMethod)
579         GetNetworkMethods = self.debug(api.GetNetworkMethods)   
580         for method in self.network_methods:
581             DeleteNetworkMethod(auth, method)
582
583         # check 
584         network_methods = GetNetworkMethods(auth)
585         network_methods = filter(lambda x: x in self.network_methods, network_methods)
586         self.islistequal(network_methods, [], 'DeleteNetworkMethods - check')
587
588         if self.config.verbose:
589             utils.header("Deleted network methods: %s" % self.network_methods)
590         self.network_methods = []    
591
592     def NetworkTypes(self, n=2):
593         net_types = []
594         AddNetworkType = self.debug(api.AddNetworkType)
595         GetNetworkTypes = self.debug(api.GetNetworkTypes)
596          
597         for i in range(n):
598             # Add Network Type 
599             type = randstr(10)
600             AddNetworkType(auth, type)
601       
602             # Should return a unique id
603             self.isunique(type, net_types, 'AddNetworkType - isunique')
604             net_types.append(type)
605             types = GetNetworkTypes(auth)
606             if types is None: continue
607             types = filter(lambda x: x in [type], types)
608             if types is None: continue
609             net_type = types[0]
610             self.isequal(net_type, type, 'AddNetworkType - isequal')
611     
612         types = GetNetworkTypes(auth)
613         if types is not None:
614             types = filter(lambda x: x in net_types, types)
615             self.islistequal(types, net_types, 'GetNetworkTypes - isequal')
616
617         if self.config.verbose:
618             utils.header("Added network types: %s" % net_types)
619
620         return net_types        
621
622     def DeleteNetworkTypes(self):       
623         DeleteNetworkType = self.debug(api.DeleteNetworkType)
624         GetNetworkTypes = self.debug(api.GetNetworkTypes)
625         for type in self.network_types:
626             DeleteNetworkType(auth, type)
627
628         # check 
629         network_types = GetNetworkTypes(auth)
630         network_types = filter(lambda x: x in self.network_types, network_types)
631         self.islistequal(network_types, [], 'DeleteNetworkTypes - check')
632
633         if self.config.verbose:
634             utils.header("Deleted network types: %s" % self.network_types)
635         self.network_types = [] 
636
637
638     def NodeGroups(self, n = 4):
639         nodegroup_ids = []
640         AddNodeGroup = self.debug(api.AddNodeGroup)
641         UpdateNodeGroup = self.debug(api.UpdateNodeGroup)
642         GetNodeGroups = self.debug(api.GetNodeGroups)
643
644         for i in range(n):
645             # Add Nodegroups
646             nodegroup_fields = random_nodegroup()
647             nodegroup_id = AddNodeGroup(auth, nodegroup_fields)
648             if nodegroup_id is None: continue
649         
650             # Should return a unique id
651             self.isunique(nodegroup_id, nodegroup_ids, 'AddNodeGroup - isunique')
652             nodegroup_ids.append(nodegroup_id)
653             nodegroups = GetNodeGroups(auth, [nodegroup_id])
654             if nodegroups is None: continue
655             nodegroup = nodegroups[0]
656             self.isequal(nodegroup, nodegroup_fields, 'AddNodeGroup - isequal')
657         
658             # Update NodeGroups
659             nodegroup_fields = random_nodegroup()
660             UpdateNodeGroup(auth, nodegroup_id, nodegroup_fields)
661
662             # Check again
663             nodegroups = GetNodeGroups(auth, [nodegroup_id])
664             if nodegroups is None: continue
665             nodegroup = nodegroups[0]
666             self.isequal(nodegroup, nodegroup_fields, 'UpdateNodeGroup - isequal')
667
668         nodegroups = GetNodeGroups(auth, nodegroup_ids)
669         if nodegroups is not None:
670             self.islistequal(nodegroup_ids, [n['nodegroup_id'] for n in nodegroups], 'GetNodeGroups - isequal')
671         if self.config.verbose:
672             utils.header("Added nodegroups: %s" % nodegroup_ids)
673         
674         return nodegroup_ids
675
676     def DeleteNodeGroups(self):
677         # Delete all NodeGroups
678         GetNodeGroups = self.debug(api.GetNodeGroups)
679         DeleteNodeGroup = self.debug(api.DeleteNodeGroup)
680         
681         for nodegroup_id in self.nodegroup_ids:
682             result = DeleteNodeGroup(auth, nodegroup_id)
683         
684         # Check is nodegroups are deleted
685         nodegroups = GetNodeGroups(auth, self.nodegroup_ids)
686         self.islistequal(nodegroups, [], 'DeleteNodeGroup - check')
687         
688         if self.config.verbose:
689             utils.header("Deleted nodegroups: %s" % self.nodegroup_ids)
690         
691         self.nodegroup_ids = []    
692
693     def PCUTypes(self, n=2):
694         pcu_type_ids = []
695         AddPCUType = self.debug(api.AddPCUType)
696         UpdatePCUType = self.debug(api.UpdatePCUType)
697         GetPCUTypes = self.debug(api.GetPCUTypes)
698  
699         for i in range(n):
700             # Add PCUType
701             pcu_type_fields = random_pcu_type()
702             pcu_type_id = AddPCUType(auth, pcu_type_fields)
703             if pcu_type_id is None: continue
704             # Should return a unique id
705             self.isunique(pcu_type_id, pcu_type_ids, 'AddPCUType - isunique')
706             pcu_type_ids.append(pcu_type_id)
707            
708             # Check pcu type
709             pcu_types = GetPCUTypes(auth, [pcu_type_id])
710             if pcu_types is None: continue
711             pcu_type = pcu_types[0]
712             self.isequal(pcu_type, pcu_type_fields, 'AddPCUType - isequal')
713
714             # Update PCUType
715             pcu_type_fields = random_pcu_type()
716             UpdatePCUType(auth, pcu_type_id, pcu_type_fields)
717
718             # Check again
719             pcu_types = GetPCUTypes(auth, [pcu_type_id])
720             if pcu_types is None: continue
721             pcu_type = pcu_types[0]
722             self.isequal(pcu_type, pcu_type_fields, 'UpdatePCUType - isequal')
723
724         pcu_types = GetPCUTypes(auth, pcu_type_ids)
725         if pcu_types is not None:
726             self.islistequal(pcu_type_ids, [p['pcu_type_id'] for p in pcu_types], 'GetPCUTypes - check')
727
728         if self.config.verbose:
729             utils.header("Added pcu_types: %s " % pcu_type_ids)
730         return pcu_type_ids
731           
732     def DeletePCUTypes(self):
733         GetPCUTypes = self.debug(api.GetPCUTypes)
734         DeletePCUType = self.debug(api.DeletePCUType)
735         
736         for pcu_type_id in self.pcu_type_ids:
737             DeletePCUType(auth, pcu_type_id)
738         
739         pcu_types = GetPCUTypes(auth, self.pcu_type_ids)
740         self.islistequal(pcu_types, [], 'DeletePCUType - check')  
741
742     def PCUProtocolTypes(self, n=2):
743         protocol_type_ids = []
744         AddPCUProtocolType = self.debug(api.AddPCUProtocolType)
745         UpdatePCUProtocolType = self.debug(api.UpdatePCUProtocolType)
746         GetPCUProtocolTypes = self.debug(api.GetPCUProtocolTypes)
747         
748         for i in range(n):
749             # Add PCUProtocolType
750             protocol_type_fields = random_pcu_protocol_type()
751             pcu_type_id = random.sample(self.pcu_type_ids, 1)[0]
752             protocol_type_id = AddPCUProtocolType(auth, pcu_type_id, protocol_type_fields)          
753             if protocol_type_id is None: continue
754             
755             # Should return a unique id
756             self.isunique(protocol_type_id, protocol_type_ids, 'AddPCUProtocolType - isunique')
757             protocol_type_ids.append(protocol_type_id)
758
759             # Check protocol type
760             protocol_types = GetPCUProtocolTypes(auth, [protocol_type_id])
761             if protocol_types is None: continue
762             protocol_type = protocol_types[0]
763             self.isequal(protocol_type, protocol_type_fields, 'AddPCUProtocolType - isequal')
764                 
765             # Update protocol type
766             protocol_type_fields = random_pcu_protocol_type()
767             UpdatePCUProtocolType(auth, protocol_type_id, protocol_type_fields)
768             
769             # Check again
770             protocol_types = GetPCUProtocolTypes(auth, [protocol_type_id])
771             if protocol_types is None: continue
772             protocol_type = protocol_types[0]
773             self.isequal(protocol_type, protocol_type_fields, 'UpdatePCUProtocolType - isequal')
774
775         protocol_types = GetPCUProtocolTypes(auth, protocol_type_ids)
776         if protocol_types is not None: 
777             pt_ids = [p['pcu_protocol_type_id'] for p in protocol_types]
778             self.islistequal(protocol_type_ids, pt_ids, 'GetPCUProtocolTypes - isequal')
779         
780         if self.config.verbose:
781             utils.header('Added pcu_protocol_types: %s' % protocol_type_ids)
782
783         return protocol_type_ids                
784
785     def DeletePCUProtocolTypes(self):
786         GetPCUProtocolTypes = self.debug(api.GetPCUProtocolTypes)
787         DeletePCUProtocolType = self.debug(api.DeletePCUProtocolType)
788         
789         for protocol_type_id in self.pcu_protocol_type_ids:
790             DeletePCUProtocolType(auth, protocol_type_id)
791         
792         # check 
793         protocol_types = GetPCUProtocolTypes(auth, self.pcu_protocol_type_ids)
794         self.islistequal(protocol_types, [], 'DeletePCUProtocolType - check')
795         
796         if self.config.verbose:
797             utils.header("Deleted pcu_protocol_types: %s" % self.pcu_protocol_type_ids)
798         self.pcu_protocol_type_ids = []
799
800     def PCUs(self, n = 4):
801         pcu_ids = []
802         AddPCU = self.debug(api.AddPCU)
803         UpdatePCU = self.debug(api.UpdatePCU)
804         GetPCUs = self.debug(api.GetPCUs)
805
806         for site_id in self.site_ids:
807             # Add PCU           
808             pcu_fields = random_pcu()
809             pcu_id = AddPCU(auth, site_id, pcu_fields)
810             if pcu_id is None: continue
811
812             # Should return a unique id
813             self.isunique(pcu_id, pcu_ids, 'AddPCU - isunique')
814             pcu_ids.append(pcu_id)
815
816             # check PCU
817             pcus = GetPCUs(auth, [pcu_id])
818             if pcus is None: continue
819             pcu = pcus[0]
820             self.isequal(pcu, pcu_fields, 'AddPCU - isequal')
821         
822             # Update PCU
823             pcu_fields = random_pcu()
824             UpdatePCU(auth, pcu_id, pcu_fields)
825         
826             # Check again
827             pcus = GetPCUs(auth, [pcu_id])
828             if pcus is None: continue
829             pcu = pcus[0]
830             self.isequal(pcu, pcu_fields, 'UpdatePCU - isequal')
831
832         pcus = GetPCUs(auth, pcu_ids)
833         if pcus is not None:
834             self.islistequal(pcu_ids, [p['pcu_id'] for p in pcus], 'GetPCUs - isequal')
835
836         if self.config.verbose:
837             utils.header('Added pcus: %s' % pcu_ids)
838
839         return pcu_ids
840         
841     def DeletePCUs(self):
842         GetPCUs = self.debug(api.GetPCUs)
843         DeletePCU = self.debug(api.DeletePCU)
844
845         for pcu_id in self.pcu_ids:
846             DeletePCU(auth, pcu_id)
847
848         # check 
849         pcus = GetPCUs(auth, self.pcu_ids)
850         self.islistequal(pcus, [], 'DeletePCU - check')
851         
852         if self.config.verbose:
853             utils.header("Deleted pcus: %s " % self.pcu_ids)
854         self.pcu_ids = []                
855  
856     def Nodes(self, n=4):
857         node_ids = []
858         AddNode = self.debug(api.AddNode)
859         GetNodes = self.debug(api.GetNodes)
860         UpdateNode = self.debug(api.UpdateNode)
861         for i in range(n):
862             # Add Node
863             node_fields = random_node()
864             site_id = random.sample(self.site_ids, 1)[0]
865             node_id = AddNode(auth, site_id, node_fields)
866             if node_id is None: continue
867             
868             # Should return a unique id
869             self.isunique(node_id, node_ids, 'AddNode - isunique')
870             node_ids.append(node_id)
871
872             # Check nodes
873             nodes = GetNodes(auth, [node_id])
874             if nodes is None: continue
875             node = nodes[0]
876             self.isequal(node, node_fields, 'AddNode - isequal')
877         
878             # Update node
879             node_fields = random_node()
880             result = UpdateNode(auth, node_id, node_fields)
881             
882             # Check again
883             nodes = GetNodes(auth, [node_id])
884             if nodes is None: continue
885             node = nodes[0]
886             self.isequal(node, node_fields, 'UpdateNode - isequal')
887
888             # Add node to nodegroup
889             nodegroup_id = random.sample(self.nodegroup_ids, 1)[0]
890             AddNodeToNodeGroup = self.debug(api.AddNodeToNodeGroup)
891             AddNodeToNodeGroup(auth, node_id, nodegroup_id)
892
893             # Add node to PCU
894             sites = api.GetSites(auth, [node['site_id']], ['pcu_ids'])
895             if not sites: continue
896             site = sites[0]   
897             pcu_id = random.sample(site['pcu_ids'], 1)[0]
898             port = random.sample(range(65535), 1)[0] 
899             AddNodeToPCU = self.debug(api.AddNodeToPCU)
900             AddNodeToPCU(auth, node_id, pcu_id, port)
901
902             # check nodegroup, pcu
903             nodes = GetNodes(auth, [node_id], ['nodegroup_ids', 'pcu_ids'])
904             if nodes is None or not nodes: continue
905             node = nodes[0]
906             self.islistequal([nodegroup_id], node['nodegroup_ids'], 'AddNodeToNodeGroup - check')
907             self.islistequal([pcu_id], node['pcu_ids'], 'AddNodeToPCU - check')                         
908         
909         nodes = GetNodes(auth, node_ids)
910         if nodes is not None:
911             self.islistequal(node_ids, [node['node_id'] for node in nodes], 'GetNodes - isequal')
912
913         if self.config.verbose:
914             utils.header("Added nodes: %s" % node_ids)
915         
916         return node_ids
917
918     def DeleteNodes(self):
919
920         # Delete attributes manually for first node
921         GetNodes = self.debug(api.GetNodes)
922         nodes = GetNodes(auth, self.node_ids)
923         if nodes is None or not nodes: return 0
924         node = nodes[0]
925
926         if node['nodegroup_ids']:
927             # Delete NodeGroup
928             nodegroup_id = random.sample(node['nodegroup_ids'], 1)[0]
929             DeleteNodeFromNodeGroup = self.debug(api.DeleteNodeFromNodeGroup)
930             DeleteNodeFromNodeGroup(auth, node['node_id'], nodegroup_id)
931
932         if node['pcu_ids']:
933             # Delete PCU
934             pcu_id = random.sample(node['pcu_ids'], 1)[0]
935             DeleteNodeFromPCU = self.debug(api.DeleteNodeFromPCU)
936             DeleteNodeFromPCU(auth, node['node_id'], pcu_id)
937
938         # check nodegroup, pcu
939         nodes = GetNodes(auth, [node['node_id']])
940         if nodes is None or not nodes: return 0
941         self.islistequal([], node['nodegroup_ids'], 'DeleteNodeGromNodeGroup - check')
942         self.islistequal([], node['pcu_ids'], 'DeleteNodeFromPCU - check')
943
944         # Delete rest of nodes  
945         DeleteNode = self.debug(api.DeleteNode)
946         for node_id in self.node_ids:
947             result = DeleteNode(auth, node_id)
948
949         # Check if nodes are deleted
950         GetNodes = self.debug(api.GetNodes)
951         nodes = GetNodes(auth, self.node_ids)
952         self.islistequal(nodes, [], 'DeleteNode Check')
953
954         if self.config.verbose:
955             utils.header("Deleted nodes: %s" % self.node_ids)
956         
957         self.node_ids = []
958
959     def AddressTypes(self, n = 3):
960         address_type_ids = []
961         for i in range(n):
962             address_type_fields = random_address_type()
963             AddAddressType = self.debug(api.AddAddressType)
964             address_type_id = AddAddressType(auth, address_type_fields)
965             if address_type_id is None: continue
966
967             # Should return a unique address_type_id
968             self.isunique(address_type_id, address_type_ids, 'AddAddressType - isunique') 
969             address_type_ids.append(address_type_id)
970
971             # Check address type
972             GetAddressTypes = self.debug(api.GetAddressTypes)
973             address_types = GetAddressTypes(auth, [address_type_id])
974             if address_types is None: continue
975             address_type = address_types[0]
976             self.isequal(address_type, address_type_fields, 'AddAddressType - isequal')
977
978             # Update address type
979             address_type_fields = random_address_type()
980             UpdateAddressType = self.debug(api.UpdateAddressType)
981             result = UpdateAddressType(auth, address_type_id, address_type_fields)
982             if result is None: continue
983             
984             # Check address type again
985             address_types = GetAddressTypes(auth, [address_type_id])
986             if address_types is None: continue
987             address_type = address_types[0]     
988             self.isequal(address_type, address_type_fields, 'UpdateAddressType - isequal')      
989
990         # Check get all address types
991         address_types = GetAddressTypes(auth, address_type_ids)
992         if address_types is not None:
993             self.islistequal(address_type_ids, [address_type['address_type_id'] for address_type in address_types], 'GetAddressTypes - isequal')
994
995         if self.config.verbose:
996             utils.header("Added address types: %s " % address_type_ids)
997
998         return address_type_ids
999
1000     def DeleteAddressTypes(self):
1001
1002         DeleteAddressType = self.debug(api.DeleteAddressType)
1003         for address_type_id in self.address_type_ids:
1004             DeleteAddressType(auth, address_type_id)
1005
1006         GetAddressTypes = self.debug(api.GetAddressTypes)
1007         address_types = GetAddressTypes(auth, self.address_type_ids)
1008         self.islistequal(address_types, [], 'DeleteAddressType - check')
1009
1010         if self.config.verbose:
1011             utils.header("Deleted address types: %s" % self.address_type_ids)
1012
1013         self.address_type_ids = []
1014
1015     def Addresses(self, n = 3):
1016         address_ids = []
1017         AddSiteAddress = self.debug(api.AddSiteAddress)
1018         GetAddresses = self.debug(api.GetAddresses)  
1019         UpdateAddress = self.debug(api.UpdateAddress)
1020         AddAddressTypeToAddress = self.debug(api.AddAddressTypeToAddress)
1021         for i in range(n):
1022             address_fields = random_address()
1023             site_id = random.sample(self.site_ids, 1)[0]        
1024             address_id = AddSiteAddress(auth, site_id, address_fields)
1025             if address_id is None: continue 
1026         
1027             # Should return a unique address_id
1028             self.isunique(address_id, address_ids, 'AddSiteAddress - isunique')
1029             address_ids.append(address_id)
1030
1031             # Check address
1032             addresses = GetAddresses(auth, [address_id])
1033             if addresses is None: continue
1034             address = addresses[0]
1035             self.isequal(address, address_fields, 'AddSiteAddress - isequal')
1036             
1037             # Update address
1038             address_fields = random_address()
1039             result = UpdateAddress(auth, address_id, address_fields)
1040                 
1041             # Check again
1042             addresses = GetAddresses(auth, [address_id])
1043             if addresses is None: continue
1044             address = addresses[0]
1045             self.isequal(address, address_fields, 'UpdateAddress - isequal')
1046               
1047             # Add Address Type
1048             address_type_id = random.sample(self.address_type_ids, 1)[0]
1049             AddAddressTypeToAddress(auth, address_type_id, address_id)
1050             
1051             # check adress type
1052             addresses = GetAddresses(auth, [address_id], ['address_type_ids'])
1053             if addresses is None or not addresses: continue
1054             address = addresses[0]
1055             self.islistequal([address_type_id], address['address_type_ids'], 'AddAddressTypeToAddress - check')         
1056          
1057         addresses = GetAddresses(auth, address_ids)
1058         if addresses is not None:  
1059             self.islistequal(address_ids, [ad['address_id'] for ad in addresses], 'GetAddresses - isequal')     
1060         
1061         if self.config.verbose:
1062             utils.header("Added addresses: %s" % address_ids)
1063
1064         return address_ids
1065
1066     def DeleteAddresses(self):
1067
1068         DeleteAddress = self.debug(api.DeleteAddress)
1069         DeleteAddressTypeFromAddress = self.debug(api.DeleteAddressTypeFromAddress)
1070         GetAddresses = self.debug(api.GetAddresses)
1071         
1072         # Delete attributes mananually first
1073         addresses = GetAddresses(auth, self.address_ids, ['address_id', 'address_type_ids'])
1074         if addresses is None or not addresses: return 0
1075         address = addresses[0]
1076
1077         if address['address_type_ids']:
1078             address_type_id = random.sample(address['address_type_ids'], 1)[0]
1079             DeleteAddressTypeFromAddress(auth, address_type_id, address['address_id'])  
1080
1081         # check address_type_ids
1082         addresses = GetAddresses(auth, [address['address_id']], ['address_type_ids'])
1083         if addresses is None or not addresses: return 0
1084         address = addresses[0]
1085         self.islistequal([], address['address_type_ids'], 'DeleteAddressTypeFromAddress - check') 
1086
1087         # Delete site addresses
1088         for address_id in self.address_ids:
1089             result = DeleteAddress(auth, address_id)
1090         
1091         # Check 
1092         addresses = GetAddresses(auth, self.address_ids)
1093         self.islistequal(addresses, [], 'DeleteAddress - check')
1094         if self.config.verbose:
1095             utils.header("Deleted addresses: %s" % self.address_ids)
1096
1097         self.address_ids = []
1098
1099     def SliceAttributeTypes(self, n = 2):
1100         attribute_type_ids = []
1101         AddSliceAttributeType = self.debug(api.AddSliceAttributeType)
1102         GetSliceAttributeTypes = self.debug(api.GetSliceAttributeTypes)
1103         UpdateSliceAttributeType = self.debug(api.UpdateSliceAttributeType)
1104         
1105         for i in range(n):
1106             attribute_type_fields = random_attribute_type()
1107             attribute_type_id = AddSliceAttributeType(auth, attribute_type_fields)
1108             if attribute_type_id is None: continue
1109
1110             # Should return a unique slice_attribute_type_id
1111             self.isunique(attribute_type_id, attribute_type_ids, 'AddSliceAttributeType - isunique')
1112             attribute_type_ids.append(attribute_type_id)
1113
1114             # Check slice_attribute_type
1115             attribute_types = GetSliceAttributeTypes(auth, [attribute_type_id])
1116             if attribute_types is None: continue
1117             attribute_type = attribute_types[0]
1118             self.isequal(attribute_type, attribute_type_fields, 'AddSliceAttributeType - isequal')
1119
1120             # Update slice_attribute_type
1121             attribute_type_fields = random_attribute_type()
1122             result = UpdateSliceAttributeType(auth, attribute_type_id, attribute_type_fields)
1123
1124             # Check again
1125             attribute_types = GetSliceAttributeTypes(auth, [attribute_type_id])
1126             if attribute_types is None: continue
1127             attribute_type = attribute_types[0]
1128             self.isequal(attribute_type, attribute_type_fields, 'UpdateSliceAttributeType - isequal')
1129
1130         attribute_types = GetSliceAttributeTypes(auth, attribute_type_ids)
1131         if attribute_types is not None:
1132             at_ids = [at['attribute_type_id'] for at in attribute_types] 
1133             self.islistequal(attribute_type_ids, at_ids, 'GetSliceAttributeTypes - isequal')
1134
1135         if self.config.verbose:
1136             utils.header("Added slice_attribute_types: %s" % attribute_type_ids)
1137
1138         return attribute_type_ids
1139
1140     def DeleteSliceAttributeTypes(self):
1141         DeleteSliceAttributeType = self.debug(api.DeleteSliceAttributeType)
1142         GetSliceAttributeTypes = self.debug(api.GetSliceAttributeTypes)
1143
1144         # Delete slice_attribute_type
1145         for slice_attribute_type_id in self.slice_attribute_type_ids:
1146             result = DeleteSliceAttributeType(auth, slice_attribute_type_id)
1147
1148         # Check 
1149         slice_attribute_types = GetSliceAttributeTypes(auth, self.slice_attribute_type_ids)
1150         self.islistequal(slice_attribute_types, [], 'DeleteSliceAttributeTypes - check')
1151         if self.config.verbose:
1152             utils.header("Deleted slice_attributes: %s" % self.slice_attribute_type_ids)
1153
1154         self.slice_attribute_type_ids = []
1155
1156     def SliceInstantiations(self, n = 2):
1157         insts = []
1158         AddSliceInstantiation= self.debug(api.AddSliceInstantiation)
1159         GetSliceInstantiations = self.debug(api.GetSliceInstantiations)
1160
1161         for i in range(n):
1162             inst = randstr(10)
1163             result = AddSliceInstantiation(auth, inst)
1164             if result is None: continue
1165             insts.append(inst)          
1166
1167             # Check slice instantiaton
1168             instantiations = GetSliceInstantiations(auth)
1169             if instantiations is None: continue
1170             instantiations = filter(lambda x: x in [inst], instantiations)
1171             instantiation = instantiations[0]
1172             self.isequal(instantiation, inst, 'AddSliceInstantiation - isequal')
1173
1174         
1175         instantiations = GetSliceInstantiations(auth)
1176         if instantiations is not None:
1177             instantiations = filter(lambda x: x in insts, instantiations)
1178             self.islistequal(insts, instantiations, 'GetSliceInstantiations - isequal')
1179
1180         if self.config.verbose:
1181             utils.header("Added slice instantiations: %s" % insts)
1182
1183         return insts
1184         
1185     def DeleteSliceInstantiations(self):
1186         DeleteSliceInstantiation = self.debug(api.DeleteSliceInstantiation)
1187         GetSliceInstantiations = self.debug(api.GetSliceInstantiations)
1188         # Delete slice instantiation
1189         for instantiation  in self.slice_instantiations:
1190             result = DeleteSliceInstantiation(auth, instantiation)
1191
1192         # Check 
1193         instantiations = GetSliceInstantiations(auth)
1194         instantiations = filter(lambda x: x in self.slice_instantiations, instantiations)
1195         self.islistequal(instantiations, [], 'DeleteSliceInstantiation - check')
1196         if self.config.verbose:
1197             utils.header("Deleted slice instantiations: %s" % self.slice_instantiations)
1198
1199         self.slice_instantiations = []  
1200
1201     def Slices(self, n = 3):
1202         slice_ids = []
1203         AddSlice = self.debug(api.AddSlice)
1204         GetSlices = self.debug(api.GetSlices)
1205         UpdateSlice = self.debug(api.UpdateSlice)
1206         AddSliceToNodes = self.debug(api.AddSliceToNodes)
1207         for i in range(n):
1208             # Add Site
1209             slice_fields = random_slice()
1210             slice_id = AddSlice(auth, slice_fields)
1211             if slice_id is None: continue
1212
1213             # Should return a unique id
1214             self.isunique(slice_id, slice_ids, 'AddSlicel - isunique')
1215             slice_ids.append(slice_id)
1216             slices = GetSlices(auth, [slice_id])
1217             if slices is None: continue
1218             slice = slices[0]
1219             self.isequal(slice, slice_fields, 'AddSlice - isequal')
1220
1221             # Update slice
1222             slice_fields = random_slice()
1223             result = UpdateSlice(auth, slice_id, slice_fields)
1224
1225             # Check again
1226             slices = GetSlices(auth, [slice_id])
1227             if slices is None: continue
1228             slice = slices[0]
1229             self.isequal(slice, slice_fields, 'UpdateSlice - isequal')
1230
1231             # Add node
1232             node_id = random.sample(self.node_ids, 1)[0]
1233             AddSliceToNodes(auth, slice_id, [node_id])
1234         
1235             # check node
1236             slices = GetSlices(auth, [slice_id], ['node_ids'])
1237             if slices is None or not slices: continue
1238             slice = slices[0]
1239             self.islistequal([node_id], slice['node_ids'], 'AddSliceToNode - check')            
1240
1241         slices = GetSlices(auth, slice_ids)
1242         if slices is not None:
1243             self.islistequal(slice_ids, [slice['slice_id'] for slice in slices], 'GetSlices - isequal')
1244
1245         if self.config.verbose:
1246             utils.header("Added slices: %s" % slice_ids)
1247
1248         return slice_ids 
1249
1250     def DeleteSlices(self):
1251         
1252         GetSlices = self.debug(api.GetSlices)
1253         DeleteSlice = self.debug(api.DeleteSlice)
1254         DeleteSliceFromNodes = self.debug(api.DeleteSliceFromNodes)     
1255
1256         # manually delete attributes for first slice
1257         slices = GetSlices(auth, self.slice_ids, ['slice_id', 'node_ids'])
1258         if slices is None or not slices: return 0
1259         slice = slices[0]
1260         
1261         if slice['node_ids']:
1262             # Delete node from slice
1263             node_id = random.sample(slice['node_ids'], 1)[0]
1264             DeleteSliceFromNodes(slice['slice_id'], [node_id])
1265         
1266         # Check node_ids
1267         slices = GetSlices(auth, [slice['slice_id']], ['node_ids'])
1268         if slices is None or not slices: return 0
1269         slice = slices[0]
1270         self.islistequal([], slice['node_ids'], 'DeleteSliceFromNode - check')   
1271
1272         # Have DeleteSlice automatically delete attriubtes for the rest 
1273         for slice_id in self.slice_ids:
1274             # Delete account
1275             DeleteSlice(auth, slice_id)
1276
1277         # Check if slices are deleted
1278         GetSlices = self.debug(api.GetSlices)
1279         slices = GetSlices(auth, self.slice_ids)
1280         self.islistequal(slices, [], 'DeleteSlice - check')
1281
1282         if self.config.verbose:
1283             utils.header("Deleted slices: %s" % self.slice_ids)
1284
1285         self.slice_ids = []
1286
1287     def SliceAttributes(self, n = 4):
1288         attribute_ids = []
1289         AddSliceAttribute = self.debug(api.AddSliceAttribute)
1290         GetSliceAttributes = self.debug(api.GetSliceAttributes)
1291         UpdateSliceAttribute = self.debug(api.UpdateSliceAttribute)
1292
1293         for i in range(n):
1294             # Add slice attribute
1295             attribute_fields = random_slice_attribute()
1296             slice_id = random.sample(self.slice_ids, 1)[0]
1297             type = attribute_fields['attribute_type_id']
1298             value = attribute_fields['value']   
1299             attribute_id = AddSliceAttribute(auth, slice_id, type, value)
1300             if attribute_id is None: continue
1301
1302             # Should return a unique id
1303             self.isunique(attribute_id, attribute_ids, 'AddSliceAttribute - isunique')
1304             attribute_ids.append(attribute_id)
1305
1306             # Check attribute
1307             attributes = GetSliceAttributes(auth, [attribute_id])
1308             if attributes is None: continue
1309             attribute = attributes[0]
1310             self.isequal(attribute, attribute_fields, 'AddSliceAttribute - isequal')
1311
1312             # Update attribute
1313             attribute_fields = random_slice_attribute()
1314             type = attribute_fields['attribute_type_id']
1315             value = attribute_fields['value']   
1316             result = UpdateSliceAttribute(auth, attribute_id, value)
1317
1318             # Check again
1319             attributes = GetSliceAttributes(auth, [attribute_id])
1320             if attributes is None: continue
1321             attribute = attributes[0]
1322             self.isequal(attribute, attribute_fields, 'UpdateSliceAttribute - isequal')
1323
1324         attributes = GetSliceAttributes(auth, attribute_ids)
1325         if attributes is not None:
1326             attr_ids = [a['slice_attribute_id'] for a in attributes]
1327             self.islistequal(attribute_ids, attr_ids, 'GetSliceAttributes - isequal')
1328         if self.config.verbose:
1329             utils.header("Added slice attributes: %s" % attribute_ids)
1330
1331         return attribute_ids 
1332
1333     def DeleteSliceAttributes(self):
1334         DeleteSliceAttribute = self.debug(api.DeleteSliceAttribute)
1335         GetSliceAttributes = self.debug(api.GetSliceAttributes)
1336
1337         for attribute_id in self.slice_attribute_ids:
1338             DeleteSliceAttribute(auth, attribute_id)
1339
1340         attributes = GetSliceAttributes(auth, self.slice_attribute_ids)
1341         self.islistequal(attributes, [], 'DeleteSliceAttribute - check')
1342
1343         if self.config.verbose:
1344             utils.header("Deleted slice attributes: %s" % self.slice_attribute_ids)
1345
1346         self.slice_attribute_ids = []   
1347
1348     def InitScripts(self, n = 2):
1349         initscript_ids = []
1350         AddInitScript = self.debug(api.AddInitScript)
1351         GetInitScripts = self.debug(api.GetInitScripts)
1352         UpdateInitScript = self.debug(api.UpdateInitScript)
1353         for i in range(n):
1354             # Add Peer
1355             initscript_fields = random_initscript()
1356             initscript_id = AddInitScript(auth, initscript_fields)
1357
1358             # Should return a unique id
1359             self.isunique(initscript_id, initscript_ids, 'AddInitScript - isunique')
1360             initscript_ids.append(initscript_id)
1361             initscripts = GetInitScripts(auth, [initscript_id])
1362             if initscripts is None: continue
1363             initscript = initscripts[0]
1364             self.isequal(initscript, initscript_fields, 'AddInitScript - isequal')
1365
1366             # Update Peer
1367             initscript_fields = random_initscript()
1368             result = UpdateInitScript(auth, initscript_id, initscript_fields)
1369
1370             # Check again
1371             initscripts = GetInitScripts(auth, [initscript_id])
1372             if initscripts is None: continue
1373             initscript = initscripts[0]
1374             self.isequal(initscript, initscript_fields, 'UpdateInitScript - isequal')
1375
1376         initscripts = GetInitScripts(auth, initscript_ids)
1377         if initscripts is not None:
1378             self.islistequal(initscript_ids, [i['initscript_id'] for i in initscripts], 'GetInitScripts -isequal')
1379
1380         if self.config.verbose:
1381             utils.header("Added initscripts: %s" % initscript_ids)
1382
1383         return initscript_ids
1384
1385     def DeleteInitScripts(self):
1386         # Delete all initscripts
1387         DeleteInitScript = self.debug(api.DeleteInitScript)
1388         GetInitScripts = self.debug(api.GetInitScripts)
1389         for initscript_id in self.initscript_ids:
1390             result = DeleteInitScript(auth, initscript_id)
1391
1392         # Check if peers are deleted
1393         initscripts = GetInitScripts(auth, self.initscript_ids)
1394         self.islistequal(initscripts, [], 'DeletInitScript - check')
1395
1396         if self.config.verbose:
1397             utils.header("Deleted initscripts: %s" % self.initscript_ids)
1398         self.initscript_ids =[]
1399
1400     def Roles(self, n = 2):
1401         role_ids = []
1402         AddRole = self.debug(api.AddRole)
1403         GetRoles = self.debug(api.GetRoles)
1404         for i in range(n):
1405             # Add Role
1406             role_fields = random_role()
1407             role_id = role_fields['role_id']
1408             name = role_fields['name']
1409             AddRole(auth, role_id, name)
1410
1411             # Should return a unique id
1412             self.isunique(role_id, role_ids, 'AddRole - isunique')
1413             role_ids.append(role_id)
1414             roles = GetRoles(auth)
1415             if roles is None: continue
1416             roles = filter(lambda x: x['role_id'] in [role_id], roles)
1417             role = roles[0]
1418             self.isequal(role, role_fields, 'AddRole - isequal')
1419
1420         roles = GetRoles(auth)
1421         if roles is not None:
1422             roles = filter(lambda x: x['role_id'] in role_ids, roles) 
1423             self.islistequal(role_ids, [r['role_id'] for r in roles], 'GetRoles - isequal')
1424
1425         if self.config.verbose:
1426             utils.header("Added roles: %s" % role_ids)
1427
1428         return role_ids
1429
1430     def DeleteRoles(self):
1431         # Delete all roles
1432         DeleteRole = self.debug(api.DeleteRole)
1433         GetRoles = self.debug(api.GetRoles)
1434         for role_id in self.role_ids:
1435             result = DeleteRole(auth, role_id)
1436
1437         # Check if peers are deleted
1438         roles = GetRoles(auth)
1439         roles = filter(lambda x: x['role_id'] in self.role_ids, roles) 
1440         self.islistequal(roles, [], 'DeleteRole - check' % self.role_ids)
1441
1442         if self.config.verbose:
1443             utils.header("Deleted roles: %s" % self.role_ids)
1444         self.role_ids =[]
1445
1446     def Persons(self, n = 3):
1447
1448         person_ids = []
1449         for i in range(n):
1450
1451             # Add account
1452             person_fields = random_person()
1453             AddPerson = self.debug(api.AddPerson)
1454             person_id = AddPerson(auth, person_fields)
1455             if person_id is None: continue
1456         
1457             # Should return a unique person_id
1458             self.isunique(person_id, person_ids, 'AddPerson - isunique')
1459             person_ids.append(person_id)
1460             GetPersons = self.debug(api.GetPersons)
1461             persons = GetPersons(auth, [person_id])
1462             if persons is None: continue
1463             person = persons[0]
1464             self.isequal(person, person_fields, 'AddPerson - isequal')
1465
1466             # Update account
1467             person_fields = random_person()
1468             person_fields['enabled'] = True
1469             UpdatePerson = self.debug(api.UpdatePerson)
1470             result = UpdatePerson(auth, person_id, person_fields)
1471         
1472             # Add random role 
1473             AddRoleToPerson = self.debug(api.AddRoleToPerson)   
1474             role = random.sample(roles, 1)[0]
1475             result = AddRoleToPerson(auth, role, person_id)
1476
1477             # Add key to person
1478             key = random_key()
1479             key_id = AddPersonKey = self.debug(api.AddPersonKey)
1480             AddPersonKey(auth, person_id, key)
1481         
1482             # Add person to site
1483             site_id = random.sample(self.site_ids, 1)[0]
1484             AddPersonToSite = self.debug(api.AddPersonToSite)
1485             AddPersonToSite(auth, person_id, site_id)    
1486         
1487             # Add person to slice
1488             slice_id = random.sample(self.slice_ids, 1)[0]
1489             AddPersonToSlice = self.debug(api.AddPersonToSlice)
1490             AddPersonToSlice(auth, person_id, slice_id)
1491
1492             # check role, key, site, slice
1493             persons = GetPersons(auth, [person_id], ['roles', 'key_ids', 'site_ids', 'slice_ids'])
1494             if persons is None or not persons: continue
1495             person = persons[0]
1496             self.islistequal([role], person['roles'], 'AddRoleToPerson - check')
1497             self.islistequal([key_id], person['key_ids'], 'AddPersonKey - check')
1498             self.islistequal([site_id], person['site_ids'], 'AddPersonToSite - check')
1499             self.islistequal([slice_id], person['slice_ids'], 'AddPersonToSlice - check')
1500
1501         persons = GetPersons(auth, person_ids)
1502         if persons is not None:
1503             self.islistequal(person_ids, [p['person_id'] for p in persons], 'GetPersons - isequal')
1504
1505         if self.config.verbose:
1506             utils.header("Added users: %s" % person_ids)
1507
1508         return person_ids
1509
1510     def DeletePersons(self):
1511         
1512         # Delete attributes manually for first person
1513         GetPersons = self.debug(api.GetPersons)
1514         persons = GetPersons(auth, self.person_ids, ['person_id' , 'key_ids', 'site_ids', 'slice_ids', 'roles'])
1515         if persons is None or not persons: return 0
1516         person = persons[0]
1517
1518         if person['roles']:        
1519             # Delete role
1520             role = random.sample(person['roles'], 1)[0]
1521             DeleteRoleFromPerson = self.debug(api.DeleteRoleFromPerson)
1522             DeleteRoleFromPerson(auth, role, person['person_id'])
1523
1524         if person['key_ids']:
1525             # Delete key
1526             key_id = random.sample(person['key_ids'], 1)[0] 
1527             DeleteKey = self.debug(api.DeleteKey)
1528             DeleteKey(auth, key_id)
1529         
1530         if person['site_ids']:
1531             # Remove person from site
1532             site_id = random.sample(person['site_ids'], 1)[0]
1533             DeletePersonFromSite = self.debug(api.DeletePersonFromSite)
1534             DeletePersonFromSite(auth, person['person_id'], site_id)
1535
1536         if person['slice_ids']:
1537             # Remove person from slice
1538             slice_id = random.sample(person['slice_ids'], 1)[0]
1539             DeletePersonFromSlice = self.debug(api.DeletePersonFromSlice)
1540             DeletePersonFromSlice(auth, person['person_id'], slice_id)
1541
1542         # check role, key, site, slice
1543         persons = GetPersons(auth, [person['person_id']], ['roles', 'key_ids', 'site_ids', 'slice_ids'])
1544         if persons is None or not persons: return 0
1545         person = persons[0]
1546         self.islistequal([], person['roles'], 'DeleteRoleFromPerson - check')
1547         self.islistequal([], person['key_ids'], 'DeleteKey - check')
1548         self.islistequal([], person['site_ids'], 'DeletePersonFromSite - check')
1549         self.islistequal([], person['slice_ids'], 'DeletePersonFromSlice - check')
1550         
1551         DeletePerson = self.debug(api.DeletePerson)
1552         # Have DeletePeson automatically delete attriubtes for all other persons 
1553         for person_id in self.person_ids:
1554             # Delete account
1555             DeletePerson(auth, person_id)
1556
1557         # Check if persons are deleted
1558         GetPersons = self.debug(api.GetPersons)
1559         persons = GetPersons(auth, self.person_ids)
1560         self.islistequal(persons, [], 'DeletePerson - check')
1561  
1562         if self.config.verbose:
1563             utils.header("Deleted users: %s" % self.person_ids)
1564
1565         self.person_ids = []
1566
1567     def KeyTypes(self, n = 2):
1568         key_types = []
1569         AddKeyType = self.debug(api.AddKeyType)
1570         GetKeyTypes = self.debug(api.GetKeyTypes)
1571         for i in range(n):
1572             # Add key type
1573             keytype = randstr(10)
1574             result = AddKeyType(auth, keytype)
1575             if result is None: continue
1576
1577             # Check key types
1578             key_types.append(keytype)
1579             keytypes  = GetKeyTypes(auth)
1580             if not keytypes: continue
1581             keytypes = filter(lambda x: x in [keytype], keytypes)
1582             if not keytypes: continue
1583             self.isequal(keytype, keytypes[0], 'AddKeyType - isequal')
1584
1585         # Check all
1586         keytypes = GetKeyTypes(auth)
1587         if keytypes is not None:
1588             keytypes = filter(lambda x: x in key_types, keytypes)
1589             self.islistequal(key_types, keytypes, 'GetKeyTypes - isequal')
1590
1591         if self.config.verbose:
1592             utils.header("Added key types: %s" % key_types)
1593
1594         return key_types
1595
1596     def DeleteKeyTypes(self):
1597         DeleteKeyType = self.debug(api.DeleteKeyType)
1598         GetKeyTypes = self.debug(api.GetKeyTypes)
1599         for key_type in self.key_types:
1600             result = DeleteKeyType(auth, key_type)
1601
1602         # Check if key types are deleted
1603         key_types = GetKeyTypes(auth)
1604         key_types = filter(lambda x: x in self.key_types, key_types)
1605         self.islistequal(key_types, [], 'DeleteKeyType - check')
1606
1607         if self.config.verbose:
1608             utils.header("Deleted key types %s" % self.key_types)
1609
1610         self.key_types = []     
1611
1612     def Keys(self, n = 3):
1613         key_ids = []
1614         for i in range(n):
1615             # Add a key to an account
1616             key_fields = random_key()
1617             person_id = random.sample(self.person_ids, 1)[0]
1618             AddPersonKey = self.debug(api.AddPersonKey)
1619             key_id = AddPersonKey(auth, person_id, key_fields)   
1620             if key_id is None: continue
1621                 
1622             # Should return a unique key_id
1623             self.isunique(key_id, key_ids, 'AddPersonKey - isunique')
1624             key_ids.append(key_id)
1625             GetKeys = self.debug(api.GetKeys)
1626             keys = GetKeys(auth, [key_id])
1627             if keys is None: continue
1628             key = keys[0]
1629             self.isequal(key, key_fields, 'AddPersonKey - isequal')
1630             
1631             # Update Key
1632             key_fields = random_key()
1633             UpdateKey = self.debug(api.UpdateKey)
1634             result = UpdateKey(auth, key_id, key_fields)
1635             
1636             keys = GetKeys(auth, [key_id])
1637             if keys is None or not keys: continue                
1638             key = keys[0]
1639             self.isequal(key, key_fields, 'UpdatePersonKey - isequal')
1640             
1641         keys = GetKeys(auth, key_ids)
1642         if keys is not None:
1643             self.islistequal(key_ids, [key['key_id'] for key in keys], 'GetKeys - isequal')
1644         
1645         if self.config.verbose:
1646             utils.header("Added keys: %s" % key_ids)
1647         return key_ids
1648
1649
1650     def DeleteKeys(self):
1651         
1652         # Blacklist first key, Delete rest
1653         GetKeys = self.debug(api.GetKeys)
1654         DeleteKey = self.debug(api.DeleteKey)
1655         BlacklistKey = self.debug(api.BlacklistKey)
1656         
1657         key_id = self.key_ids.pop()
1658         BlacklistKey(auth, key_id)  
1659         keys = GetKeys(auth, [key_id])
1660         self.islistequal(keys, [], 'BlacklistKey - check')
1661         
1662         if self.config.verbose:
1663             utils.header("Blacklisted key: %s" % key_id)
1664
1665         for key_id in self.key_ids:
1666             DeleteKey(auth, key_id)
1667         
1668         keys = GetKeys(auth, self.key_ids)
1669         self.islistequal(keys, [], 'DeleteKey - check')
1670         
1671         if self.config.verbose:
1672             utils.header("Deleted keys: %s" % self.key_ids)  
1673              
1674         self.key_ids = []
1675
1676     def BootStates(self, n = 3):
1677         boot_states = []
1678         AddBootState = self.debug(api.AddBootState)
1679         GetBootStates = self.debug(api.GetBootStates)
1680         for i in range(n):
1681             # Add boot state
1682             bootstate_fields = randstr(10)
1683             result = AddBootState(auth, bootstate_fields)
1684             if result is None: continue
1685         
1686             # Check boot states
1687             boot_states.append(bootstate_fields)      
1688             bootstates = GetBootStates(auth)
1689             if not bootstates: continue
1690             bootstates = filter(lambda x: x in [bootstate_fields], bootstates)
1691             if not bootstates: continue
1692             bootstate = bootstates[0]
1693             self.isequal(bootstate, bootstate_fields, 'AddBootState - isequal')
1694                 
1695         # Check all
1696         bs = GetBootStates(auth)
1697         if bs is not None:
1698             bs = filter(lambda x: x in [boot_states], bs)
1699             self.islistequal(boot_states, bs, 'GetBootStates - isequal')
1700
1701         if self.config.verbose:
1702             utils.header("Added boot_states: %s" % boot_states)
1703
1704         return boot_states
1705
1706     def DeleteBootStates(self):
1707         DeleteBootState = self.debug(api.DeleteBootState)
1708         GetBootStates = self.debug(api.GetBootStates)
1709         for boot_state in self.boot_states:
1710             result = DeleteBootState(auth, boot_state)
1711         
1712         # Check if bootsates are deleted
1713         boot_states = GetBootStates(auth)
1714         boot_states = filter(lambda x: x in self.boot_states, boot_states)
1715         self.islistequal(boot_states, [], 'DeleteBootState check')
1716         
1717         if self.config.verbose:
1718             utils.header("Deleted boot_states: %s" % self.boot_states)
1719
1720         self.boot_states = []
1721             
1722          
1723     def Peers(self, n = 2):
1724         peer_ids = []
1725         AddPeer = self.debug(api.AddPeer)
1726         GetPeers = self.debug(api.GetPeers)
1727         UpdatePeer = self.debug(api.UpdatePeer)
1728         for i in range(n):
1729             # Add Peer
1730             peer_fields = random_peer()
1731             peer_id = AddPeer(auth, peer_fields)
1732         
1733             # Should return a unique id
1734             self.isunique(peer_id, peer_ids, 'AddPeer - isunique')
1735             peer_ids.append(peer_id)
1736             peers = GetPeers(auth, [peer_id])
1737             if peers is None: continue
1738             peer = peers[0]
1739             self.isequal(peer, peer_fields, 'AddPeer - isequal')
1740             
1741             # Update Peer
1742             peer_fields = random_peer()
1743             result = UpdatePeer(auth, peer_id, peer_fields)
1744             
1745             # Check again
1746             peers = GetPeers(auth, [peer_id])
1747             if peers is None: continue
1748             peer = peers[0]
1749             self.isequal(peer, peer_fields, 'UpdatePeer - isequal')
1750
1751         peers = GetPeers(auth, peer_ids)
1752         if peers is not None:
1753             self.islistequal(peer_ids, [peer['peer_id'] for peer in peers], 'GetPeers -isequal')
1754         
1755         if self.config.verbose:
1756             utils.header("Added peers: %s" % peer_ids)
1757         
1758         return peer_ids
1759
1760
1761     def DeletePeers(self):
1762         # Delete all peers
1763         DeletePeer = self.debug(api.DeletePeer)
1764         GetPeers = self.debug(api.GetPeers)
1765         for peer_id in self.peer_ids:
1766             result = DeletePeer(auth, peer_id)
1767         
1768         # Check if peers are deleted
1769         peers = GetPeers(auth, self.peer_ids)
1770         self.islistequal(peers, [], 'DeletePeer - check' % self.peer_ids)
1771         
1772         if self.config.verbose:
1773             utils.header("Deleted peers: %s" % self.peer_ids)
1774         self.peer_ids =[] 
1775                 
1776     def ConfFiles(self, n = 2):
1777         conf_file_ids = []
1778         for i in range(n):
1779             # Add ConfFile
1780             conf_file_fields = random_conf_file()
1781             AddConfFile = self.debug(api.AddConfFile)
1782             conf_file_id = AddConfFile(auth, conf_file_fields)
1783             if conf_file_id is None: continue
1784         
1785             # Should return a unique id
1786             self.isunique(conf_file_id, conf_file_ids, 'AddConfFile - isunique')
1787             conf_file_ids.append(conf_file_id)
1788             
1789             # Get ConfFiles
1790             GetConfFiles = self.debug(api.GetConfFiles)
1791             conf_files = GetConfFiles(auth, [conf_file_id])
1792             if conf_files is None: continue
1793             conf_file = conf_files[0]
1794             self.isequal(conf_file, conf_file_fields, 'AddConfFile - isunique')
1795             
1796             # Update ConfFile
1797             conf_file_fields = random_conf_file()
1798             UpdateConfFile = self.debug(api.UpdateConfFile)
1799             result = UpdateConfFile(auth, conf_file_id, conf_file_fields)
1800            
1801             # Check again
1802             conf_files = GetConfFiles(auth, [conf_file_id])
1803             if conf_files is None: continue
1804             conf_file = conf_files[0]
1805             self.isequal(conf_file, conf_file_fields, 'UpdateConfFile - isunique')
1806
1807
1808             # Add this conf file to a random node
1809             node_id = random.sample(self.node_ids, 1)[0]
1810             AddConfFileToNode = self.debug(api.AddConfFileToNode)
1811             AddConfFileToNode(auth, conf_file_id, node_id)
1812
1813             # Add this conf file to a random node group
1814             nodegroup_id = random.sample(self.nodegroup_ids, 1)[0]
1815             AddConfFileToNodeGroup = self.debug(api.AddConfFileToNodeGroup)
1816             AddConfFileToNodeGroup(auth, conf_file_id, nodegroup_id)
1817
1818             # Check node, nodegroup
1819             conf_files = GetConfFiles(auth, [conf_file_id], ['node_ids', 'nodegroup_ids'])
1820             if conf_files is None or not conf_files: continue
1821             conf_file = conf_files[0]
1822             self.islistequal([node_id], conf_file['node_ids'], 'AddConfFileToNode - check')
1823             self.islistequal([nodegroup_id], conf_file['nodegroup_ids'], 'AddConfFileToNodeGroup - check')
1824         
1825
1826
1827         conf_files = GetConfFiles(auth, conf_file_ids)
1828         if conf_files is not None:
1829             self.islistequal(conf_file_ids, [c['conf_file_id'] for c in conf_files], 'GetConfFiles - isequal')
1830         if self.config.verbose:
1831             utils.header("Added conf_files: %s" % conf_file_ids)        
1832
1833         return conf_file_ids
1834
1835     def DeleteConfFiles(self):
1836         
1837             GetConfFiles = self.debug(api.GetConfFiles)
1838             DeleteConfFile = self.debug(api.DeleteConfFile)
1839             DeleteConfFileFromNode = self.debug(api.DeleteConfFileFromNode)
1840             DeleteConfFileFromNodeGroup = self.debug(api.DeleteConfFileFromNodeGroup)
1841
1842             conf_files = GetConfFiles(auth, self.conf_file_ids)
1843             if conf_files is None or not conf_files: return 0           
1844             conf_file = conf_files[0]
1845             if conf_file['node_ids']:
1846                 node_id = random.sample(conf_file['node_ids'], 1)[0]
1847                 DeleteConfFileFromNode(auth, conf_file['conf_file_id'], node_id)
1848             if conf_file['nodegroup_ids']:
1849                 nodegroup_id = random.sample(conf_file['nodegroup_ids'], 1)[0]
1850                 DeleteConfFileFromNodeGroup(auth, conf_file['conf_file_id'], nodegroup_id)
1851
1852             # check
1853             conf_files = GetConfFiles(auth, [conf_file['conf_file_id']], ['node_ids', 'nodegroup_ids'])
1854             if conf_files is None or not conf_files: return 0 
1855             conf_file = conf_files[0]
1856             self.islistequal([], conf_file['node_ids'], 'AddConfFileToNode - check')
1857             self.islistequal([], conf_file['nodegroup_ids'], 'AddConfFileToNodeGroup - check')
1858
1859             for conf_file_id in self.conf_file_ids:
1860                 DeleteConfFile(auth, conf_file_id)
1861
1862             # check 
1863             conf_files = GetConfFiles(auth, self.conf_file_ids)
1864             self.islistequal(conf_files, [], 'DeleteConfFile - check')
1865             
1866             if self.config.verbose:
1867                 utils.header("Deleted conf_files: %s" % self.conf_file_ids)
1868
1869             self.conf_file_ids = []
1870     
1871     def NodeNetworks(self, n = 4):
1872         nodenetwork_ids = []
1873         AddNodeNetwork = self.debug(api.AddNodeNetwork)
1874         UpdateNodeNetwork = self.debug(api.UpdateNodeNetwork)
1875         GetNodeNetworks = self.debug(api.GetNodeNetworks)
1876
1877         for i in range(n):
1878             # Add Node Network          
1879             nodenetwork_fields = random_nodenetwork()
1880             node_id = random.sample(self.node_ids, 1)[0]
1881             nodenetwork_id = AddNodeNetwork(auth, node_id, nodenetwork_fields)
1882             if nodenetwork_id is None: continue
1883
1884             # Should return a unique id
1885             self.isunique(nodenetwork_ids, nodenetwork_ids, 'AddNodeNetwork - isunique')
1886             nodenetwork_ids.append(nodenetwork_id)
1887
1888             # check Node Network
1889             nodenetworks = GetNodeNetworks(auth, [nodenetwork_id])
1890             if nodenetworks is None: continue
1891             nodenetwork = nodenetworks[0]
1892             self.isequal(nodenetwork, nodenetwork_fields, 'AddNodeNetwork - isequal')
1893         
1894             # Update NodeNetwork
1895             nodenetwork_fields = random_nodenetwork()
1896             UpdateNodeNetwork(auth, nodenetwork_id, nodenetwork_fields)
1897
1898             # Check again
1899             nodenetworks = GetNodeNetworks(auth, [nodenetwork_id])
1900             if nodenetworks is None: continue
1901             nodenetwork = nodenetworks[0]
1902             self.isequal(nodenetwork,  nodenetwork_fields, 'UpdateNodeNetwork - isequal')
1903
1904         nodenetworks = GetNodeNetworks(auth, nodenetwork_ids)
1905         if nodenetworks is not None:
1906             self.islistequal(nodenetwork_ids, [n['nodenetwork_id'] for n in nodenetworks], 'GetNodeNetworks - isequal')
1907
1908         if self.config.verbose:
1909             utils.header('Added nodenetworks: %s' % nodenetwork_ids)
1910
1911         return nodenetwork_ids
1912
1913     def DeleteNodeNetworks(self):
1914         GetNodeNetworks = self.debug(api.GetNodeNetworks)
1915         DeleteNodeNetwork = self.debug(api.DeleteNodeNetwork)
1916
1917         for nodenetwork_id in self.nodenetwork_ids:
1918             DeleteNodeNetwork(auth, nodenetwork_id)
1919
1920         # check 
1921         nodenetworks = GetNodeNetworks(auth, self.nodenetwork_ids)
1922         self.islistequal(nodenetworks, [], 'DeleteNodeNetwork - check')
1923
1924         if self.config.verbose:
1925             utils.header("Deleted nodenetworks: %s " % self.nodenetwork_ids)
1926         self.nodenetwork_ids = []                       
1927         
1928     def NodeNetworkSettings(self, n=2):
1929         nodenetwork_setting_ids = []
1930         AddNodeNetworkSetting = self.debug(api.AddNodeNetworkSetting)
1931         UpdateNodeNetworkSetting = self.debug(api.UpdateNodeNetworkSetting)
1932         GetNodeNetworkSettings = self.debug(api.GetNodeNetworkSettings)
1933
1934         for nodenetwork_id in self.nodenetwork_ids:
1935             # Add Node Network          
1936             nodenetwork_setting_fields = random_nodenetwork_setting()
1937             #nodenetwork_id = random.sample(self.nodenetwork_ids, 1)[0]
1938             nodenetwork_setting_type_id = random.sample(self.nodenetwork_setting_type_ids, 1)[0]
1939             value = nodenetwork_setting_fields['value']
1940             nodenetwork_setting_id = AddNodeNetworkSetting(auth, nodenetwork_id, nodenetwork_setting_type_id, value)
1941             if nodenetwork_setting_id is None: continue
1942
1943             # Should return a unique id
1944             self.isunique(nodenetwork_setting_ids, nodenetwork_setting_ids, 'AddNodeNetworkSetting - isunique')
1945             nodenetwork_setting_ids.append(nodenetwork_setting_id)
1946
1947             # check Node Network
1948             nodenetwork_settings = GetNodeNetworkSettings(auth, [nodenetwork_setting_id])
1949             if nodenetwork_settings is None: continue
1950             nodenetwork_setting = nodenetwork_settings[0]
1951             self.isequal(nodenetwork_setting, nodenetwork_setting_fields, 'AddNodeNetworkSetting - isequal')
1952
1953             # Update NodeNetworkSetting
1954             nodenetwork_setting_fields = random_nodenetwork_setting()
1955             value = nodenetwork_setting_fields['value'] 
1956             UpdateNodeNetworkSetting(auth, nodenetwork_setting_id, value)
1957
1958             # Check again
1959             nodenetwork_settings = GetNodeNetworkSettings(auth, [nodenetwork_setting_id])
1960             if nodenetwork_settings is None: continue
1961             nodenetwork_setting = nodenetwork_settings[0]
1962             self.isequal(nodenetwork_setting,  nodenetwork_setting_fields, 'UpdateNodeNetworkSetting - isequal')
1963
1964         nodenetwork_settings = GetNodeNetworkSettings(auth, nodenetwork_setting_ids)
1965         if nodenetwork_settings is not None:
1966             self.islistequal(nodenetwork_setting_ids, [n['nodenetwork_setting_id'] for n in nodenetwork_settings], 'GetNodeNetworkSettings - isequal')
1967
1968         if self.config.verbose:
1969             utils.header('Added nodenetwork_settings: %s' % nodenetwork_setting_ids)
1970
1971         return nodenetwork_setting_ids
1972
1973     def DeleteNodeNetworkSettings(self):
1974         GetNodeNetworkSettings = self.debug(api.GetNodeNetworkSettings)
1975         DeleteNodeNetworkSetting = self.debug(api.DeleteNodeNetworkSetting)
1976
1977         for nodenetwork_setting_id in self.nodenetwork_setting_ids:
1978             DeleteNodeNetworkSetting(auth, nodenetwork_setting_id)
1979
1980         # check 
1981         nodenetwork_settings = GetNodeNetworkSettings(auth, self.nodenetwork_setting_ids)
1982         self.islistequal(nodenetwork_settings, [], 'DeleteNodeNetworkSetting - check')
1983
1984         if self.config.verbose:
1985             utils.header("Deleted nodenetwork settings: %s " % self.nodenetwork_setting_ids)
1986         self.nodenetwork_setting_ids = []       
1987         
1988     def NodeNetworkSettingTypes(self, n = 2):
1989         nodenetwork_setting_type_ids = []
1990         AddNodeNetworkSettingType = self.debug(api.AddNodeNetworkSettingType)
1991         UpdateNodeNetworkSettingType = self.debug(api.UpdateNodeNetworkSettingType)
1992         GetNodeNetworkSettingTypes = self.debug(api.GetNodeNetworkSettingTypes)
1993
1994         for i in range(n):
1995             # Add Node Network Settings Type         
1996             nodenetwork_setting_type_fields = random_nodenetwork_setting_type()
1997             nodenetwork_setting_type_id = AddNodeNetworkSettingType(auth, nodenetwork_setting_type_fields)
1998             if nodenetwork_setting_type_id is None: continue
1999
2000             # Should return a unique id
2001             self.isunique(nodenetwork_setting_type_ids, nodenetwork_setting_type_ids, 'AddNodeNetworkSettingType - isunique')
2002             nodenetwork_setting_type_ids.append(nodenetwork_setting_type_id)
2003
2004             # check Node Network Settings Type
2005             nodenetwork_setting_types = GetNodeNetworkSettingTypes(auth, [nodenetwork_setting_type_id])
2006             if nodenetwork_setting_types is None: continue
2007             nodenetwork_setting_type = nodenetwork_setting_types[0]
2008             self.isequal(nodenetwork_setting_type, nodenetwork_setting_type_fields, 'AddNodeNetworkSettingType - isequal')
2009
2010             # Update NodeNetworkSetting
2011             nodenetwork_setting_type_fields = random_nodenetwork_setting_type()
2012             UpdateNodeNetworkSettingType(auth, nodenetwork_setting_type_id, nodenetwork_setting_type_fields)
2013
2014             # Check again
2015             nodenetwork_setting_types = GetNodeNetworkSettingTypes(auth, [nodenetwork_setting_type_id])
2016             if nodenetwork_setting_types is None: continue
2017             nodenetwork_setting_type = nodenetwork_setting_types[0]
2018             self.isequal(nodenetwork_setting_type,  nodenetwork_setting_type_fields, 'UpdateNodeNetworkSettingType - isequal')
2019
2020         nodenetwork_setting_types = GetNodeNetworkSettingTypes(auth, nodenetwork_setting_type_ids)
2021         if nodenetwork_setting_types is not None:
2022             self.islistequal(nodenetwork_setting_type_ids, [n['nodenetwork_setting_type_id'] for n in nodenetwork_setting_types], 'GetNodeNetworkSettingTypes - isequal')
2023
2024         if self.config.verbose:
2025             utils.header('Added nodenetwork_setting_types: %s' % nodenetwork_setting_type_ids)
2026
2027         return nodenetwork_setting_type_ids
2028
2029     def DeleteNodeNetworkSettingTypes(self):
2030         GetNodeNetworkSettingTypes = self.debug(api.GetNodeNetworkSettingTypes)
2031         DeleteNodeNetworkSettingType = self.debug(api.DeleteNodeNetworkSettingType)
2032
2033         for nodenetwork_setting_type_id in self.nodenetwork_setting_type_ids:
2034             DeleteNodeNetworkSettingType(auth, nodenetwork_setting_type_id)
2035
2036         # check 
2037         nodenetwork_setting_types = GetNodeNetworkSettingTypes(auth, self.nodenetwork_setting_type_ids)
2038         self.islistequal(nodenetwork_setting_types, [], 'DeleteNodeNetworkSettingType - check')
2039
2040         if self.config.verbose:
2041             utils.header("Deleted nodenetwork setting types: %s " % self.nodenetwork_setting_type_ids)
2042         self.nodenetwork_setting_type_ids = []  
2043
2044 if __name__ == '__main__':
2045     args = tuple(sys.argv[1:])
2046     api_unit_test()(*args)