1 #include <gtest/gtest.h>
4 #include <boost/process.hpp>
6 #include "authenticate.h"
8 #include "ostree_dir_repo.h"
9 #include "ostree_object.h"
10 #include "request_pool.h"
11 #include "server_credentials.h"
12 #include "test_utils.h"
15 std::string repo_path;
18 TEST(OstreeObject, ConstructorBad) {
20 EXPECT_THROW(
OSTreeObject(bad_repo,
"bad"), std::runtime_error);
24 TEST(OstreeObject, ConstructorGood) {
26 OSTreeHash hash = good_repo.GetRef(
"master").GetHash();
27 boost::filesystem::path objpath = hash.string().insert(2, 1,
'/');
35 static void MakeTestRequest(
const OSTreeRepo::ptr src_repo,
const OSTreeHash& hash,
const long expected) {
36 curl_global_init(CURL_GLOBAL_DEFAULT);
37 CURLM* multi = curl_multi_init();
38 curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_HTTP1 | CURLPIPE_MULTIPLEX);
41 push_server.root_url(
"http://localhost:" + port);
42 OSTreeObject::ptr
object = src_repo->GetObject(hash, OstreeObjectType::OSTREE_OBJECT_TYPE_COMMIT);
44 object->MakeTestRequest(push_server, multi);
49 CURLMcode mc = curl_multi_perform(multi, &running_requests);
50 EXPECT_EQ(mc, CURLM_OK);
51 }
while (running_requests > 0);
55 CURLMsg* msg = curl_multi_info_read(multi, &msgs_in_queue);
56 if ((msg !=
nullptr) && msg->msg == CURLMSG_DONE) {
57 OSTreeObject::ptr h = ostree_object_from_curl(msg->easy_handle);
59 EXPECT_EQ(h->current_operation_, CurrentOp::kOstreeObjectPresenceCheck);
63 EXPECT_GE(h->refcount_, 1);
65 curl_easy_getinfo(h->curl_handle_, CURLINFO_RESPONSE_CODE, &rescode);
66 EXPECT_EQ(rescode, expected);
67 curl_multi_remove_handle(multi, h->curl_handle_);
68 curl_easy_cleanup(h->curl_handle_);
69 h->curl_handle_ =
nullptr;
71 }
while (msgs_in_queue > 0);
73 curl_multi_cleanup(multi);
74 curl_global_cleanup();
80 TEST(OstreeObject, MakeTestRequestPresent) {
81 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeDirRepo>(repo_path);
82 OSTreeHash hash = src_repo->GetRef(
"master").GetHash();
83 OstreeObject_Request_Test::MakeTestRequest(src_repo, hash, 200);
90 TEST(OstreeObject, MakeTestRequestMissing) {
91 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeDirRepo>(
"tests/sota_tools/bigger_repo");
92 OSTreeHash hash = src_repo->GetRef(
"master").GetHash();
93 OstreeObject_Request_Test::MakeTestRequest(src_repo, hash, 404);
97 TEST(OstreeObject, UploadDryRun) {
99 push_server.root_url(
"http://localhost:" + port);
101 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeDirRepo>(repo_path);
102 OSTreeHash hash = src_repo->GetRef(
"master").GetHash();
103 OSTreeObject::ptr
object = src_repo->GetObject(hash, OstreeObjectType::OSTREE_OBJECT_TYPE_COMMIT);
105 object->is_on_server_ = PresenceOnServer::kObjectStateUnknown;
106 object->current_operation_ = CurrentOp::kOstreeObjectPresenceCheck;
108 EXPECT_EQ(object->is_on_server_, PresenceOnServer::kObjectPresent);
110 EXPECT_EQ(object->current_operation_, CurrentOp::kOstreeObjectPresenceCheck);
116 TEST(OstreeObject, UploadFail) {
118 push_server.root_url(
"http://localhost:" + port);
120 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeDirRepo>(repo_path);
121 OSTreeHash hash = src_repo->GetRef(
"master").GetHash();
122 OSTreeObject::ptr
object = src_repo->GetObject(hash, OstreeObjectType::OSTREE_OBJECT_TYPE_COMMIT);
124 object->is_on_server_ = PresenceOnServer::kObjectStateUnknown;
125 object->current_operation_ = CurrentOp::kOstreeObjectPresenceCheck;
127 EXPECT_EQ(object->is_on_server_, PresenceOnServer::kObjectStateUnknown);
128 EXPECT_EQ(object->current_operation_, CurrentOp::kOstreeObjectUploading);
132 TEST(OstreeObject, UploadSuccess) {
133 curl_global_init(CURL_GLOBAL_DEFAULT);
134 CURLM* multi = curl_multi_init();
135 curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_HTTP1 | CURLPIPE_MULTIPLEX);
138 const std::string dp = TestUtils::getFreePort();
140 auth[
"ostree"][
"server"] = std::string(
"https://localhost:") + dp;
141 Utils::writeFile(temp_dir.Path() /
"auth.json", auth);
142 boost::process::child deploy_server_process(
"tests/sota_tools/treehub_server.py", std::string(
"-p"), dp,
143 std::string(
"-d"), temp_dir.Path().string(), std::string(
"--tls"));
144 TestUtils::waitForServer(
"https://localhost:" + dp +
"/");
147 push_server.root_url(
"https://localhost:" + dp);
149 boost::filesystem::path filepath = (temp_dir.Path() /
"auth.json").
string();
150 boost::filesystem::path cert_path =
"tests/fake_http_server/server.crt";
151 EXPECT_EQ(authenticate(cert_path.string(),
ServerCredentials(filepath), push_server), EXIT_SUCCESS);
153 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeDirRepo>(repo_path);
154 OSTreeHash hash = src_repo->GetRef(
"master").GetHash();
155 OSTreeObject::ptr
object = src_repo->GetObject(hash, OstreeObjectType::OSTREE_OBJECT_TYPE_COMMIT);
160 int running_requests;
162 CURLMcode mc = curl_multi_perform(multi, &running_requests);
163 EXPECT_EQ(mc, CURLM_OK);
164 }
while (running_requests > 0);
168 CURLMsg* msg = curl_multi_info_read(multi, &msgs_in_queue);
169 if ((msg !=
nullptr) && msg->msg == CURLMSG_DONE) {
170 OSTreeObject::ptr h = ostree_object_from_curl(msg->easy_handle);
171 EXPECT_EQ(
object, h);
172 EXPECT_EQ(h->current_operation_, CurrentOp::kOstreeObjectUploading);
176 EXPECT_GE(h->refcount_, 1);
178 curl_easy_getinfo(h->curl_handle_, CURLINFO_RESPONSE_CODE, &rescode);
179 EXPECT_EQ(rescode, 204);
181 curl_multi_remove_handle(multi, h->curl_handle_);
182 curl_easy_cleanup(h->curl_handle_);
183 h->curl_handle_ =
nullptr;
185 }
while (msgs_in_queue > 0);
187 curl_multi_cleanup(multi);
188 curl_global_cleanup();
192 int main(
int argc,
char** argv) {
193 ::testing::InitGoogleTest(&argc, argv);
195 std::string server =
"tests/sota_tools/treehub_server.py";
196 port = TestUtils::getFreePort();
198 repo_path = repo_dir.PathString();
200 boost::process::child server_process(server, std::string(
"-p"), port, std::string(
"-d"), repo_path,
201 std::string(
"--create"));
202 TestUtils::waitForServer(
"http://localhost:" + port +
"/");
204 return RUN_ALL_TESTS();