Skip to content

Commit

Permalink
add test that verifies SRA order of operations (#3025)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws authored Mar 4, 2025
1 parent 3d547b0 commit 8f2dd23
Show file tree
Hide file tree
Showing 824 changed files with 599,202 additions and 1 deletion.
409 changes: 409 additions & 0 deletions .changelog/95191f0d1ce44022aa8cc3ff1871bbb2.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion SMITHY_GO_CODEGEN_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b5c304bb2a4b0306cb8de41bf2d842fe4e76eaf6
af7d6d6b39cbb90e1dd09fcec8bc7c7e8883b2a9
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package software.amazon.smithy.aws.go.codegen.customization;

import software.amazon.smithy.go.codegen.GoCodegenContext;
import software.amazon.smithy.go.codegen.GoWriter;
import software.amazon.smithy.go.codegen.SmithyGoDependency;
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.model.knowledge.TopDownIndex;

import static software.amazon.smithy.go.codegen.GoWriter.goTemplate;

/**
* Validates that service client operations are performed in the orders specified by the Smithy Reference Architecture (SRA).
*/
public class SraOperationOrderTest implements GoIntegration {
@Override
public void writeAdditionalFiles(GoCodegenContext ctx) {
ctx.writerDelegator().useFileWriter("sra_operation_order_test.go", ctx.settings().getModuleName(), writer -> {
writer.write(renderCommonTestSource());

TopDownIndex.of(ctx.model())
.getContainedOperations(ctx.settings().getService(ctx.model()))
.forEach(it -> {
var operationName = ctx.symbolProvider().toSymbol(it).getName();
writer.write(renderTest(operationName));
});
});
}

private GoWriter.Writable renderCommonTestSource() {
return goTemplate("""
$D $D
var errTestReturnEarly = errors.New("errTestReturnEarly")
func captureMiddlewareStack(stack *middleware.Stack) func(*middleware.Stack) error {
return func(inner *middleware.Stack) error {
*stack = *inner
return errTestReturnEarly
}
}
""", SmithyGoDependency.ERRORS, SmithyGoDependency.SMITHY_MIDDLEWARE);
}

private GoWriter.Writable renderTest(String operationName) {
return goTemplate("""
$1D $2D $3D $4D $5D $6D
func TestOp$7LSRAOperationOrder(t *testing.T) {
expect := []string{
"OperationSerializer",
"Retry",
"ResolveAuthScheme",
"GetIdentity",
"ResolveEndpointV2",
"Signing",
"OperationDeserializer",
}
var captured middleware.Stack
svc := New(Options{
APIOptions: []func(*middleware.Stack) error{
captureMiddlewareStack(&captured),
},
})
_, err := svc.$7L(context.Background(), nil)
if err != nil && !errors.Is(err, errTestReturnEarly) {
t.Fatalf("unexpected error: %v", err)
}
var actual, all []string
for _, step := range strings.Split(captured.String(), "\\n") {
trimmed := strings.TrimSpace(step)
all = append(all, trimmed)
if slices.Contains(expect, trimmed) {
actual = append(actual, trimmed)
}
}
if !slices.Equal(expect, actual) {
t.Errorf("order mismatch:\\nexpect: %v\\nactual: %v\\nall: %v", expect, actual, all)
}
}
""",
SmithyGoDependency.ERRORS,
SmithyGoDependency.TESTING,
SmithyGoDependency.CONTEXT,
SmithyGoDependency.STRINGS,
SmithyGoDependency.SLICES,
SmithyGoDependency.SMITHY_MIDDLEWARE,
operationName
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ software.amazon.smithy.aws.go.codegen.customization.BasicUserAgentFeatures
software.amazon.smithy.aws.go.codegen.customization.ChecksumMetricsTracking
software.amazon.smithy.aws.go.codegen.customization.AccountIdEndpointModeUserAgent
software.amazon.smithy.aws.go.codegen.CredentialSourceFeatureTrackerGenerator
software.amazon.smithy.aws.go.codegen.customization.SraOperationOrderTest
1 change: 1 addition & 0 deletions internal/kitchensinktest/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"protocol_test.go",
"serializers.go",
"snapshot_test.go",
"sra_operation_order_test.go",
"types/errors.go",
"types/types.go",
"validators.go"
Expand Down
56 changes: 56 additions & 0 deletions internal/kitchensinktest/sra_operation_order_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/protocoltest/awsrestjson/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
"protocol_test.go",
"serializers.go",
"snapshot_test.go",
"sra_operation_order_test.go",
"types/enums.go",
"types/errors.go",
"types/types.go",
Expand Down
Loading

0 comments on commit 8f2dd23

Please sign in to comment.