Skip to content

Commit

Permalink
Merge pull request #20 from cajun-rat/clarify-registration-requirements
Browse files Browse the repository at this point in the history
Add stricter testing to reregistration tests
  • Loading branch information
pattivacek authored Oct 4, 2021
2 parents 524179b + 7e78cc0 commit b7c8369
Showing 1 changed file with 86 additions and 8 deletions.
94 changes: 86 additions & 8 deletions src/libaktualizr/primary/reregistration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,34 @@ class HttpFakeRegistration : public HttpFake {
: HttpFake(test_dir_in, "noupdates", meta_dir_in) {}

HttpResponse post(const std::string& url, const Json::Value& data) override {
if (url.find("/devices") != std::string::npos) {
device_registration_count++;
auto this_device_id = data["deviceId"].asString();
if (ecu_registration_count <= 1) {
device_id = this_device_id;
} else {
EXPECT_EQ(device_id, this_device_id) << "deviceId should change during provisioning";
}
}
if (url.find("/director/ecus") != std::string::npos) {
registration_count += 1;
ecu_registration_count++;
EXPECT_EQ(data["primary_ecu_serial"].asString(), "CA:FE:A6:D2:84:9D");
EXPECT_EQ(data["ecus"][0]["ecu_serial"].asString(), "CA:FE:A6:D2:84:9D");
EXPECT_EQ(data["ecus"][0]["hardware_identifier"].asString(), "primary_hw");
if (ecu_registration_count == 1) {
primary_ecu_info = data["ecus"][0];
} else {
EXPECT_EQ(primary_ecu_info, data["ecus"][0]) << "Information about primary ECU shouldn't change";
}
}

return HttpFake::post(url, data);
}

unsigned int registration_count{0};
unsigned int ecu_registration_count{0};
unsigned int device_registration_count{0};
Json::Value primary_ecu_info;
std::string device_id;
};

/*
Expand All @@ -45,14 +63,16 @@ TEST(Aktualizr, AddSecondary) {

std::vector<std::string> expected_ecus = {"CA:FE:A6:D2:84:9D", "ecuserial3", "secondary_ecu_serial"};
UptaneTestCommon::verifyEcus(temp_dir, expected_ecus);
EXPECT_EQ(http->registration_count, 1);
EXPECT_EQ(http->device_registration_count, 1);
EXPECT_EQ(http->ecu_registration_count, 1);

ecu_config.ecu_serial = "ecuserial4";
aktualizr.AddSecondary(std::make_shared<Primary::VirtualSecondary>(ecu_config));
aktualizr.Initialize();
expected_ecus.push_back(ecu_config.ecu_serial);
UptaneTestCommon::verifyEcus(temp_dir, expected_ecus);
EXPECT_EQ(http->registration_count, 2);
EXPECT_EQ(http->device_registration_count, 1);
EXPECT_EQ(http->ecu_registration_count, 2);
}

/*
Expand All @@ -72,7 +92,8 @@ TEST(Aktualizr, RemoveSecondary) {

std::vector<std::string> expected_ecus = {"CA:FE:A6:D2:84:9D", "ecuserial3", "secondary_ecu_serial"};
UptaneTestCommon::verifyEcus(temp_dir, expected_ecus);
EXPECT_EQ(http->registration_count, 1);
EXPECT_EQ(http->device_registration_count, 1);
EXPECT_EQ(http->ecu_registration_count, 1);
}

{
Expand All @@ -81,7 +102,8 @@ TEST(Aktualizr, RemoveSecondary) {

std::vector<std::string> expected_ecus = {"CA:FE:A6:D2:84:9D", "secondary_ecu_serial"};
UptaneTestCommon::verifyEcus(temp_dir, expected_ecus);
EXPECT_EQ(http->registration_count, 2);
EXPECT_EQ(http->device_registration_count, 1);
EXPECT_EQ(http->ecu_registration_count, 2);
}
}

Expand All @@ -102,7 +124,8 @@ TEST(Aktualizr, ReplaceSecondary) {

std::vector<std::string> expected_ecus = {"CA:FE:A6:D2:84:9D", "ecuserial3", "secondary_ecu_serial"};
UptaneTestCommon::verifyEcus(temp_dir, expected_ecus);
EXPECT_EQ(http->registration_count, 1);
EXPECT_EQ(http->device_registration_count, 1);
EXPECT_EQ(http->ecu_registration_count, 1);
}

{
Expand All @@ -114,7 +137,62 @@ TEST(Aktualizr, ReplaceSecondary) {

std::vector<std::string> expected_ecus = {"CA:FE:A6:D2:84:9D", "ecuserial4", "secondary_ecu_serial"};
UptaneTestCommon::verifyEcus(temp_dir, expected_ecus);
EXPECT_EQ(http->registration_count, 2);
EXPECT_EQ(http->device_registration_count, 1);
EXPECT_EQ(http->ecu_registration_count, 2);
}
}

/**
* Restarting Aktualizr without changing the secondaries should not result in it getting re-registered
*/
TEST(Aktualizr, RestartNoRegisterSecondaries) {
TemporaryDirectory temp_dir;
auto http = std::make_shared<HttpFakeRegistration>(temp_dir.Path(), fake_meta_dir);
Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
auto storage = INvStorage::newStorage(conf.storage);

{
UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http);
Primary::VirtualSecondaryConfig ecu_config = UptaneTestCommon::altVirtualConfiguration(temp_dir.Path());
aktualizr.AddSecondary(std::make_shared<Primary::VirtualSecondary>(ecu_config));
aktualizr.Initialize();
EXPECT_EQ(http->device_registration_count, 1);
EXPECT_EQ(http->ecu_registration_count, 1);
}

{
UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http);
Primary::VirtualSecondaryConfig ecu_config = UptaneTestCommon::altVirtualConfiguration(temp_dir.Path());
aktualizr.AddSecondary(std::make_shared<Primary::VirtualSecondary>(ecu_config));
aktualizr.Initialize();
EXPECT_EQ(http->device_registration_count, 1);
EXPECT_EQ(http->ecu_registration_count, 1);
}
}

/**
* Restarting Aktualizr should not result in it getting re-registered if it has no secondaries.
* This is similar to RestartNoRegisterSecondaries, but with zero secondaries.
*/
TEST(Aktualizr, RestartNoRegisterPrimaryOnly) {
TemporaryDirectory temp_dir;
auto http = std::make_shared<HttpFakeRegistration>(temp_dir.Path(), fake_meta_dir);
Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);

{
auto storage = INvStorage::newStorage(conf.storage);
UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http);
aktualizr.Initialize();
EXPECT_EQ(http->device_registration_count, 1);
EXPECT_EQ(http->ecu_registration_count, 1);
}

{
auto storage = INvStorage::newStorage(conf.storage);
UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http);
aktualizr.Initialize();
EXPECT_EQ(http->device_registration_count, 1);
EXPECT_EQ(http->ecu_registration_count, 1);
}
}

Expand Down

0 comments on commit b7c8369

Please sign in to comment.