Skip to content

Commit

Permalink
More CI Tests (#30)
Browse files Browse the repository at this point in the history
* fake manifest tests: use new test repo

* add upgrade test and initial sort test

* use portable md5, adjust tests

* more test changes

* fix manifest ugprade tests

* more test fixes
  • Loading branch information
vgmoose authored Nov 25, 2023
1 parent c239817 commit bcfe914
Show file tree
Hide file tree
Showing 26 changed files with 542 additions and 55 deletions.
8 changes: 4 additions & 4 deletions .get/repos.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"repos": [
{
"name": "Switchbru Default",
"url": "https://switchbru.com/appstore",
"enabled": true
"url": "http://localhost",
"enabled": false
},
{
"name": "VGMoose Apps",
Expand All @@ -12,8 +12,8 @@
},
{
"name": "Sample repo",
"url": "http://localhost:8000",
"enabled": false
"url": "http://localhost:8000/c",
"enabled": true
}
]
}
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ build:
run_tests:
rm -rf tests/.get/packages tests/.get/tmp
gcc -c $(MINIZIP)/*.c
g++ -g tests/*.cpp src/*.cpp -std=gnu++20 -lm -lssl -lcrypto -L/usr/lib -L/usr/local/Cellar//[email protected]/1.1.0g/lib/ -I/usr/local/Cellar//[email protected]/1.1.0g/include -I $(RAPIDJSON) $(MINIZIP_O) -I $(MINIZIP) -lz -lcurl -o get_tests
g++ -g tests/*.cpp src/*.cpp -std=gnu++20 -lm -L/usr/lib -I $(RAPIDJSON) $(MINIZIP_O) -I $(MINIZIP) -lz -lcurl -o get_tests
cd tests/server && python3 -m http.server &
export SERVERD=$!
sleep 2
./get_tests

clean:
rm *.o get get_tests
endif
endif
9 changes: 7 additions & 2 deletions src/Get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,15 @@ void Get::update()
this->removeDuplicates();

// check for any installed packages to update their status
for (const auto& package : packages)
{
for (const auto& package : packages) {
package->updateStatus(mPkg_path);
}

// sort the packages by name
// TODO: apply other sort orders here, and potentially search filters
// std::sort(packages.begin(), packages.end(), [](const std::shared_ptr<Package>& a, const std::shared_ptr<Package>& b) {
// return a->getPackageName() < b->getPackageName();
// });
}

int Get::validateRepos() const
Expand Down
2 changes: 1 addition & 1 deletion src/Get.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Get
std::optional<Package> lookup(const std::string& pkg_name);
void addLocalRepo();
void removeDuplicates();
void update();

// map of word -> list of packages whose info matches that word
// TODO: this
Expand All @@ -47,7 +48,6 @@ class Get

private:
void loadRepos();
void update();
int validateRepos() const;

// the remote repos and packages
Expand Down
3 changes: 2 additions & 1 deletion src/Repo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class Repo
[[nodiscard]] bool isEnabled() const;
[[nodiscard]] bool isLoaded() const; // whether this server could be reached or not

protected:
void setEnabled(bool enabled);

protected:

std::string name;
std::string url;
bool enabled {};
Expand Down
10 changes: 10 additions & 0 deletions tests/.get/repos.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"name": "Test Repo C",
"url": "http://127.0.0.1:8000/c",
"enabled": false
},
{
"name": "Test Repo D",
"url": "http://127.0.0.1:8000/d",
"enabled": false
},
{
"name": "Test Repo E",
"url": "http://127.0.0.1:8000/e",
"enabled": false
}
]
}
69 changes: 69 additions & 0 deletions tests/10_FakeManifestUpgrade.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "tests.hpp"

class FakeManifestUpgradeTest : public Test {
public:
FakeManifestUpgradeTest() {
// depends on the previous test having succeeded
purpose = "Packages without manifests can be upgraded";
}
bool execute()
{
// enable the 5th repo (server e)
get->toggleRepo(*get->getRepos()[5]);

// there should be 1 available UPDATE package, and 0 installed packages
if (count(get, UPDATE) != 1 || count(get, INSTALLED) != 0) {
error << "There should be 1 available UPDATE package, but there are " << count(get, UPDATE) << endl;
error << "There should be 0 installed package, but there are " << count(get, INSTALLED) << endl;
return false;
}

install(get, "missingmanifest");

if (!exists("sdroot/image.png")) {
error << "The downloaded file in package 'missingmanifest' on server 'e' did not successfully extract" << endl;
return false;
}

// there should be 1 INSTALLED package, and 0 upgrade packages
if (count(get, UPDATE) != 0 || count(get, INSTALLED) != 1) {
error << "There should be 0 available UPDATE package, but there are " << count(get, UPDATE) << endl;
error << "There should be 1 installed package, but there are " << count(get, INSTALLED) << endl;
return false;
}

std::string sum = calculateMD5("sdroot/image.png");
const char * rightSum = "26a7965e5aa6acced42de92eeee76d7a";
if (rightSum != sum) {
error << "The downloaded file in package 'missingmanifest' on server 'e' has incorrect md5 sum, expected: " << rightSum << ", received: " << sum.c_str() << endl;
return false;
}

// make sure the missingmanifest has "installed" state
auto missingmanifest = get->lookup("missingmanifest");
if (!missingmanifest || missingmanifest->getStatus() != INSTALLED) {
error << "The package 'missingmanifest' on server 'e' was not installed" << endl;
return false;
}

// install one more time, and make sure we're still all good
install(get, "missingmanifest");

sum = calculateMD5("sdroot/image.png");
if (rightSum != sum) {
error << "The redownloaded file in package 'missingmanifest' on server 'e' has incorrect md5 sum, expected: " << rightSum << ", received: " << sum.c_str() << endl;
return false;
}

// there should still be 1 INSTALLED package, and 0 upgrade packages
if (count(get, UPDATE) != 0 || count(get, INSTALLED) != 1) {
error << "There should be 0 available UPDATE package, but there are " << count(get, UPDATE) << endl;
error << "There should be 1 installed package, but there are " << count(get, INSTALLED) << endl;
return false;
}

// TODO: test zip extractions with an extra folder or no folder directory

return true;
}
};
38 changes: 38 additions & 0 deletions tests/11_PackagesSorted.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "tests.hpp"
#include "../src/Utils.hpp"

class PackagesSortedTest : public Test {
public:
PackagesSortedTest() {
// TODO: support other sort modes directly in libget (currently in hb-appstore)
// also TODO: the code doesn't do sorting at all! so implement that so this test passes
purpose = "Packages return in alphabetically sorted order";
}
bool execute()
{
// enable all test repos
for (int i = 0; i < get->getRepos().size(); i++) {
get->getRepos()[i]->setEnabled(true);
}
get->update();

// get all packages from the server
auto packages = get->list();

// create a sorted copy of the packages
auto sorted = packages;
std::sort(sorted.begin(), sorted.end(), [](const Package a, const Package b) {
return toLower(a.getPackageName()) < toLower(b.getPackageName());
});

// compare the two lists
for (int i = 0; i < packages.size(); i++) {
if (packages[i].getPackageName() != sorted[i].getPackageName()) {
error << "Package " << i << " is not sorted correctly" << endl;
return false;
}
}

return true;
}
};
7 changes: 5 additions & 2 deletions tests/3_InstallPackages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ class InstallPackages : public Test {
{
auto& packages = get->getPackages();
// install two of the packages
get->install(*packages[0]);
get->install(*packages[2]);
auto threepkg = get->lookup("three");
get->install(threepkg.value());

auto twopkg = get->lookup("two");
get->install(twopkg.value());

// verify that some files got created
// TODO: verify some!
Expand Down
5 changes: 5 additions & 0 deletions tests/7_UpgradePackages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class UpgradePackages : public Test {
error << "(2) After upgrading, some unexpected files were left behind" << endl;
return false;
}

// clean up the rest of the files directly on sdroot
remove("sdroot/dir2/x");
remove("sdroot/newdir/dogs");
remove("sdroot/dir2/mario");

return true;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/8_ContentTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ class ContentTest : public Test {
{
install(get, "raichu");

std::string sum = calculateMD5("sdroot/image.jpg").c_str();
const char * rightSum = "737b4d9cc7a7957aaa28d7b088e5acb8";
std::string sum = calculateMD5("sdroot/image.jpg");
std::string rightSum = "737b4d9cc7a7957aaa28d7b088e5acb8";
if (rightSum != sum) {
error << "The downloaded file in package 'raichu' on server 'c' has incorrect md5 sum, expected: " << rightSum << ", received: " << sum.c_str() << endl;
error << "The downloaded file in package 'raichu' on server 'c' has incorrect md5 sum, expected: " << rightSum << ", received: " << sum << endl;
return false;
}

// install one more time, and make sure we're still all good
install(get, "raichu");

sum = calculateMD5("sdroot/image.jpg").c_str();
sum = calculateMD5("sdroot/image.jpg");
if (rightSum != sum) {
error << "The redownloaded file in package 'raichu' on server 'c' has incorrect md5 sum, expected: " << rightSum << ", received: " << sum.c_str() << endl;
error << "The redownloaded file in package 'raichu' on server 'c' has incorrect md5 sum, expected: " << rightSum << ", received: " << sum << endl;
return false;
}

Expand Down
44 changes: 35 additions & 9 deletions tests/9_FakeManifestTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,68 @@
class FakeManifestTest : public Test {
public:
FakeManifestTest() {
purpose = "If the manifest is missing, the package is still installed";
purpose = "Packages without manifests are still installed";
}
bool execute()
{
// disable all repos
for (int i = 0; i < get->getRepos().size(); i++) {
printf("Disabling repo %s\n", get->getRepos()[i]->getUrl().c_str());
get->getRepos()[i]->setEnabled(false);
}

// enable only the 4th repo (server d)
get->getRepos()[4]->setEnabled(true);
get->update();

// there should be 1 available GET package
if (count(get, GET) != 1) {
error << "(1) There should be 1 GET package, but there are " << count(get, GET) << endl;
return false;
}

install(get, "missingmanifest");

if (!exists("sdroot/image.png")) {
error << "The downloaded file in package 'missingmanifest' on server 'c' did not successfully extract" << endl;
error << "The downloaded file in package 'missingmanifest' on server 'd' did not successfully extract" << endl;
return false;
}

std::string sum = calculateMD5("sdroot/image.png").c_str();
const char * rightSum = "26a7965e5aa6acced42de92eeee76d7a";
std::string sum = calculateMD5("sdroot/image.png");
std::string rightSum = "26a7965e5aa6acced42de92eeee76d7a";
if (rightSum != sum) {
error << "The downloaded file in package 'missingmanifest' on server 'c' has incorrect md5 sum, expected: " << rightSum << ", received: " << sum.c_str() << endl;
error << "The downloaded file in package 'missingmanifest' on server 'd' has incorrect md5 sum, expected: " << rightSum << ", received: " << sum << endl;
return false;
}

// make sure the missingmanifest has "installed" state
auto missingmanifest = get->lookup("missingmanifest");
if (!missingmanifest || missingmanifest->getStatus() != INSTALLED) {
error << "The package 'missingmanifest' on server 'c' was not installed" << endl;
error << "The package 'missingmanifest' on server 'd' was not installed" << endl;
return false;
}

// there should be one installed package total
if (count(get, INSTALLED) != 1) {
error << "(2) There should be 1 installed package, but there are " << count(get, INSTALLED) << endl;
return false;
}

// manually remove the image on disk, breaking the package
// TODO: a test to show broken packages as their own state
std::remove("sdroot/image.png");

// install one more time, and make sure we're still all good
// should work even though the package is broken
install(get, "missingmanifest");

sum = calculateMD5("sdroot/image.png").c_str();
sum = calculateMD5("sdroot/image.png");
if (rightSum != sum) {
error << "The redownloaded file in package 'missingmanifest' on server 'c' has incorrect md5 sum, expected: " << rightSum << ", received: " << sum.c_str() << endl;
error << "The redownloaded file in package 'missingmanifest' on server 'd' has incorrect md5 sum, expected: " << rightSum << ", received: " << sum.c_str() << endl;
return false;
}

// TODO: test zip extractions with an extra folder or no folder directory
// TODO: test upgrade of fake manifest packages

return true;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "7_UpgradePackages.hpp"
#include "8_ContentTest.hpp"
#include "9_FakeManifestTest.hpp"
#include "10_FakeManifestUpgrade.hpp"
#include "11_PackagesSorted.hpp"

using namespace std;

Expand All @@ -36,6 +38,10 @@ int main()
// create a Get object
Get get("./tests/.get/", URL "a");

// print all repos:
for (auto& repo : get.getRepos())
cout << repo->getUrl() << endl;

// all the tests are "system" tests, even though this
// main test suite is linked against the libget library (and so
// it's not a true end-to-end test), the tests depend on the previous
Expand All @@ -52,6 +58,8 @@ int main()
tests.push_back(std::make_unique<UpgradePackages>());
tests.push_back(std::make_unique<ContentTest>());
tests.push_back(std::make_unique<FakeManifestTest>());
tests.push_back(std::make_unique<FakeManifestUpgradeTest>());
// tests.push_back(std::make_unique<PackagesSortedTest>());

// main test loop that goes through all our tests, and prints out
// their status with a happy friendly emoji
Expand Down
Loading

0 comments on commit bcfe914

Please sign in to comment.