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 14d6afa commit ca49515
Show file tree
Hide file tree
Showing 3 changed files with 64 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
48 changes: 48 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,51 @@ 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);
const auto delta_size{getDeltaSize(getInitialTarget(), new_target)};
const auto expected_available{15 - 5};
storage::Volume::UsageInfo usage_info{.size = {100 * 4096, 100},
.available = {expected_available * 4096, expected_available}};
std::stringstream expected_msg;
expected_msg << "before ostree pull; required: " << usage_info.withRequired(delta_size).required
<< ", available: " << usage_info.available;
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(expected_msg.str())) << 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 ca49515

Please sign in to comment.