def createSparkConf(): from pyspark import SparkConf test_properties = conftest.test_properties() conf = SparkConf() conf.set("cloudant.host", test_properties["cloudanthost"]) conf.set("cloudant.username", test_properties["cloudantusername"]) conf.set("cloudant.password", test_properties["cloudantpassword"]) return conf
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #******************************************************************************/ import requests import os import json from helpers.dbutils import CloudantDbUtils import conftest # get the cloudant credentials from pytest config file test_properties = conftest.test_properties() class DataLoader: """ Test data loader related functions """ def getInputJsonFilepath(self, db_name): return os.path.join("resources", "schema_data", "{}.json".format(db_name)) def loadData(self): cloudantUtils = CloudantDbUtils(test_properties) for db in cloudantUtils.test_dbs: inputJsonFilePath = self.getInputJsonFilepath(db) if os.path.exists(inputJsonFilePath):
# distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #******************************************************************************/ import requests import sys import os import json from helpers.dbutils import CloudantDbUtils from helpers.acmeair_utils import AcmeAirUtils import conftest # get the cloudant credentials from pytest config file test_properties = conftest.test_properties() class DataLoader: """ Test data loader related functions """ def load_AcmeData(self, num_of_cust): """ Reset databases and use the AcmeAir database loader to populate initial customer, flight and airportmapping data. Does NOT generate user data like bookings. """ print ("num_of_cust: ", num_of_cust) acmeair = AcmeAirUtils()
def get_test_properties(): return conftest.test_properties()
class AcmeAirUtils: """ Test AcmeAir app related functions """ _api_context_root = "/rest/api" _app_host = "http://*****:*****@{}".format( self.test_properties["cloudantusername"], self.test_properties["cloudantpassword"], self.test_properties["cloudanthost"]) # start the acmeair app os.chdir(self.acmehome) command = ["node", "app.js"] self.proc = subprocess.Popen(command, env=new_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait at most 10 sec for app to come up timeout = time.time() + 10 while (True): if not self.is_acmeair_running() == 1: time.sleep(1) else: print("\nAcemAir started!") break if time.time() > timeout: raise RuntimeError("Cannot connect to AcmeAir!") def stop_acmeair(self): """ Stop the AcmeAir App that was started by this util """ if hasattr(self, "proc"): os.kill(self.proc.pid, signal.SIGTERM) print("AcmeAir is shutdown") def is_acmeair_running(self): """ Check if AcmeAir app is running Return 0: app is not running Return 1: app is running Return -1: app is running but malfunctioning, possibly because db were rebuilt when app was running """ r = requests.Session() url = "{}{}/{}".format(self._app_host, self._api_context_root, "config/countCustomers") try: response = r.get(url) if response.status_code == 200: status = 1 else: # happens when db were rebuilt while app is running status = -1 except: # happens when app is not running status = 0 return status def load_data(self, num_of_customers): """ Call the AcmeAir app REST API to populate with the given # of users """ cloudantUtils = CloudantDbUtils(self.test_properties) if cloudantUtils.get_doc_count("n_customer") > 0: raise RuntimeException( "Test databases already contains unknown data so AcmeAir data loader will not run!" ) r = requests.Session() url = "{}{}/{}".format(self._app_host, self._api_context_root, "loader/load") if isinstance(num_of_customers, int): num_of_customers = str(num_of_customers) param = {"numCustomers": num_of_customers} headers = {"Content-Type": self.FORM_CONTENT_TYPE} print("Start AcmeAir database loader with num_of_customers = ", num_of_customers) start_time = time.time() try: r.get(url, headers=headers, params=param) except requests.exceptions.ConnectionError: # the request aborts after 2 mins due to server timeout, wait until expected # of rows is reached cloudantUtils.wait_for_doc_count("n_customer", num_of_customers, int(num_of_customers) / 500) cloudantUtils.wait_for_doc_count("n_airportcodemapping", 14, 5) cloudantUtils.wait_for_doc_count("n_flightsegment", 395, 5) cloudantUtils.wait_for_doc_count("n_flight", 1971, 20) print("Database load completed after {} mins\n".format( int(round((time.time() - start_time) / 60)))) def login(self, user): """ Create a user session for the given user. It's needed in order to book a flight. @return session ID """ r = requests.Session() url = "{}{}/{}".format(self._app_host, self._api_context_root, "login") payload = {"login": user, "password": "******"} headers = {"Content-Type": self.FORM_CONTENT_TYPE} print("Login as user: "******"sessionid"] else: raise RuntimeError(response.text) def book_flights(self, user, toFlightId, retFlightId): """ Login as the given user and booking flights. Set retFlightId=None if booking one way. """ r = requests.Session() url = "{}{}/{}".format(self._app_host, self._api_context_root, "bookings/bookflights") headers = {"Content-Type": self.FORM_CONTENT_TYPE} payload = {"userid": user, "toFlightId": toFlightId} # see if it's round trip if retFlightId is None: payload["oneWayFlight"] = "true" else: payload["oneWayFlight"] = "false" payload["retFlightId"] = retFlightId # the request must include the cookie retrieved from login sessionId = self.login(user) cookies = {"sessionid": sessionId, "loggedinuser": user} print("Book flight(s) " + str(payload)) response = r.post(url, headers=headers, data=payload, cookies=cookies) if response.status_code == 200: print("\nFlight(s) booked: {}\n".format(response.text)) else: raise RuntimeError(response.text) def get_flightId_by_number(self, flightNum): """ Get the generated flight ID for the given flight number """ cloudantUtils = CloudantDbUtils(self.test_properties) url = "https://{}/{}".format( self.test_properties["cloudanthost"], "n_flight/_design/view/_search/n_flights?q=flightSegmentId:" + flightNum) param = {"q": "flightSegmentId:" + flightNum} response = cloudantUtils.r.get(url, params=param) data = response.json() if int(data["total_rows"]) > 0: # just get one from the dict return data["rows"][0]["id"] else: raise RuntimeError("n_flights has no data for ", flightNum)