Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: python
python:
- "2.7"
- "3.5"
# command to install dependencies
install:
- python setup.py install
Expand Down
2 changes: 1 addition & 1 deletion iotqatools/cb_ngsiv2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def __send_request(self, method, url, headers=None, payload=None, verify=None, q
# Send the requests
try:
response = requests.request(**parameters)
except RequestException, e:
except RequestException as e:
PqaTools.log_requestAndResponse(url=url, headers=headers, params=query, data=payload, comp='CB',
method=method)
assert False, 'ERROR: [NETWORK ERROR] {}'.format(e)
Expand Down
47 changes: 29 additions & 18 deletions iotqatools/cb_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
from requests.exceptions import RequestException
from iotqatools.iot_tools import PqaTools

try:
unicode()
except NameError:
unicode = str
str = bytes


# Utilities
def check_valid_json(payload):
Expand Down Expand Up @@ -965,7 +971,7 @@ def __send_request(self, method, url, headers=None, payload=None, verify=None, q
# Send the requests
try:
response = requests.request(**parameters)
except RequestException, e:
except RequestException as e:
PqaTools.log_requestAndResponse(url=url, headers=headers, data=payload, comp='CB', method=method)
assert False, 'ERROR: [NETWORK ERROR] {}'.format(e)

Expand Down Expand Up @@ -1718,10 +1724,10 @@ def __send_request(self, method, url, headers=None, payload=None, verify=None, q
# Send the requests
try:
response = requests.request(**parameters)
except RequestException, e:
except RequestException as e:
PqaTools.log_requestAndResponse(url=url, headers=headers, data=payload, comp='CB', method=method)
assert False, 'ERROR: [NETWORK ERROR] {}'.format(e)
print response
print(response)

# Log data
PqaTools.log_fullRequest(comp='CB', response=response, params=parameters)
Expand Down Expand Up @@ -2168,7 +2174,7 @@ def __send_request(self, method, url, headers=None, payload=None, verify=None, q
# Send the requests
try:
response = requests.request(**parameters)
except RequestException, e:
except RequestException as e:
PqaTools.log_requestAndResponse(url=url, headers=headers, data=payload, comp='CB', method=method)
assert False, 'ERROR: [NETWORK ERROR] {}'.format(e)

Expand Down Expand Up @@ -2238,6 +2244,17 @@ def entities_get(self, service, entity, entity_id, pattern='false', subservice='
response = self.__send_request('post', url, headers, json.loads(payload))
return response

@staticmethod
def __replace_quotes(text):
# format avoid urlencoding due to not array inputs
return (
text
.replace("'", '"')
.replace("u"", '"')
.replace(""", '"')
.replace("'", '"')
.replace(""", '"'))

def entity_append(self, service, entity_data, subservice=''):
"""
Create if not exist a CB entity or update it
Expand Down Expand Up @@ -2265,10 +2282,7 @@ def entity_append(self, service, entity_data, subservice=''):
'ent_attributes': entity_data['attributes'],
'action_mode': 'APPEND'})

# format avoid urlencoding due to not array inputs
payload = payload.replace("'", '"')
payload = payload.replace("u"", '"')
payload = payload.replace(""", '"')
payload = self.__replace_quotes(payload)

# send the request for the subscription
response = self.__send_request('post', url, headers, json.loads(payload))
Expand Down Expand Up @@ -2301,10 +2315,7 @@ def entity_update(self, service, entity_data, subservice=''):
'ent_attributes': entity_data['attributes'],
'action_mode': 'APPEND'})

# format avoid urlencoding due to not array inputs
payload = payload.replace("'", '"')
payload = payload.replace("u"", '"')
payload = payload.replace(""", '"')
payload = self.__replace_quotes(payload)

# send the request for the subscription
response = self.__send_request('post', url, headers, json.loads(payload))
Expand All @@ -2323,7 +2334,7 @@ def subscription_add(self, service, template_data={}, verifySSL=False, subservic
"""
# show info received
if self.verbosity >= 2:
print "###> INPUT > {}".format(template_data)
print("###> INPUT > {}".format(template_data))

# set the service header id
headers = dict(self.default_headers)
Expand Down Expand Up @@ -2365,7 +2376,7 @@ def subscription_add(self, service, template_data={}, verifySSL=False, subservic
'subs_type': template_data['subs_type'],
'ent_att_notif': template_data['ent_att_notif'],
'ent_att_cond': template_data['ent_att_cond']})
payload = payload.replace("'", '"')
payload = self.__replace_quotes(payload)
# send the request for the subscription
return self.__send_request('post', url, headers, json.loads(payload), verifySSL)

Expand Down Expand Up @@ -2405,8 +2416,8 @@ def set_auth_token(self, auth_token):
ce = ContextElements()
ce.add_context_element('Room1', 'Room', attr)
payload = PayloadUtils.build_standard_entity_creation_payload(ce)
print payload
print(payload)
resp = cb.standard_entity_creation(payload)
print resp
print resp.text
print resp.headers
print(resp)
print(resp.text)
print(resp.headers)
6 changes: 3 additions & 3 deletions iotqatools/cb_v2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def harakiri(self):
url = "%s/%s" % (self.cb_url, "exit/harakiri")
resp = requests.get(url=url)
return resp.status_code
except Exception, e:
except Exception as e:
return -1

def is_cb_started(self):
Expand All @@ -355,7 +355,7 @@ def is_cb_started(self):
resp = requests.get(url=url)
__logger__.debug("CB code returned with version request is: %s " % str(resp.status_code))
return resp.status_code == 200
except Exception, e:
except Exception as e:
return False

def __update_headers(self):
Expand Down Expand Up @@ -521,7 +521,7 @@ def __send_request(self, method, path, **kwargs):
url = "%s/%s" % (self.cb_url, path)
try:
resp = requests.request(method=method, url=url, headers=headers, data=payload, params=parameters, verify=False)
except Exception, e:
except Exception as e:
assert False, "ERROR - send request \n - url: %s\n - %s" % (url, str(e))
if show:
self.response_string = "http code: %s - %s" % (resp.status_code, resp.reason)
Expand Down
4 changes: 2 additions & 2 deletions iotqatools/cep_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ def create_visual_rule(self, rule_name, service, sensor_card_list, action_card_l

cep_payload = cep_payload.replace("'", '"')
url = self.default_endpoint + self.path
print url
print json.dumps(yaml.load(cep_payload))
print(url)
print(json.dumps(yaml.load(cep_payload)))
response = requests.post(url, data=json.dumps(yaml.load(cep_payload)), headers=headers, verify=False)
assert response.status_code == 201, 'ERROR {}, the rule {} cannot be created. {}'.format(response.status_code,
rule_name,
Expand Down
4 changes: 2 additions & 2 deletions iotqatools/ckan_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __send_request(self, method, url, headers=None, payload=None, verify=None, q
# Send the requests
try:
response = requests.request(**parameters)
except RequestException, e:
except RequestException as e:
PqaTools.log_requestAndResponse(url=url, headers=headers, data=payload, comp='CKAN')
assert False, 'ERROR: [NETWORK ERROR] {}'.format(e)

Expand Down Expand Up @@ -291,7 +291,7 @@ def get_resource_id(self, resource_name, package_name, verify_ssl=False):
if resources['name'] == resource_name:
resource_id = resources['id']
return resource_id
except Exception, e:
except Exception as e:
raise Exception("\n-- ERROR -- get_resource_id \n{}".format(e))

def delete_resource(self, resource_name, package_name, verify_ssl=False):
Expand Down
6 changes: 3 additions & 3 deletions iotqatools/fabric_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def run(self, command, **kwargs):
return self.__sub_run(command, path, sudo_run)
else:
return self.__sub_run(command, path, sudo_run)
except Exception, e:
except Exception as e:
assert False, "ERROR - running the command \"%s\" remotely with Fabric \n - %s" % (command, str(e))

def runs(self, ops_list, **kwargs):
Expand Down Expand Up @@ -184,7 +184,7 @@ def __sub_read_file(self, file, sudo_run):
fd = StringIO()
get(file, fd, use_sudo=sudo_run)
return fd.getvalue()
except Exception, e:
except Exception as e:
__logger__.error("ERROR - reading %s file\n %s" % (file, e))


Expand All @@ -205,5 +205,5 @@ def read_file(self, file, **kwargs):
return self.__sub_read_file(file, sudo_run)
else:
return self.__sub_read_file(file, sudo_run)
except Exception, e:
except Exception as e:
assert False, "ERROR -reading a File \"%s\" remotely with Fabric \n - %s" % (file, str(e))
26 changes: 13 additions & 13 deletions iotqatools/helpers_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def convert_str_to_dict(body, content):
return xmltodict.parse(body)
else:
return json.loads(body)
except Exception, e:
except Exception as e:
assert False, " ERROR - converting string to %s dictionary: \n" \
" %s \n " \
" Exception error: %s" % (str(content), str(body), str(e))
Expand All @@ -106,7 +106,7 @@ def convert_dict_to_str(body, content):
return xmltodict.unparse(body)
else:
return str(json.dumps(body, ensure_ascii=False).encode('utf-8'))
except Exception, e:
except Exception as e:
assert False, " ERROR - converting %s dictionary to string: \n" \
" %s \n" \
" Exception error: %s" % (str(content), str(body), str(e))
Expand All @@ -121,7 +121,7 @@ def convert_str_to_list(text, separator):
"""
try:
return text.split(separator)
except Exception, e:
except Exception as e:
assert False, " ERROR - converting %s string to list with separator: %s \n" \
" Exception error:%s" % (str(text), str(separator), str(e))

Expand All @@ -135,7 +135,7 @@ def convert_list_to_string(list, separator):
"""
try:
return separator.join(list)
except Exception, e:
except Exception as e:
assert False, " ERROR - converting list to string with separator: %s \n" \
" Exception error:%s" % (str(separator), str(e))

Expand All @@ -145,10 +145,10 @@ def show_times(init_value):
shows the time duration of the entire test
:param init_value: initial time
"""
print "**************************************************************"
print "Initial (date & time): " + str(init_value)
print "Final (date & time): " + str(time.strftime("%c"))
print "**************************************************************"
print("**************************************************************")
print("Initial (date & time): " + str(init_value))
print("Final (date & time): " + str(time.strftime("%c")))
print("**************************************************************")


def generate_timestamp(**kwargs):
Expand Down Expand Up @@ -210,7 +210,7 @@ def is_an_integer_value(value):
if dec_v == 0:
return True
return False
except Exception, e:
except Exception as e:
assert False, " Error - %s is not numeric... \n %s" % (str(value), str(e))


Expand Down Expand Up @@ -262,7 +262,7 @@ def read_file_to_json(file_name):
try:
with open(file_name) as config_file:
return json.load(config_file)
except Exception, e:
except Exception as e:
raise Exception("\n ERROR - parsing the %s/%s file \n msg= %s" % (path, file_name, str(e)))


Expand Down Expand Up @@ -300,7 +300,7 @@ def eval_binary_expr(op1, operator, op2):
if operator not in ['==', '!=']:
try:
op1, op2 = int(float(op1)), int(float(op2))
except Exception, e:
except Exception as e:
__logger__.warn("Some value is not a numeric format. (%s)" % str(e))
return False
return get_operator_fn(operator)(op1, op2)
Expand Down Expand Up @@ -336,7 +336,7 @@ def list_swap(l, init_pos, end_pos):
"""
try:
l[int(init_pos)], l[int(end_pos)] = l[int(end_pos)], l[int(init_pos)]
except Exception, e:
except Exception as e:
raise "ERROR - trying to swap items in a list: \n - %s" % e
return l

Expand Down Expand Up @@ -377,4 +377,4 @@ class LogLevelConfiguration:

In addition, not sure if this file is the best place for this class or it should be put in its own .py
"""
default_log_level = 'DEBUG'
default_log_level = 'DEBUG'
16 changes: 8 additions & 8 deletions iotqatools/iota_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def sendMeasure(self, measure_type, apikey, idDevice, measures, field={}):
url += '&ip='
url += field["ip"]

print 'url: ' + url
print('url: ' + url)
data = self.getMeasure(measure_type, measures, field)
res = requests.post(url,headers = {'content-type': 'text/plain'},data=data)

Expand All @@ -126,7 +126,7 @@ def sendMeasure(self, measure_type, apikey, idDevice, measures, field={}):

def getMeasure(self, protocol, measures, field={}):
result = ""
print measures
print(measures)
for measure in measures:
# Format type UL or UL2
if protocol == "UL":
Expand All @@ -150,7 +150,7 @@ def getMeasure(self, protocol, measures, field={}):
result = str(measure)
if "UL" in protocol:
result = result.rpartition("#")[0] # delete the last "#"
print result
print(result)
# Replace specials words and characters
replaces = {
"True": "1",
Expand All @@ -169,15 +169,15 @@ def getMeasure(self, protocol, measures, field={}):

def sendRegister(self, apikey, device, asset, model, phenomena):
url = self.getUrl('RegLight', apikey, device)
print 'url: ' + url
print('url: ' + url)
uom_id = 1
result = "<rs>"
result += "<id href=\"1:1\">" + device + "</id>"
result += "<id href=\"1:8\">" + asset + "</id>"
result += "<param name=\"ModelName\">"
result += "<text>" + model + "</text>"
result += "</param>"
print phenomena
print(phenomena)
for phenom in phenomena:
result += "<what href=\"" + str(phenom.get("href", "")) + "\" id=\"" + str(phenom.get("alias", "")) + "\"/>"
for phenom in phenomena:
Expand All @@ -190,7 +190,7 @@ def sendRegister(self, apikey, device, asset, model, phenomena):
uom_id += 1
result += "</data>"
result += "</rs>"
print result
print(result)
res = requests.post(url, data=result)

#log request
Expand All @@ -210,7 +210,7 @@ def getCommand(self, measure_type, apikey, idDevice, ip={}):
url = self.getUrl(measure_type, apikey, idDevice)
if ip:
url += "&ip=" + ip
print 'url: ' + url
print('url: ' + url)
res = requests.get(url)

#log request
Expand All @@ -227,7 +227,7 @@ def getCommand(self, measure_type, apikey, idDevice, ip={}):

def SendCmdResponse(self, measure_type, apikey, idDevice, command, response):
url = self.getUrl(measure_type, apikey, idDevice, command)
print 'url: ' + url
print('url: ' + url)
data = str(command) + "|" + str(response)
res = requests.post(url, data=data)

Expand Down
2 changes: 1 addition & 1 deletion iotqatools/iota_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def update_service_with_params(self, json, service_name, service_path={}, resour
headers[self.srv_path_header] = '/'
else:
headers[self.srv_path_header] = '/'
print params
print(params)
req = self.put_service('', json, headers, params)
return req

Expand Down
Loading