1 #include <gtest/gtest.h> 8 #include <boost/algorithm/hex.hpp> 9 #include <boost/filesystem.hpp> 11 #include "libaktualizr/config.h" 12 #include "package_manager/ostreemanager.h" 13 #include "storage/invstorage.h" 14 #include "utilities/utils.h" 16 boost::filesystem::path test_sysroot;
22 config.pacman.ostree_server =
"bad-url";
23 config.pacman.type = PACKAGE_MANAGER_OSTREE;
24 config.pacman.sysroot = test_sysroot;
25 config.storage.path = temp_dir.Path();
27 std::shared_ptr<INvStorage> storage = INvStorage::newStorage(config.storage);
28 KeyManager keys(storage, config.keymanagerConfig());
30 Json::Value target_json_test;
31 target_json_test[
"hashes"][
"sha256"] =
"some_hash";
32 target_json_test[
"length"] = 0;
35 OstreeManager::pull(config.pacman.sysroot, config.pacman.ostree_server, keys, target_test);
38 EXPECT_EQ(result.description,
"Failed to parse uri: bad-url");
45 config.pacman.ostree_server =
"bad-url";
46 config.pacman.type = PACKAGE_MANAGER_OSTREE;
47 config.pacman.sysroot = test_sysroot;
48 config.storage.path = temp_dir.Path();
50 std::shared_ptr<INvStorage> storage = INvStorage::newStorage(config.storage);
51 std::string ca = Utils::readFile(
"tests/test_data/prov/root.crt");
52 std::string pkey = Utils::readFile(
"tests/test_data/prov/pkey.pem");
53 std::string cert = Utils::readFile(
"tests/test_data/prov/client.pem");
54 storage->storeTlsCa(ca);
55 storage->storeTlsPkey(pkey);
56 storage->storeTlsCert(cert);
57 KeyManager keys(storage, config.keymanagerConfig());
59 Json::Value target_json_test;
60 target_json_test[
"hashes"][
"sha256"] =
"some_hash";
61 target_json_test[
"length"] = 0;
64 OstreeManager::pull(config.pacman.sysroot, config.pacman.ostree_server, keys, target_test);
67 EXPECT_EQ(result.description,
"Failed to parse uri: bad-url");
72 Json::Value target_json;
73 target_json[
"hashes"][
"sha256"] =
"hash";
74 target_json[
"length"] = 0;
78 config.pacman.type = PACKAGE_MANAGER_OSTREE;
79 config.pacman.sysroot = test_sysroot;
80 config.storage.path = temp_dir.Path();
82 std::shared_ptr<INvStorage> storage = INvStorage::newStorage(config.storage);
83 OstreeManager ostree(config.pacman, config.bootloader, storage,
nullptr);
86 EXPECT_EQ(result.description,
"Refspec 'hash' not found");
93 config.pacman.type = PACKAGE_MANAGER_OSTREE;
94 config.pacman.sysroot =
"sysroot-that-is-missing";
95 config.storage.path = temp_dir.Path();
96 std::shared_ptr<INvStorage> storage = INvStorage::newStorage(config.storage);
97 EXPECT_THROW(
OstreeManager ostree(config.pacman, config.bootloader, storage,
nullptr), std::runtime_error);
103 boost::filesystem::path packages_file = temp_dir /
"package.manifest";
107 content +=
"vim 1.0\n";
108 content +=
"emacs 2.0\n";
109 content +=
"bash 1.1\n";
111 Utils::writeFile(packages_file, content);
114 config.pacman.type = PACKAGE_MANAGER_OSTREE;
115 config.pacman.sysroot = test_sysroot;
116 config.pacman.packages_file = packages_file;
117 config.storage.path = temp_dir.Path();
119 std::shared_ptr<INvStorage> storage = INvStorage::newStorage(config.storage);
120 OstreeManager ostree(config.pacman, config.bootloader, storage,
nullptr);
121 Json::Value packages = ostree.getInstalledPackages();
122 EXPECT_EQ(packages[0][
"name"].asString(),
"vim");
123 EXPECT_EQ(packages[0][
"version"].asString(),
"1.0");
124 EXPECT_EQ(packages[1][
"name"].asString(),
"emacs");
125 EXPECT_EQ(packages[1][
"version"].asString(),
"2.0");
126 EXPECT_EQ(packages[2][
"name"].asString(),
"bash");
127 EXPECT_EQ(packages[2][
"version"].asString(),
"1.1");
134 config.pacman.type = PACKAGE_MANAGER_OSTREE;
135 config.pacman.sysroot = test_sysroot;
136 config.storage.path = temp_dir.Path();
138 std::shared_ptr<INvStorage> storage = INvStorage::newStorage(config.storage);
139 KeyManager keys(storage, config.keymanagerConfig());
142 OstreeRepo *repo =
nullptr;
143 GError *error =
nullptr;
144 std::shared_ptr<OstreeSysroot> sysroot = OstreeManager::LoadSysroot(config.pacman.sysroot);
145 EXPECT_TRUE(ostree_sysroot_get_repo(sysroot.get(), &repo,
nullptr, &error));
146 EXPECT_TRUE(OstreeManager::addRemote(repo, config.pacman.ostree_server, keys));
148 g_autofree
char *url =
nullptr;
149 EXPECT_TRUE(ostree_repo_get_remote_option(repo, remote,
"url",
nullptr, &url, &error));
150 EXPECT_EQ(url, config.pacman.ostree_server);
152 gboolean out_gpg_verify;
153 EXPECT_TRUE(ostree_repo_get_remote_boolean_option(repo, remote,
"gpg-verify", FALSE, &out_gpg_verify, &error));
155 g_autofree
char *ostree_cert =
nullptr;
156 EXPECT_TRUE(ostree_repo_get_remote_option(repo, remote,
"tls-client-cert-path",
nullptr, &ostree_cert, &error));
157 EXPECT_EQ(ostree_cert,
nullptr);
159 g_autofree
char *ostree_key =
nullptr;
160 EXPECT_TRUE(ostree_repo_get_remote_option(repo, remote,
"tls-client-key-path",
nullptr, &ostree_key, &error));
161 EXPECT_EQ(ostree_key,
nullptr);
163 g_autofree
char *ostree_ca =
nullptr;
164 EXPECT_TRUE(ostree_repo_get_remote_option(repo, remote,
"tls-ca-path",
nullptr, &ostree_ca, &error));
165 EXPECT_EQ(ostree_ca,
nullptr);
167 g_object_unref(repo);
174 config.pacman.type = PACKAGE_MANAGER_OSTREE;
175 config.pacman.sysroot = test_sysroot;
176 config.storage.path = temp_dir.Path();
178 std::shared_ptr<INvStorage> storage = INvStorage::newStorage(config.storage);
179 std::string ca = Utils::readFile(
"tests/test_data/prov/root.crt");
180 std::string pkey = Utils::readFile(
"tests/test_data/prov/pkey.pem");
181 std::string cert = Utils::readFile(
"tests/test_data/prov/client.pem");
182 storage->storeTlsCa(ca);
183 storage->storeTlsPkey(pkey);
184 storage->storeTlsCert(cert);
185 KeyManager keys(storage, config.keymanagerConfig());
188 OstreeRepo *repo =
nullptr;
189 GError *error =
nullptr;
190 std::shared_ptr<OstreeSysroot> sysroot = OstreeManager::LoadSysroot(config.pacman.sysroot);
191 EXPECT_TRUE(ostree_sysroot_get_repo(sysroot.get(), &repo,
nullptr, &error));
192 EXPECT_TRUE(OstreeManager::addRemote(repo, config.pacman.ostree_server, keys));
194 g_autofree
char *url =
nullptr;
195 EXPECT_TRUE(ostree_repo_get_remote_option(repo, remote,
"url",
nullptr, &url, &error));
196 EXPECT_EQ(url, config.pacman.ostree_server);
198 gboolean out_gpg_verify;
199 EXPECT_TRUE(ostree_repo_get_remote_boolean_option(repo, remote,
"gpg-verify", FALSE, &out_gpg_verify, &error));
201 g_autofree
char *ostree_cert =
nullptr;
202 EXPECT_TRUE(ostree_repo_get_remote_option(repo, remote,
"tls-client-cert-path",
nullptr, &ostree_cert, &error));
203 EXPECT_EQ(ostree_cert, keys.getCertFile());
205 g_autofree
char *ostree_key =
nullptr;
206 EXPECT_TRUE(ostree_repo_get_remote_option(repo, remote,
"tls-client-key-path",
nullptr, &ostree_key, &error));
207 EXPECT_EQ(ostree_key, keys.getPkeyFile());
209 g_autofree
char *ostree_ca =
nullptr;
210 EXPECT_TRUE(ostree_repo_get_remote_option(repo, remote,
"tls-ca-path",
nullptr, &ostree_ca, &error));
211 EXPECT_EQ(ostree_ca, keys.getCaFile());
213 g_object_unref(repo);
217 int main(
int argc,
char **argv) {
218 ::testing::InitGoogleTest(&argc, argv);
221 std::cerr <<
"Error: " << argv[0] <<
" requires the path to an OSTree sysroot as an input argument.\n";
226 test_sysroot = temp_sysroot /
"sysroot";
228 int r = system((std::string(
"cp -r ") + argv[1] + std::string(
" ") + test_sysroot.string()).c_str());
233 return RUN_ALL_TESTS();
Configuration object for an aktualizr instance running on a Primary ECU.
Package installation failed.
Results of libaktualizr API calls.