make primary executable a more robust integration test harness

This commit is contained in:
Emil Lerch 2021-06-23 18:23:07 -07:00
parent 06479b8cb7
commit 93536aa4ad
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -2,6 +2,8 @@ const std = @import("std");
const aws = @import("aws.zig");
const json = @import("json.zig");
var verbose = false;
pub fn log(
comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral),
@ -9,7 +11,7 @@ pub fn log(
args: anytype,
) void {
// Ignore awshttp messages
if (scope == .awshttp and @enumToInt(level) >= @enumToInt(std.log.Level.debug))
if (!verbose and scope == .awshttp and @enumToInt(level) >= @enumToInt(std.log.Level.debug))
return;
const scope_prefix = "(" ++ @tagName(scope) ++ "): ";
const prefix = "[" ++ @tagName(level) ++ "] " ++ scope_prefix;
@ -21,60 +23,90 @@ pub fn log(
nosuspend stderr.print(prefix ++ format ++ "\n", args) catch return;
}
const Tests = enum {
query_no_input,
query_with_input,
ec2_query_no_input,
};
pub fn main() anyerror!void {
// Uncomment if you want to log allocations
// const file = try std.fs.cwd().createFile("/tmp/allocations.log", .{ .truncate = true });
// defer file.close();
// var child_allocator = std.heap.c_allocator;
// const allocator = &std.heap.loggingAllocator(child_allocator, file.writer()).allocator;
// Flip to true to run a second time. This will help debug
// allocation/deallocation issues
const test_twice = false;
// Flip to true to run through the json parsing changes made to stdlib
const test_json = false;
if (test_json) try jsonFun();
const c_allocator = std.heap.c_allocator;
var gpa = std.heap.GeneralPurposeAllocator(.{}){
.backing_allocator = c_allocator,
};
defer if (!gpa.deinit()) @panic("memory leak detected");
defer _ = gpa.deinit();
const allocator = &gpa.allocator;
// const allocator = std.heap.c_allocator;
var tests = std.ArrayList(Tests).init(allocator);
defer tests.deinit();
var args = std.process.args();
while (args.next(allocator)) |arg_or_error| {
const arg = try arg_or_error;
defer allocator.free(arg);
if (std.mem.eql(u8, "-v", arg)) {
verbose = true;
continue;
}
inline for (@typeInfo(Tests).Enum.fields) |f| {
if (std.mem.eql(u8, f.name, arg)) {
try tests.append(@field(Tests, f.name));
break;
}
}
}
if (tests.items.len == 0) {
inline for (@typeInfo(Tests).Enum.fields) |f|
try tests.append(@field(Tests, f.name));
}
const options = aws.Options{
.region = "us-west-2",
};
std.log.info("Start", .{});
std.log.info("Start\n", .{});
var client = aws.Aws.init(allocator);
defer client.deinit();
const services = aws.Services(.{.sts}){};
const resp = try client.call(services.sts.get_caller_identity.Request{}, options);
// TODO: This is a bit wonky. Root cause is lack of declarations in
// comptime-generated types
defer resp.deinit();
if (test_twice) {
std.time.sleep(1000 * std.time.ns_per_ms);
std.log.info("second request", .{});
var client2 = aws.Aws.init(allocator);
defer client2.deinit();
const resp2 = try client2.call(services.sts.get_caller_identity.Request{}, options); // catch here and try alloc?
defer resp2.deinit();
for (tests.items) |t| {
std.log.info("===== Start Test: {s} =====", .{@tagName(t)});
switch (t) {
.query_no_input => {
const resp = try client.call(services.sts.get_caller_identity.Request{}, options);
defer resp.deinit();
std.log.info("arn: {s}", .{resp.response.arn});
std.log.info("id: {s}", .{resp.response.user_id});
std.log.info("account: {s}", .{resp.response.account});
std.log.info("requestId: {s}", .{resp.response_metadata.request_id});
},
.query_with_input => {
// TODO: Find test without sensitive info
const access = try client.call(services.sts.get_session_token.Request{
.duration_seconds = 900,
}, options);
defer access.deinit();
std.log.info("access key: {s}", .{access.response.credentials.access_key_id});
},
.ec2_query_no_input => {
// TODO: Find test
},
}
std.log.info("===== End Test: {s} =====\n", .{@tagName(t)});
}
std.log.info("arn: {s}", .{resp.response.arn});
std.log.info("id: {s}", .{resp.response.user_id});
std.log.info("account: {s}", .{resp.response.account});
std.log.info("requestId: {s}", .{resp.response_metadata.request_id});
// if (test_twice) {
// std.time.sleep(1000 * std.time.ns_per_ms);
// std.log.info("second request", .{});
//
// var client2 = aws.Aws.init(allocator);
// defer client2.deinit();
// const resp2 = try client2.call(services.sts.get_caller_identity.Request{}, options); // catch here and try alloc?
// defer resp2.deinit();
// }
std.log.info("Departing main", .{});
std.log.info("===== Tests complete =====", .{});
}
// TODO: Move into json.zig
pub fn jsonFun() !void {
// Standard behavior
const payload =