From 238952d127fbf0bac6089e1373202f6b52bafd70 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Fri, 23 Aug 2024 12:53:58 -0700 Subject: [PATCH] add iam getRole test This test triggers the scenario that a required response element exists, which forces our check for "we do not expect data for this call" to be comptime. It previously was accidentally runtime, which was solved by making expected_body_field_len a comptime var. --- src/aws.zig | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/aws.zig b/src/aws.zig index 80b9ba9..28d2cbe 100644 --- a/src/aws.zig +++ b/src/aws.zig @@ -353,7 +353,7 @@ pub fn Request(comptime request_action: anytype) type { // First, we need to determine if we care about a response at all // If the expected result has no fields, there's no sense in // doing any more work. Let's bail early - var expected_body_field_len = std.meta.fields(action.Response).len; + comptime var expected_body_field_len = std.meta.fields(action.Response).len; if (@hasDecl(action.Response, "http_header")) expected_body_field_len -= std.meta.fields(@TypeOf(action.Response.http_header)).len; if (@hasDecl(action.Response, "http_payload")) { @@ -379,8 +379,6 @@ pub fn Request(comptime request_action: anytype) type { // We don't care about the body if there are no fields we expect there... if (std.meta.fields(action.Response).len == 0 or expected_body_field_len == 0) { - // ^^ This should be redundant, but is necessary. I suspect it's a compiler quirk - // // Do we care if an unexpected body comes in? return FullResponseType{ .response = .{}, @@ -1636,6 +1634,58 @@ test "query_no_input: sts getCallerIdentity comptime" { try std.testing.expectEqualStrings("123456789012", call.response.account.?); try std.testing.expectEqualStrings("8f0d54da-1230-40f7-b4ac-95015c4b84cd", call.response_metadata.request_id); } +test "query_with_input: iam getRole runtime" { + // sqs switched from query to json in aws sdk for go v2 commit f5a08768ef820ff5efd62a49ba50c61c9ca5dbcb + const allocator = std.testing.allocator; + var test_harness = TestSetup.init(.{ + .allocator = allocator, + .server_response = + \\ + \\ + \\ + \\ /application_abc/component_xyz/ + \\ arn:aws:iam::123456789012:role/application_abc/component_xyz/S3Access + \\ S3Access + \\ + \\ {"Version":"2012-10-17","Statement":[{"Effect":"Allow", + \\ "Principal":{"Service":["ec2.amazonaws.com"]},"Action":["sts:AssumeRole"]}]} + \\ + \\ 2012-05-08T23:34:01Z + \\ AROADBQP57FF2AEXAMPLE + \\ + \\ 2019-11-20T17:09:20Z + \\ us-east-1 + \\ + \\ + \\ + \\ + \\ df37e965-9967-11e1-a4c3-270EXAMPLE04 + \\ + \\ + , + .server_response_headers = &.{ + .{ .name = "Content-Type", .value = "text/xml" }, + .{ .name = "x-amzn-RequestId", .value = "df37e965-9967-11e1-a4c3-270EXAMPLE04" }, + }, + }); + defer test_harness.deinit(); + const options = try test_harness.start(); + const iam = (Services(.{.iam}){}).iam; + const call = try test_harness.client.call(iam.get_role.Request{ + .role_name = "S3Access", + }, options); + defer call.deinit(); + test_harness.stop(); + // Request expectations + try std.testing.expectEqual(std.http.Method.POST, test_harness.request_options.request_method); + try std.testing.expectEqualStrings("/", test_harness.request_options.request_target); + try std.testing.expectEqualStrings( + \\Action=GetRole&Version=2010-05-08&RoleName=S3Access + , test_harness.request_options.request_body); + // Response expectations + try std.testing.expectEqualStrings("arn:aws:iam::123456789012:role/application_abc/component_xyz/S3Access", call.response.role.arn); + try std.testing.expectEqualStrings("df37e965-9967-11e1-a4c3-270EXAMPLE04", call.response_metadata.request_id); +} test "query_with_input: sts getAccessKeyInfo runtime" { // sqs switched from query to json in aws sdk for go v2 commit f5a08768ef820ff5efd62a49ba50c61c9ca5dbcb const allocator = std.testing.allocator;