Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
ostree_http_repo_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <boost/process.hpp>
4 
5 #include "authenticate.h"
6 #include "deploy.h"
7 #include "garage_common.h"
8 #include "ostree_http_repo.h"
9 #include "ostree_ref.h"
10 #include "server_credentials.h"
11 #include "test_utils.h"
12 
13 std::string port;
14 
15 /* Verify a remote OSTree repository. */
16 TEST(http_repo, valid_repo) {
17  TreehubServer server;
18  server.root_url("http://localhost:" + port);
19  OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
20  EXPECT_TRUE(src_repo->LooksValid());
21 }
22 
23 /* Reject an invalid remote OSTree repository. */
24 TEST(http_repo, invalid_repo) {
25  TreehubServer server;
26  server.root_url("http://wronghost");
27  OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
28  EXPECT_FALSE(src_repo->LooksValid());
29 }
30 
31 /* Find OSTree ref in remote repository. */
32 TEST(http_repo, getRef) {
33  TreehubServer server;
34  server.root_url("http://localhost:" + port);
35  OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
36  EXPECT_EQ(src_repo->GetRef("master").GetHash().string(),
37  std::string("b9ac1e45f9227df8ee191b6e51e09417bd36c6ebbeff999431e3073ac50f0563"));
38 }
39 
40 /* Fetch OSTree object from remote repository.
41  * Check all valid OSTree object extensions. */
42 TEST(http_repo, GetObject) {
43  TreehubServer server;
44  server.root_url("http://localhost:" + port);
45  OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
46  const uint8_t hash[32] = {0x44, 0x6a, 0x0e, 0xf1, 0x1b, 0x7c, 0xc1, 0x67, 0xf3, 0xb6, 0x03,
47  0xe5, 0x85, 0xc7, 0xee, 0xee, 0xb6, 0x75, 0xfa, 0xa4, 0x12, 0xd5,
48  0xec, 0x73, 0xf6, 0x29, 0x88, 0xeb, 0x0b, 0x6c, 0x54, 0x88};
49  auto object = src_repo->GetObject(hash, OstreeObjectType::OSTREE_OBJECT_TYPE_DIR_META);
50  std::stringstream s;
51  s << object;
52  EXPECT_EQ(s.str(), std::string("44/6a0ef11b7cc167f3b603e585c7eeeeb675faa412d5ec73f62988eb0b6c5488.dirmeta"));
53 }
54 
55 /* Abort if OSTree object is not found after retry. */
56 TEST(http_repo, GetWrongObject) {
57  TreehubServer server;
58  server.root_url("http://localhost:" + port);
59  OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
60  const uint8_t hash[32] = {0x00, 0x28, 0xda, 0xc4, 0x2b, 0x76, 0xc2, 0x01, 0x5e, 0xe3, 0xc4,
61  0x1c, 0xc4, 0x18, 0x3b, 0xb8, 0xb5, 0xc7, 0x90, 0xfd, 0x21, 0xfa,
62  0x5c, 0xfa, 0x08, 0x02, 0xc6, 0xe1, 0x1f, 0xd0, 0xed, 0xbe};
63  EXPECT_THROW(src_repo->GetObject(hash, OstreeObjectType::OSTREE_OBJECT_TYPE_DIR_META), OSTreeObjectMissing);
64 }
65 
66 /* Retry fetch if not found after first try.
67  *
68  * This test uses servers that drop every other request. The test should pass
69  * anyway because we've learned not to always trust the server the first time
70  * and to try again before giving up. */
71 TEST(http_repo, bad_connection) {
72  TemporaryDirectory src_dir, dst_dir;
73  std::string sp = TestUtils::getFreePort();
74 
75  boost::process::child server_process("tests/sota_tools/treehub_server.py", std::string("-p"), sp, std::string("-d"),
76  src_dir.PathString(), std::string("-f2"), std::string("--create"));
77  TestUtils::waitForServer("http://localhost:" + sp + "/");
78 
79  TreehubServer server;
80  server.root_url("http://localhost:" + sp);
81  OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
82 
83  std::string dp = TestUtils::getFreePort();
84  Json::Value auth;
85  auth["ostree"]["server"] = std::string("https://localhost:") + dp;
86  Utils::writeFile(dst_dir.Path() / "auth.json", auth);
87  boost::process::child deploy_server_process("tests/sota_tools/treehub_server.py", std::string("-p"), dp,
88  std::string("-d"), dst_dir.PathString(), std::string("-f2"),
89  std::string("--tls"));
90  TestUtils::waitForServer("https://localhost:" + dp + "/");
91 
92  boost::filesystem::path filepath = (dst_dir.Path() / "auth.json").string();
93  boost::filesystem::path cert_path = "tests/fake_http_server/server.crt";
94 
95  auto hash = OSTreeHash::Parse("b9ac1e45f9227df8ee191b6e51e09417bd36c6ebbeff999431e3073ac50f0563");
96  TreehubServer push_server;
97  EXPECT_EQ(authenticate(cert_path.string(), ServerCredentials(filepath), push_server), EXIT_SUCCESS);
98  UploadToTreehub(src_repo, push_server, hash, RunMode::kDefault, 1);
99 
100  std::string diff("diff -r ");
101  std::string src_path((src_dir.Path() / "objects").string() + " ");
102  std::string repo_path((src_repo->root() / "objects").string() + " ");
103  std::string dst_path((dst_dir.Path() / "objects").string() + " ");
104 
105  int result = system((diff + src_path + repo_path).c_str());
106  result |= system((diff + repo_path + dst_path).c_str());
107 
108  EXPECT_EQ(result, 0) << "Diff between source and destination repos is nonzero.";
109 }
110 
111 TEST(http_repo, root) {
112  TreehubServer server;
113  server.root_url("http://localhost:" + port);
114  OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
115  EXPECT_TRUE(src_repo->LooksValid());
116  std::string conf = Utils::readFile(src_repo->root() / "config");
117  EXPECT_EQ(conf, std::string("[core]\nrepo_version=1\nmode=archive-z2\n"));
118 }
119 
120 #ifndef __NO_MAIN__
121 int main(int argc, char **argv) {
122  ::testing::InitGoogleTest(&argc, argv);
123 
124  std::string server = "tests/sota_tools/treehub_server.py";
125  port = TestUtils::getFreePort();
126 
127  boost::process::child server_process(server, std::string("-p"), port, std::string("--create"));
128  TestUtils::waitForServer("http://localhost:" + port + "/");
129 
130  return RUN_ALL_TESTS();
131 }
132 #endif
133 
134 // vim: set tabstop=2 shiftwidth=2 expandtab:
OSTreeObjectMissing
Thrown by GetObject when the object requested is not present in the repository.
Definition: ostree_repo.h:46
ServerCredentials
Definition: server_credentials.h:25
TreehubServer
Definition: treehub_server.h:11
TemporaryDirectory
Definition: utils.h:82
OSTreeHash::Parse
static OSTreeHash Parse(const std::string &hash)
Parse an OSTree hash from a string.
Definition: ostree_hash.cc:7
result
Results of libaktualizr API calls.
Definition: results.h:12
garage_common.h
RunMode::kDefault
@ kDefault
Default operation.