Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

fix/ota-3865/Log connectivity restored after an interruption #1463

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/libaktualizr/primary/sotauptaneclient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -948,15 +948,22 @@ bool SotaUptaneClient::putManifestSimple(const Json::Value &custom) {
return false;
}

static bool connected = true;
auto manifest = AssembleManifest();
if (custom != Json::nullValue) {
manifest["custom"] = custom;
}
auto signed_manifest = uptane_manifest.signManifest(manifest);
HttpResponse response = http->put(config.uptane.director_server + "/manifest", signed_manifest);
if (response.isOk()) {
if (!connected) {
LOG_INFO << "Connectivity is restored.";
}
connected = true;
storage->clearInstallationResults();
return true;
} else {
connected = false;
}

LOG_WARNING << "Put manifest request failed: " << response.getStatusStr();
Expand Down
1 change: 1 addition & 0 deletions src/libaktualizr/primary/sotauptaneclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class SotaUptaneClient {
FRIEND_TEST(UptaneCI, CheckKeys);
FRIEND_TEST(UptaneKey, Check); // Note hacky name
FRIEND_TEST(UptaneNetwork, DownloadFailure);
FRIEND_TEST(UptaneNetwork, LogConnectivityRestored);
FRIEND_TEST(UptaneVector, Test);
FRIEND_TEST(aktualizr_secondary_uptane, credentialsPassing);
friend class CheckForUpdate; // for load tests
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/uptane/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ add_dependencies(t_uptane_delegation uptane-generator)
target_link_libraries(t_uptane_delegation virtual_secondary)
set_tests_properties(test_uptane_delegation PROPERTIES LABELS "crypto")

add_aktualizr_test(NAME uptane_network SOURCES uptane_network_test.cc PROJECT_WORKING_DIRECTORY)
add_aktualizr_test(NAME uptane_network SOURCES uptane_network_test.cc PROJECT_WORKING_DIRECTORY LIBRARIES uptane_generator_lib)
set_tests_properties(test_uptane_network PROPERTIES LABELS "crypto")
target_link_libraries(t_uptane_network virtual_secondary)

Expand Down
68 changes: 68 additions & 0 deletions src/libaktualizr/uptane/uptane_network_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* network issues.
*/
#include <gtest/gtest.h>
#include <gtest/internal/gtest-port.h>

#include <boost/process.hpp>
#include <fstream>
Expand All @@ -14,6 +15,7 @@
#include <vector>

#include "http/httpclient.h"
#include "httpfake.h"
#include "logging/logging.h"
#include "primary/initializer.h"
#include "primary/sotauptaneclient.h"
Expand Down Expand Up @@ -139,6 +141,72 @@ TEST(UptaneNetwork, DownloadFailure) {
EXPECT_TRUE(result.first);
}

/*
* Output a log when connectivity is restored.
*/
class HttpUnstable : public HttpFake {
public:
HttpUnstable(const boost::filesystem::path &test_dir_in) : HttpFake(test_dir_in, "hasupdates") {}
HttpResponse get(const std::string &url, int64_t maxsize) override {
if (!connectSwitch) {
return HttpResponse({}, 503, CURLE_OK, "");
} else {
return HttpFake::get(url, maxsize);
}
}

HttpResponse put(const std::string &url, const Json::Value &data) override {
if (!connectSwitch) {
(void)data;
return HttpResponse(url, 503, CURLE_OK, "");
} else {
return HttpFake::put(url, data);
}
}

HttpResponse post(const std::string &url, const Json::Value &data) override {
if (!connectSwitch) {
(void)data;
return HttpResponse(url, 503, CURLE_OK, "");
} else {
return HttpFake::post(url, data);
}
}

bool connectSwitch = true;
};

TEST(UptaneNetwork, LogConnectivityRestored) {
TemporaryDirectory temp_dir;
auto http = std::make_shared<HttpUnstable>(temp_dir.Path());
Config config = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
config.uptane.director_server = http->tls_server + "director";
config.uptane.repo_server = http->tls_server + "repo";
config.pacman.type = PackageManager::kNone;
config.provision.primary_ecu_serial = "CA:FE:A6:D2:84:9D";
config.provision.primary_ecu_hardware_id = "primary_hw";
config.storage.path = temp_dir.Path();
config.tls.server = http->tls_server;
UptaneTestCommon::addDefaultSecondary(config, temp_dir, "secondary_ecu_serial", "secondary_hw");

auto storage = INvStorage::newStorage(config.storage);
auto up = std_::make_unique<UptaneTestCommon::TestUptaneClient>(config, storage, http);
EXPECT_NO_THROW(up->initialize());

result::UpdateCheck result = up->fetchMeta();
EXPECT_EQ(result.status, result::UpdateStatus::kUpdatesAvailable);

http->connectSwitch = false;
result = up->fetchMeta();
EXPECT_EQ(result.status, result::UpdateStatus::kError);

http->connectSwitch = true;
testing::internal::CaptureStdout();
result = up->fetchMeta();
EXPECT_EQ(result.status, result::UpdateStatus::kUpdatesAvailable);
EXPECT_NE(std::string::npos, testing::internal::GetCapturedStdout().find("Connectivity is restored."));
}

#ifndef __NO_MAIN__
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
Expand Down