b411816d2314039d00a50597aad038e618db2e85
[tests.git] / qaapi / qa / tests / add_test_data.py
1 #!/usr/bin/python
2 import os,sys
3 from Test import Test
4 from qa import utils
5 from qa.PLCs import PLC, PLCs
6   
7 class add_test_data(Test):
8     """
9     Adds the test data found in config to the plc db
10     """   
11     def call(self,  plc_name = None):
12
13         # Determine which plc to talk to 
14         plc = PLC(self.config)
15         plcs = getattr(self.config, 'plcs', [])
16         for p in plcs:
17             if p['name'] in [plc_name]:
18                 plc.update(p)
19         plc.config.update_api(plc)
20
21         api = plc.config.api
22         auth = plc.config.auth
23
24
25         # Search config for objects that belong to this plc
26         # Any object with 'plc' defined as this plc's name or with 
27         # no 'plc' defined will be added
28         this_plc = lambda object: 'plc' not in object or \
29                                   'plc' in object and object['plc'] == plc['name'] or \
30                                   object['plc'] == None   
31      
32         sitelist = filter(this_plc, self.config.sites)          
33         nodelist = filter(this_plc, self.config.nodes)
34         slicelist = filter(this_plc, self.config.slices)
35         personlist = filter(this_plc, self.config.persons) 
36         
37         # Add Test site
38         for site in sitelist:
39             sites = api.GetSites(auth, [site['login_base']])
40             if not sites:
41                 site_id = api.AddSite(auth, dict(site))
42                 site['site_id'] = site_id
43                 if self.config.verbose:
44                     utils.header("Added site: %s" % site['name'])
45             else:
46                 site.update(sites[0])
47                 if self.config.verbose:
48                     utils.header("Site %s found" % site['name'])
49
50         # Add Test nodes
51         for node in nodelist:
52             nodes = api.GetNodes(auth, [node['hostname']])
53             if not nodes:
54                 node_id = api.AddNode(auth, node['site'], dict(node))
55                 node['node_id'] = node_id
56                 if self.config.verbose:
57                     utils.header("Added node: %s" % node['hostname'])
58             else:
59                 node.update(nodes[0])
60                 if self.config.verbose:
61                     utils.header("Node %s found" % node['hostname'])
62
63             # Add node network
64             if 'nodenetwork_ids' not in node or not node['nodenetwork_ids']:
65                 for nodenetwork in node['nodenetworks']:
66                     nodenetwork_id = api.AddNodeNetwork(auth, node['hostname'], dict(nodenetwork))
67                     if self.config.verbose:
68                         utils.header("Added nodenetwork to %s" % node['hostname'])
69                 else:
70                     if self.config.verbose:
71                         utils.header("Nodenetwork found on node %s" % node['hostname']) 
72         
73         # Add Test slice
74         for slice in slicelist:
75             slices = api.GetSlices(auth, [slice['name']])
76             if not slices:
77                 slice_id = api.AddSlice(auth, dict(slice))
78                 slice['slice_id'] = slice_id
79                 if self.config.verbose:
80                     utils.header("Added slice: %s" % slice['name'])
81             else:
82                 slice.update(slices[0])
83                 if self.config.verbose:
84                     utils.header("Slice %s found" % slice['name'])
85             
86             # Add slice to nodes
87             for node in slice['nodes']:
88                 api.AddSliceToNodes(auth, slice['name'], [node])                
89                 if self.config.verbose:
90                     utils.header("Added slice to %s" % node)
91         
92         # Add test person
93         for person in personlist:
94             roles = person['roles']
95             persons = api.GetPersons(auth, [person['email']])
96             if not persons:
97                 person_id = api.AddPerson(auth, dict(person))
98                 person['person_id'] = person_id
99                 api.UpdatePerson(auth, person_id, {'enabled': True})
100                 if self.config.verbose:
101                     utils.header("Added person: %s" % person['email'])
102             else:
103                 person.update(persons[0])
104                 if self.config.verbose:
105                     utils.header("Person %s found" % person['email'])
106         
107             # Add roles to person
108             for role in roles:
109                 api.AddRoleToPerson(auth, role, person['email'])
110                 if self.config.verbose:
111                     utils.header("Added %s to %s" % (role, person['email']))
112             # Add person to site
113             for site in person['sites']:
114                 api.AddPersonToSite(auth, person['email'], site)
115                 if self.config.verbose:
116                     utils.header("Added %s to %s" % (person['email'], site))
117
118             # Add person to slice
119             for slice in person['slices']:
120                 api.AddPersonToSlice(auth, person['email'], slice)
121                 if self.config.verbose:
122                     utils.header("Added %s to %s" % (person['email'], slice))
123         return 1
124
125 if __name__ == '__main__':
126     args = tuple(sys.argv[1:])
127     add_test_data()(*args)