-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.zig
More file actions
53 lines (43 loc) · 1.72 KB
/
basic.zig
File metadata and controls
53 lines (43 loc) · 1.72 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
/// Basic testcontainers-zig example.
///
/// Starts an nginx container, waits for it to become ready via an HTTP check,
/// fetches the root page, then terminates the container.
///
/// Run with:
/// zig build example
const std = @import("std");
const tc = @import("testcontainers");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
std.log.info("Starting nginx container...", .{});
const ctr = try tc.run(allocator, "nginx:latest", .{
.exposed_ports = &.{"80/tcp"},
.wait_strategy = tc.wait.forHttp("/"),
});
defer {
ctr.terminate() catch |err| {
std.log.err("Failed to terminate container: {}", .{err});
};
ctr.deinit();
tc.deinitProvider();
}
const port = try ctr.mappedPort("80/tcp", allocator);
std.log.info("nginx is ready on localhost:{d}", .{port});
// Verify the nginx page is reachable using std.http.Client
const url = try std.fmt.allocPrint(allocator, "http://localhost:{d}/", .{port});
defer allocator.free(url);
var http_client: std.http.Client = .{ .allocator = allocator };
defer http_client.deinit();
const fetch_result = try http_client.fetch(.{
.location = .{ .url = url },
});
std.log.info("HTTP status: {d}", .{@intFromEnum(fetch_result.status)});
// Demonstrate exec
const result = try ctr.exec(&.{ "echo", "hello from container" });
defer allocator.free(result.output);
std.log.info("Exec exit code: {d}", .{result.exit_code});
std.log.info("Exec output: {s}", .{std.mem.trim(u8, result.output, "\n\r ")});
std.log.info("Done — container will be terminated on defer.", .{});
}