From 32181a3956a8e7e1cb4bdb5338a260df7d343534 Mon Sep 17 00:00:00 2001 From: James Turner Date: Mon, 18 Apr 2016 13:41:25 +0100 Subject: [PATCH] Expand package-system unit-tests. More to come, but this covers the absolute basics now. --- simgear/package/CatalogTest.cxx | 209 +++++++++++++++++- simgear/package/Package.cxx | 2 +- simgear/package/catalogTest1/alpha.zip | Bin 0 -> 593 bytes .../package/catalogTest1/alpha/alpha-set.xml | 7 + .../package/catalogTest1/alpha/somefile.txt | 1 + simgear/package/catalogTest1/c172p.zip | Bin 0 -> 860 bytes .../catalogTest1/c172p/c172p-2d-panel-set.xml | 7 + .../catalogTest1/c172p/c172p-floats-set.xml | 7 + .../package/catalogTest1/c172p/c172p-set.xml | 7 + simgear/package/catalogTest1/catalog.xml | 20 +- .../package/catalogTest1/common-sounds.zip | Bin 0 -> 360 bytes .../catalogTest1/sounds/sharedfile.txt | 1 + 12 files changed, 257 insertions(+), 4 deletions(-) create mode 100644 simgear/package/catalogTest1/alpha.zip create mode 100644 simgear/package/catalogTest1/alpha/alpha-set.xml create mode 100644 simgear/package/catalogTest1/alpha/somefile.txt create mode 100644 simgear/package/catalogTest1/c172p.zip create mode 100644 simgear/package/catalogTest1/c172p/c172p-2d-panel-set.xml create mode 100644 simgear/package/catalogTest1/c172p/c172p-floats-set.xml create mode 100644 simgear/package/catalogTest1/c172p/c172p-set.xml create mode 100644 simgear/package/catalogTest1/common-sounds.zip create mode 100644 simgear/package/catalogTest1/sounds/sharedfile.txt diff --git a/simgear/package/CatalogTest.cxx b/simgear/package/CatalogTest.cxx index 12dd42d7..6f6f5a80 100644 --- a/simgear/package/CatalogTest.cxx +++ b/simgear/package/CatalogTest.cxx @@ -23,15 +23,92 @@ #include #include +#include #include #include #include +#include +#include #include +#include + +#include +#include +#include +#include using namespace simgear; +std::string readFileIntoString(const SGPath& path) +{ + std::string contents; + + size_t readLen; + SGFile f(path.str()); + if (!f.open(SG_IO_IN)) { + throw sg_io_exception("Couldn't open file", path); + } + + char buf[8192]; + while ((readLen = f.read(buf, 8192)) > 0) { + contents += std::string(buf, readLen); + } + + return contents; +} + +SGPath global_serverFilesRoot; + +class TestPackageChannel : public TestServerChannel +{ +public: + + virtual void processRequestHeaders() + { + state = STATE_IDLE; + + SGPath localPath(global_serverFilesRoot); + localPath.append(path); + + //SG_LOG(SG_IO, SG_INFO, "local path is:" << localPath.str()); + + if (localPath.exists()) { + std::string content = readFileIntoString(localPath); + std::stringstream d; + d << "HTTP/1.1 " << 200 << " " << reasonForCode(200) << "\r\n"; + d << "Content-Length:" << content.size() << "\r\n"; + d << "\r\n"; // final CRLF to terminate the headers + d << content; + + std::string ds(d.str()); + bufferSend(ds.data(), ds.size()); + } else { + sendErrorResponse(404, false, ""); + } + } +}; + +TestServer testServer; + +void waitForUpdateComplete(HTTP::Client* cl, pkg::Root* root) +{ + SGTimeStamp start(SGTimeStamp::now()); + while (start.elapsedMSec() < 10000) { + cl->update(); + testServer.poll(); + + if (!cl->hasActiveRequests()) { + return; + } + + SGTimeStamp::sleepForMSec(15); + } + + std::cerr << "timed out" << std::endl; +} + int parseTest() { SGPath rootPath = simgear::Dir::current().path(); @@ -55,7 +132,7 @@ int parseTest() COMPARE(p1->qualifiedId(), "org.flightgear.test.catalog1.alpha"); COMPARE(p1->name(), "Alpha package"); COMPARE(p1->revision(), 8); - COMPARE(p1->fileSizeBytes(), 1234567); + COMPARE(p1->fileSizeBytes(), 593); pkg::PackageRef p2 = cat->getPackageById("c172p"); @@ -99,9 +176,139 @@ int parseTest() return EXIT_SUCCESS; } +void testAddCatalog(HTTP::Client* cl) +{ +// erase dir + SGPath rootPath(simgear::Dir::current().path()); + rootPath.append("pkg_add_catalog"); + simgear::Dir pd(rootPath); + pd.removeChildren(); + + + pkg::RootRef root(new pkg::Root(rootPath, "8.1.2")); + // specify a test dir + root->setHTTPClient(cl); + + pkg::CatalogRef c = pkg::Catalog::createFromUrl(root.ptr(), "http://localhost:2000/catalogTest1/catalog.xml"); + + waitForUpdateComplete(cl, root); + +// verify on disk state + SGPath p(rootPath); + p.append("org.flightgear.test.catalog1"); + p.append("catalog.xml"); + VERIFY(p.exists()); + COMPARE(root->allPackages().size(), 3); + COMPARE(root->catalogs().size(), 1); + + pkg::PackageRef p1 = root->getPackageById("alpha"); + COMPARE(p1->id(), "alpha"); + + pkg::PackageRef p2 = root->getPackageById("org.flightgear.test.catalog1.c172p"); + COMPARE(p2->id(), "c172p"); + COMPARE(p2->qualifiedId(), "org.flightgear.test.catalog1.c172p"); + +} + +void testInstallPackage(HTTP::Client* cl) +{ + SGPath rootPath(simgear::Dir::current().path()); + rootPath.append("pkg_install_with_dep"); + simgear::Dir pd(rootPath); + pd.removeChildren(); + + pkg::RootRef root(new pkg::Root(rootPath, "8.1.2")); + // specify a test dir + root->setHTTPClient(cl); + + pkg::CatalogRef c = pkg::Catalog::createFromUrl(root.ptr(), "http://localhost:2000/catalogTest1/catalog.xml"); + waitForUpdateComplete(cl, root); + + pkg::PackageRef p1 = root->getPackageById("org.flightgear.test.catalog1.c172p"); + pkg::InstallRef ins = p1->install(); + + VERIFY(ins->isQueued()); + + waitForUpdateComplete(cl, root); + VERIFY(p1->isInstalled()); + VERIFY(p1->existingInstall() == ins); + + pkg::PackageRef commonDeps = root->getPackageById("common-sounds"); + VERIFY(commonDeps->existingInstall()); + + // verify on disk state + SGPath p(rootPath); + p.append("org.flightgear.test.catalog1"); + p.append("Aircraft"); + p.append("c172p"); + + COMPARE(p, ins->path()); + + p.append("c172p-floats-set.xml"); + VERIFY(p.exists()); + + SGPath p2(rootPath); + p2.append("org.flightgear.test.catalog1"); + p2.append("Aircraft"); + p2.append("sounds"); + p2.append("sharedfile.txt"); + VERIFY(p2.exists()); +} + +void testUninstall(HTTP::Client* cl) +{ + SGPath rootPath(simgear::Dir::current().path()); + rootPath.append("pkg_uninstall"); + simgear::Dir pd(rootPath); + pd.removeChildren(); + + pkg::RootRef root(new pkg::Root(rootPath, "8.1.2")); + root->setHTTPClient(cl); + + pkg::CatalogRef c = pkg::Catalog::createFromUrl(root.ptr(), "http://localhost:2000/catalogTest1/catalog.xml"); + waitForUpdateComplete(cl, root); + + pkg::PackageRef p1 = root->getPackageById("org.flightgear.test.catalog1.c172p"); + pkg::InstallRef ins = p1->install(); + + waitForUpdateComplete(cl, root); + + VERIFY(p1->isInstalled()); + + ins->uninstall(); + + VERIFY(!ins->path().exists()); +} + +void testRemoveCatalog() +{ + +} + +void testRefreshCatalog() +{ + + // check for pending updates +} + + int main(int argc, char* argv[]) { + sglog().setLogLevels( SG_ALL, SG_DEBUG ); + + HTTP::Client cl; + cl.setMaxConnections(1); + + global_serverFilesRoot = SGPath(SRC_DIR); + + testAddCatalog(&cl); + parseTest(); + + testInstallPackage(&cl); + + testUninstall(&cl); + std::cout << "Successfully passed all tests!" << std::endl; return EXIT_SUCCESS; } diff --git a/simgear/package/Package.cxx b/simgear/package/Package.cxx index b32e2fc3..393e1298 100644 --- a/simgear/package/Package.cxx +++ b/simgear/package/Package.cxx @@ -281,7 +281,7 @@ PackageList Package::dependencies() const PackageList result; BOOST_FOREACH(SGPropertyNode* dep, m_props->getChildren("depends")) { - std::string depName = dep->getStringValue("package"); + std::string depName = dep->getStringValue("id"); unsigned int rev = dep->getIntValue("revision", 0); // prefer local hangar package if possible, in case someone does something diff --git a/simgear/package/catalogTest1/alpha.zip b/simgear/package/catalogTest1/alpha.zip new file mode 100644 index 0000000000000000000000000000000000000000..c07e1d481f5fc09b5177e8506ca677908b5b191a GIT binary patch literal 593 zcmWIWW@h1H0DE-1Ln|cxeSU10%~ODXV+G{-0I@J!KiCxA;?xqoirgHqVd}3%!Zm;x&9KcI*Pl2W zsNt=vb?(gh&7nqDi~}}pfBa|@r^YFL4_&R5Ury@iZG0RQutD?O8Pv08mGcw6B;|f0sp!Fai!0^@) zM8iUp6%v|go`;x)D=Z;q0YjZ(NuvYYEVPgX8Vm|qtOnx>9b|)#!wp6aE1lgqDkcs91 literal 0 HcmV?d00001 diff --git a/simgear/package/catalogTest1/alpha/alpha-set.xml b/simgear/package/catalogTest1/alpha/alpha-set.xml new file mode 100644 index 00000000..6f81cb47 --- /dev/null +++ b/simgear/package/catalogTest1/alpha/alpha-set.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/simgear/package/catalogTest1/alpha/somefile.txt b/simgear/package/catalogTest1/alpha/somefile.txt new file mode 100644 index 00000000..de656e38 --- /dev/null +++ b/simgear/package/catalogTest1/alpha/somefile.txt @@ -0,0 +1 @@ +The quick brown fox jumps over a lazy dog. diff --git a/simgear/package/catalogTest1/c172p.zip b/simgear/package/catalogTest1/c172p.zip new file mode 100644 index 0000000000000000000000000000000000000000..cbd67499077315b57baee174c7d2d9a867b5e62b GIT binary patch literal 860 zcmWIWW@h1H0D(1)lRUr-D8a@c!;oxfZd9Nj8p6rI-11r^JWfh1ytIOwfsy4aBLf4A z2vB7JP(O&~U|60z*`xdS-+l`q4`h%GTtCYVc8#K?IVeMj; z{FN}R*KQRTl519>xkd`jHEB8di6zA-Zt(}Y1BB6hNV!{9qq#*G%`M0Q5(snv2vf}w zj7)OOxFT2r7)>A`!0^@)M8l$+6%yTOZV2!OnT0E|A!Y%S1;dg?2e?_dq8?}iDC+Up zfEj_vHk?DV0X;f_=3|LUWFKHg3bOf8K#xE|1L6aC6afv!5 + + + + + + diff --git a/simgear/package/catalogTest1/c172p/c172p-floats-set.xml b/simgear/package/catalogTest1/c172p/c172p-floats-set.xml new file mode 100644 index 00000000..7e7f54b9 --- /dev/null +++ b/simgear/package/catalogTest1/c172p/c172p-floats-set.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/simgear/package/catalogTest1/c172p/c172p-set.xml b/simgear/package/catalogTest1/c172p/c172p-set.xml new file mode 100644 index 00000000..7e7f54b9 --- /dev/null +++ b/simgear/package/catalogTest1/c172p/c172p-set.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/simgear/package/catalogTest1/catalog.xml b/simgear/package/catalogTest1/catalog.xml index 79879f2e..94760688 100644 --- a/simgear/package/catalogTest1/catalog.xml +++ b/simgear/package/catalogTest1/catalog.xml @@ -4,6 +4,8 @@ org.flightgear.test.catalog1 First test catalog http://download.flightgear.org/catalog1/catalog.xml + 4 + 8.1.* 8.0.0 8.2.0 @@ -12,15 +14,21 @@ alpha Alpha package 8 - 1234567 + 593 + + a469c4b837f0521db48616cfe65ac1ea + http://localhost:2000/catalogTest1/alpha.zip + + alpha c172p Cessna 172-P + c172p A plane made by Cessna 42 - 34567 + 860 cessna ga @@ -54,11 +62,19 @@ c172p-skis C172 with skis + + ec0e2ffdf98d6a5c05c77445e5447ff5 + http://localhost:2000/catalogTest1/c172p.zip + common-sounds Common sound files for test catalog aircraft 10 + sounds + http://localhost:2000/catalogTest1/common-sounds.zip + 360 + acf9eb89cf396eb42f8823d9cdf17584 diff --git a/simgear/package/catalogTest1/common-sounds.zip b/simgear/package/catalogTest1/common-sounds.zip new file mode 100644 index 0000000000000000000000000000000000000000..0e3c9a8af6677122fb3c898b3e7f6002bda47aa4 GIT binary patch literal 360 zcmWIWW@h1H0D&2elRUr-D8bGk!%&=GnwL_n9~#2Rz#RWtBs@h*EWEUWn}Lz#DZQxy`6 zk}^w*5{oJoQW8rN6*5wbQuVk3ycwC~m~pvP0_ti3hPRF&Cfwz$5SL@P8etfwyCH@F keaNt+5o8+FFmzu4jRyGw!)R7EkPDcAa0QTF1>!IO0D(e6^8f$< literal 0 HcmV?d00001 diff --git a/simgear/package/catalogTest1/sounds/sharedfile.txt b/simgear/package/catalogTest1/sounds/sharedfile.txt new file mode 100644 index 00000000..cf47e9ed --- /dev/null +++ b/simgear/package/catalogTest1/sounds/sharedfile.txt @@ -0,0 +1 @@ +Some arbitrary data here. -- 2.39.5