blob: 8cbf8e69c5c8e7ff9f54d690362f3223639fa7a5 [file] [log] [blame]
Kailash Khalasi3e67c0a2017-08-25 14:51:51 -07001import sys
2sys.path.append("/opt/xos/coreapi/")
3
4import grpc_client
5from grpc_client import Empty
6from testconfig import *
7from protos.common_pb2 import *
8import random, string
9import unittest
10import pytz
11from itertools import combinations
12
13permutations = False
14{% autoescape false %}
15{% endautoescape %}
16{%- for m in proto.messages -%}
17{%- if m.name != 'XOSBase' -%}
18{{ m.name | lower }} = None
19{{ m.name | lower }}_dependent_list = []
20{% endif %}
21{% endfor %}
22c=grpc_client.SecureClient("xos-core.cord.lab", username=USERNAME, password=PASSWORD)
23
24def to_dict_func(dlist):
25 return dict(dlist)
26
27def generate_random_value(value):
28 if value == 'string':
29 return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
30 if value == 'bool':
31 return random.choice([True, False])
32 if value == 'int32' or value == 'uint32':
33 return random.randint(1,10000)
34 if value == 'float':
35 return random.uniform(1,10)
36 else:
37 return None
38
39def generate_random_slice_name():
40 random_name = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10))
41 return 'testloginbase' + random_name
42
43def generate_random_timezone():
44 return random.choice(pytz.all_timezones)
45
46def delete_dependent_objects(dlist):
47 for index, item in enumerate(dlist):
48 try:
49 fn = getattr(c.stub, 'Delete' + item.__class__.__name__)
50 deleted = fn(grpc_client.ID(id=item.id))
51 except Exception, e:
52 print 'Failed to deleted: ' + str(item.__class__.__name__)
53 print e
54
55{% for m in proto.messages %}
56{%- if m.name != 'XOSBase' -%}
57def create_{{ m.name | lower }}():
58 # create {{ m.name | lower }}
59 {{ m.name | lower }}=grpc_client.{{ m.name }}()
60 # set required fields for {{ m.name | lower }} object
61 {% for f in m.fields -%}
62 {% if f.link -%}
63 {% if f.modifier == "required" -%}
64 {% if f.options.blank == "False" -%}
65 # create dependent {{ f.model }}
66 {{ f.options.model }}=create_{{ f.options.model | lower }}()
67 {{ m.name | lower }}.{{ f.name }}_id = {{ f.options.model }}.id
68 {{ m.name | lower }}_dependent_list.append({{ f.options.model }})
69 {% endif -%}
70 {% endif -%}
71 {% else %}
72 {% if f.modifier == "required" -%}
73 {% if f.options.blank == "False" -%}
74 {% if not f.options.default -%}
75 {{ m.name | lower }}.{{ f.name }} = generate_random_value("{{ f.type }}")
76 {% endif %}
77 {% endif %}
78 {% endif %}
79 {% endif %}
80 {% endfor %}
81 {% if m.name == "Slice" -%}
82 {{ m.name | lower }}.name = str(Site.login_base) + str({{ m.name | lower }}.name)
83 {% endif %}
84 {% if m.name == "Site" -%}
85 {{ m.name | lower }}.login_base = "testloginbase"
86 {% endif %}
87 {% if m.name == "Network" -%}
88 {{ m.name | lower }}.permit_all_slices = True
89 {% endif %}
90 {{ m.name | lower }}_2=c.stub.Create{{ m.name }}({{ m.name | lower }})
91 return {{ m.name | lower }}_2
92{% endif %}
93{% endfor %}
94
95class TestSequenceMeta(type):
96 def __new__(mcs, name, bases, dict):
97
98 def gen_test(xos_model, params_req=[], fields_req=[], dependents=[], dependent_functions=[], params=[], fields=[]):
99 def test(self):
100 dependent_objects = []
101 str = "grpc_client." + xos_model + "()"
102 model = eval(str)
103 try:
104 params.remove("deployments")
105 except:
106 pass
107
108 ## set required fields
109 for i in params_req:
110 if not ((xos_model == "Slice") and (i == "name")):
111 test_value = generate_random_value(fields_req[i])
112 setattr(model, i, test_value)
113 else:
114 test_value = generate_random_slice_name()
115 setattr(model, i, test_value)
116
117 ## set optional fields
118 for i in params:
119 if not ((xos_model == "User") and (i == "timezone")):
120 test_value = generate_random_value(fields[i])
121 setattr(model, i, test_value)
122 else:
123 test_value = generate_random_timezone()
124 setattr(model, i, test_value)
125
126 ## create dependent objects
127 for i, j in zip(dependents,dependent_functions):
128 str = "create_" + j.lower() + "()"
129 dep_i = eval(str)
130 dep_id = i + "_id"
131 setattr(model, dep_id.lower(), dep_i.id)
132 dependent_objects.append(dep_i)
133
134 str = "c.stub.Create" + xos_model + "(model)"
135 model_2 = eval(str)
136
137 for i in params_req:
138 val = getattr(model, i)
139 val2 = getattr(model_2, i)
140 if fields_req[i] == 'float':
141 assert abs(val - val2) < 0.5000
142 else:
143 assert (val == val2)
144
145 for i in params:
146 val = getattr(model, i)
147 val2 = getattr(model_2, i)
148 if fields[i] == 'float':
149 assert abs(val - val2) < 0.5000
150 elif i == 'password':
151 pass
152 else:
153 assert (val == val2)
154
155 ## test list call
156 str = "c.stub.List" + xos_model + "(grpc_client.Empty()).items"
157 model_list = eval(str)
158 for i in params_req:
159 val = getattr(model, i)
160 model_item = [x for x in model_list if getattr(x, i) == val]
161 if fields_req[i] == 'bool':
162 assert (len(model_item) >= 1)
163 elif fields_req[i] == 'float':
164 # assert(len(model_item)==1)
165 pass
166 else:
167 assert (len(model_item) == 1)
168 for i in params:
169 val = getattr(model, i)
170 model_item = [x for x in model_list if getattr(x, i) == val]
171 if fields[i] == 'bool':
172 assert (len(model_item) >= 1)
173 elif fields[i] == 'float':
174 # assert(len(model_item)==1)
175 pass
176 elif i == 'password':
177 pass
178 else:
179 assert (len(model_item) == 1)
180
181 ## test delete method
182 str = "c.stub.Delete" + xos_model + "(grpc_client.ID(id=model_2.id))"
183 eval(str)
184 # validate deletion
185 str = "c.stub.List" + xos_model + "(grpc_client.Empty()).items"
186 all_items = eval(str)
187 for i in params_req:
188 val = getattr(model, i)
189 all_models = [x for x in all_items if getattr(x, i) == val]
190 if fields_req[i] == 'bool':
191 continue
192 else:
193 assert (len(all_models) == 0)
194 for i in params:
195 val = getattr(model, i)
196 all_models = [x for x in all_items if getattr(x, i) == val]
197 if fields[i] == 'bool':
198 continue
199 else:
200 assert (len(all_models) == 0)
201 delete_dependent_objects(dependent_objects)
202 return test
203
204
205 {% for m in proto.messages %}
206 {%- if m.name != 'XOSBase' -%}
207 # create {{ m.name }}
208 module = "{{ m.name }}"
209 {{ m.name | lower }}_fields_req = []
210 {{ m.name | lower }}_fields_types_req = ()
211 {{ m.name | lower }}_fields = []
212 {{ m.name | lower }}_fields_type = ()
213 {{ m.name | lower }}_dependent_list = []
214 {{ m.name | lower }}_dependent_list_functions = []
215
216 {% for f in m.fields -%}
217 {% if f.modifier == "required" -%}
218 {% if f.options.blank == "False" -%}
219 {% if not f.options.default -%}
220 {% if not f.link -%}
221 # set required fields for {{ m.name | lower }} object
222 {{ m.name | lower }}_fields_types_req = {{ m.name | lower }}_fields_type + ('{{ f.name }}', '{{ f.type }}')
223 {{ m.name | lower }}_fields_req.append({{ m.name | lower }}_fields_types_req)
224 {% endif -%}
225 {% endif -%}
226 {% endif -%}
227 {% endif -%}
228 {% if f.link -%}
229 {% if f.modifier == "required" -%}
230 {% if f.options.blank == "False" -%}
231
232 ## get dependent objects
233 # add dependent {{ f.name | lower }} to list
234 {{ m.name | lower }}_dependent_list.append('{{ f.name }}')
235 {{ m.name | lower }}_dependent_list_functions.append('{{ f.options.model }}')
236 {% endif -%}
237 {% endif -%}
238 {% endif -%}
239 {% endfor %}
240
241 {% for f in m.fields -%}
242 ## get optional fields
243 {% if f.options.default -%}
244 {{ m.name | lower }}_fields_types = {{ m.name | lower }}_fields_type + ('{{ f.name }}', '{{ f.type}}')
245 {{ m.name | lower }}_fields.append({{ m.name | lower }}_fields_types)
246 {% endif -%}
247 {% endfor %}
248 if permutations:
249 #Loop through param options, create permutations, and test with each
250 index = 1
251 field_names_req = [x[0] for x in {{ m.name | lower }}_fields_req]
252 field_types_req = [x[1] for x in {{ m.name | lower }}_fields_req]
253 field_names = [x[0] for x in {{ m.name | lower }}_fields]
254 field_types = [x[1] for x in {{ m.name | lower }}_fields]
255 {{ m.name | lower }}_fields_req = to_dict_func({{ m.name | lower }}_fields_req)
256 {{ m.name | lower }}_fields = to_dict_func({{ m.name | lower }}_fields)
257 for i in xrange(1, len(field_names) + 1):
258 perm = list(combinations(field_names, i))
259 for j in perm:
260 test_name = "test_{}_{}".format(module.lower(), str(index))
261 dict[test_name] = gen_test(module, list(field_names_req), {{ m.name | lower }}_fields_req, {{ m.name | lower }}_dependent_list,{{ m.name | lower }}_dependent_list_functions,list(j), {{ m.name | lower }}_fields)
262 index += 1
263 else:
264 #Loop through required parameters
265 index = 1
266 field_names_req = [x[0] for x in {{ m.name | lower }}_fields_req]
267 field_types_req = [x[1] for x in {{ m.name | lower }}_fields_req]
268 {{ m.name | lower }}_fields_req = to_dict_func({{ m.name | lower }}_fields_req)
269 test_name = "test_{}_{}".format(module.lower(), str(index))
270 dict[test_name] = gen_test(module, list(field_names_req), {{ m.name | lower }}_fields_req, {{ m.name | lower }}_dependent_list, {{ m.name | lower }}_dependent_list_functions)
271 {% endif %}
272 {% endfor %}
273 return type.__new__(mcs, name, bases, dict)
274
275class TestSequence(unittest.TestCase):
276 __metaclass__ = TestSequenceMeta
277
278if __name__ == '__main__':
279 unittest.main()