You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 2 | import requests, json, os, sys, time |
| 3 | #sys.path.append('common-utils') |
| 4 | sys.path.append(os.path.join(sys.path[0],'utils')) |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 5 | from readProperties import readProperties |
| 6 | |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 7 | class restApi(object): |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 8 | ''' |
| 9 | Functions for testing CORD API with POST, GET, PUT, DELETE method |
| 10 | ''' |
| 11 | def __init__(self): |
You Wang | d4dacd5 | 2017-02-16 10:58:53 -0800 | [diff] [blame] | 12 | self.rp = readProperties(os.path.abspath(os.path.dirname(__file__))+"/../Properties/RestApiProperties.py") |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 13 | self.controllerIP = self.getValueFromProperties("SERVER_IP") |
| 14 | self.controllerPort = self.getValueFromProperties("SERVER_PORT") |
| 15 | self.user = self.getValueFromProperties("USER") |
| 16 | self.password = self.getValueFromProperties("PASSWD") |
| 17 | self.jsonHeader = {'Content-Type': 'application/json'} |
| 18 | |
| 19 | def getValueFromProperties(self, key): |
| 20 | ''' |
| 21 | Get and return values from properties file |
| 22 | ''' |
| 23 | rawValue = self.rp.getValueProperties(key) |
| 24 | value = rawValue.replace("'","") |
| 25 | return value |
| 26 | |
| 27 | def getURL(self, key): |
| 28 | ''' |
| 29 | Get REST API suffix from key and return the full URL |
| 30 | ''' |
| 31 | urlSuffix = self.getValueFromProperties(key) |
| 32 | url = "http://" + self.controllerIP + ":" + self.controllerPort + urlSuffix |
| 33 | return url |
| 34 | |
| 35 | def checkResult(self, resp, expectedStatus): |
| 36 | ''' |
| 37 | Check if the status code in resp equals to the expected number. |
| 38 | Return True or False based on the check result. |
| 39 | ''' |
| 40 | if resp.status_code == expectedStatus: |
| 41 | print "Test passed: " + str(resp.status_code) + ": " + resp.text |
| 42 | return True |
| 43 | else: |
| 44 | print "Test failed: " + str(resp.status_code) + ": " + resp.text |
| 45 | return False |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 46 | ''' |
| 47 | @method getAccountNum |
| 48 | @Returns AccountNumber for the subscriber |
| 49 | @params: jsonData is Dictionary |
| 50 | ''' |
| 51 | def getAccountNum(self, jsonData): |
| 52 | print type(str(jsonData['identity']['account_num'])) |
| 53 | return jsonData['identity']['account_num'] |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 54 | |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 55 | def getSubscriberId(self, jsonDataList, accountNum): |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 56 | ''' |
| 57 | Search in each json data in the given list to find and return the |
| 58 | subscriber id that corresponds to the given account number. |
| 59 | ''' |
| 60 | # Here we assume subscriber id starts from 1 |
| 61 | subscriberId = 0 |
| 62 | try: |
| 63 | for jsonData in jsonDataList: |
| 64 | if jsonData["identity"]["account_num"] == str(accountNum): |
| 65 | subscriberId = jsonData["id"] |
| 66 | break |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 67 | return str(subscriberId) |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 68 | except KeyError: |
| 69 | print "Something wrong with the json data provided: ", jsonData |
| 70 | return -1 |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 71 | ''' |
| 72 | Retrieve the correct jsonDict from the List of json objects returned |
| 73 | from Get Reponse |
| 74 | Account Number is the one used to post "Data" |
| 75 | ''' |
| 76 | def getJsonDictOfAcctNum(self, getResponseList, AccountNum): |
| 77 | getJsonDict = {} |
| 78 | try: |
| 79 | for data in getResponseList: |
| 80 | if data['identity']['account_num'] == AccountNum: |
| 81 | getJsonDict = data |
| 82 | break |
| 83 | return getJsonDict |
| 84 | except KeyError: |
| 85 | print "Could not find the related account number in Get Resonse Data" |
| 86 | return -1 |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 87 | |
| 88 | def ApiPost(self, key, jsonData): |
| 89 | url = self.getURL(key) |
| 90 | data = json.dumps(jsonData) |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 91 | print "url, data..", url, data |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 92 | resp = requests.post(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password)) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 93 | print "requests.codes.....",requests.codes.created |
| 94 | passed = self.checkResult(resp, requests.codes.created) or self.checkResult(resp, requests.codes.ok) |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 95 | return passed |
| 96 | |
| 97 | def ApiGet(self, key, urlSuffix=""): |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 98 | url = self.getURL(key) + str(urlSuffix) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 99 | print "get url...",url |
| 100 | resp = requests.get(url, auth=(self.user, self.password)) |
| 101 | passed = self.checkResult(resp, requests.codes.ok) |
| 102 | if not passed: |
| 103 | return None |
| 104 | else: |
| 105 | return resp.json() |
| 106 | |
| 107 | def ApiChameleonGet(self, key, urlSuffix=""): |
| 108 | url = self.getURL(key) + "/" + str(urlSuffix) |
| 109 | print "get url...",url |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 110 | resp = requests.get(url, auth=(self.user, self.password)) |
| 111 | passed = self.checkResult(resp, requests.codes.ok) |
| 112 | if not passed: |
| 113 | return None |
| 114 | else: |
| 115 | return resp.json() |
| 116 | |
| 117 | def ApiPut(self, key, jsonData, urlSuffix=""): |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 118 | print "urlSuffix....",type(urlSuffix) |
| 119 | url = self.getURL(key) + str(urlSuffix) + "/" |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 120 | data = json.dumps(jsonData) |
| 121 | resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password)) |
| 122 | passed = self.checkResult(resp, requests.codes.ok) |
| 123 | return passed |
| 124 | |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 125 | def ApiChameleonPut(self, key, jsonData, urlSuffix=""): |
| 126 | print "urlSuffix....",type(urlSuffix) |
| 127 | url = self.getURL(key) + "/" + str(urlSuffix) |
| 128 | print "url", url |
| 129 | data = json.dumps(jsonData) |
| 130 | resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password)) |
| 131 | passed = self.checkResult(resp, requests.codes.ok) |
| 132 | return passed |
| 133 | |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 134 | def ApiDelete(self, key, urlSuffix=""): |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 135 | url = self.getURL(key) + str(urlSuffix) |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 136 | print "url",url |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 137 | resp = requests.delete(url, auth=(self.user, self.password)) |
You Wang | 853c025 | 2017-05-17 13:28:31 -0700 | [diff] [blame] | 138 | passed = self.checkResult(resp, requests.codes.no_content) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 139 | return passed |
| 140 | |
| 141 | def ApiChameleonDelete(self, key, urlSuffix=""): |
| 142 | url = self.getURL(key) + "/" + str(urlSuffix) |
| 143 | print "url",url |
| 144 | resp = requests.delete(url, auth=(self.user, self.password)) |
| 145 | #passed = self.checkResult(resp, requests.codes.no_content) |
| 146 | passed = self.checkResult(resp, requests.codes.created) or self.checkResult(resp, requests.codes.ok) |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 147 | return passed |
| 148 | |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 149 | #test |
| 150 | ''' |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 151 | if __name__ == '__main__': |
| 152 | test = RestApi() |
| 153 | key = "TENANT_SUBSCRIBER" |
| 154 | account_num = 5 |
| 155 | result = test.ApiPost(key, {"identity":{"account_num":str(account_num)}}) |
| 156 | time.sleep(5) |
| 157 | result = test.ApiGet(key) |
| 158 | subId = test.getSubscriberIdFromAccountNum(result, account_num) |
| 159 | urlSuffix = str(subId) + "/" |
| 160 | time.sleep(5) |
| 161 | result = test.ApiPut(key, {"identity":{"name":"My House 2"}}, urlSuffix) |
| 162 | time.sleep(5) |
| 163 | result = test.ApiDelete(key, urlSuffix) |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 164 | ''' |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 165 | ''' |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 166 | test = restApi() |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 167 | #key = "UTILS_SYNCHRONIZER" |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 168 | #key = "CORE_USERS" |
| 169 | #key2 = "UTILS_LOGIN" |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 170 | #key = "TENANT_SUBSCRIBER" |
| 171 | #jsonGetData = test.ApiGet(key) |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 172 | #jsonResponse = test.ApiPost(key,{"identity":{"name":"My House 22"}}) |
| 173 | #jsonResponse = test.ApiPost(key,{"firstname":"Test002","lastname":"User002","email":"test002@onlab.us","password":"TestUser002","site": "http://localhost:8000/api/core/sites/1/"}) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 174 | key = "VOLT_TENANT" |
| 175 | key = "VOLT_SUBSCRIBER" |
| 176 | #jsonResponse = test.ApiDelete(key,204) |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 177 | #jsonResponse = test.ApiPut(key,{"firstname":"Test002","lastname":"User002","email":"test002update@onlab.us","password":"TestUser002","site": "http://localhost:8000/api/core/sites/1/"},14) |
| 178 | #jsonResponse = test.ApiPost(key2,{"username":"test002update@onlab.us","password":"TestUser002"}) |
| 179 | #jsonResponse = test.ApiPost(key2,{"username":"padmin@vicci.org","password":"letmein"}) |
| 180 | #jsonResponse = test.ApiPut(key,{"username":"testuser","password":"TestUser001"},"9") |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 181 | #key = "CORE_INSTANCES" |
| 182 | #key1 = "CORE_SANITY_SLICES" |
| 183 | #key2 = "CORE_SLICES" |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 184 | #input_dict = { "s_tag" : "111", "c_tag" : "222", "subscriber" : 23} |
Suchitra.Vemuri | 75dffd4 | 2016-12-20 15:35:25 -0800 | [diff] [blame] | 185 | input_dict = { |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 186 | "s_tag" : 117, |
| 187 | "c_tag" : 227 |
| 188 | } |
| 189 | |
| 190 | #input_dict1 = { "name" : "mysite_Test1", "site" : 1 , "creator" : 1} |
| 191 | input_dict2 = { |
| 192 | |
| 193 | "cdn_enable": "true", |
| 194 | "uplink_speed": 1000000000, |
| 195 | "downlink_speed": 1000000000, |
| 196 | "enable_uverse": "true", |
| 197 | "status": "enabled", |
| 198 | "service_specific_id": "100", |
| 199 | "name": "My House" |
| 200 | } |
| 201 | #jsonResponse = test.ApiPost(key,input_dict) |
| 202 | #jsonResponse = test.ApiChameleonPut(key,input_dict,12) |
| 203 | #jsonGetData = test.ApiGet(key,"/12") |
| 204 | #print "=========" |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 205 | #print jsonGetData |
Suchitra.Vemuri | 75dffd4 | 2016-12-20 15:35:25 -0800 | [diff] [blame] | 206 | #jsonEdit = test.ApiPut(key,{"c_tag" : "666","s_tag" : "123"},"30") |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 207 | jsonO = test.ApiDelete(key,"/7") |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 208 | #jsonResponse = test.ApiPut(key,{"identity":{"name":"My House 22"}},"71") |
| 209 | #jsonResponse = test.ApiPost(key,{"name":"test-2"}) |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 210 | #jsonResponse = test.ApiPut(key,{"name":"test1-changed"},"9") |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 211 | print "=========" |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 212 | ''' |