| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700 |
- /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
- file Copyright.txt or https://cmake.org/licensing for details. */
- #include "cmCTestSubmitHandler.h"
- #include "cm_curl.h"
- #include "cm_jsoncpp_reader.h"
- #include "cm_jsoncpp_value.h"
- #include "cmsys/Process.h"
- #include <chrono>
- #include <cstring>
- #include <sstream>
- #include <stdio.h>
- #include <stdlib.h>
- #include "cmAlgorithms.h"
- #include "cmCTest.h"
- #include "cmCTestCurl.h"
- #include "cmCTestScriptHandler.h"
- #include "cmCryptoHash.h"
- #include "cmCurl.h"
- #include "cmDuration.h"
- #include "cmGeneratedFileStream.h"
- #include "cmProcessOutput.h"
- #include "cmState.h"
- #include "cmSystemTools.h"
- #include "cmThirdParty.h"
- #include "cmWorkingDirectory.h"
- #include "cmXMLParser.h"
- #include "cmake.h"
- #if defined(CTEST_USE_XMLRPC)
- # include "cmVersion.h"
- # include "cm_sys_stat.h"
- # include "cm_xmlrpc.h"
- #endif
- #define SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT 120
- typedef std::vector<char> cmCTestSubmitHandlerVectorOfChar;
- class cmCTestSubmitHandler::ResponseParser : public cmXMLParser
- {
- public:
- ResponseParser() { this->Status = STATUS_OK; }
- ~ResponseParser() override {}
- public:
- enum StatusType
- {
- STATUS_OK,
- STATUS_WARNING,
- STATUS_ERROR
- };
- StatusType Status;
- std::string Filename;
- std::string MD5;
- std::string Message;
- std::string BuildID;
- private:
- std::vector<char> CurrentValue;
- std::string GetCurrentValue()
- {
- std::string val;
- if (!this->CurrentValue.empty()) {
- val.assign(&this->CurrentValue[0], this->CurrentValue.size());
- }
- return val;
- }
- void StartElement(const std::string& /*name*/,
- const char** /*atts*/) override
- {
- this->CurrentValue.clear();
- }
- void CharacterDataHandler(const char* data, int length) override
- {
- this->CurrentValue.insert(this->CurrentValue.end(), data, data + length);
- }
- void EndElement(const std::string& name) override
- {
- if (name == "status") {
- std::string status = cmSystemTools::UpperCase(this->GetCurrentValue());
- if (status == "OK" || status == "SUCCESS") {
- this->Status = STATUS_OK;
- } else if (status == "WARNING") {
- this->Status = STATUS_WARNING;
- } else {
- this->Status = STATUS_ERROR;
- }
- } else if (name == "filename") {
- this->Filename = this->GetCurrentValue();
- } else if (name == "md5") {
- this->MD5 = this->GetCurrentValue();
- } else if (name == "message") {
- this->Message = this->GetCurrentValue();
- } else if (name == "buildId") {
- this->BuildID = this->GetCurrentValue();
- }
- }
- };
- static size_t cmCTestSubmitHandlerWriteMemoryCallback(void* ptr, size_t size,
- size_t nmemb, void* data)
- {
- int realsize = static_cast<int>(size * nmemb);
- cmCTestSubmitHandlerVectorOfChar* vec =
- static_cast<cmCTestSubmitHandlerVectorOfChar*>(data);
- const char* chPtr = static_cast<char*>(ptr);
- vec->insert(vec->end(), chPtr, chPtr + realsize);
- return realsize;
- }
- static size_t cmCTestSubmitHandlerCurlDebugCallback(CURL* /*unused*/,
- curl_infotype /*unused*/,
- char* chPtr, size_t size,
- void* data)
- {
- cmCTestSubmitHandlerVectorOfChar* vec =
- static_cast<cmCTestSubmitHandlerVectorOfChar*>(data);
- vec->insert(vec->end(), chPtr, chPtr + size);
- return size;
- }
- cmCTestSubmitHandler::cmCTestSubmitHandler()
- : HTTPProxy()
- , FTPProxy()
- {
- this->Initialize();
- }
- void cmCTestSubmitHandler::Initialize()
- {
- // We submit all available parts by default.
- for (cmCTest::Part p = cmCTest::PartStart; p != cmCTest::PartCount;
- p = cmCTest::Part(p + 1)) {
- this->SubmitPart[p] = true;
- }
- this->CDash = false;
- this->HasWarnings = false;
- this->HasErrors = false;
- this->Superclass::Initialize();
- this->HTTPProxy.clear();
- this->HTTPProxyType = 0;
- this->HTTPProxyAuth.clear();
- this->FTPProxy.clear();
- this->FTPProxyType = 0;
- this->LogFile = nullptr;
- this->Files.clear();
- }
- bool cmCTestSubmitHandler::SubmitUsingFTP(
- const std::string& localprefix, const std::vector<std::string>& files,
- const std::string& remoteprefix, const std::string& url)
- {
- CURL* curl;
- CURLcode res;
- FILE* ftpfile;
- char error_buffer[1024];
- /* In windows, this will init the winsock stuff */
- ::curl_global_init(CURL_GLOBAL_ALL);
- for (std::string const& file : files) {
- /* get a curl handle */
- curl = curl_easy_init();
- if (curl) {
- // Using proxy
- if (this->FTPProxyType > 0) {
- curl_easy_setopt(curl, CURLOPT_PROXY, this->FTPProxy.c_str());
- switch (this->FTPProxyType) {
- case 2:
- curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
- break;
- case 3:
- curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
- break;
- default:
- curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
- }
- }
- // enable uploading
- ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
- // if there is little to no activity for too long stop submitting
- ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
- ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME,
- SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT);
- ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
- std::string local_file = file;
- if (!cmSystemTools::FileExists(local_file)) {
- local_file = localprefix + "/" + file;
- }
- std::string upload_as =
- url + "/" + remoteprefix + cmSystemTools::GetFilenameName(file);
- if (!cmSystemTools::FileExists(local_file)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Cannot find file: " << local_file << std::endl);
- ::curl_easy_cleanup(curl);
- ::curl_global_cleanup();
- return false;
- }
- unsigned long filelen = cmSystemTools::FileLength(local_file);
- ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
- *this->LogFile << "\tUpload file: " << local_file << " to " << upload_as
- << std::endl;
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- " Upload file: " << local_file << " to "
- << upload_as << std::endl,
- this->Quiet);
- ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- // specify target
- ::curl_easy_setopt(curl, CURLOPT_URL, upload_as.c_str());
- // now specify which file to upload
- ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
- // and give the size of the upload (optional)
- ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(filelen));
- // and give curl the buffer for errors
- ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
- // specify handler for output
- ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
- cmCTestSubmitHandlerWriteMemoryCallback);
- ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
- cmCTestSubmitHandlerCurlDebugCallback);
- /* we pass our 'chunk' struct to the callback function */
- cmCTestSubmitHandlerVectorOfChar chunk;
- cmCTestSubmitHandlerVectorOfChar chunkDebug;
- ::curl_easy_setopt(curl, CURLOPT_FILE, &chunk);
- ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &chunkDebug);
- // Now run off and do what you've been told!
- res = ::curl_easy_perform(curl);
- if (!chunk.empty()) {
- cmCTestOptionalLog(this->CTest, DEBUG,
- "CURL output: ["
- << cmCTestLogWrite(&*chunk.begin(), chunk.size())
- << "]" << std::endl,
- this->Quiet);
- }
- if (!chunkDebug.empty()) {
- cmCTestOptionalLog(
- this->CTest, DEBUG,
- "CURL debug output: ["
- << cmCTestLogWrite(&*chunkDebug.begin(), chunkDebug.size()) << "]"
- << std::endl,
- this->Quiet);
- }
- fclose(ftpfile);
- if (res) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Error when uploading file: " << local_file
- << std::endl);
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Error message was: " << error_buffer << std::endl);
- *this->LogFile << " Error when uploading file: " << local_file
- << std::endl
- << " Error message was: " << error_buffer << std::endl
- << " Curl output was: ";
- // avoid dereference of empty vector
- if (!chunk.empty()) {
- *this->LogFile << cmCTestLogWrite(&*chunk.begin(), chunk.size());
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "CURL output: ["
- << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
- << std::endl);
- }
- *this->LogFile << std::endl;
- ::curl_easy_cleanup(curl);
- ::curl_global_cleanup();
- return false;
- }
- // always cleanup
- ::curl_easy_cleanup(curl);
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Uploaded: " + local_file << std::endl,
- this->Quiet);
- }
- }
- ::curl_global_cleanup();
- return true;
- }
- // Uploading files is simpler
- bool cmCTestSubmitHandler::SubmitUsingHTTP(
- const std::string& localprefix, const std::vector<std::string>& files,
- const std::string& remoteprefix, const std::string& url)
- {
- CURL* curl;
- CURLcode res;
- FILE* ftpfile;
- char error_buffer[1024];
- // Set Content-Type to satisfy fussy modsecurity rules.
- struct curl_slist* headers =
- ::curl_slist_append(nullptr, "Content-Type: text/xml");
- // Add any additional headers that the user specified.
- for (std::string const& h : this->HttpHeaders) {
- cmCTestOptionalLog(this->CTest, DEBUG,
- " Add HTTP Header: \"" << h << "\"" << std::endl,
- this->Quiet);
- headers = ::curl_slist_append(headers, h.c_str());
- }
- /* In windows, this will init the winsock stuff */
- ::curl_global_init(CURL_GLOBAL_ALL);
- std::string dropMethod(this->CTest->GetCTestConfiguration("DropMethod"));
- std::string curlopt(this->CTest->GetCTestConfiguration("CurlOptions"));
- std::vector<std::string> args;
- cmSystemTools::ExpandListArgument(curlopt, args);
- bool verifyPeerOff = false;
- bool verifyHostOff = false;
- for (std::string const& arg : args) {
- if (arg == "CURLOPT_SSL_VERIFYPEER_OFF") {
- verifyPeerOff = true;
- }
- if (arg == "CURLOPT_SSL_VERIFYHOST_OFF") {
- verifyHostOff = true;
- }
- }
- for (std::string const& file : files) {
- /* get a curl handle */
- curl = curl_easy_init();
- if (curl) {
- cmCurlSetCAInfo(curl);
- if (verifyPeerOff) {
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- " Set CURLOPT_SSL_VERIFYPEER to off\n",
- this->Quiet);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
- }
- if (verifyHostOff) {
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- " Set CURLOPT_SSL_VERIFYHOST to off\n",
- this->Quiet);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
- }
- // Using proxy
- if (this->HTTPProxyType > 0) {
- curl_easy_setopt(curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
- switch (this->HTTPProxyType) {
- case 2:
- curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
- break;
- case 3:
- curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
- break;
- default:
- curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
- if (!this->HTTPProxyAuth.empty()) {
- curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD,
- this->HTTPProxyAuth.c_str());
- }
- }
- }
- if (this->CTest->ShouldUseHTTP10()) {
- curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- }
- // enable HTTP ERROR parsing
- curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
- /* enable uploading */
- curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
- // if there is little to no activity for too long stop submitting
- ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
- ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME,
- SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT);
- /* HTTP PUT please */
- ::curl_easy_setopt(curl, CURLOPT_PUT, 1);
- ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- ::curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
- std::string local_file = file;
- bool initialize_cdash_buildid = false;
- if (!cmSystemTools::FileExists(local_file)) {
- local_file = localprefix + "/" + file;
- // If this file exists within the local Testing directory we assume
- // that it will be associated with the current build in CDash.
- initialize_cdash_buildid = true;
- }
- std::string remote_file =
- remoteprefix + cmSystemTools::GetFilenameName(file);
- *this->LogFile << "\tUpload file: " << local_file << " to "
- << remote_file << std::endl;
- std::string ofile;
- for (char c : remote_file) {
- char hexCh[4] = { 0, 0, 0, 0 };
- hexCh[0] = c;
- switch (c) {
- case '+':
- case '?':
- case '/':
- case '\\':
- case '&':
- case ' ':
- case '=':
- case '%':
- sprintf(hexCh, "%%%02X", static_cast<int>(c));
- ofile.append(hexCh);
- break;
- default:
- ofile.append(hexCh);
- }
- }
- std::string upload_as = url +
- ((url.find('?') == std::string::npos) ? '?' : '&') +
- "FileName=" + ofile;
- if (initialize_cdash_buildid) {
- // Provide extra arguments to CDash so that it can initialize and
- // return a buildid.
- cmCTestCurl ctest_curl(this->CTest);
- upload_as += "&build=";
- upload_as +=
- ctest_curl.Escape(this->CTest->GetCTestConfiguration("BuildName"));
- upload_as += "&site=";
- upload_as +=
- ctest_curl.Escape(this->CTest->GetCTestConfiguration("Site"));
- upload_as += "&stamp=";
- upload_as += ctest_curl.Escape(this->CTest->GetCurrentTag());
- upload_as += "-";
- upload_as += ctest_curl.Escape(this->CTest->GetTestModelString());
- cmCTestScriptHandler* ch = static_cast<cmCTestScriptHandler*>(
- this->CTest->GetHandler("script"));
- cmake* cm = ch->GetCMake();
- if (cm) {
- const char* subproject =
- cm->GetState()->GetGlobalProperty("SubProject");
- if (subproject) {
- upload_as += "&subproject=";
- upload_as += ctest_curl.Escape(subproject);
- }
- }
- }
- upload_as += "&MD5=";
- if (cmSystemTools::IsOn(this->GetOption("InternalTest"))) {
- upload_as += "bad_md5sum";
- } else {
- upload_as +=
- cmSystemTools::ComputeFileHash(local_file, cmCryptoHash::AlgoMD5);
- }
- if (!cmSystemTools::FileExists(local_file)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Cannot find file: " << local_file << std::endl);
- ::curl_easy_cleanup(curl);
- ::curl_slist_free_all(headers);
- ::curl_global_cleanup();
- return false;
- }
- unsigned long filelen = cmSystemTools::FileLength(local_file);
- ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- " Upload file: " << local_file << " to "
- << upload_as << " Size: "
- << filelen << std::endl,
- this->Quiet);
- // specify target
- ::curl_easy_setopt(curl, CURLOPT_URL, upload_as.c_str());
- // CURLAUTH_BASIC is default, and here we allow additional methods,
- // including more secure ones
- ::curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- // now specify which file to upload
- ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
- // and give the size of the upload (optional)
- ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(filelen));
- // and give curl the buffer for errors
- ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
- // specify handler for output
- ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
- cmCTestSubmitHandlerWriteMemoryCallback);
- ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
- cmCTestSubmitHandlerCurlDebugCallback);
- /* we pass our 'chunk' struct to the callback function */
- cmCTestSubmitHandlerVectorOfChar chunk;
- cmCTestSubmitHandlerVectorOfChar chunkDebug;
- ::curl_easy_setopt(curl, CURLOPT_FILE, &chunk);
- ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &chunkDebug);
- // Now run off and do what you've been told!
- res = ::curl_easy_perform(curl);
- if (!chunk.empty()) {
- cmCTestOptionalLog(this->CTest, DEBUG,
- "CURL output: ["
- << cmCTestLogWrite(&*chunk.begin(), chunk.size())
- << "]" << std::endl,
- this->Quiet);
- this->ParseResponse(chunk);
- }
- if (!chunkDebug.empty()) {
- cmCTestOptionalLog(
- this->CTest, DEBUG,
- "CURL debug output: ["
- << cmCTestLogWrite(&*chunkDebug.begin(), chunkDebug.size()) << "]"
- << std::endl,
- this->Quiet);
- }
- // If curl failed for any reason, or checksum fails, wait and retry
- //
- if (res != CURLE_OK || this->HasErrors) {
- std::string retryDelay = this->GetOption("RetryDelay") == nullptr
- ? ""
- : this->GetOption("RetryDelay");
- std::string retryCount = this->GetOption("RetryCount") == nullptr
- ? ""
- : this->GetOption("RetryCount");
- auto delay = cmDuration(
- retryDelay.empty()
- ? atoi(this->CTest->GetCTestConfiguration("CTestSubmitRetryDelay")
- .c_str())
- : atoi(retryDelay.c_str()));
- int count = retryCount.empty()
- ? atoi(this->CTest->GetCTestConfiguration("CTestSubmitRetryCount")
- .c_str())
- : atoi(retryCount.c_str());
- for (int i = 0; i < count; i++) {
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Submit failed, waiting " << delay.count()
- << " seconds...\n",
- this->Quiet);
- auto stop = std::chrono::steady_clock::now() + delay;
- while (std::chrono::steady_clock::now() < stop) {
- cmSystemTools::Delay(100);
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Retry submission: Attempt "
- << (i + 1) << " of " << count << std::endl,
- this->Quiet);
- ::fclose(ftpfile);
- ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
- ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
- chunk.clear();
- chunkDebug.clear();
- this->HasErrors = false;
- res = ::curl_easy_perform(curl);
- if (!chunk.empty()) {
- cmCTestOptionalLog(
- this->CTest, DEBUG,
- "CURL output: ["
- << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
- << std::endl,
- this->Quiet);
- this->ParseResponse(chunk);
- }
- if (res == CURLE_OK && !this->HasErrors) {
- break;
- }
- }
- }
- fclose(ftpfile);
- if (res) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Error when uploading file: " << local_file
- << std::endl);
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Error message was: " << error_buffer << std::endl);
- *this->LogFile << " Error when uploading file: " << local_file
- << std::endl
- << " Error message was: " << error_buffer
- << std::endl;
- // avoid deref of begin for zero size array
- if (!chunk.empty()) {
- *this->LogFile << " Curl output was: "
- << cmCTestLogWrite(&*chunk.begin(), chunk.size())
- << std::endl;
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "CURL output: ["
- << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
- << std::endl);
- }
- ::curl_easy_cleanup(curl);
- ::curl_slist_free_all(headers);
- ::curl_global_cleanup();
- return false;
- }
- // always cleanup
- ::curl_easy_cleanup(curl);
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Uploaded: " + local_file << std::endl,
- this->Quiet);
- }
- }
- ::curl_slist_free_all(headers);
- ::curl_global_cleanup();
- return true;
- }
- void cmCTestSubmitHandler::ParseResponse(
- cmCTestSubmitHandlerVectorOfChar chunk)
- {
- std::string output;
- output.append(chunk.begin(), chunk.end());
- if (output.find("<cdash") != std::string::npos) {
- ResponseParser parser;
- parser.Parse(output.c_str());
- if (parser.Status != ResponseParser::STATUS_OK) {
- this->HasErrors = true;
- cmCTestLog(this->CTest, HANDLER_OUTPUT,
- " Submission failed: " << parser.Message << std::endl);
- return;
- }
- this->CTest->SetBuildID(parser.BuildID);
- }
- output = cmSystemTools::UpperCase(output);
- if (output.find("WARNING") != std::string::npos) {
- this->HasWarnings = true;
- }
- if (output.find("ERROR") != std::string::npos) {
- this->HasErrors = true;
- }
- if (this->HasWarnings || this->HasErrors) {
- cmCTestLog(this->CTest, HANDLER_OUTPUT,
- " Server Response:\n"
- << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "\n");
- }
- }
- bool cmCTestSubmitHandler::TriggerUsingHTTP(
- const std::vector<std::string>& files, const std::string& remoteprefix,
- const std::string& url)
- {
- CURL* curl;
- char error_buffer[1024];
- /* In windows, this will init the winsock stuff */
- ::curl_global_init(CURL_GLOBAL_ALL);
- for (std::string const& file : files) {
- /* get a curl handle */
- curl = curl_easy_init();
- if (curl) {
- // Using proxy
- if (this->HTTPProxyType > 0) {
- curl_easy_setopt(curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
- switch (this->HTTPProxyType) {
- case 2:
- curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
- break;
- case 3:
- curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
- break;
- default:
- curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
- if (!this->HTTPProxyAuth.empty()) {
- curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD,
- this->HTTPProxyAuth.c_str());
- }
- }
- }
- ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- // and give curl the buffer for errors
- ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
- // specify handler for output
- ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
- cmCTestSubmitHandlerWriteMemoryCallback);
- ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
- cmCTestSubmitHandlerCurlDebugCallback);
- /* we pass our 'chunk' struct to the callback function */
- cmCTestSubmitHandlerVectorOfChar chunk;
- cmCTestSubmitHandlerVectorOfChar chunkDebug;
- ::curl_easy_setopt(curl, CURLOPT_FILE, &chunk);
- ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &chunkDebug);
- std::string rfile = remoteprefix + cmSystemTools::GetFilenameName(file);
- std::string ofile;
- for (char c : rfile) {
- char hexCh[4] = { 0, 0, 0, 0 };
- hexCh[0] = c;
- switch (c) {
- case '+':
- case '?':
- case '/':
- case '\\':
- case '&':
- case ' ':
- case '=':
- case '%':
- sprintf(hexCh, "%%%02X", static_cast<int>(c));
- ofile.append(hexCh);
- break;
- default:
- ofile.append(hexCh);
- }
- }
- std::string turl = url +
- ((url.find('?') == std::string::npos) ? '?' : '&') +
- "xmlfile=" + ofile;
- *this->LogFile << "Trigger url: " << turl << std::endl;
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- " Trigger url: " << turl << std::endl, this->Quiet);
- curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- curl_easy_setopt(curl, CURLOPT_URL, turl.c_str());
- if (curl_easy_perform(curl)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Error when triggering: " << turl << std::endl);
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Error message was: " << error_buffer << std::endl);
- *this->LogFile << "\tTriggering failed with error: " << error_buffer
- << std::endl
- << " Error message was: " << error_buffer
- << std::endl;
- if (!chunk.empty()) {
- *this->LogFile << " Curl output was: "
- << cmCTestLogWrite(&*chunk.begin(), chunk.size())
- << std::endl;
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "CURL output: ["
- << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
- << std::endl);
- }
- ::curl_easy_cleanup(curl);
- ::curl_global_cleanup();
- return false;
- }
- if (!chunk.empty()) {
- cmCTestOptionalLog(this->CTest, DEBUG,
- "CURL output: ["
- << cmCTestLogWrite(&*chunk.begin(), chunk.size())
- << "]" << std::endl,
- this->Quiet);
- }
- if (!chunkDebug.empty()) {
- cmCTestOptionalLog(
- this->CTest, DEBUG,
- "CURL debug output: ["
- << cmCTestLogWrite(&*chunkDebug.begin(), chunkDebug.size()) << "]"
- << std::endl,
- this->Quiet);
- }
- // always cleanup
- ::curl_easy_cleanup(curl);
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl,
- this->Quiet);
- }
- }
- ::curl_global_cleanup();
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Dart server triggered..." << std::endl, this->Quiet);
- return true;
- }
- bool cmCTestSubmitHandler::SubmitUsingSCP(
- const std::string& scp_command, const std::string& localprefix,
- const std::vector<std::string>& files, const std::string& remoteprefix,
- const std::string& url)
- {
- if (scp_command.empty() || localprefix.empty() || files.empty() ||
- remoteprefix.empty() || url.empty()) {
- return false;
- }
- std::vector<const char*> argv;
- argv.push_back(scp_command.c_str()); // Scp command
- argv.push_back(scp_command.c_str()); // Dummy string for file
- argv.push_back(scp_command.c_str()); // Dummy string for remote url
- argv.push_back(nullptr);
- cmsysProcess* cp = cmsysProcess_New();
- cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
- // cmsysProcess_SetTimeout(cp, timeout);
- int problems = 0;
- for (std::string const& file : files) {
- int retVal;
- std::string lfname = localprefix;
- cmSystemTools::ConvertToUnixSlashes(lfname);
- lfname += "/" + file;
- lfname = cmSystemTools::ConvertToOutputPath(lfname);
- argv[1] = lfname.c_str();
- std::string rfname = url + "/" + remoteprefix + file;
- argv[2] = rfname.c_str();
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- "Execute \"" << argv[0] << "\" \"" << argv[1] << "\" \""
- << argv[2] << "\"" << std::endl,
- this->Quiet);
- *this->LogFile << "Execute \"" << argv[0] << "\" \"" << argv[1] << "\" \""
- << argv[2] << "\"" << std::endl;
- cmsysProcess_SetCommand(cp, &*argv.begin());
- cmsysProcess_Execute(cp);
- char* data;
- int length;
- cmProcessOutput processOutput;
- std::string strdata;
- while (cmsysProcess_WaitForData(cp, &data, &length, nullptr)) {
- processOutput.DecodeText(data, length, strdata);
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- cmCTestLogWrite(strdata.c_str(), strdata.size()),
- this->Quiet);
- }
- processOutput.DecodeText(std::string(), strdata);
- if (!strdata.empty()) {
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- cmCTestLogWrite(strdata.c_str(), strdata.size()),
- this->Quiet);
- }
- cmsysProcess_WaitForExit(cp, nullptr);
- int result = cmsysProcess_GetState(cp);
- if (result == cmsysProcess_State_Exited) {
- retVal = cmsysProcess_GetExitValue(cp);
- if (retVal != 0) {
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- "\tSCP returned: " << retVal << std::endl,
- this->Quiet);
- *this->LogFile << "\tSCP returned: " << retVal << std::endl;
- problems++;
- }
- } else if (result == cmsysProcess_State_Exception) {
- retVal = cmsysProcess_GetExitException(cp);
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "\tThere was an exception: " << retVal << std::endl);
- *this->LogFile << "\tThere was an exception: " << retVal << std::endl;
- problems++;
- } else if (result == cmsysProcess_State_Expired) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "\tThere was a timeout" << std::endl);
- *this->LogFile << "\tThere was a timeout" << std::endl;
- problems++;
- } else if (result == cmsysProcess_State_Error) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "\tError executing SCP: " << cmsysProcess_GetErrorString(cp)
- << std::endl);
- *this->LogFile << "\tError executing SCP: "
- << cmsysProcess_GetErrorString(cp) << std::endl;
- problems++;
- }
- }
- cmsysProcess_Delete(cp);
- return problems == 0;
- }
- bool cmCTestSubmitHandler::SubmitUsingCP(const std::string& localprefix,
- const std::vector<std::string>& files,
- const std::string& remoteprefix,
- const std::string& destination)
- {
- if (localprefix.empty() || files.empty() || remoteprefix.empty() ||
- destination.empty()) {
- /* clang-format off */
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "Missing arguments for submit via cp:\n"
- << "\tlocalprefix: " << localprefix << "\n"
- << "\tNumber of files: " << files.size() << "\n"
- << "\tremoteprefix: " << remoteprefix << "\n"
- << "\tdestination: " << destination << std::endl);
- /* clang-format on */
- return false;
- }
- for (std::string const& file : files) {
- std::string lfname = localprefix;
- cmSystemTools::ConvertToUnixSlashes(lfname);
- lfname += "/" + file;
- std::string rfname = destination + "/" + remoteprefix + file;
- cmSystemTools::CopyFileAlways(lfname, rfname);
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- " Copy file: " << lfname << " to " << rfname
- << std::endl,
- this->Quiet);
- }
- std::string tagDoneFile = destination + "/" + remoteprefix + "DONE";
- cmSystemTools::Touch(tagDoneFile, true);
- return true;
- }
- #if defined(CTEST_USE_XMLRPC)
- bool cmCTestSubmitHandler::SubmitUsingXMLRPC(
- const std::string& localprefix, const std::vector<std::string>& files,
- const std::string& remoteprefix, const std::string& url)
- {
- xmlrpc_env env;
- char ctestString[] = "CTest";
- std::string ctestVersionString = cmVersion::GetCMakeVersion();
- char* ctestVersion = const_cast<char*>(ctestVersionString.c_str());
- std::string realURL = url + "/" + remoteprefix + "/Command/";
- /* Start up our XML-RPC client library. */
- xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, ctestString, ctestVersion);
- /* Initialize our error-handling environment. */
- xmlrpc_env_init(&env);
- /* Call the famous server at UserLand. */
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Submitting to: " << realURL << " (" << remoteprefix
- << ")" << std::endl,
- this->Quiet);
- for (std::string const& file : files) {
- xmlrpc_value* result;
- std::string local_file = file;
- if (!cmSystemTools::FileExists(local_file)) {
- local_file = localprefix + "/" + file;
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Submit file: " << local_file << std::endl,
- this->Quiet);
- struct stat st;
- if (::stat(local_file.c_str(), &st)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Cannot find file: " << local_file << std::endl);
- return false;
- }
- // off_t can be bigger than size_t. fread takes size_t.
- // make sure the file is not too big.
- if (static_cast<off_t>(static_cast<size_t>(st.st_size)) !=
- static_cast<off_t>(st.st_size)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " File too big: " << local_file << std::endl);
- return false;
- }
- size_t fileSize = static_cast<size_t>(st.st_size);
- FILE* fp = cmsys::SystemTools::Fopen(local_file, "rb");
- if (!fp) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Cannot open file: " << local_file << std::endl);
- return false;
- }
- unsigned char* fileBuffer = new unsigned char[fileSize];
- if (fread(fileBuffer, 1, fileSize, fp) != fileSize) {
- delete[] fileBuffer;
- fclose(fp);
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Cannot read file: " << local_file << std::endl);
- return false;
- }
- fclose(fp);
- char remoteCommand[] = "Submit.put";
- char* pRealURL = const_cast<char*>(realURL.c_str());
- result =
- xmlrpc_client_call(&env, pRealURL, remoteCommand, "(6)", fileBuffer,
- static_cast<xmlrpc_int32>(fileSize));
- delete[] fileBuffer;
- if (env.fault_occurred) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Submission problem: " << env.fault_string << " ("
- << env.fault_code << ")"
- << std::endl);
- xmlrpc_env_clean(&env);
- xmlrpc_client_cleanup();
- return false;
- }
- /* Dispose of our result value. */
- xmlrpc_DECREF(result);
- }
- /* Clean up our error-handling environment. */
- xmlrpc_env_clean(&env);
- /* Shutdown our XML-RPC client library. */
- xmlrpc_client_cleanup();
- return true;
- }
- #else
- bool cmCTestSubmitHandler::SubmitUsingXMLRPC(
- std::string const& /*unused*/, std::vector<std::string> const& /*unused*/,
- std::string const& /*unused*/, std::string const& /*unused*/)
- {
- return false;
- }
- #endif
- void cmCTestSubmitHandler::ConstructCDashURL(std::string& dropMethod,
- std::string& url)
- {
- dropMethod = this->CTest->GetCTestConfiguration("DropMethod");
- url = dropMethod;
- url += "://";
- if (!this->CTest->GetCTestConfiguration("DropSiteUser").empty()) {
- url += this->CTest->GetCTestConfiguration("DropSiteUser");
- cmCTestOptionalLog(
- this->CTest, HANDLER_OUTPUT,
- this->CTest->GetCTestConfiguration("DropSiteUser").c_str(), this->Quiet);
- if (!this->CTest->GetCTestConfiguration("DropSitePassword").empty()) {
- url += ":" + this->CTest->GetCTestConfiguration("DropSitePassword");
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, ":******", this->Quiet);
- }
- url += "@";
- }
- url += this->CTest->GetCTestConfiguration("DropSite") +
- this->CTest->GetCTestConfiguration("DropLocation");
- }
- int cmCTestSubmitHandler::HandleCDashUploadFile(std::string const& file,
- std::string const& typeString)
- {
- if (file.empty()) {
- cmCTestLog(this->CTest, ERROR_MESSAGE, "Upload file not specified\n");
- return -1;
- }
- if (!cmSystemTools::FileExists(file)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "Upload file not found: '" << file << "'\n");
- return -1;
- }
- cmCTestCurl curl(this->CTest);
- curl.SetQuiet(this->Quiet);
- std::string curlopt(this->CTest->GetCTestConfiguration("CurlOptions"));
- std::vector<std::string> args;
- cmSystemTools::ExpandListArgument(curlopt, args);
- curl.SetCurlOptions(args);
- curl.SetTimeOutSeconds(SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT);
- curl.SetHttpHeaders(this->HttpHeaders);
- std::string dropMethod;
- std::string url;
- this->ConstructCDashURL(dropMethod, url);
- std::string::size_type pos = url.find("submit.php?");
- url = url.substr(0, pos + 10);
- if (!(dropMethod == "http" || dropMethod == "https")) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "Only http and https are supported for CDASH_UPLOAD\n");
- return -1;
- }
- bool internalTest = cmSystemTools::IsOn(this->GetOption("InternalTest"));
- // Get RETRY_COUNT and RETRY_DELAY values if they were set.
- std::string retryDelayString = this->GetOption("RetryDelay") == nullptr
- ? ""
- : this->GetOption("RetryDelay");
- std::string retryCountString = this->GetOption("RetryCount") == nullptr
- ? ""
- : this->GetOption("RetryCount");
- auto retryDelay = std::chrono::seconds(0);
- if (!retryDelayString.empty()) {
- unsigned long retryDelayValue = 0;
- if (!cmSystemTools::StringToULong(retryDelayString.c_str(),
- &retryDelayValue)) {
- cmCTestLog(this->CTest, WARNING,
- "Invalid value for 'RETRY_DELAY' : " << retryDelayString
- << std::endl);
- } else {
- retryDelay = std::chrono::seconds(retryDelayValue);
- }
- }
- unsigned long retryCount = 0;
- if (!retryCountString.empty()) {
- if (!cmSystemTools::StringToULong(retryCountString.c_str(), &retryCount)) {
- cmCTestLog(this->CTest, WARNING,
- "Invalid value for 'RETRY_DELAY' : " << retryCountString
- << std::endl);
- }
- }
- std::string md5sum =
- cmSystemTools::ComputeFileHash(file, cmCryptoHash::AlgoMD5);
- // 1. request the buildid and check to see if the file
- // has already been uploaded
- // TODO I added support for subproject. You would need to add
- // a "&subproject=subprojectname" to the first POST.
- cmCTestScriptHandler* ch =
- static_cast<cmCTestScriptHandler*>(this->CTest->GetHandler("script"));
- cmake* cm = ch->GetCMake();
- const char* subproject = cm->GetState()->GetGlobalProperty("SubProject");
- // TODO: Encode values for a URL instead of trusting caller.
- std::ostringstream str;
- str << "project="
- << curl.Escape(this->CTest->GetCTestConfiguration("ProjectName")) << "&";
- if (subproject) {
- str << "subproject=" << curl.Escape(subproject) << "&";
- }
- auto timeNow =
- std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
- str << "stamp=" << curl.Escape(this->CTest->GetCurrentTag()) << "-"
- << curl.Escape(this->CTest->GetTestModelString()) << "&"
- << "model=" << curl.Escape(this->CTest->GetTestModelString()) << "&"
- << "build="
- << curl.Escape(this->CTest->GetCTestConfiguration("BuildName")) << "&"
- << "site=" << curl.Escape(this->CTest->GetCTestConfiguration("Site"))
- << "&"
- << "track=" << curl.Escape(this->CTest->GetTestModelString()) << "&"
- << "starttime=" << timeNow << "&"
- << "endtime=" << timeNow << "&"
- << "datafilesmd5[0]=" << md5sum << "&"
- << "type=" << curl.Escape(typeString);
- std::string fields = str.str();
- cmCTestOptionalLog(this->CTest, DEBUG,
- "fields: " << fields << "\nurl:" << url
- << "\nfile: " << file << "\n",
- this->Quiet);
- std::string response;
- bool requestSucceeded = curl.HttpRequest(url, fields, response);
- if (!internalTest && !requestSucceeded) {
- // If request failed, wait and retry.
- for (unsigned long i = 0; i < retryCount; i++) {
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Request failed, waiting " << retryDelay.count()
- << " seconds...\n",
- this->Quiet);
- auto stop = std::chrono::steady_clock::now() + retryDelay;
- while (std::chrono::steady_clock::now() < stop) {
- cmSystemTools::Delay(100);
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Retry request: Attempt "
- << (i + 1) << " of " << retryCount << std::endl,
- this->Quiet);
- requestSucceeded = curl.HttpRequest(url, fields, response);
- if (requestSucceeded) {
- break;
- }
- }
- }
- if (!internalTest && !requestSucceeded) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "Error in HttpRequest\n"
- << response);
- return -1;
- }
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- "Request upload response: [" << response << "]\n",
- this->Quiet);
- Json::Value json;
- Json::Reader reader;
- if (!internalTest && !reader.parse(response, json)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "error parsing json string ["
- << response << "]\n"
- << reader.getFormattedErrorMessages() << "\n");
- return -1;
- }
- if (!internalTest && json["status"].asInt() != 0) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "Bad status returned from CDash: " << json["status"].asInt());
- return -1;
- }
- if (!internalTest) {
- if (json["datafilesmd5"].isArray()) {
- int datares = json["datafilesmd5"][0].asInt();
- if (datares == 1) {
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- "File already exists on CDash, skip upload "
- << file << "\n",
- this->Quiet);
- return 0;
- }
- } else {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "bad datafilesmd5 value in response " << response << "\n");
- return -1;
- }
- }
- std::string upload_as = cmSystemTools::GetFilenameName(file);
- std::ostringstream fstr;
- fstr << "type=" << curl.Escape(typeString) << "&"
- << "md5=" << md5sum << "&"
- << "filename=" << curl.Escape(upload_as) << "&"
- << "buildid=" << json["buildid"].asString();
- bool uploadSucceeded = false;
- if (!internalTest) {
- uploadSucceeded = curl.UploadFile(file, url, fstr.str(), response);
- }
- if (!uploadSucceeded) {
- // If upload failed, wait and retry.
- for (unsigned long i = 0; i < retryCount; i++) {
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Upload failed, waiting " << retryDelay.count()
- << " seconds...\n",
- this->Quiet);
- auto stop = std::chrono::steady_clock::now() + retryDelay;
- while (std::chrono::steady_clock::now() < stop) {
- cmSystemTools::Delay(100);
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Retry upload: Attempt "
- << (i + 1) << " of " << retryCount << std::endl,
- this->Quiet);
- if (!internalTest) {
- uploadSucceeded = curl.UploadFile(file, url, fstr.str(), response);
- }
- if (uploadSucceeded) {
- break;
- }
- }
- }
- if (!uploadSucceeded) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "error uploading to CDash. " << file << " " << url << " "
- << fstr.str());
- return -1;
- }
- if (!reader.parse(response, json)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "error parsing json string ["
- << response << "]\n"
- << reader.getFormattedErrorMessages() << "\n");
- return -1;
- }
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- "Upload file response: [" << response << "]\n",
- this->Quiet);
- return 0;
- }
- int cmCTestSubmitHandler::ProcessHandler()
- {
- const char* cdashUploadFile = this->GetOption("CDashUploadFile");
- const char* cdashUploadType = this->GetOption("CDashUploadType");
- if (cdashUploadFile && cdashUploadType) {
- return this->HandleCDashUploadFile(cdashUploadFile, cdashUploadType);
- }
- std::string iscdash = this->CTest->GetCTestConfiguration("IsCDash");
- // cdash does not need to trigger so just return true
- if (!iscdash.empty()) {
- this->CDash = true;
- }
- const std::string& buildDirectory =
- this->CTest->GetCTestConfiguration("BuildDirectory");
- if (buildDirectory.empty()) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- "Cannot find BuildDirectory key in the DartConfiguration.tcl"
- << std::endl);
- return -1;
- }
- if (getenv("HTTP_PROXY")) {
- this->HTTPProxyType = 1;
- this->HTTPProxy = getenv("HTTP_PROXY");
- if (getenv("HTTP_PROXY_PORT")) {
- this->HTTPProxy += ":";
- this->HTTPProxy += getenv("HTTP_PROXY_PORT");
- }
- if (getenv("HTTP_PROXY_TYPE")) {
- std::string type = getenv("HTTP_PROXY_TYPE");
- // HTTP/SOCKS4/SOCKS5
- if (type == "HTTP") {
- this->HTTPProxyType = 1;
- } else if (type == "SOCKS4") {
- this->HTTPProxyType = 2;
- } else if (type == "SOCKS5") {
- this->HTTPProxyType = 3;
- }
- }
- if (getenv("HTTP_PROXY_USER")) {
- this->HTTPProxyAuth = getenv("HTTP_PROXY_USER");
- }
- if (getenv("HTTP_PROXY_PASSWD")) {
- this->HTTPProxyAuth += ":";
- this->HTTPProxyAuth += getenv("HTTP_PROXY_PASSWD");
- }
- }
- if (getenv("FTP_PROXY")) {
- this->FTPProxyType = 1;
- this->FTPProxy = getenv("FTP_PROXY");
- if (getenv("FTP_PROXY_PORT")) {
- this->FTPProxy += ":";
- this->FTPProxy += getenv("FTP_PROXY_PORT");
- }
- if (getenv("FTP_PROXY_TYPE")) {
- std::string type = getenv("FTP_PROXY_TYPE");
- // HTTP/SOCKS4/SOCKS5
- if (type == "HTTP") {
- this->FTPProxyType = 1;
- } else if (type == "SOCKS4") {
- this->FTPProxyType = 2;
- } else if (type == "SOCKS5") {
- this->FTPProxyType = 3;
- }
- }
- }
- if (!this->HTTPProxy.empty()) {
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Use HTTP Proxy: " << this->HTTPProxy << std::endl,
- this->Quiet);
- }
- if (!this->FTPProxy.empty()) {
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Use FTP Proxy: " << this->FTPProxy << std::endl,
- this->Quiet);
- }
- cmGeneratedFileStream ofs;
- this->StartLogFile("Submit", ofs);
- std::vector<std::string> files;
- std::string prefix = this->GetSubmitResultsPrefix();
- if (!this->Files.empty()) {
- // Submit the explicitly selected files:
- //
- files.insert(files.end(), this->Files.begin(), this->Files.end());
- }
- // Add to the list of files to submit from any selected, existing parts:
- //
- // TODO:
- // Check if test is enabled
- this->CTest->AddIfExists(cmCTest::PartUpdate, "Update.xml");
- this->CTest->AddIfExists(cmCTest::PartConfigure, "Configure.xml");
- this->CTest->AddIfExists(cmCTest::PartBuild, "Build.xml");
- this->CTest->AddIfExists(cmCTest::PartTest, "Test.xml");
- if (this->CTest->AddIfExists(cmCTest::PartCoverage, "Coverage.xml")) {
- std::vector<std::string> gfiles;
- std::string gpath =
- buildDirectory + "/Testing/" + this->CTest->GetCurrentTag();
- std::string::size_type glen = gpath.size() + 1;
- gpath = gpath + "/CoverageLog*";
- cmCTestOptionalLog(this->CTest, DEBUG,
- "Globbing for: " << gpath << std::endl, this->Quiet);
- if (cmSystemTools::SimpleGlob(gpath, gfiles, 1)) {
- for (std::string& gfile : gfiles) {
- gfile = gfile.substr(glen);
- cmCTestOptionalLog(this->CTest, DEBUG,
- "Glob file: " << gfile << std::endl, this->Quiet);
- this->CTest->AddSubmitFile(cmCTest::PartCoverage, gfile.c_str());
- }
- } else {
- cmCTestLog(this->CTest, ERROR_MESSAGE, "Problem globbing" << std::endl);
- }
- }
- this->CTest->AddIfExists(cmCTest::PartMemCheck, "DynamicAnalysis.xml");
- this->CTest->AddIfExists(cmCTest::PartMemCheck, "Purify.xml");
- this->CTest->AddIfExists(cmCTest::PartNotes, "Notes.xml");
- this->CTest->AddIfExists(cmCTest::PartUpload, "Upload.xml");
- // Query parts for files to submit.
- for (cmCTest::Part p = cmCTest::PartStart; p != cmCTest::PartCount;
- p = cmCTest::Part(p + 1)) {
- // Skip parts we are not submitting.
- if (!this->SubmitPart[p]) {
- continue;
- }
- // Submit files from this part.
- std::vector<std::string> const& pfiles = this->CTest->GetSubmitFiles(p);
- files.insert(files.end(), pfiles.begin(), pfiles.end());
- }
- // Make sure files are unique, but preserve order.
- {
- // This endPos intermediate is needed to work around non-conformant C++11
- // standard libraries that have erase(iterator,iterator) instead of
- // erase(const_iterator,const_iterator).
- size_t endPos = cmRemoveDuplicates(files) - files.cbegin();
- files.erase(files.begin() + endPos, files.end());
- }
- // Submit Done.xml last
- if (this->SubmitPart[cmCTest::PartDone]) {
- this->CTest->GenerateDoneFile();
- files.push_back("Done.xml");
- }
- if (ofs) {
- ofs << "Upload files:" << std::endl;
- int cnt = 0;
- for (std::string const& file : files) {
- ofs << cnt << "\t" << file << std::endl;
- cnt++;
- }
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- "Submit files (using "
- << this->CTest->GetCTestConfiguration("DropMethod")
- << ")" << std::endl,
- this->Quiet);
- const char* specificTrack = this->CTest->GetSpecificTrack();
- if (specificTrack) {
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Send to track: " << specificTrack << std::endl,
- this->Quiet);
- }
- this->SetLogFile(&ofs);
- std::string dropMethod(this->CTest->GetCTestConfiguration("DropMethod"));
- if (dropMethod.empty() || dropMethod == "ftp") {
- ofs << "Using drop method: FTP" << std::endl;
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Using FTP submit method" << std::endl
- << " Drop site: ftp://",
- this->Quiet);
- std::string url = "ftp://";
- url += cmCTest::MakeURLSafe(
- this->CTest->GetCTestConfiguration("DropSiteUser")) +
- ":" +
- cmCTest::MakeURLSafe(
- this->CTest->GetCTestConfiguration("DropSitePassword")) +
- "@" + this->CTest->GetCTestConfiguration("DropSite") +
- cmCTest::MakeURLSafe(this->CTest->GetCTestConfiguration("DropLocation"));
- if (!this->CTest->GetCTestConfiguration("DropSiteUser").empty()) {
- cmCTestOptionalLog(
- this->CTest, HANDLER_OUTPUT,
- this->CTest->GetCTestConfiguration("DropSiteUser").c_str(),
- this->Quiet);
- if (!this->CTest->GetCTestConfiguration("DropSitePassword").empty()) {
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, ":******",
- this->Quiet);
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "@", this->Quiet);
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- this->CTest->GetCTestConfiguration("DropSite")
- << this->CTest->GetCTestConfiguration("DropLocation")
- << std::endl,
- this->Quiet);
- if (!this->SubmitUsingFTP(buildDirectory + "/Testing/" +
- this->CTest->GetCurrentTag(),
- files, prefix, url)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Problems when submitting via FTP" << std::endl);
- ofs << " Problems when submitting via FTP" << std::endl;
- return -1;
- }
- if (!this->CDash) {
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Using HTTP trigger method"
- << std::endl
- << " Trigger site: "
- << this->CTest->GetCTestConfiguration("TriggerSite")
- << std::endl,
- this->Quiet);
- if (!this->TriggerUsingHTTP(
- files, prefix,
- this->CTest->GetCTestConfiguration("TriggerSite"))) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Problems when triggering via HTTP" << std::endl);
- ofs << " Problems when triggering via HTTP" << std::endl;
- return -1;
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Submission successful" << std::endl, this->Quiet);
- ofs << " Submission successful" << std::endl;
- return 0;
- }
- } else if (dropMethod == "http" || dropMethod == "https") {
- std::string url = dropMethod;
- url += "://";
- ofs << "Using drop method: " << dropMethod << std::endl;
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Using HTTP submit method" << std::endl
- << " Drop site:" << url,
- this->Quiet);
- if (!this->CTest->GetCTestConfiguration("DropSiteUser").empty()) {
- url += this->CTest->GetCTestConfiguration("DropSiteUser");
- cmCTestOptionalLog(
- this->CTest, HANDLER_OUTPUT,
- this->CTest->GetCTestConfiguration("DropSiteUser").c_str(),
- this->Quiet);
- if (!this->CTest->GetCTestConfiguration("DropSitePassword").empty()) {
- url += ":" + this->CTest->GetCTestConfiguration("DropSitePassword");
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, ":******",
- this->Quiet);
- }
- url += "@";
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "@", this->Quiet);
- }
- url += this->CTest->GetCTestConfiguration("DropSite") +
- this->CTest->GetCTestConfiguration("DropLocation");
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- this->CTest->GetCTestConfiguration("DropSite")
- << this->CTest->GetCTestConfiguration("DropLocation")
- << std::endl,
- this->Quiet);
- if (!this->SubmitUsingHTTP(buildDirectory + "/Testing/" +
- this->CTest->GetCurrentTag(),
- files, prefix, url)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Problems when submitting via HTTP" << std::endl);
- ofs << " Problems when submitting via HTTP" << std::endl;
- return -1;
- }
- if (!this->CDash) {
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Using HTTP trigger method"
- << std::endl
- << " Trigger site: "
- << this->CTest->GetCTestConfiguration("TriggerSite")
- << std::endl,
- this->Quiet);
- if (!this->TriggerUsingHTTP(
- files, prefix,
- this->CTest->GetCTestConfiguration("TriggerSite"))) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Problems when triggering via HTTP" << std::endl);
- ofs << " Problems when triggering via HTTP" << std::endl;
- return -1;
- }
- }
- if (this->HasErrors) {
- cmCTestLog(this->CTest, HANDLER_OUTPUT,
- " Errors occurred during "
- "submission."
- << std::endl);
- ofs << " Errors occurred during submission. " << std::endl;
- } else {
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Submission successful"
- << (this->HasWarnings ? ", with warnings." : "")
- << std::endl,
- this->Quiet);
- ofs << " Submission successful"
- << (this->HasWarnings ? ", with warnings." : "") << std::endl;
- }
- return 0;
- } else if (dropMethod == "xmlrpc") {
- #if defined(CTEST_USE_XMLRPC)
- ofs << "Using drop method: XML-RPC" << std::endl;
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Using XML-RPC submit method" << std::endl,
- this->Quiet);
- std::string url = this->CTest->GetCTestConfiguration("DropSite");
- prefix = this->CTest->GetCTestConfiguration("DropLocation");
- if (!this->SubmitUsingXMLRPC(buildDirectory + "/Testing/" +
- this->CTest->GetCurrentTag(),
- files, prefix, url)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Problems when submitting via XML-RPC" << std::endl);
- ofs << " Problems when submitting via XML-RPC" << std::endl;
- return -1;
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Submission successful" << std::endl, this->Quiet);
- ofs << " Submission successful" << std::endl;
- return 0;
- #else
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Submission method \"xmlrpc\" not compiled into CTest!"
- << std::endl);
- return -1;
- #endif
- } else if (dropMethod == "scp") {
- std::string url;
- if (!this->CTest->GetCTestConfiguration("DropSiteUser").empty()) {
- url += this->CTest->GetCTestConfiguration("DropSiteUser") + "@";
- }
- url += this->CTest->GetCTestConfiguration("DropSite") + ":" +
- this->CTest->GetCTestConfiguration("DropLocation");
- // change to the build directory so that we can uses a relative path
- // on windows since scp doesn't support "c:" a drive in the path
- cmWorkingDirectory workdir(buildDirectory);
- if (workdir.Failed()) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Failed to change directory to "
- << buildDirectory << " : "
- << std::strerror(workdir.GetLastResult()) << std::endl);
- ofs << " Failed to change directory to " << buildDirectory << " : "
- << std::strerror(workdir.GetLastResult()) << std::endl;
- return -1;
- }
- if (!this->SubmitUsingSCP(this->CTest->GetCTestConfiguration("ScpCommand"),
- "Testing/" + this->CTest->GetCurrentTag(), files,
- prefix, url)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Problems when submitting via SCP" << std::endl);
- ofs << " Problems when submitting via SCP" << std::endl;
- return -1;
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Submission successful" << std::endl, this->Quiet);
- ofs << " Submission successful" << std::endl;
- return 0;
- } else if (dropMethod == "cp") {
- std::string location = this->CTest->GetCTestConfiguration("DropLocation");
- // change to the build directory so that we can uses a relative path
- // on windows since scp doesn't support "c:" a drive in the path
- cmWorkingDirectory workdir(buildDirectory);
- if (workdir.Failed()) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Failed to change directory to "
- << buildDirectory << " : "
- << std::strerror(workdir.GetLastResult()) << std::endl);
- ofs << " Failed to change directory to " << buildDirectory << " : "
- << std::strerror(workdir.GetLastResult()) << std::endl;
- return -1;
- }
- cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
- " Change directory: " << buildDirectory << std::endl,
- this->Quiet);
- if (!this->SubmitUsingCP("Testing/" + this->CTest->GetCurrentTag(), files,
- prefix, location)) {
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Problems when submitting via CP" << std::endl);
- ofs << " Problems when submitting via cp" << std::endl;
- return -1;
- }
- cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
- " Submission successful" << std::endl, this->Quiet);
- ofs << " Submission successful" << std::endl;
- return 0;
- }
- cmCTestLog(this->CTest, ERROR_MESSAGE,
- " Unknown submission method: \"" << dropMethod << "\""
- << std::endl);
- return -1;
- }
- std::string cmCTestSubmitHandler::GetSubmitResultsPrefix()
- {
- std::string buildname =
- cmCTest::SafeBuildIdField(this->CTest->GetCTestConfiguration("BuildName"));
- std::string name = this->CTest->GetCTestConfiguration("Site") + "___" +
- buildname + "___" + this->CTest->GetCurrentTag() + "-" +
- this->CTest->GetTestModelString() + "___XML___";
- return name;
- }
- void cmCTestSubmitHandler::SelectParts(std::set<cmCTest::Part> const& parts)
- {
- // Check whether each part is selected.
- for (cmCTest::Part p = cmCTest::PartStart; p != cmCTest::PartCount;
- p = cmCTest::Part(p + 1)) {
- this->SubmitPart[p] =
- (std::set<cmCTest::Part>::const_iterator(parts.find(p)) != parts.end());
- }
- }
- void cmCTestSubmitHandler::SelectFiles(cmCTest::SetOfStrings const& files)
- {
- this->Files.insert(files.begin(), files.end());
- }
|