*** Settings *** Documentation Démarre et arrête un serveur Mockoon pour simuler une API *** Keywords *** Start API mock [Tags] mockoon [Documentation] Démarrage de Mockoon et de la fausse API spécifiée, retourne le port du serveur [Arguments] ${specs_path} ${alias}=None # if the alias wasn't specified then create one from a hash of the specs path Run Keyword If "${alias}" == "None" ${alias} = run md5sum "${specs_path}" | awk '{print $1}' Log ${alias} ${tmpdir} = Set Variable ${EXECDIR}/../var/tmp ${mockoon_log_filename} = Set Variable mockoon-${alias}.log ${mockoon_log_filepath} = Set Variable ${tmpdir}/mockoon-${alias}.log # start Mockoon server and grab its PID ${handle} = Start Process npx mockoon-cli start --disable-admin-api --data ${specs_path} --port 0 ... cwd=${tmpdir} stdout=${mockoon_log_filename} stderr=STDOUT alias=${alias} Process Should Be Running ${alias} # get the PID ${mockoon_pid} = Get Process Id ${alias} # FAIL if no PID Run Keyword If "${mockoon_pid}" == "" FAIL Failed to get Mockoon PID for alias '${alias}' Wait Until Keyword Succeeds ${TIMEOUT} ${RETRY_INTERVAL} ... File Should Exist ${mockoon_log_filepath} Wait Until Keyword Succeeds ${TIMEOUT} ${RETRY_INTERVAL} ... File Should Not Be Empty ${mockoon_log_filepath} # find port number using log file ${log_first_line} = Run head -n 1 "${mockoon_log_filepath}" Log ${log_first_line} ${log_first_line_dict} = To Json ${log_first_line} Log ${log_first_line_dict} Should Match Regexp ${log_first_line_dict['message']} ^Server started on port ([0-9]+)$ ${log_port} = Replace String Using Regexp ${log_first_line_dict['message']} ^Server started on port ([0-9]+)$ \\1 Should Be Equal As Integers ${log_port} 0 # find port number using `lsof` ${p_infos} = Run lsof -F pgcnT -T s -g ${mockoon_pid} -a -i TCP -a -sTCP:LISTEN ${p_port_lines} = Get Lines Matching Regexp ${p_infos} ^n\\*:[0-9]+$ ${p_port_lines_count} = Get Line Count ${p_port_lines} Should Be Equal As Integers ${p_port_lines_count} 1 ${lsof_port} = Replace String Using Regexp ${p_port_lines} ^n\\*:([0-9]+)$ \\1 # the server real port is the one from `lsof` Should Not Be Equal As Integers ${lsof_port} ${log_port} ${mockoon_port} = Set Variable ${lsof_port} # return the port [Return] ${mockoon_port} Stop API mock [Tags] mockoon [Documentation] Arrêt de Mockoon et de la fausse API spécifiée [Arguments] ${specs_path}=None ${alias}=None Run Keyword If "${specs_path}" == "None" and "${alias}" == "None" ... FAIL No argument was specified, at least one is required. # if the alias wasn't specified then create one from a hash of the specs path Run Keyword If "${alias}" == "None" ${alias} = run md5sum "${specs_path}" | awk '{print $1}' # kill the Mockoon server ${running} = Is Process Running ${alias} Run Keyword If ${running} Terminate Process ${alias} Process Should Be Stopped ${alias}