6 class add_test_data(Test):
8 Adds the test data found in config to the plc db
12 auth = self.config.auth
13 # Make sure some required fields are in config
14 required_fields = ['TEST_SITE_NAME', 'TEST_SITE_LOGIN_BASE', 'TEST_SLICE_NAME', 'TEST_PERSON_EMAIL']
15 required_node_fields = ['TEST_NODE_TYPE', 'TEST_NODE_METHOD', 'TEST_NODE_HOSTNAME', 'TEST_NODE_IP',
16 'TEST_NODE_GATEWAY', 'TEST_NODE_DNS', 'TEST_NODE_NETWORK', 'TEST_NODE_BROADCAST',
19 for field in required_fields:
20 if not hasattr(self.config, field) or \
21 len(getattr(self.config, field).strip()) < 1:
22 raise Exception, "%(field)s must be set and cannot be blank" % locals()
24 # Look for node configurations
26 for attr in dir(self.config):
27 if attr.find("NODE") > 0:
28 parts = attr.split('_')
29 node_prefix = parts[1] +"_"+ parts[3]
30 name = "_".join(parts[:3])
31 value = getattr(self.config, attr)
32 # start a new node dictionary
33 if node_prefix not in node_params:
34 node_params[node_prefix] = {'prefix': node_prefix}
35 node_params[node_prefix][name] = value
37 node_configs = node_params.values()
40 # Make sure required node fields are preset for each node config
41 for node_config in node_configs:
42 for field in required_node_fields:
43 if field not in node_config or len(node_config[field].strip()) < 1:
44 raise Exception, "%s must be set for %s and cannot be blank" % (field, node_config['prefix'])
45 node = {'type': node_config['TEST_NODE_TYPE'],
46 'method': node_config['TEST_NODE_METHOD'],
47 'hostname': node_config['TEST_NODE_HOSTNAME'],
48 'ip': node_config['TEST_NODE_IP'],
49 'gateway': node_config['TEST_NODE_GATEWAY'],
50 'dns1': node_config['TEST_NODE_DNS'],
51 'broadcast': node_config['TEST_NODE_BROADCAST'],
52 'network': node_config['TEST_NODE_NETWORK'],
53 'netmask': node_config['TEST_NODE_NETMASK'],
55 'nodenetwork_ids': []}
56 node_list.append(node)
60 site_fields = {'name': self.config.TEST_SITE_NAME, 'login_base': self.config.TEST_SITE_LOGIN_BASE,
61 'url': 'http://google.com', 'enabled': True, 'max_slices': 1000,
62 'max_slivers': 1000, 'is_public': True, 'abbreviated_name': 'Test',
65 slice_fields = {'name': self.config.TEST_SLICE_NAME, 'instantiation': 'plc-instantiated',
66 'max_nodes': 1000, 'description': 'blank', 'person_ids': [], 'node_ids': []}
68 person_fields = {'first_name': 'fname', 'last_name': 'lname', 'password': 'password',
69 'email': self.config.TEST_PERSON_EMAIL, 'site_ids': [], 'slice_ids': []}
73 sites = api.GetSites(auth, {'login_base': site_fields['login_base']})
75 site_id = api.AddSite(auth, site_fields)
76 site_fields['site_id'] = site_id
78 if self.config.verbose:
79 utils.header("Added test site")
82 if self.config.verbose:
83 utils.header("Test site found")
86 for node_fields in node_list:
87 nodes = api.GetNodes(auth, [node_fields['hostname']])
89 node_id = api.AddNode(auth, site_fields['login_base'], node_fields)
90 node_fields['node_id'] = node_id
92 nodes.append(node_fields)
93 if self.config.verbose:
94 utils.header("Added test node")
97 if self.config.verbose:
98 utils.header("Test node found")
101 if not node['nodenetwork_ids']:
102 nodenetwork_id = api.AddNodeNetwork(auth, node_fields['hostname'], node_fields)
103 if self.config.verbose:
104 utils.header("Added test nodenetwork")
106 if self.config.verbose:
107 utils.header("Nodenetwork found")
110 slices = api.GetSlices(auth, [slice_fields['name']])
112 slice_id = api.AddSlice(auth, slice_fields)
113 slice_fields['slice_id'] = slice_id
115 if self.config.verbose:
116 utils.header("Added test slice")
119 if self.config.verbose:
120 utils.header("Test slice found")
123 node_ids = [n['node_id'] for n in nodes]
124 node_ids = filter(lambda node_id: node_id not in slice['node_ids'], node_ids)
126 api.AddSliceToNodes(auth, slice['name'], node_ids)
127 if self.config.verbose:
128 utils.header("Added test slice to test nodes")
130 if self.config.verbose:
131 utils.header("Test slice found on test nodes")
134 persons = api.GetPersons(auth, [person_fields['email']])
136 person_id = api.AddPerson(auth, person_fields)
137 person_fields['person_id'] = person_id
138 person = person_fields
139 if self.config.verbose:
140 utils.header("Added test person")
143 if self.config.verbose:
144 utils.header("Test person found")
146 # Add roles to person
147 api.AddRoleToPerson(auth, 'user', person['email'])
149 if site['site_id'] not in person['site_ids']:
150 api.AddPersonToSite(auth, person['email'], site['login_base'])
151 if self.config.verbose:
152 utils.header("Added test person to test site")
154 if self.config.verbose:
155 utils.header("Test person found on test site")
157 # Add person to slice
158 if slice['slice_id'] not in person['slice_ids']:
159 api.AddPersonToSlice(auth, person_fields['email'], slice_fields['name'])
160 if self.config.verbose:
161 utils.header("Added test person to slice")
163 if self.config.verbose:
164 utils.header("Test person found on test slice")
167 if __name__ == '__main__':
168 args = tuple(sys.argv[1:])
169 add_test_data()(*args)