Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
dockerappmanager_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <boost/filesystem.hpp>
4 #include <boost/process.hpp>
5 
6 #include "config/config.h"
7 #include "http/httpclient.h"
8 #include "package_manager/packagemanagerfactory.h"
9 #include "package_manager/packagemanagerinterface.h"
10 #include "primary/sotauptaneclient.h"
11 #include "storage/invstorage.h"
12 #include "test_utils.h"
13 #include "uptane/fetcher.h"
14 
15 static std::string repo_server = "http://127.0.0.1:";
16 static std::string treehub_server = "http://127.0.0.1:";
17 static boost::filesystem::path test_sysroot;
18 static boost::filesystem::path uptane_gen;
19 
20 static void progress_cb(const Uptane::Target& target, const std::string& description, unsigned int progress) {
21  (void)description;
22  LOG_INFO << "progress_cb " << target << " " << progress;
23 }
24 
25 static std::unique_ptr<boost::process::child> create_repo(const boost::filesystem::path& repo_path) {
26  std::string port = TestUtils::getFreePort();
27  repo_server += port;
28  auto p = std_::make_unique<boost::process::child>("src/libaktualizr/package_manager/dockerapp_test_repo.sh",
29  uptane_gen, repo_path, port);
30  TestUtils::waitForServer(repo_server + "/");
31  return p;
32 }
33 
34 TEST(DockerAppManager, PackageManager_Factory_Good) {
35  Config config;
36  config.pacman.type = PackageManager::kOstreeDockerApp;
37  config.pacman.sysroot = test_sysroot;
39  config.storage.path = dir.Path();
40 
41  std::shared_ptr<INvStorage> storage = INvStorage::newStorage(config.storage);
42  auto pacman = PackageManagerFactory::makePackageManager(config.pacman, config.bootloader, storage, nullptr);
43  EXPECT_TRUE(pacman);
44 }
45 
46 TEST(DockerAppManager, DockerApp_Fetch) {
47  std::string sha = Utils::readFile(test_sysroot / "ostree/repo/refs/heads/ostree/1/1/0", true);
48  Json::Value target_json;
49  target_json["hashes"]["sha256"] = sha;
50  target_json["custom"]["targetFormat"] = "OSTREE";
51  target_json["length"] = 0;
52  target_json["custom"]["docker_apps"]["app1"]["filename"] = "foo.dockerapp";
53  Uptane::Target target("pull", target_json);
54 
55  TemporaryDirectory temp_dir;
56  auto repo = temp_dir.Path();
57  auto repod = create_repo(repo);
58 
59  Config config;
60  config.pacman.type = PackageManager::kOstreeDockerApp;
61  config.pacman.sysroot = test_sysroot;
62  config.pacman.docker_apps_root = temp_dir / "docker_apps";
63  config.pacman.docker_apps.push_back("app1");
64  config.pacman.docker_apps.push_back("app2");
65  config.pacman.docker_app_bin = config.pacman.docker_compose_bin = "src/libaktualizr/package_manager/docker_fake.sh";
66  config.pacman.ostree_server = treehub_server;
67  config.uptane.repo_server = repo_server + "/repo/repo";
69  config.storage.path = dir.Path();
70 
71  // Create a fake "docker-app" that's not configured. We'll test below
72  // to ensure its removed
73  boost::filesystem::create_directories(config.pacman.docker_apps_root / "delete-this-app");
74  // This is app is configured, but not a part of the install Target, so
75  // it should get removed below
76  boost::filesystem::create_directories(config.pacman.docker_apps_root / "app2");
77 
78  std::shared_ptr<INvStorage> storage = INvStorage::newStorage(config.storage);
79  KeyManager keys(storage, config.keymanagerConfig());
80  auto client = std_::make_unique<SotaUptaneClient>(config, storage);
81  ASSERT_TRUE(client->updateImagesMeta());
82 
83  std::string targets = Utils::readFile(repo / "repo/repo/targets.json");
84  LOG_INFO << "Repo targets " << targets;
85 
86  bool result = client->package_manager_->fetchTarget(target, *(client->uptane_fetcher), keys, progress_cb, nullptr);
87  ASSERT_TRUE(result);
88 
89  auto hashes = std::vector<Uptane::Hash>{
90  Uptane::Hash(Uptane::Hash::Type::kSha256, "dfca385c923400228c8ddd3c2d572919985e48a9409a2d71dab33148017231c3"),
91  Uptane::Hash(Uptane::Hash::Type::kSha512,
92  "76b183d51f53613a450825afc6f984077b68ae7b321ba041a2b3871f3c25a9a20d964ad0b60352e5fdd09b78fd53879f4e3"
93  "fa3dcc8335b26d3bbf455803d2ecb")};
94  Uptane::Target app_target("foo.dockerapp", Uptane::EcuMap{}, hashes, 8);
95  ASSERT_TRUE(storage->checkTargetFile(app_target));
96 
97  client->package_manager_->install(target);
98  std::string content = Utils::readFile(config.pacman.docker_apps_root / "app1/docker-compose.yml");
99  ASSERT_EQ("DOCKER-APP RENDER OUTPUT\nfake contents of a docker app\n", content);
100 
101  // Make sure the unconfigured docker app has been removed:
102  ASSERT_FALSE(boost::filesystem::exists(config.pacman.docker_apps_root / "delete-this-app"));
103  ASSERT_FALSE(boost::filesystem::exists(config.pacman.docker_apps_root / "app2"));
104  ASSERT_TRUE(boost::filesystem::exists(config.pacman.docker_apps_root / "docker-compose-down-called"));
105 
106  setenv("DOCKER_APP_FAIL", "1", 1);
107  ASSERT_EQ(TargetStatus::kInvalid, client->VerifyTarget(target));
108 }
109 
110 #ifndef __NO_MAIN__
111 int main(int argc, char** argv) {
112  ::testing::InitGoogleTest(&argc, argv);
113 
114  if (argc != 3) {
115  std::cerr << "Error: " << argv[0]
116  << " requires the path to an OSTree sysroot and uptane-generator as an input argument.\n";
117  return EXIT_FAILURE;
118  }
119  uptane_gen = argv[2];
120 
121  std::string port = TestUtils::getFreePort();
122  treehub_server += port;
123  boost::process::child server_process("tests/fake_http_server/fake_test_server.py", port);
124 
125  TemporaryDirectory temp_dir;
126  // Utils::copyDir doesn't work here. Complaints about non existent symlink path
127  int r = system((std::string("cp -r ") + argv[1] + std::string(" ") + temp_dir.PathString()).c_str());
128  if (r != 0) {
129  return -1;
130  }
131  test_sysroot = (temp_dir.Path() / "ostree_repo").string();
132 
133  TestUtils::waitForServer(treehub_server + "/");
134 
135  return RUN_ALL_TESTS();
136 }
137 #endif
KeyManager
Definition: keymanager.h:13
Uptane::Hash
The hash of a file or TUF metadata.
Definition: tuf.h:209
Config
Configuration object for an aktualizr instance running on a primary ECU.
Definition: config.h:73
TemporaryDirectory
Definition: utils.h:82
result
Results of libaktualizr API calls.
Definition: results.h:13
Uptane::Target
Definition: tuf.h:238
DockerAppManager
Definition: dockerappmanager.h:9