must remember test data is stored in dicts now, not lists. use .values() to get list...
[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 = self.config.get_plc(plc_name)
15         api = plc.config.api
16         auth = plc.config.auth
17
18         # Search config for objects that belong to this plc
19         # Any object with 'plc' defined as this plc's name or with 
20         # no 'plc' defined will be added
21         this_plc = lambda object: 'plc' not in object or \
22                                   'plc' in object and object['plc'] == plc['name'] or \
23                                   object['plc'] == None   
24      
25         sitelist = filter(this_plc, self.config.sites.values())         
26         nodelist = filter(this_plc, self.config.nodes.values())
27         slicelist = filter(this_plc, self.config.slices.values())
28         personlist = filter(this_plc, self.config.persons.values()) 
29
30         # Add Test site
31         for site in sitelist:
32             sites = api.GetSites(auth, [site['login_base']])
33             if not sites:
34                 site_id = api.AddSite(auth, dict(site))
35                 site['site_id'] = site_id
36                 if self.config.verbose:
37                     utils.header("Added site: %s" % site['name'])
38             else:
39                 site.update(sites[0])
40                 if self.config.verbose:
41                     utils.header("Site %s found" % site['name'])
42
43         # Add Test nodes
44         for node in nodelist:
45             nodes = api.GetNodes(auth, [node['hostname']])
46             if not nodes:
47                 node_id = api.AddNode(auth, node['site'], dict(node))
48                 node['node_id'] = node_id
49                 if self.config.verbose:
50                     utils.header("Added node: %s" % node['hostname'])
51             else:
52                 node.update(nodes[0])
53                 if self.config.verbose:
54                     utils.header("Node %s found" % node['hostname'])
55
56             # Add node network
57             if 'nodenetwork_ids' not in node or not node['nodenetwork_ids']:
58                 for nodenetwork in node['nodenetworks']:
59                     nodenetwork_id = api.AddNodeNetwork(auth, node['hostname'], dict(nodenetwork))
60                     if self.config.verbose:
61                         utils.header("Added nodenetwork to %s" % node['hostname'])
62                 else:
63                     if self.config.verbose:
64                         utils.header("Nodenetwork found on node %s" % node['hostname']) 
65         
66         # Add Test slice
67         for slice in slicelist:
68             slices = api.GetSlices(auth, [slice['name']])
69             if not slices:
70                 slice_id = api.AddSlice(auth, dict(slice))
71                 slice['slice_id'] = slice_id
72                 if self.config.verbose:
73                     utils.header("Added slice: %s" % slice['name'])
74             else:
75                 slice.update(slices[0])
76                 if self.config.verbose:
77                     utils.header("Slice %s found" % slice['name'])
78             
79             # Add slice to nodes
80             for node in slice['nodes']:
81                 api.AddSliceToNodes(auth, slice['name'], [node])                
82                 if self.config.verbose:
83                     utils.header("Added slice to %s" % node)
84         
85         # Add test person
86         for person in personlist:
87             roles = person['roles']
88             persons = api.GetPersons(auth, [person['email']])
89             if not persons:
90                 person_id = api.AddPerson(auth, dict(person))
91                 person['person_id'] = person_id
92                 api.UpdatePerson(auth, person_id, {'enabled': True})
93                 if self.config.verbose:
94                     utils.header("Added person: %s" % person['email'])
95             else:
96                 person.update(persons[0])
97                 if self.config.verbose:
98                     utils.header("Person %s found" % person['email'])
99         
100             # Add roles to person
101             for role in roles:
102                 api.AddRoleToPerson(auth, role, person['email'])
103                 if self.config.verbose:
104                     utils.header("Added %s to %s" % (role, person['email']))
105             # Add person to site
106             for site in person['sites']:
107                 api.AddPersonToSite(auth, person['email'], site)
108                 if self.config.verbose:
109                     utils.header("Added %s to %s" % (person['email'], site))
110
111             # Add person to slice
112             for slice in person['slices']:
113                 api.AddPersonToSlice(auth, person['email'], slice)
114                 if self.config.verbose:
115                     utils.header("Added %s to %s" % (person['email'], slice))
116         return 1
117
118 if __name__ == '__main__':
119     args = tuple(sys.argv[1:])
120     add_test_data()(*args)