Skip to content

Commit

Permalink
rootfsmanager: Add usage stats if download error
Browse files Browse the repository at this point in the history
- Print and add storage usage statistic to the "download complete" event
  if a generic download error occurs, e.g. 404.
- Add unit tests to make sure that the stat is added to the update event
  context.

Signed-off-by: Mike Sul <[email protected]>
  • Loading branch information
mike-sul committed Sep 4, 2023
1 parent 62e3fad commit 9542445
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/rootfstreemanager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ DownloadResult RootfsTreeManager::Download(const TufTarget& target) {
sysroot_->repoPath()};
break;
}
error_desc += pull_err.description + "\n";
res = {DownloadResult::Status::DownloadFailed, error_desc};
error_desc += pull_err.description + "\nbefore ostree pull; " + pre_pull_usage_info.str() +
"\nafter ostree pull; " + post_pull_usage_info.str();
res = {DownloadResult::Status::DownloadFailed, error_desc, sysroot_->repoPath()};
}

return res;
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/liteclient/ostreerepomock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ class OSTreeRepoMock {

const std::string& getPath() const { return path_; }

void removeCommitObject(const std::string& hash) {
const auto commit_object_path{path_ + "/objects/" + hash.substr(0, 2) + "/" + hash.substr(2) + ".commit"};
boost::filesystem::remove(commit_object_path);
}
void removeDeltas() {
const auto deltas_path{path_ + "/deltas/"};
boost::filesystem::remove_all(deltas_path);
}
void removeDeltaStats() {
const auto deltas_path{path_ + "/delta-stats/"};
boost::filesystem::remove_all(deltas_path);
}

private:
const std::string path_;
};
Expand Down
43 changes: 43 additions & 0 deletions tests/liteclient_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include "fixtures/liteclienttest.cc"

extern void SetFreeBlockNumb(uint64_t, uint64_t);
extern void UnsetFreeBlockNumb();

using ::testing::NiceMock;
using ::testing::Return;

Expand Down Expand Up @@ -323,6 +326,46 @@ TEST_F(LiteClientTest, OstreeAndAppUpdate) {
}
}

TEST_F(LiteClientTest, OstreeAndAppUpdateIfOstreeDownloadFailure) {
auto client = createLiteClient();
ASSERT_TRUE(targetsMatch(client->getCurrent(), getInitialTarget()));
std::vector<AppEngine::App> apps{createApp("app-01")};
auto new_target = createTarget(&apps);

SetFreeBlockNumb(10 + 3 /* default reserved */, 100);

getOsTreeRepo().removeCommitObject(new_target.sha256Hash());
update(*client, getInitialTarget(), new_target, data::ResultCode::Numeric::kDownloadFailed,
{DownloadResult::Status::DownloadFailed, ""});
const auto event_err_msg{getEventContext("EcuDownloadCompleted")};
ASSERT_TRUE(std::string::npos != event_err_msg.find("Server returned HTTP 404")) << event_err_msg;
ASSERT_TRUE(std::string::npos != event_err_msg.find("before ostree pull; available: 40960B 10%")) << event_err_msg;
ASSERT_TRUE(std::string::npos != event_err_msg.find("after ostree pull; available: 40960B 10%")) << event_err_msg;
UnsetFreeBlockNumb();
}

TEST_F(LiteClientTest, OstreeAndAppUpdateIfOstreeDownloadFailureAndStaticDeltaStats) {
auto client = createLiteClient();
ASSERT_TRUE(targetsMatch(client->getCurrent(), getInitialTarget()));
std::vector<AppEngine::App> apps{createApp("app-01")};
// Delta size will be 2 + 1 = 3 blocks, 1 block for additional data like boot loader version file.
setGenerateStaticDelta(2, true);
auto new_target = createTarget(&apps);

SetFreeBlockNumb(10 + 5 /* default reserved for delta case */, 100);

getOsTreeRepo().removeDeltas();
getOsTreeRepo().removeCommitObject(new_target.sha256Hash());
update(*client, getInitialTarget(), new_target, data::ResultCode::Numeric::kDownloadFailed,
{DownloadResult::Status::DownloadFailed, ""});
const auto event_err_msg{getEventContext("EcuDownloadCompleted")};
ASSERT_TRUE(std::string::npos != event_err_msg.find("Server returned HTTP 404")) << event_err_msg;
ASSERT_TRUE(std::string::npos != event_err_msg.find("before ostree pull; required: 9629B 3%, available: 40960B 10%"))
<< event_err_msg;
ASSERT_TRUE(std::string::npos != event_err_msg.find("after ostree pull; available: 40960B 10%")) << event_err_msg;
UnsetFreeBlockNumb();
}

TEST_F(LiteClientTest, AppUpdateDownloadFailure) {
// boot device
auto client = createLiteClient();
Expand Down

0 comments on commit 9542445

Please sign in to comment.