-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathdocker_compose_util.cc
More file actions
87 lines (73 loc) · 2.85 KB
/
docker_compose_util.cc
File metadata and controls
87 lines (73 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "iceberg/test/util/docker_compose_util.h"
#include <unistd.h>
#include <cctype>
#include <chrono>
#include <format>
#include <print>
#include <sys/stat.h>
#include "iceberg/test/util/cmd_util.h"
namespace iceberg {
namespace {
/// \brief Generate a unique test data directory path
std::filesystem::path GenerateTestDataDir() {
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
auto pid = getpid();
return {std::format("/tmp/iceberg-test-{}-{}", timestamp, pid)};
}
} // namespace
DockerCompose::DockerCompose(std::string project_name,
std::filesystem::path docker_compose_dir)
: project_name_(std::move(project_name)),
docker_compose_dir_(std::move(docker_compose_dir)),
test_data_dir_(GenerateTestDataDir()) {
std::filesystem::create_directories(test_data_dir_);
chmod(test_data_dir_.c_str(), 0777);
}
DockerCompose::~DockerCompose() { Down(); }
void DockerCompose::Up() {
auto cmd = BuildDockerCommand({"up", "-d", "--wait", "--timeout", "60"});
return cmd.RunCommand("docker compose up");
}
void DockerCompose::Down() {
auto cmd = BuildDockerCommand({"down", "-v", "--remove-orphans"});
cmd.RunCommand("docker compose down");
// Clean up the test data directory
if (!test_data_dir_.empty() && std::filesystem::exists(test_data_dir_)) {
std::error_code ec;
std::filesystem::remove_all(test_data_dir_, ec);
if (!ec) {
std::println("[INFO] Cleaned up test data directory: {}", test_data_dir_.string());
}
}
}
Command DockerCompose::BuildDockerCommand(const std::vector<std::string>& args) const {
Command cmd("docker");
// Set working directory
cmd.CurrentDir(docker_compose_dir_);
// Set the test data directory environment variable
cmd.Env("ICEBERG_TEST_DATA_DIR", test_data_dir_.string());
// Use 'docker compose' subcommand with project name
cmd.Arg("compose").Arg("-p").Arg(project_name_).Args(args);
return cmd;
}
} // namespace iceberg