feat: model-driven request conformance in the integration suite (#1122)

This commit is contained in:
Julian Sommer
2026-09-28 14:14:15 +10:00
committed by GitHub
parent 0c3403889c
commit 2d2d2f27ed
26 changed files with 2844 additions and 168 deletions
+4 -2
View File
@@ -202,10 +202,12 @@ AWS_MODEL_OPERATION_COVERAGE_DIR ?= $(CURDIR)/docs/coverage
# -count=1 is load-bearing: the conformance report is written by the test binary,
# so a cached pass skips the run, leaves no report, and the cat below fails the
# target. It also keeps the conformance gate honest — a cached result would mean
# the check never ran against this commit.
# the check never ran against this commit. The generated request sweep adds
# about 15s, most of it EC2 fan-outs waiting out their window for instances
# no daemon-lite owns.
test-integration:
@echo -e "\n....Running in-process integration tests...."
$(_Q)LOG_IGNORE=1 AWS_MODEL_CONFORMANCE_MODE=$(AWS_MODEL_CONFORMANCE_MODE) AWS_MODEL_CONFORMANCE_REPORT=$(AWS_MODEL_CONFORMANCE_REPORT) go test -count=1 -tags=integration -timeout 60s ./tests/integration/... $(_RACEQ)
$(_Q)LOG_IGNORE=1 AWS_MODEL_CONFORMANCE_MODE=$(AWS_MODEL_CONFORMANCE_MODE) AWS_MODEL_CONFORMANCE_REPORT=$(AWS_MODEL_CONFORMANCE_REPORT) go test -count=1 -tags=integration -timeout 120s ./tests/integration/... $(_RACEQ)
@cat $(AWS_MODEL_CONFORMANCE_REPORT)
@$(MAKE) --no-print-directory generate-aws-model-coverage
@echo "AWS operation coverage: $(AWS_MODEL_OPERATION_COVERAGE_DIR)"
+450
View File
@@ -0,0 +1,450 @@
package awsmodel
import (
"encoding/base64"
"encoding/json"
"fmt"
"maps"
"net/http"
"net/url"
"slices"
"strconv"
"strings"
"time"
"unicode"
)
// EncodedRequest is an operation input serialized for the service's protocol,
// ready to be addressed to an endpoint and signed.
type EncodedRequest struct {
Method string
// Path is escaped; it may carry a query string of its own.
Path string
Query url.Values
Header http.Header
Body []byte
}
// URL joins the request's path and query onto endpoint.
func (r *EncodedRequest) URL(endpoint string) string {
target := strings.TrimSuffix(endpoint, "/") + r.Path
if len(r.Query) == 0 {
return target
}
separator := "?"
if strings.Contains(r.Path, "?") {
separator = "&"
}
return target + separator + r.Query.Encode()
}
// EncodeRequest serializes input, as produced by GenerateRequests, for the
// service's protocol. S3's rest-xml is not supported.
func EncodeRequest(service Service, operationName string, input map[string]any) (*EncodedRequest, error) {
model, err := Load(service)
if err != nil {
return nil, err
}
operation, ok := model.Operation(operationName)
if !ok {
return nil, fmt.Errorf("awsmodel: %s operation %q is not modelled", service, operationName)
}
encoder := requestEncoder{model: model}
switch model.metadata.Protocol {
case "query", "ec2":
return encoder.query(operation, input)
case "json":
return encoder.json(operation, input)
case "rest-json":
return encoder.restJSON(operation, input)
default:
return nil, fmt.Errorf("awsmodel: encoding %s requests is not implemented", model.metadata.Protocol)
}
}
type requestEncoder struct {
model *Model
}
func (e requestEncoder) query(operation *Operation, input map[string]any) (*EncodedRequest, error) {
values := url.Values{
"Action": {operation.Name},
"Version": {e.model.metadata.APIVersion},
}
if operation.Input != nil {
if err := e.queryStructure(values, "", operation.Input.Shape, input); err != nil {
return nil, err
}
}
return &EncodedRequest{
Method: http.MethodPost,
Path: "/",
Header: http.Header{"Content-Type": {"application/x-www-form-urlencoded; charset=utf-8"}},
Body: []byte(values.Encode()),
}, nil
}
func (e requestEncoder) queryStructure(values url.Values, prefix, shapeName string, input map[string]any) error {
shape := e.model.shapes[shapeName]
for _, member := range slices.Sorted(maps.Keys(input)) {
ref, ok := shape.Members[member]
if !ok {
return fmt.Errorf("%s has no member %q", shapeName, member)
}
if err := e.queryValue(values, prefix+e.queryName(member, ref), ref, input[member]); err != nil {
return err
}
}
return nil
}
// queryName is the parameter name for a member: the ec2 protocol capitalises
// its XML name unless ec2QueryName gives one; awsQuery uses the XML name.
func (e requestEncoder) queryName(member string, ref ShapeRef) string {
if e.model.metadata.Protocol != "ec2" {
if ref.LocationName != "" {
return ref.LocationName
}
return member
}
if ref.QueryName != "" {
return ref.QueryName
}
name := member
if ref.LocationName != "" {
name = ref.LocationName
}
runes := []rune(name)
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}
func (e requestEncoder) queryValue(values url.Values, name string, ref ShapeRef, value any) error {
shape := e.model.shapes[ref.Shape]
switch shape.Type {
case "structure":
fields, ok := value.(map[string]any)
if !ok {
return fmt.Errorf("%s: want a structure, got %T", name, value)
}
return e.queryStructure(values, name+".", ref.Shape, fields)
case "list":
items, ok := value.([]any)
if !ok {
return fmt.Errorf("%s: want a list, got %T", name, value)
}
if len(items) == 0 {
// awsQuery sends an empty list as an empty parameter; ec2 cannot.
if e.model.metadata.Protocol != "ec2" {
values.Set(name, "")
}
return nil
}
itemPrefix := name + "."
if e.model.metadata.Protocol != "ec2" && !ref.Flattened && !shape.Flattened {
memberName := "member"
if shape.Member.LocationName != "" {
memberName = shape.Member.LocationName
}
itemPrefix += memberName + "."
}
for i, item := range items {
if err := e.queryValue(values, itemPrefix+strconv.Itoa(i+1), *shape.Member, item); err != nil {
return err
}
}
return nil
case "map":
entries, ok := value.(map[string]any)
if !ok {
return fmt.Errorf("%s: want a map, got %T", name, value)
}
entryPrefix := name + "."
if e.model.metadata.Protocol != "ec2" && !ref.Flattened && !shape.Flattened {
entryPrefix += "entry."
}
keyName, valueName := "key", "value"
if shape.Key.LocationName != "" {
keyName = shape.Key.LocationName
}
if shape.Value.LocationName != "" {
valueName = shape.Value.LocationName
}
for i, key := range slices.Sorted(maps.Keys(entries)) {
entry := entryPrefix + strconv.Itoa(i+1) + "."
values.Set(entry+keyName, key)
if err := e.queryValue(values, entry+valueName, *shape.Value, entries[key]); err != nil {
return err
}
}
return nil
default:
scalar, err := scalarString(value, ref, shape, "iso8601")
if err != nil {
return fmt.Errorf("%s: %w", name, err)
}
values.Set(name, scalar)
return nil
}
}
func (e requestEncoder) json(operation *Operation, input map[string]any) (*EncodedRequest, error) {
body := []byte("{}")
if operation.Input != nil {
document, err := e.jsonValue(*operation.Input, input)
if err != nil {
return nil, err
}
if body, err = json.Marshal(document); err != nil {
return nil, err
}
}
metadata := e.model.metadata
return &EncodedRequest{
Method: http.MethodPost,
Path: "/",
Header: http.Header{
"Content-Type": {"application/x-amz-json-" + metadata.JSONVersion},
"X-Amz-Target": {metadata.TargetPrefix + "." + operation.Name},
},
Body: body,
}, nil
}
// jsonValue converts a generated value into its JSON document form.
func (e requestEncoder) jsonValue(ref ShapeRef, value any) (any, error) {
shape := e.model.shapes[ref.Shape]
switch shape.Type {
case "structure":
fields, ok := value.(map[string]any)
if !ok {
return nil, fmt.Errorf("%s: want a structure, got %T", ref.Shape, value)
}
document := make(map[string]any, len(fields))
for member, field := range fields {
memberRef, ok := shape.Members[member]
if !ok {
return nil, fmt.Errorf("%s has no member %q", ref.Shape, member)
}
encoded, err := e.jsonValue(memberRef, field)
if err != nil {
return nil, err
}
document[member] = encoded
}
return document, nil
case "list":
items, ok := value.([]any)
if !ok {
return nil, fmt.Errorf("%s: want a list, got %T", ref.Shape, value)
}
document := make([]any, len(items))
for i, item := range items {
encoded, err := e.jsonValue(*shape.Member, item)
if err != nil {
return nil, err
}
document[i] = encoded
}
return document, nil
case "map":
entries, ok := value.(map[string]any)
if !ok {
return nil, fmt.Errorf("%s: want a map, got %T", ref.Shape, value)
}
document := make(map[string]any, len(entries))
for key, entry := range entries {
encoded, err := e.jsonValue(*shape.Value, entry)
if err != nil {
return nil, err
}
document[key] = encoded
}
return document, nil
case "timestamp":
stamp, ok := value.(time.Time)
if !ok {
return nil, fmt.Errorf("%s: want a timestamp, got %T", ref.Shape, value)
}
if format := timestampFormatFor(ref, shape, "unixTimestamp"); format != "unixTimestamp" {
return formatTimestamp(stamp, format), nil
}
return stamp.Unix(), nil
case "blob":
data, ok := value.([]byte)
if !ok {
return nil, fmt.Errorf("%s: want a blob, got %T", ref.Shape, value)
}
return base64.StdEncoding.EncodeToString(data), nil
default:
return value, nil
}
}
func (e requestEncoder) restJSON(operation *Operation, input map[string]any) (*EncodedRequest, error) {
request := &EncodedRequest{Method: operation.HTTP.Method, Query: url.Values{}, Header: http.Header{}}
path := operation.HTTP.RequestURI
if operation.Input == nil {
request.Path = path
return request, nil
}
shape := e.model.shapes[operation.Input.Shape]
body := map[string]any{}
for _, member := range slices.Sorted(maps.Keys(input)) {
ref, ok := shape.Members[member]
if !ok {
return nil, fmt.Errorf("%s has no member %q", operation.Input.Shape, member)
}
value := input[member]
target := e.model.shapes[ref.Shape]
switch ref.Location {
case "uri":
text, err := scalarString(value, ref, target, "iso8601")
if err != nil {
return nil, fmt.Errorf("%s: %w", member, err)
}
path = strings.Replace(path, "{"+member+"+}", escapeGreedyLabel(text), 1)
path = strings.Replace(path, "{"+member+"}", url.PathEscape(text), 1)
case "querystring":
if err := e.restQuery(request.Query, ref, target, value); err != nil {
return nil, fmt.Errorf("%s: %w", member, err)
}
case "header":
text, err := e.headerValue(ref, target, value)
if err != nil {
return nil, fmt.Errorf("%s: %w", member, err)
}
request.Header.Set(ref.LocationName, text)
case "headers":
entries, _ := value.(map[string]any)
for key, entry := range entries {
request.Header.Set(ref.LocationName+key, fmt.Sprint(entry))
}
default:
encoded, err := e.jsonValue(ref, value)
if err != nil {
return nil, err
}
name := member
if ref.LocationName != "" {
name = ref.LocationName
}
body[name] = encoded
}
}
request.Path = path
if payload := shape.Payload; payload != "" {
if document, ok := body[payload]; ok {
body = nil
encoded, err := json.Marshal(document)
if err != nil {
return nil, err
}
request.Body = encoded
}
}
if len(body) > 0 {
encoded, err := json.Marshal(body)
if err != nil {
return nil, err
}
request.Body = encoded
}
if request.Body != nil {
request.Header.Set("Content-Type", "application/json")
}
return request, nil
}
func (e requestEncoder) restQuery(query url.Values, ref ShapeRef, shape *Shape, value any) error {
switch shape.Type {
case "list":
items, _ := value.([]any)
for _, item := range items {
text, err := scalarString(item, *shape.Member, e.model.shapes[shape.Member.Shape], "iso8601")
if err != nil {
return err
}
query.Add(ref.LocationName, text)
}
return nil
case "map":
entries, _ := value.(map[string]any)
for key, entry := range entries {
query.Add(key, fmt.Sprint(entry))
}
return nil
default:
text, err := scalarString(value, ref, shape, "iso8601")
if err != nil {
return err
}
query.Set(ref.LocationName, text)
return nil
}
}
func (e requestEncoder) headerValue(ref ShapeRef, shape *Shape, value any) (string, error) {
if shape.Type == "list" {
items, _ := value.([]any)
texts := make([]string, len(items))
for i, item := range items {
text, err := scalarString(item, *shape.Member, e.model.shapes[shape.Member.Shape], "rfc822")
if err != nil {
return "", err
}
texts[i] = text
}
return strings.Join(texts, ","), nil
}
return scalarString(value, ref, shape, "rfc822")
}
// escapeGreedyLabel escapes a greedy label's segments but keeps its slashes.
func escapeGreedyLabel(value string) string {
segments := strings.Split(value, "/")
for i, segment := range segments {
segments[i] = url.PathEscape(segment)
}
return strings.Join(segments, "/")
}
func scalarString(value any, ref ShapeRef, shape *Shape, defaultTimestamp string) (string, error) {
switch value := value.(type) {
case string:
return value, nil
case bool:
return strconv.FormatBool(value), nil
case int64:
return strconv.FormatInt(value, 10), nil
case float64:
return strconv.FormatFloat(value, 'g', -1, 64), nil
case []byte:
return base64.StdEncoding.EncodeToString(value), nil
case time.Time:
return formatTimestamp(value, timestampFormatFor(ref, shape, defaultTimestamp)), nil
default:
return "", fmt.Errorf("unsupported scalar %T", value)
}
}
func timestampFormatFor(ref ShapeRef, shape *Shape, defaultFormat string) string {
if ref.TimestampFormat != "" {
return ref.TimestampFormat
}
if shape.TimestampFormat != "" {
return shape.TimestampFormat
}
return defaultFormat
}
func formatTimestamp(value time.Time, format string) string {
switch format {
case "rfc822":
return value.UTC().Format(http.TimeFormat)
case "unixTimestamp":
return strconv.FormatInt(value.Unix(), 10)
default:
return value.UTC().Format(time.RFC3339)
}
}
+124
View File
@@ -0,0 +1,124 @@
package awsmodel_test
import (
"encoding/json"
"net/http"
"net/url"
"testing"
"github.com/mulgadc/spinifex/internal/awsmodel"
"github.com/stretchr/testify/require"
)
func encodedForm(t *testing.T, request *awsmodel.EncodedRequest) url.Values {
t.Helper()
require.Equal(t, http.MethodPost, request.Method)
require.Equal(t, "/", request.Path)
form, err := url.ParseQuery(string(request.Body))
require.NoError(t, err)
return form
}
func TestEncodeRequestAWSQueryWrapsListMembers(t *testing.T) {
request, err := awsmodel.EncodeRequest(awsmodel.IAM, "CreateRole", map[string]any{
"RoleName": "app",
"AssumeRolePolicyDocument": "{}",
"MaxSessionDuration": int64(3600),
"Tags": []any{map[string]any{"Key": "team", "Value": "core"}},
})
require.NoError(t, err)
require.Equal(t, url.Values{
"Action": {"CreateRole"},
"Version": {"2010-05-08"},
"RoleName": {"app"},
"AssumeRolePolicyDocument": {"{}"},
"MaxSessionDuration": {"3600"},
"Tags.member.1.Key": {"team"},
"Tags.member.1.Value": {"core"},
}, encodedForm(t, request))
}
func TestEncodeRequestEC2UsesQueryNamesWithoutMemberWrapper(t *testing.T) {
request, err := awsmodel.EncodeRequest(awsmodel.EC2, "RunInstances", map[string]any{
"ImageId": "ami-0123456789abcdef0",
"MinCount": int64(1),
"MaxCount": int64(1),
"SecurityGroupIds": []any{"sg-0123456789abcdef0"},
"TagSpecifications": []any{map[string]any{
"ResourceType": "instance",
"Tags": []any{map[string]any{"Key": "Name", "Value": "web"}},
}},
})
require.NoError(t, err)
require.Equal(t, url.Values{
"Action": {"RunInstances"},
"Version": {"2016-11-15"},
"ImageId": {"ami-0123456789abcdef0"},
"MinCount": {"1"},
"MaxCount": {"1"},
"SecurityGroupId.1": {"sg-0123456789abcdef0"},
"TagSpecification.1.ResourceType": {"instance"},
"TagSpecification.1.Tag.1.Key": {"Name"},
"TagSpecification.1.Tag.1.Value": {"web"},
}, encodedForm(t, request))
}
func TestEncodeRequestJSONTargetsOperation(t *testing.T) {
tests := []struct {
service awsmodel.Service
operation string
target string
}{
{awsmodel.ECS, "ListClusters", "AmazonEC2ContainerServiceV20141113.ListClusters"},
{awsmodel.ECR, "DescribeRepositories", "AmazonEC2ContainerRegistry_V20150921.DescribeRepositories"},
{awsmodel.ACM, "ListCertificates", "CertificateManager.ListCertificates"},
}
for _, test := range tests {
t.Run(string(test.service), func(t *testing.T) {
request, err := awsmodel.EncodeRequest(test.service, test.operation, map[string]any{"maxResults": int64(5)})
if test.service == awsmodel.ACM {
request, err = awsmodel.EncodeRequest(test.service, test.operation, map[string]any{"MaxItems": int64(5)})
}
require.NoError(t, err)
require.Equal(t, http.MethodPost, request.Method)
require.Equal(t, test.target, request.Header.Get("X-Amz-Target"))
require.Equal(t, "application/x-amz-json-1.1", request.Header.Get("Content-Type"))
var body map[string]any
require.NoError(t, json.Unmarshal(request.Body, &body))
require.Len(t, body, 1)
})
}
}
func TestEncodeRequestRestJSONPlacesMembers(t *testing.T) {
describe, err := awsmodel.EncodeRequest(awsmodel.EKS, "DescribeCluster", map[string]any{"name": "prod cluster"})
require.NoError(t, err)
require.Equal(t, http.MethodGet, describe.Method)
require.Equal(t, "https://eks.example/clusters/prod%20cluster", describe.URL("https://eks.example"))
list, err := awsmodel.EncodeRequest(awsmodel.EKS, "ListClusters", map[string]any{"maxResults": int64(5), "include": []any{"all"}})
require.NoError(t, err)
require.Equal(t, url.Values{"maxResults": {"5"}, "include": {"all"}}, list.Query)
require.Empty(t, list.Body)
create, err := awsmodel.EncodeRequest(awsmodel.EKS, "CreateCluster", map[string]any{
"name": "prod",
"roleArn": "arn:aws:iam::123456789012:role/eks",
"resourcesVpcConfig": map[string]any{"subnetIds": []any{"subnet-1"}},
})
require.NoError(t, err)
require.Equal(t, http.MethodPost, create.Method)
require.Equal(t, "/clusters", create.Path)
require.Equal(t, "application/json", create.Header.Get("Content-Type"))
require.JSONEq(t, `{"name":"prod","roleArn":"arn:aws:iam::123456789012:role/eks","resourcesVpcConfig":{"subnetIds":["subnet-1"]}}`, string(create.Body))
}
func TestEncodeRequestRejectsUnmodelledMember(t *testing.T) {
_, err := awsmodel.EncodeRequest(awsmodel.IAM, "GetRole", map[string]any{"RoleName": "app", "Bogus": "x"})
require.ErrorContains(t, err, `no member "Bogus"`)
}
func TestEncodeRequestS3IsNotSupported(t *testing.T) {
_, err := awsmodel.EncodeRequest(awsmodel.S3, "ListBuckets", map[string]any{})
require.ErrorContains(t, err, "not implemented")
}
+5
View File
@@ -40,12 +40,16 @@ const (
// Metadata describes the service and wire protocol represented by a model.
// Protocol uses the api-2.json names: json, rest-json, rest-xml, query, ec2.
// JSONVersion and TargetPrefix are set only for the json protocol.
type Metadata struct {
APIVersion string
EndpointPrefix string
JSONVersion string
Protocol string
ServiceFullName string
ServiceID string
SigningName string
TargetPrefix string
}
// Operation describes an AWS API operation and the shapes used by its input,
@@ -83,6 +87,7 @@ type Shape struct {
Payload string
Flattened bool
Sensitive bool
Union bool
Exception bool
Fault bool
Error *ErrorInfo
+238
View File
@@ -0,0 +1,238 @@
package awsmodel
import (
"regexp"
"regexp/syntax"
"strings"
"sync"
"unicode/utf8"
)
// maxGeneratedLength bounds generated strings, so a constraint such as a
// 128 KiB policy document maximum does not produce a request that size.
const maxGeneratedLength = 1 << 16
// patternSampler produces strings matching a model pattern. Smithy patterns
// are not implicitly anchored, so a match anywhere in the value satisfies one.
type patternSampler struct {
re *regexp.Regexp
anchored *regexp.Regexp
syntax *syntax.Regexp
mu sync.Mutex
samples map[[2]int]sampleResult
}
type sampleResult struct {
value string
ok bool
}
type samplerResult struct {
sampler *patternSampler
err error
}
// samplers caches one sampler per pattern: every request case regenerates
// its input, and the same patterns recur across operations.
var samplers sync.Map
// javaEscape matches the \uXXXX escapes AWS patterns use, which Go spells \x{XXXX}.
var javaEscape = regexp.MustCompile(`\\u([0-9A-Fa-f]{4})`)
func newPatternSampler(pattern string) (*patternSampler, error) {
if cached, ok := samplers.Load(pattern); ok {
if result, ok := cached.(samplerResult); ok {
return result.sampler, result.err
}
}
sampler, err := compilePatternSampler(pattern)
samplers.Store(pattern, samplerResult{sampler: sampler, err: err})
return sampler, err
}
func compilePatternSampler(pattern string) (*patternSampler, error) {
pattern = javaEscape.ReplaceAllString(pattern, `\x{$1}`)
re, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
parsed, err := syntax.Parse(pattern, syntax.Perl)
if err != nil {
return nil, err
}
anchored, err := regexp.Compile(`^(?:` + pattern + `)$`)
if err != nil {
return nil, err
}
return &patternSampler{re: re, anchored: anchored, syntax: parsed.Simplify(), samples: map[[2]int]sampleResult{}}, nil
}
// sample returns a string fully matching the pattern with a rune length in
// [minLength, maxLength], expanding every repetition the same number of times
// and searching for that count, since the length only grows with it.
func (p *patternSampler) sample(minLength, maxLength int) (string, bool) {
key := [2]int{minLength, maxLength}
p.mu.Lock()
defer p.mu.Unlock()
if cached, ok := p.samples[key]; ok {
return cached.value, cached.ok
}
value, ok := p.search(minLength, maxLength)
p.samples[key] = sampleResult{value: value, ok: ok}
return value, ok
}
func (p *patternSampler) search(minLength, maxLength int) (string, bool) {
lengthAt := func(repeat int) (string, int, bool) {
var builder strings.Builder
if !writeSample(&builder, p.syntax, repeat, maxLength+1) {
return "", 0, false
}
value := builder.String()
return value, utf8.RuneCountInString(value), true
}
value, length, ok := lengthAt(0)
if !ok {
return "", false
}
repeat := 0
if length < minLength {
// Double until long enough, then bisect for the smallest count.
low, high := 0, 1
for {
_, length, ok = lengthAt(high)
if !ok {
return "", false
}
if length >= minLength {
break
}
if high >= maxGeneratedLength {
return "", false
}
low, high = high, high*2
}
for low+1 < high {
middle := (low + high) / 2
if _, length, _ = lengthAt(middle); length >= minLength {
high = middle
} else {
low = middle
}
}
repeat = high
}
// A pattern whose fixed parts need a larger count than the minimum does
// can still fit a few counts higher.
for attempt := range 4 {
value, length, ok = lengthAt(repeat + attempt)
if !ok || length > maxLength {
return "", false
}
if length >= minLength && p.fullMatch(value) {
return value, true
}
}
return "", false
}
// fullMatch reports whether the whole value matches the pattern. Generated
// values are held to this, stricter than Smithy's unanchored match, so a value
// like "/a" is not taken for a path because it contains a "/".
func (p *patternSampler) fullMatch(value string) bool {
return p.anchored.MatchString(value)
}
func writeSample(builder *strings.Builder, re *syntax.Regexp, repeat, limit int) bool {
// Past the limit the value is already too long; the caller only needs to
// see that.
if builder.Len() > limit*utf8.UTFMax {
return true
}
switch re.Op {
case syntax.OpNoMatch:
return false
case syntax.OpLiteral:
for _, r := range re.Rune {
builder.WriteRune(r)
}
case syntax.OpCharClass:
r, ok := classRune(re.Rune)
if !ok {
return false
}
builder.WriteRune(r)
case syntax.OpAnyChar, syntax.OpAnyCharNotNL:
builder.WriteRune('a')
case syntax.OpCapture:
return writeSample(builder, re.Sub[0], repeat, limit)
case syntax.OpConcat:
for _, sub := range re.Sub {
if !writeSample(builder, sub, repeat, limit) {
return false
}
}
case syntax.OpAlternate:
for _, sub := range re.Sub {
var alternative strings.Builder
if writeSample(&alternative, sub, repeat, limit) {
builder.WriteString(alternative.String())
return true
}
}
return false
case syntax.OpStar, syntax.OpPlus, syntax.OpQuest, syntax.OpRepeat:
count := repetitions(re, repeat)
for range count {
if !writeSample(builder, re.Sub[0], repeat, limit) {
return false
}
}
}
// Anchors, word boundaries and empty matches contribute no characters.
return true
}
func repetitions(re *syntax.Regexp, repeat int) int {
low, high := 0, -1
switch re.Op {
case syntax.OpPlus:
low = 1
case syntax.OpQuest:
high = 1
case syntax.OpRepeat:
low, high = re.Min, re.Max
}
count := max(repeat, low)
if high >= 0 {
count = min(count, high)
}
return count
}
// classRune picks a readable member of a character class: a lower-case
// letter, then a digit, then any printable ASCII, then the first rune.
func classRune(ranges []rune) (rune, bool) {
if len(ranges) == 0 {
return 0, false
}
for _, preferred := range []string{"abcdefghijklmnopqrstuvwxyz", "0123456789", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "-_.!#$%&'()*+,/:;<=>?@[]^`{|}~"} {
for _, r := range preferred {
if inClass(ranges, r) {
return r, true
}
}
}
return ranges[0], true
}
func inClass(ranges []rune, r rune) bool {
for i := 0; i+1 < len(ranges); i += 2 {
if r >= ranges[i] && r <= ranges[i+1] {
return true
}
}
return false
}
+634
View File
@@ -0,0 +1,634 @@
package awsmodel
import (
"fmt"
"maps"
"math"
"regexp"
"slices"
"strconv"
"strings"
"time"
)
// Constraint names the modelled constraint a rejection request breaks.
type Constraint string
const (
ConstraintRequired Constraint = "required"
ConstraintEnum Constraint = "enum"
ConstraintLength Constraint = "length"
ConstraintRange Constraint = "range"
ConstraintPattern Constraint = "pattern"
)
// RequestCase is one generated request: the required members plus at most one
// optional top-level member, named by Member. An acceptance case has no
// Constraint; a rejection case breaks the constraint it names, at Path.
type RequestCase struct {
Operation string
Member string
Constraint Constraint
Path string
Detail string
Input map[string]any
}
// Acceptance reports whether the case should be accepted.
func (c RequestCase) Acceptance() bool { return c.Constraint == "" }
// RequestPlan is every request generated for one operation, acceptance cases
// first. Skipped lists members left out of every request, Unbroken the
// constraints no request breaks; DeclaredErrors are the operation's error codes.
type RequestPlan struct {
Cases []RequestCase
Skipped []Skip
Unbroken []Skip
DeclaredErrors []string
}
// Skip names a member or constraint the generator could not exercise.
type Skip struct {
Path string
Reason string
}
// GenerateRequests builds a required-members-only acceptance request, one
// adding each optional top-level member, and one rejection request per
// breakable constraint. Identifiers are well formed but name nothing.
func GenerateRequests(service Service, operationName string, options RequestOptions) (RequestPlan, error) {
model, err := Load(service)
if err != nil {
return RequestPlan{}, err
}
operation, ok := model.Operation(operationName)
if !ok {
return RequestPlan{}, fmt.Errorf("awsmodel: %s operation %q is not modelled", service, operationName)
}
declared := model.operationErrorCodes(operation)
if operation.Input == nil {
return RequestPlan{Cases: []RequestCase{{Operation: operationName, Input: map[string]any{}}}, DeclaredErrors: declared}, nil
}
first := model.newRequestGenerator(0, options)
full, ok := first.structure(operation.Input.Shape, nil, 0)
plan := RequestPlan{Skipped: first.skipped, Unbroken: first.unbroken, DeclaredErrors: declared}
if !ok {
plan.Skipped = append(plan.Skipped, Skip{Path: "$", Reason: "a required member cannot be generated"})
return plan, nil
}
input := model.shapes[operation.Input.Shape]
optional := make([]string, 0, len(full))
for _, member := range slices.Sorted(maps.Keys(full)) {
if !slices.Contains(input.Required, member) {
optional = append(optional, member)
}
}
// Each case is generated afresh with its own salt, so the names a create
// request carries differ between cases and one case cannot collide with
// what an earlier case created.
salt := 0
generate := func() (map[string]any, []constraintSite, error) {
generator := model.newRequestGenerator(salt, options)
salt++
value, _ := generator.structure(operation.Input.Shape, nil, 0)
if len(generator.sites) != len(first.sites) {
return nil, nil, fmt.Errorf("awsmodel: %s %s generated different constraints for different salts", service, operationName)
}
return value, generator.sites, nil
}
for _, member := range append([]string{""}, optional...) {
if unguessable[member] {
continue
}
value, _, err := generate()
if err != nil {
return RequestPlan{}, err
}
plan.Cases = append(plan.Cases, RequestCase{
Operation: operationName,
Member: member,
Path: memberPathOf(member),
Input: keepMembers(value, input.Required, member),
})
}
for index := range first.sites {
value, sites, err := generate()
if err != nil {
return RequestPlan{}, err
}
site := sites[index]
member, _ := site.path[0].(string)
if slices.Contains(input.Required, member) {
member = ""
}
broken, err := site.apply(keepMembers(value, input.Required, member))
if err != nil {
return RequestPlan{}, err
}
path := formatPath(site.path)
if site.renameKey {
path = formatPath(site.path[:len(site.path)-1]) + mapKeyPath
}
plan.Cases = append(plan.Cases, RequestCase{
Operation: operationName,
Member: member,
Constraint: site.constraint,
Path: path,
Detail: site.detail,
Input: broken,
})
}
return plan, nil
}
// unguessable members take values the model cannot describe: a pagination
// token the service issued, or a filter name only its documentation lists.
// They get rejection requests but no acceptance request.
var unguessable = map[string]bool{
"Filter": true,
"Filters": true,
"Marker": true,
"NextToken": true,
"nextToken": true,
}
func memberPathOf(member string) string {
if member == "" {
return "$"
}
return "$." + member
}
// keepMembers drops every top-level member but the required ones and member.
func keepMembers(input map[string]any, required []string, member string) map[string]any {
for name := range input {
if name != member && !slices.Contains(required, name) {
delete(input, name)
}
}
return input
}
// pathStep is a structure member or map key (string) or a list index (int).
type pathStep any
func formatPath(path []pathStep) string {
var builder strings.Builder
builder.WriteString("$")
for _, step := range path {
switch step := step.(type) {
case int:
fmt.Fprintf(&builder, "[%d]", step)
case string:
builder.WriteString(".")
builder.WriteString(step)
}
}
return builder.String()
}
// mapKeyPath stands for a map's generated key in a rejection case's Path.
const mapKeyPath = ".{key}"
// constraintSite is one constraint a rejection request can break: it removes a
// required member, renames a map key, or replaces the value at path.
type constraintSite struct {
path []pathStep
constraint Constraint
detail string
remove bool
renameKey bool
replacement any
// build makes a replacement too costly to make for every case.
build func() any
}
// apply returns input with the site's constraint broken. input is a freshly
// generated value tree that nothing else holds.
func (s constraintSite) apply(input map[string]any) (map[string]any, error) {
var container any = input
for _, step := range s.path[:len(s.path)-1] {
switch step := step.(type) {
case int:
list, ok := container.([]any)
if !ok {
return nil, fmt.Errorf("awsmodel: %s: want a list", formatPath(s.path))
}
container = list[step]
case string:
fields, ok := container.(map[string]any)
if !ok {
return nil, fmt.Errorf("awsmodel: %s: want a structure or map", formatPath(s.path))
}
container = fields[step]
}
}
replacement := s.replacement
if s.build != nil {
replacement = s.build()
}
switch last := s.path[len(s.path)-1].(type) {
case int:
list, ok := container.([]any)
if !ok {
return nil, fmt.Errorf("awsmodel: %s: want a list", formatPath(s.path))
}
list[last] = replacement
case string:
fields, ok := container.(map[string]any)
if !ok {
return nil, fmt.Errorf("awsmodel: %s: want a structure or map", formatPath(s.path))
}
switch {
case s.remove:
delete(fields, last)
case s.renameKey:
key, ok := replacement.(string)
if !ok {
return nil, fmt.Errorf("awsmodel: %s: want a string key", formatPath(s.path))
}
fields[key] = fields[last]
delete(fields, last)
default:
fields[last] = replacement
}
}
return input, nil
}
// maxStructureDepth stops recursive shapes, such as nested filters.
const maxStructureDepth = 8
// maxListRejectionLength keeps a request that breaks a list maximum small.
const maxListRejectionLength = 1000
var generatedTimestamp = time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC)
type requestGenerator struct {
model *Model
options RequestOptions
salt string
saltNumber int
sites []constraintSite
skipped []Skip
unbroken []Skip
visiting map[string]bool
}
func (m *Model) newRequestGenerator(salt int, options RequestOptions) *requestGenerator {
return &requestGenerator{model: m, options: options, salt: saltLetters(salt), saltNumber: salt, visiting: map[string]bool{}}
}
// saltLetters spells n in base 26 with lower-case letters, which fit more
// patterns than digits do.
func saltLetters(n int) string {
letters := ""
for {
letters = string(rune('a'+n%26)) + letters
n /= 26
if n == 0 {
return letters
}
}
}
func (g *requestGenerator) skip(path []pathStep, reason string) {
g.skipped = append(g.skipped, Skip{Path: formatPath(path), Reason: reason})
}
// leaveUnbroken records a constraint no rejection request breaks.
func (g *requestGenerator) leaveUnbroken(path []pathStep, reason string) {
g.unbroken = append(g.unbroken, Skip{Path: formatPath(path), Reason: reason})
}
func (g *requestGenerator) site(path []pathStep, site constraintSite) {
site.path = slices.Clone(path)
g.sites = append(g.sites, site)
}
// value generates a valid value for ref at path, reporting false when none
// can be generated.
func (g *requestGenerator) value(ref ShapeRef, path []pathStep, depth int) (any, bool) {
shape := g.model.shapes[ref.Shape]
if ref.Streaming {
g.skip(path, "streaming payload")
return nil, false
}
switch shape.Type {
case "structure":
if value, ok := g.structure(ref.Shape, path, depth+1); ok {
return value, true
}
return nil, false
case "list":
return g.list(shape, path, depth)
case "map":
return g.mapValue(shape, path, depth)
case "string":
return g.stringValue(shape, ref, path)
case "integer", "long":
return g.integer(shape, path), true
case "float", "double":
return g.float(shape, path), true
case "boolean":
return false, true
case "timestamp":
return generatedTimestamp, true
case "blob":
return g.blob(shape, path), true
default:
g.skip(path, shape.Type+" values are not generated")
return nil, false
}
}
func (g *requestGenerator) structure(name string, path []pathStep, depth int) (map[string]any, bool) {
if depth > maxStructureDepth || g.visiting[name] {
g.skip(path, "recursive structure")
return nil, false
}
g.visiting[name] = true
defer delete(g.visiting, name)
shape := g.model.shapes[name]
required := make(map[string]bool, len(shape.Required))
for _, member := range shape.Required {
required[member] = true
}
value := map[string]any{}
for _, member := range slices.Sorted(maps.Keys(shape.Members)) {
ref := shape.Members[member]
memberPath := append(slices.Clone(path), member)
memberValue, ok := g.value(ref, memberPath, depth)
if !ok {
if required[member] {
return nil, false
}
continue
}
value[member] = memberValue
// An empty URI label changes which route the request reaches, rather
// than omitting a member from it.
if required[member] && ref.Location == "uri" {
g.leaveUnbroken(memberPath, "a required URI label cannot be omitted")
} else if required[member] {
g.site(memberPath, constraintSite{constraint: ConstraintRequired, detail: "member omitted", remove: true})
}
// A union takes exactly one member.
if shape.Union {
break
}
}
return value, true
}
func (g *requestGenerator) list(shape *Shape, path []pathStep, depth int) (any, bool) {
element, ok := g.value(*shape.Member, append(slices.Clone(path), 0), depth)
if !ok {
return nil, false
}
count := 1
if shape.Min != nil {
count = max(count, int(*shape.Min))
}
if shape.Max != nil && count > int(*shape.Max) {
return nil, false
}
list := make([]any, count)
for i := range list {
list[i] = element
if i > 0 {
list[i], _ = g.quietValue(*shape.Member, depth)
}
}
if shape.Min != nil && *shape.Min > 0 {
short := slices.Clone(list[:int(*shape.Min)-1])
g.site(path, constraintSite{constraint: ConstraintLength, detail: fmt.Sprintf("%d items, below min %g", len(short), *shape.Min), replacement: short})
}
if shape.Max != nil && *shape.Max >= maxListRejectionLength {
g.leaveUnbroken(path, fmt.Sprintf("list max %g is above the generated limit", *shape.Max))
} else if shape.Max != nil {
build := func() any {
long := make([]any, int(*shape.Max)+1)
for i := range long {
long[i], _ = g.quietValue(*shape.Member, depth)
}
return long
}
g.site(path, constraintSite{constraint: ConstraintLength, detail: fmt.Sprintf("%d items, above max %g", int(*shape.Max)+1, *shape.Max), build: build})
}
return list, true
}
// quietValue generates a further valid value without recording sites or
// skips, for list entries beyond the first.
func (g *requestGenerator) quietValue(ref ShapeRef, depth int) (any, bool) {
sites, skipped, unbroken := len(g.sites), len(g.skipped), len(g.unbroken)
value, ok := g.value(ref, nil, depth)
g.sites, g.skipped, g.unbroken = g.sites[:sites], g.skipped[:skipped], g.unbroken[:unbroken]
return value, ok
}
// mapValue generates a one-entry map. The key's constraints are broken by
// renaming the key, since its path is only known once it is generated.
func (g *requestGenerator) mapValue(shape *Shape, path []pathStep, depth int) (any, bool) {
sites, skipped, unbroken := len(g.sites), len(g.skipped), len(g.unbroken)
key, ok := g.value(*shape.Key, append(slices.Clone(path), "{key}"), depth)
keyString, isString := key.(string)
if !ok || !isString {
g.sites, g.skipped, g.unbroken = g.sites[:sites], g.skipped[:skipped], g.unbroken[:unbroken]
g.skip(path, "map key cannot be generated")
return nil, false
}
for i := range g.sites[sites:] {
site := &g.sites[sites+i]
site.path = append(slices.Clone(path), keyString)
site.renameKey = true
}
if shape.Min != nil || shape.Max != nil {
g.leaveUnbroken(path, "map entry counts are not broken")
}
value, ok := g.value(*shape.Value, append(slices.Clone(path), keyString), depth)
if !ok {
return nil, false
}
return map[string]any{keyString: value}, true
}
func (g *requestGenerator) stringValue(shape *Shape, ref ShapeRef, path []pathStep) (any, bool) {
if len(shape.Enum) > 0 {
g.site(path, constraintSite{constraint: ConstraintEnum, detail: "value not in enum", replacement: invalidEnumValue(shape.Enum)})
return shape.Enum[0], true
}
minLength, maxLength := 0, maxGeneratedLength
if shape.Min != nil {
minLength = int(*shape.Min)
}
if shape.Max != nil {
maxLength = min(maxLength, int(*shape.Max))
}
var sampler *patternSampler
if shape.Pattern != "" {
var err error
if sampler, err = newPatternSampler(shape.Pattern); err != nil {
g.skip(path, "pattern is not supported by Go regexp")
return nil, false
}
}
value, ok := g.hint(path)
if !ok || !fits(value, sampler, minLength, maxLength) {
value, ok = g.validString(sampler, minLength, maxLength)
}
if !ok {
g.skip(path, "no value satisfies the pattern and length")
return nil, false
}
label := ref.Location == "uri"
switch {
case shape.Min == nil || minLength == 0:
case label && minLength == 1:
g.leaveUnbroken(path, "an empty URI label changes the route")
default:
g.site(path, g.lengthSite(sampler, 0, minLength-1, minLength-1, fmt.Sprintf("below min %d", minLength)))
}
if shape.Max != nil && maxLength < maxGeneratedLength {
g.site(path, g.lengthSite(sampler, maxLength+1, maxGeneratedLength, maxLength+1, fmt.Sprintf("above max %d", maxLength)))
} else if shape.Max != nil {
g.leaveUnbroken(path, fmt.Sprintf("max length %g is above the generated limit", *shape.Max))
}
if sampler != nil {
if broken, ok := patternBreaker(sampler.re, minLength, maxLength); ok {
g.site(path, constraintSite{constraint: ConstraintPattern, detail: "value does not match " + shape.Pattern, replacement: broken})
} else {
g.leaveUnbroken(path, "no value of a valid length breaks the pattern")
}
}
return value, true
}
func fits(value string, sampler *patternSampler, minLength, maxLength int) bool {
length := len([]rune(value))
return length >= minLength && length <= maxLength && (sampler == nil || sampler.fullMatch(value))
}
// validString builds a salted string that satisfies the pattern and length.
func (g *requestGenerator) validString(sampler *patternSampler, minLength, maxLength int) (string, bool) {
if sampler == nil {
return fitLength("spx"+g.salt, minLength, maxLength), minLength <= maxLength
}
base, ok := sampler.sample(minLength, maxLength)
if !ok {
return "", false
}
for _, candidate := range []string{base + g.salt, g.salt + base} {
if fits(candidate, sampler, minLength, maxLength) {
return candidate, true
}
}
return base, true
}
// lengthSite breaks a string length, keeping to the pattern where it can and
// otherwise falling back to a plain string of fallback runes.
func (g *requestGenerator) lengthSite(sampler *patternSampler, minLength, maxLength, fallback int, detail string) constraintSite {
site := constraintSite{constraint: ConstraintLength, detail: detail}
if sampler != nil {
if value, ok := sampler.sample(minLength, maxLength); ok {
site.replacement = value
return site
}
site.detail += ", also breaking the pattern"
}
site.replacement = fitLength("spx"+g.salt, fallback, fallback)
return site
}
// fitLength pads value with 'a' to minLength runes and truncates it to maxLength.
func fitLength(value string, minLength, maxLength int) string {
runes := []rune(value)
for len(runes) < minLength {
runes = append(runes, 'a')
}
if len(runes) > maxLength {
runes = runes[:max(maxLength, 0)]
}
return string(runes)
}
// patternBreaker finds a string of a valid length that does not match re.
func patternBreaker(re *regexp.Regexp, minLength, maxLength int) (string, bool) {
length := min(max(minLength, 1), maxLength)
for _, filler := range []string{"~", "!", " ", "#", "-", "Z", "0", "a"} {
candidate := strings.Repeat(filler, length)
if !re.MatchString(candidate) {
return candidate, true
}
}
return "", false
}
func invalidEnumValue(values []string) string {
candidate := "NotAModelledValue"
for slices.Contains(values, candidate) {
candidate += "X"
}
return candidate
}
func (g *requestGenerator) integer(shape *Shape, path []pathStep) int64 {
value := int64(1)
if shape.Min != nil {
value = max(value, int64(math.Ceil(*shape.Min)))
}
if shape.Max != nil {
value = min(value, int64(math.Floor(*shape.Max)))
}
if shape.Min != nil && *shape.Min > math.MinInt64+1 {
below := int64(math.Ceil(*shape.Min)) - 1
g.site(path, constraintSite{constraint: ConstraintRange, detail: "below min " + strconv.FormatFloat(*shape.Min, 'g', -1, 64), replacement: below})
}
if shape.Max != nil && *shape.Max < math.MaxInt64-1 {
above := int64(math.Floor(*shape.Max)) + 1
g.site(path, constraintSite{constraint: ConstraintRange, detail: "above max " + strconv.FormatFloat(*shape.Max, 'g', -1, 64), replacement: above})
}
return value
}
func (g *requestGenerator) float(shape *Shape, path []pathStep) float64 {
value := 1.0
if shape.Min != nil {
value = max(value, *shape.Min)
g.site(path, constraintSite{constraint: ConstraintRange, detail: "below min " + strconv.FormatFloat(*shape.Min, 'g', -1, 64), replacement: *shape.Min - 1})
}
if shape.Max != nil {
value = min(value, *shape.Max)
g.site(path, constraintSite{constraint: ConstraintRange, detail: "above max " + strconv.FormatFloat(*shape.Max, 'g', -1, 64), replacement: *shape.Max + 1})
}
return value
}
func (g *requestGenerator) blob(shape *Shape, path []pathStep) []byte {
minLength, maxLength := 0, maxGeneratedLength
if shape.Min != nil {
minLength = int(*shape.Min)
if minLength > 0 {
g.site(path, constraintSite{constraint: ConstraintLength, detail: fmt.Sprintf("below min %d bytes", minLength), replacement: []byte(fitLength("", minLength-1, minLength-1))})
}
}
if shape.Max != nil {
maxLength = min(maxLength, int(*shape.Max))
if maxLength < maxGeneratedLength {
g.site(path, constraintSite{constraint: ConstraintLength, detail: fmt.Sprintf("above max %d bytes", maxLength), replacement: []byte(fitLength("", maxLength+1, maxLength+1))})
} else {
g.leaveUnbroken(path, fmt.Sprintf("max length %g bytes is above the generated limit", *shape.Max))
}
}
return []byte(fitLength("spx"+g.salt, minLength, maxLength))
}
+161
View File
@@ -0,0 +1,161 @@
package awsmodel
import (
"fmt"
"regexp"
"slices"
"strings"
)
// RequestOptions places generated ARNs in the account and region the
// requests are sent as.
type RequestOptions struct {
AccountID string
Region string
}
// Models constrain most identifiers only by length, so "spxa" is a model-valid
// AMI ID that AWS rejects. Hints give resource-naming members a value of the
// right form, used only if the member's own constraints accept it.
// ec2IDPrefixes maps the resource an EC2 "<Resource>Id" member names to its
// ID prefix.
var ec2IDPrefixes = map[string]string{
"Allocation": "eipalloc",
"CapacityReservation": "cr",
"CarrierGateway": "cagw",
"DhcpOptions": "dopt",
"EgressOnlyInternetGateway": "eigw",
"Fleet": "fleet",
"Group": "sg",
"Host": "h",
"Image": "ami",
"Instance": "i",
"InternetGateway": "igw",
"KeyPair": "key",
"LaunchTemplate": "lt",
"NatGateway": "nat",
"NetworkAcl": "acl",
"NetworkInterface": "eni",
"PlacementGroup": "pg",
"PrefixList": "pl",
"RouteTable": "rtb",
"SecurityGroup": "sg",
"SecurityGroupRule": "sgr",
"Snapshot": "snap",
"SourceImage": "ami",
"SourceSnapshot": "snap",
"SpotInstanceRequest": "sir",
"Subnet": "subnet",
"TransitGateway": "tgw",
"Volume": "vol",
"Vpc": "vpc",
"VpcEndpoint": "vpce",
"VpcPeeringConnection": "pcx",
}
var ec2IDMember = regexp.MustCompile(`^(.*?)Ids?$`)
const (
policyDocument = `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]}`
trustPolicyDocument = `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}`
)
// hint returns a value of the right form for the member at path, if one is
// known. salt keeps names distinct between request cases.
func (g *requestGenerator) hint(path []pathStep) (string, bool) {
member := memberName(path)
if member == "" {
return "", false
}
options := g.options
suffix := fmt.Sprintf("%017x", g.saltNumber)
name := "spx" + g.salt
switch g.model.service {
case EC2:
if match := ec2IDMember.FindStringSubmatch(capitalize(member)); match != nil {
if prefix, ok := ec2IDPrefixes[match[1]]; ok {
return prefix + "-" + suffix, true
}
}
case ElasticLoadBalancingV2:
base := fmt.Sprintf("arn:aws:elasticloadbalancing:%s:%s:", options.Region, options.AccountID)
switch member {
case "LoadBalancerArn", "LoadBalancerArns", "ResourceArns":
return base + "loadbalancer/app/" + name + "/" + suffix[1:], true
case "TargetGroupArn", "TargetGroupArns":
return base + "targetgroup/" + name + "/" + suffix[1:], true
case "ListenerArn", "ListenerArns":
return base + "listener/app/" + name + "/" + suffix[1:] + "/" + suffix[1:], true
case "RuleArn", "RuleArns":
return base + "listener-rule/app/" + name + "/" + suffix[1:] + "/" + suffix[1:] + "/" + suffix[1:], true
}
case RDS:
switch member {
case "ResourceName":
return fmt.Sprintf("arn:aws:rds:%s:%s:db:%s", options.Region, options.AccountID, name), true
case "Engine":
return "postgres", true
case "DBInstanceClass":
return "db.t3.medium", true
case "DBParameterGroupFamily":
return "postgres18", true
case "SnapshotType":
return "manual", true
case "Source":
return "user", true
}
case ECS:
switch member {
case "resourceArn":
return fmt.Sprintf("arn:aws:ecs:%s:%s:cluster/%s", options.Region, options.AccountID, name), true
case "taskDefinition":
return name + ":1", true
}
case ECR:
if member == "resourceArn" {
return fmt.Sprintf("arn:aws:ecr:%s:%s:repository/%s", options.Region, options.AccountID, name), true
}
case EKS:
if member == "resourceArn" {
return fmt.Sprintf("arn:aws:eks:%s:%s:cluster/%s", options.Region, options.AccountID, name), true
}
}
switch member {
case "PolicyArn", "PermissionsBoundary":
return fmt.Sprintf("arn:aws:iam::%s:policy/%s", options.AccountID, name), true
case "RoleArn":
return fmt.Sprintf("arn:aws:iam::%s:role/%s", options.AccountID, name), true
case "CertificateArn":
return fmt.Sprintf("arn:aws:acm:%s:%s:certificate/%s-0000-0000-0000-000000000000", options.Region, options.AccountID, suffix[9:]), true
case "PolicyDocument", "Policy":
return policyDocument, true
case "AssumeRolePolicyDocument":
return trustPolicyDocument, true
case "Url":
return "https://" + name + ".example.com", true
case "AccessKeyId":
return "AKIA" + strings.ToUpper(fmt.Sprintf("%016x", g.saltNumber)), true
}
return "", false
}
// memberName is the structure member a value at path belongs to, looking
// through list indices.
func memberName(path []pathStep) string {
for _, step := range slices.Backward(path) {
if name, ok := step.(string); ok {
return name
}
}
return ""
}
func capitalize(name string) string {
if name == "" {
return name
}
return strings.ToUpper(name[:1]) + name[1:]
}
+356
View File
@@ -0,0 +1,356 @@
package awsmodel
//test:in-package — checks generated inputs against the unexported model
//shapes, and the unexported pattern sampler and salt helpers.
import (
"fmt"
"maps"
"regexp"
"slices"
"strings"
"sync"
"testing"
"time"
"unicode/utf8"
"github.com/stretchr/testify/require"
)
var testRequestOptions = RequestOptions{AccountID: "123456789012", Region: "ap-southeast-2"}
// constraintViolation is one model constraint an input breaks, found by
// walking the input against the model independently of the generator.
type constraintViolation struct {
path string
constraint Constraint
}
func (m *Model) inputViolations(shapeName, path string, value any, violations *[]constraintViolation) {
shape := m.shapes[shapeName]
add := func(constraint Constraint) {
*violations = append(*violations, constraintViolation{path: path, constraint: constraint})
}
outside := func(length float64) bool {
return (shape.Min != nil && length < *shape.Min) || (shape.Max != nil && length > *shape.Max)
}
switch shape.Type {
case "structure":
fields := value.(map[string]any)
for _, member := range shape.Required {
if _, ok := fields[member]; !ok {
*violations = append(*violations, constraintViolation{path: path + "." + member, constraint: ConstraintRequired})
}
}
for member, field := range fields {
m.inputViolations(shape.Members[member].Shape, path+"."+member, field, violations)
}
case "list":
items := value.([]any)
if outside(float64(len(items))) {
add(ConstraintLength)
}
for i, item := range items {
m.inputViolations(shape.Member.Shape, fmt.Sprintf("%s[%d]", path, i), item, violations)
}
case "map":
for key, entry := range value.(map[string]any) {
m.inputViolations(shape.Key.Shape, path+mapKeyPath, key, violations)
m.inputViolations(shape.Value.Shape, path+"."+key, entry, violations)
}
case "string":
text := value.(string)
if len(shape.Enum) > 0 {
if !slices.Contains(shape.Enum, text) {
add(ConstraintEnum)
}
return
}
if outside(float64(utf8.RuneCountInString(text))) {
add(ConstraintLength)
}
if shape.Pattern != "" {
if !testPattern(shape.Pattern).MatchString(text) {
add(ConstraintPattern)
}
}
case "integer", "long":
if outside(float64(value.(int64))) {
add(ConstraintRange)
}
case "float", "double":
if outside(value.(float64)) {
add(ConstraintRange)
}
case "blob":
if outside(float64(len(value.([]byte)))) {
add(ConstraintLength)
}
case "boolean":
_ = value.(bool)
case "timestamp":
_ = value.(time.Time)
}
}
var testPatterns sync.Map
func testPattern(pattern string) *regexp.Regexp {
if re, ok := testPatterns.Load(pattern); ok {
return re.(*regexp.Regexp)
}
re := regexp.MustCompile(javaEscape.ReplaceAllString(pattern, `\x{$1}`))
testPatterns.Store(pattern, re)
return re
}
// TestGeneratedRequestsBreakOnlyTheirConstraint checks every modelled
// operation: acceptance cases come first and break nothing, a rejection breaks
// only its named constraint, and every case encodes.
func TestGeneratedRequestsBreakOnlyTheirConstraint(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("generates requests for every modelled operation")
}
for _, service := range Services() {
model, err := Load(service)
require.NoError(t, err)
t.Run(string(service), func(t *testing.T) {
t.Parallel()
for _, operationName := range model.Operations() {
operation, _ := model.Operation(operationName)
if operation.Input == nil {
continue
}
plan, err := GenerateRequests(service, operationName, testRequestOptions)
require.NoError(t, err, operationName)
input := model.shapes[operation.Input.Shape]
rejecting := false
for _, request := range plan.Cases {
require.False(t, rejecting && request.Acceptance(), "%s acceptance %s follows a rejection", operationName, request.Path)
rejecting = !request.Acceptance()
if service != S3 {
_, err := EncodeRequest(service, operationName, request.Input)
require.NoError(t, err, "%s %s %s encodes", operationName, request.Constraint, request.Path)
}
var violations []constraintViolation
model.inputViolations(operation.Input.Shape, "$", request.Input, &violations)
for member := range request.Input {
if !slices.Contains(input.Required, member) {
require.Equal(t, request.Member, member, "%s %s %s carries an unrelated optional member", operationName, request.Constraint, request.Path)
}
}
if request.Acceptance() {
require.Empty(t, violations, "%s acceptance %s", operationName, request.Path)
continue
}
require.NotEmpty(t, violations, "%s %s %s breaks nothing", operationName, request.Constraint, request.Path)
for _, violation := range violations {
require.Equal(t, request.Path, violation.path, "%s %s %s also breaks %v", operationName, request.Constraint, request.Path, violation)
}
require.Contains(t, violations, constraintViolation{path: request.Path, constraint: request.Constraint}, operationName)
}
}
})
}
}
func TestGenerateRequestsIsolatesEachOptionalMember(t *testing.T) {
plan, err := GenerateRequests(IAM, "CreateRole", testRequestOptions)
require.NoError(t, err)
acceptance := map[string]map[string]any{}
for _, request := range plan.Cases {
if request.Acceptance() {
acceptance[request.Member] = request.Input
}
}
require.ElementsMatch(t, []string{"AssumeRolePolicyDocument", "RoleName"}, slices.Collect(maps.Keys(acceptance[""])))
require.ElementsMatch(t, []string{"AssumeRolePolicyDocument", "RoleName", "Tags"}, slices.Collect(maps.Keys(acceptance["Tags"])))
require.ElementsMatch(t, []string{"", "Description", "MaxSessionDuration", "Path", "PermissionsBoundary", "Tags"}, slices.Collect(maps.Keys(acceptance)))
// Every case gets its own names, so one case cannot collide with what an
// earlier case created.
names := map[any]bool{}
for _, request := range plan.Cases {
if name, ok := request.Input["RoleName"]; ok && request.Path != "$.RoleName" {
require.False(t, names[name], "RoleName %v reused", name)
names[name] = true
}
}
}
func TestGenerateRequestsAttributesRejections(t *testing.T) {
plan, err := GenerateRequests(IAM, "CreateRole", testRequestOptions)
require.NoError(t, err)
byPath := map[string]RequestCase{}
for _, request := range plan.Cases {
if !request.Acceptance() {
byPath[string(request.Constraint)+" "+request.Path] = request
}
}
// A broken required member is judged against the required-only request;
// a broken optional one against the request that adds just that member.
require.Empty(t, byPath["required $.RoleName"].Member)
require.Equal(t, "MaxSessionDuration", byPath["range $.MaxSessionDuration"].Member)
require.Equal(t, "Tags", byPath["required $.Tags[0].Key"].Member)
}
func TestGenerateRequestsBreaksEachConstraintKind(t *testing.T) {
plan, err := GenerateRequests(IAM, "CreateRole", testRequestOptions)
require.NoError(t, err)
var rejections []string
for _, request := range plan.Cases {
if !request.Acceptance() {
rejections = append(rejections, string(request.Constraint)+" "+request.Path)
}
}
for _, want := range []string{
"required $.RoleName", "length $.RoleName", "pattern $.RoleName",
"range $.MaxSessionDuration", "length $.Tags", "required $.Tags[0].Key",
} {
require.Contains(t, rejections, want)
}
plan, err = GenerateRequests(EC2, "CreateVolume", testRequestOptions)
require.NoError(t, err)
var enums []string
for _, request := range plan.Cases {
if request.Constraint == ConstraintEnum {
enums = append(enums, request.Path)
}
}
require.Contains(t, enums, "$.VolumeType")
}
func TestGenerateRequestsBreaksMapKeys(t *testing.T) {
plan, err := GenerateRequests(EKS, "TagResource", testRequestOptions)
require.NoError(t, err)
var keyRejections []Constraint
for _, request := range plan.Cases {
if request.Path == "$.tags"+mapKeyPath {
keyRejections = append(keyRejections, request.Constraint)
tags := request.Input["tags"].(map[string]any)
require.Len(t, tags, 1, "the key is renamed, not added")
}
}
require.Equal(t, []Constraint{ConstraintLength, ConstraintLength}, keyRejections, "below min and above max")
}
func TestGenerateRequestsRecordsUnbrokenConstraints(t *testing.T) {
plan, err := GenerateRequests(EKS, "DescribeCluster", testRequestOptions)
require.NoError(t, err)
require.Contains(t, plan.Unbroken, Skip{Path: "$.name", Reason: "a required URI label cannot be omitted"})
plan, err = GenerateRequests(IAM, "CreateRole", testRequestOptions)
require.NoError(t, err)
require.Contains(t, plan.Unbroken, Skip{Path: "$.AssumeRolePolicyDocument", Reason: "max length 131072 is above the generated limit"})
require.Contains(t, plan.DeclaredErrors, "InvalidInput")
}
func TestGenerateRequestsSkipsAcceptanceForUnguessableMembers(t *testing.T) {
plan, err := GenerateRequests(IAM, "ListRoles", testRequestOptions)
require.NoError(t, err)
var acceptance, markerRejections []string
for _, request := range plan.Cases {
if request.Acceptance() {
acceptance = append(acceptance, request.Member)
} else if request.Member == "Marker" {
markerRejections = append(markerRejections, string(request.Constraint))
}
}
require.ElementsMatch(t, []string{"", "MaxItems", "PathPrefix"}, acceptance)
require.NotEmpty(t, markerRejections, "a pagination token still gets rejection requests")
}
func TestGenerateRequestsUsesResourceHints(t *testing.T) {
tests := []struct {
service Service
operation string
member string
want *regexp.Regexp
}{
{EC2, "TerminateInstances", "InstanceIds", regexp.MustCompile(`^i-[0-9a-f]{17}$`)},
{EC2, "DeleteSecurityGroup", "GroupId", regexp.MustCompile(`^sg-[0-9a-f]{17}$`)},
{ElasticLoadBalancingV2, "DeleteLoadBalancer", "LoadBalancerArn", regexp.MustCompile(`^arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:loadbalancer/app/`)},
{IAM, "AttachRolePolicy", "PolicyArn", regexp.MustCompile(`^arn:aws:iam::123456789012:policy/`)},
{RDS, "CreateDBInstance", "Engine", regexp.MustCompile(`^postgres$`)},
{ECR, "TagResource", "resourceArn", regexp.MustCompile(`^arn:aws:ecr:ap-southeast-2:123456789012:repository/`)},
{EKS, "TagResource", "resourceArn", regexp.MustCompile(`^arn:aws:eks:ap-southeast-2:123456789012:cluster/`)},
}
for _, test := range tests {
t.Run(test.operation+"."+test.member, func(t *testing.T) {
plan, err := GenerateRequests(test.service, test.operation, testRequestOptions)
require.NoError(t, err)
for _, request := range plan.Cases {
if !request.Acceptance() {
continue
}
value, ok := request.Input[test.member]
if !ok {
continue
}
if values, isList := value.([]any); isList {
value = values[0]
}
require.Regexp(t, test.want, value)
return
}
t.Fatalf("no acceptance request sets %s", test.member)
})
}
}
func TestPatternSamplerMatchesWholeValue(t *testing.T) {
tests := []struct {
pattern string
min, max int
}{
{`^[\w+=,.@-]+$`, 1, 64},
{`(/)|(/[!-~]+/)`, 1, 512},
{`[a-z]{3}-[0-9]{4}`, 8, 8},
{`^arn:aws:iam::\d{12}:role/[a-zA-Z0-9]+$`, 20, 2048},
{`(Tcp|Udp)`, 3, 3},
{`[\p{L}\p{Z}\p{N}_.:/=+\-@]*`, 0, 128},
}
for _, test := range tests {
t.Run(test.pattern, func(t *testing.T) {
sampler, err := newPatternSampler(test.pattern)
require.NoError(t, err)
value, ok := sampler.sample(test.min, test.max)
require.True(t, ok)
require.True(t, sampler.fullMatch(value), "%q does not fully match", value)
length := utf8.RuneCountInString(value)
require.GreaterOrEqual(t, length, test.min)
require.LessOrEqual(t, length, test.max)
})
}
}
func TestPatternSamplerRejectsLookahead(t *testing.T) {
_, err := newPatternSampler(`^(?!aws:).*`)
require.Error(t, err)
}
func TestPatternBreakerAvoidsUnanchoredMatches(t *testing.T) {
// Unanchored, the pattern matches any value with one letter in it.
re := regexp.MustCompile(`[a-z]`)
broken, ok := patternBreaker(re, 3, 10)
require.True(t, ok)
require.False(t, re.MatchString(broken), "%q", broken)
require.GreaterOrEqual(t, len(broken), 3)
_, ok = patternBreaker(regexp.MustCompile(`.*`), 1, 10)
require.False(t, ok, "nothing breaks a pattern that matches everything")
}
func TestSaltLettersAreDistinct(t *testing.T) {
seen := map[string]bool{}
for n := range 2000 {
salt := saltLetters(n)
require.False(t, seen[salt], "salt %d repeats %q", n, salt)
require.Equal(t, strings.ToLower(salt), salt)
seen[salt] = true
}
}
+17 -2
View File
@@ -229,7 +229,10 @@ func (t *smithyTranslator) translate() (*Model, error) {
}
func (t *smithyTranslator) metadata() (Metadata, error) {
var service *smithyShape
var (
service *smithyShape
serviceID string
)
for id, shape := range t.document.Shapes {
if shape.Type != "service" {
continue
@@ -237,7 +240,7 @@ func (t *smithyTranslator) metadata() (Metadata, error) {
if service != nil {
return Metadata{}, fmt.Errorf("model defines more than one service, including %s", id)
}
service = &shape
service, serviceID = &shape, id
}
if service == nil {
return Metadata{}, errors.New("model defines no service")
@@ -252,6 +255,10 @@ func (t *smithyTranslator) metadata() (Metadata, error) {
return Metadata{}, fmt.Errorf("service declares more than one protocol")
}
metadata.Protocol = protocol
if version, ok := strings.CutPrefix(trait, "aws.protocols#awsJson"); ok {
metadata.JSONVersion = strings.ReplaceAll(version, "_", ".")
metadata.TargetPrefix = localName(serviceID)
}
}
if metadata.Protocol == "" {
return Metadata{}, errors.New("service declares no supported protocol")
@@ -268,9 +275,16 @@ func (t *smithyTranslator) metadata() (Metadata, error) {
if err != nil {
return Metadata{}, err
}
var sigv4 struct {
Name string `json:"name"`
}
if _, err := service.Traits.decode("aws.auth#sigv4", &sigv4); err != nil {
return Metadata{}, err
}
metadata.EndpointPrefix = awsService.EndpointPrefix
metadata.ServiceID = awsService.SDKID
metadata.ServiceFullName = title
metadata.SigningName = sigv4.Name
return metadata, nil
}
@@ -371,6 +385,7 @@ func (t *smithyTranslator) shape(source smithyShape) (*Shape, error) {
shape.Enum = append(shape.Enum, value)
}
case "structure", "union":
shape.Union = source.Type == "union"
shape.Members = make(map[string]ShapeRef, len(source.Members))
for _, member := range source.Members {
ref, err := t.memberRef(member)
+168
View File
@@ -0,0 +1,168 @@
package awsmodel
import (
"net/http"
"slices"
"strings"
)
// Verdict is the outcome of one generated request.
type Verdict string
const (
VerdictPass Verdict = "pass"
VerdictFinding Verdict = "finding"
VerdictInconclusive Verdict = "inconclusive"
// VerdictUndeclaredError is a rejection refused with a validation code
// that neither the operation nor the common errors declare.
VerdictUndeclaredError Verdict = "undeclared_error"
)
// validationErrorCodes are the codes that refuse a request as failing
// parameter validation, as opposed to naming something missing or conflicting
// with state. InvalidParameterValueException is not modelled but is one.
var validationErrorCodes = map[string]bool{
"InvalidInput": true,
"InvalidParameter": true,
"InvalidParameterCombination": true,
"InvalidParameterException": true,
"InvalidParameterValue": true,
"InvalidParameterValueException": true,
"MissingParameter": true,
"SerializationException": true,
"UnknownParameter": true,
"ValidationError": true,
"ValidationException": true,
}
// commonValidationCodes are validation errors AWS lists as common to every
// service rather than declaring them on each operation.
var commonValidationCodes = map[string]bool{
"InvalidParameterCombination": true,
"InvalidParameterValue": true,
"MissingParameter": true,
"ValidationError": true,
"ValidationException": true,
}
// IsValidationErrorCode reports whether code is a parameter validation error,
// including EC2's per-resource "*.Malformed" codes.
func IsValidationErrorCode(code string) bool {
return validationErrorCodes[code] || strings.HasSuffix(code, ".Malformed")
}
// RequestResult is what came back for one generated request. Code is empty for
// a success.
type RequestResult struct {
Status int
Code string
Message string
}
func (r RequestResult) succeeded() bool { return r.Status >= 200 && r.Status < 300 }
// conditionalRequirementCodes name a member the request omits or a pairing it
// breaks. Models mark conditionally required members optional, so an
// acceptance request refused with one of these may be refused by AWS too.
var conditionalRequirementCodes = map[string]bool{
"InvalidParameterCombination": true,
"MissingParameter": true,
}
// RequestJudge judges a plan's cases in plan order. A refusal counts for a
// rejection case only if the acceptance cases it builds on were accepted; a
// rejection case that succeeds is a finding regardless.
type RequestJudge struct {
declared []string
refused map[string]bool
}
func (p RequestPlan) NewJudge() *RequestJudge {
return &RequestJudge{declared: p.DeclaredErrors, refused: map[string]bool{}}
}
func (j *RequestJudge) Judge(c RequestCase, result RequestResult) Verdict {
validation := !result.succeeded() && IsValidationErrorCode(result.Code)
if c.Acceptance() {
baseRefused := c.Member != "" && j.refused[""]
j.refused[c.Member] = validation
switch {
case baseRefused:
return VerdictInconclusive
case result.succeeded():
return VerdictPass
case validation && !conditionalRequirementCodes[result.Code]:
return VerdictFinding
default:
return VerdictInconclusive
}
}
if result.succeeded() {
return VerdictFinding
}
// A member with no acceptance case, such as a pagination token, gives a
// refusal nothing to be attributed against.
baseRefused, baseJudged := j.refused[""]
memberRefused, memberJudged := j.refused[c.Member]
switch {
case !validation, !baseJudged, !memberJudged, baseRefused, memberRefused:
return VerdictInconclusive
case len(j.declared) > 0 && !slices.Contains(j.declared, result.Code) && !commonValidationCodes[result.Code]:
return VerdictUndeclaredError
default:
return VerdictPass
}
}
// DecodeRequestResult extracts the error code and message from a response.
func DecodeRequestResult(service Service, status int, header http.Header, body []byte) RequestResult {
result := RequestResult{Status: status}
if status >= 200 && status < 300 {
return result
}
model, err := Load(service)
if err != nil {
return result
}
// rest-json services may carry the code only in a header.
if code := header.Get("X-Amzn-Errortype"); code != "" {
result.Code = trimErrorCode(code)
} else if code, err := model.decodeErrorCode(body); err == nil {
result.Code = code
}
result.Message = decodeErrorMessage(model.metadata.Protocol, body)
return result
}
func trimErrorCode(code string) string {
if separator := strings.Index(code, ":"); separator >= 0 {
code = code[:separator]
}
if separator := strings.LastIndex(code, "#"); separator >= 0 {
code = code[separator+1:]
}
return code
}
func decodeErrorMessage(protocol string, body []byte) string {
switch protocol {
case "json", "rest-json":
document, err := decodeJSONResponse(body)
if err != nil {
return ""
}
fields, _ := document.(map[string]any)
for _, key := range []string{"message", "Message", "errorMessage"} {
if message, ok := fields[key].(string); ok {
return message
}
}
return ""
default:
root, err := parseXML(body)
if err != nil {
return ""
}
return findXMLText(root, "Message")
}
}
+123
View File
@@ -0,0 +1,123 @@
package awsmodel_test
import (
"net/http"
"testing"
"github.com/mulgadc/spinifex/internal/awsmodel"
"github.com/stretchr/testify/require"
)
var (
accepted = awsmodel.RequestResult{Status: 200}
invalid = awsmodel.RequestResult{Status: 400, Code: "ValidationError"}
missing = awsmodel.RequestResult{Status: 400, Code: "MissingParameter"}
notFound = awsmodel.RequestResult{Status: 404, Code: "NoSuchEntity"}
createRolePlan = awsmodel.RequestPlan{DeclaredErrors: []string{"InvalidInput", "LimitExceeded"}}
)
func TestRequestJudgeAcceptance(t *testing.T) {
tests := []struct {
name string
result awsmodel.RequestResult
want awsmodel.Verdict
}{
{"accepted valid request", accepted, awsmodel.VerdictPass},
{"valid request refused as invalid", invalid, awsmodel.VerdictFinding},
{"valid request refused as malformed", awsmodel.RequestResult{Status: 400, Code: "InvalidInstanceID.Malformed"}, awsmodel.VerdictFinding},
{"valid request missing a conditionally required member", missing, awsmodel.VerdictInconclusive},
{"valid request names a missing resource", notFound, awsmodel.VerdictInconclusive},
{"valid request times out", awsmodel.RequestResult{Code: "Timeout"}, awsmodel.VerdictInconclusive},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.want, createRolePlan.NewJudge().Judge(awsmodel.RequestCase{}, test.result))
})
}
}
func TestRequestJudgeRejection(t *testing.T) {
rejection := awsmodel.RequestCase{Constraint: awsmodel.ConstraintLength, Path: "$.RoleName"}
tests := []struct {
name string
plan awsmodel.RequestPlan
result awsmodel.RequestResult
want awsmodel.Verdict
}{
{"invalid request accepted", createRolePlan, accepted, awsmodel.VerdictFinding},
{"refused with a common validation error", createRolePlan, invalid, awsmodel.VerdictPass},
{"refused for a missing member", createRolePlan, missing, awsmodel.VerdictPass},
{"refused with a declared error", createRolePlan, awsmodel.RequestResult{Status: 400, Code: "InvalidInput"}, awsmodel.VerdictPass},
{"refused with an undeclared error", createRolePlan, awsmodel.RequestResult{Status: 400, Code: "InvalidParameterValueException"}, awsmodel.VerdictUndeclaredError},
{"operation declares no errors", awsmodel.RequestPlan{}, awsmodel.RequestResult{Status: 400, Code: "InvalidParameterValueException"}, awsmodel.VerdictPass},
{"refused for another reason", createRolePlan, notFound, awsmodel.VerdictInconclusive},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
judge := test.plan.NewJudge()
require.Equal(t, awsmodel.VerdictPass, judge.Judge(awsmodel.RequestCase{}, accepted))
require.Equal(t, test.want, judge.Judge(rejection, test.result))
})
}
}
func TestRequestJudgeDiscountsRefusalsOfRefusedAcceptance(t *testing.T) {
judge := createRolePlan.NewJudge()
require.Equal(t, awsmodel.VerdictPass, judge.Judge(awsmodel.RequestCase{}, accepted))
require.Equal(t, awsmodel.VerdictFinding, judge.Judge(awsmodel.RequestCase{Member: "Tags"}, invalid))
require.Equal(t, awsmodel.VerdictPass, judge.Judge(awsmodel.RequestCase{Member: "Path"}, accepted))
// Tags alone was refused, so a refusal of its rejection proves nothing.
require.Equal(t, awsmodel.VerdictInconclusive, judge.Judge(awsmodel.RequestCase{Member: "Tags", Constraint: awsmodel.ConstraintLength}, invalid))
require.Equal(t, awsmodel.VerdictFinding, judge.Judge(awsmodel.RequestCase{Member: "Tags", Constraint: awsmodel.ConstraintLength}, accepted))
require.Equal(t, awsmodel.VerdictPass, judge.Judge(awsmodel.RequestCase{Member: "Path", Constraint: awsmodel.ConstraintPattern}, invalid))
// A refused base leaves no refusal attributable to any single member, but
// a request breaking a constraint that succeeds is still a finding.
judge = createRolePlan.NewJudge()
require.Equal(t, awsmodel.VerdictInconclusive, judge.Judge(awsmodel.RequestCase{}, missing))
require.Equal(t, awsmodel.VerdictInconclusive, judge.Judge(awsmodel.RequestCase{Constraint: awsmodel.ConstraintRequired, Path: "$.RoleName"}, invalid))
require.Equal(t, awsmodel.VerdictFinding, judge.Judge(awsmodel.RequestCase{Constraint: awsmodel.ConstraintRequired, Path: "$.RoleName"}, accepted))
require.Equal(t, awsmodel.VerdictInconclusive, judge.Judge(awsmodel.RequestCase{Member: "Tags"}, accepted))
require.Equal(t, awsmodel.VerdictFinding, judge.Judge(awsmodel.RequestCase{Member: "Tags", Constraint: awsmodel.ConstraintLength}, accepted))
}
func TestRequestJudgeDiscountsRefusalsOfMembersWithoutAcceptance(t *testing.T) {
judge := createRolePlan.NewJudge()
require.Equal(t, awsmodel.VerdictPass, judge.Judge(awsmodel.RequestCase{}, accepted))
// A pagination token gets no acceptance case, so its refusal may be of
// the token rather than the broken constraint.
marker := awsmodel.RequestCase{Member: "Marker", Constraint: awsmodel.ConstraintLength, Path: "$.Marker"}
require.Equal(t, awsmodel.VerdictInconclusive, judge.Judge(marker, invalid))
require.Equal(t, awsmodel.VerdictFinding, judge.Judge(marker, accepted))
}
func TestDecodeRequestResult(t *testing.T) {
tests := []struct {
name string
service awsmodel.Service
header http.Header
body string
code string
message string
}{
{"awsQuery", awsmodel.IAM, nil,
`<ErrorResponse><Error><Type>Sender</Type><Code>ValidationError</Code><Message>bad name</Message></Error></ErrorResponse>`,
"ValidationError", "bad name"},
{"ec2", awsmodel.EC2, nil,
`<Response><Errors><Error><Code>InvalidParameterValue</Code><Message>bad id</Message></Error></Errors><RequestID>r</RequestID></Response>`,
"InvalidParameterValue", "bad id"},
{"json", awsmodel.ECS, nil,
`{"__type":"com.amazonaws.ecs#InvalidParameterException","message":"bad cluster"}`,
"InvalidParameterException", "bad cluster"},
{"rest-json header", awsmodel.EKS, http.Header{"X-Amzn-Errortype": {"InvalidParameterException:http://internal.amazon.com/"}},
`{"message":"bad name"}`,
"InvalidParameterException", "bad name"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := awsmodel.DecodeRequestResult(test.service, 400, test.header, []byte(test.body))
require.Equal(t, awsmodel.RequestResult{Status: 400, Code: test.code, Message: test.message}, result)
})
}
require.Equal(t, awsmodel.RequestResult{Status: 200}, awsmodel.DecodeRequestResult(awsmodel.IAM, 200, nil, []byte("<ok/>")))
}
@@ -32,3 +32,28 @@ Six additional production-referenced values are deliberately not accepted by the
Phase 5 generates the public model-versus-dispatch inventory as one page per service under `docs/coverage/`, plus an index carrying the cross-service summary. Run `make aws-model-coverage` to regenerate the pages and print a terminal summary. A normal `make build` and the integration target both regenerate the pages so handler-map changes cannot silently leave them stale.
The inventory counts modelled operations bound to real handlers separately from registered stubs and deliberately unsupported handlers. S3 is marked opaque because Spinifex delegates its REST surface to Predastore rather than using an operation-name dispatch table; no mechanical S3 coverage percentage is claimed.
## Request conformance
`TestRequestConformance` generates requests from the service models for every implemented operation except S3's, sends them through the in-process gateway with every daemon-lite wired, and judges each response. An acceptance request carries the required members plus at most one optional member and should not be refused as invalid. A rejection request breaks one constraint (a required member, enum, length, range or pattern, including a map key's) and should be refused with a validation error. Anything else, such as a missing resource, is inconclusive.
A rejection that succeeds is always a finding. A refusal counts as a pass only when the acceptance request it builds on was accepted, and only when its code is one the operation declares or one AWS lists as common to every service (`ValidationError`, `ValidationException`, `MissingParameter`, `InvalidParameterValue`, `InvalidParameterCombination`). Any other validation code is reported as an undeclared error. Operations that declare no errors, which includes all of EC2, are not compared.
Request findings and undeclared errors block only for services listed under `promotedRequestServices` in `conformance-promoted-services.json`, which starts empty; response promotion does not carry over.
The 2026-09-28 baseline sent 3986 requests across 368 operations: 794 pass, 396 findings, 101 undeclared errors, 2695 inconclusive. None of it has been checked against AWS, so each finding below is a lead to verify before any fix.
| Finding | Count | Notes |
|---|---:|---|
| A request breaking a model constraint is accepted | 336 | IAM 116, ECS 63, ELBv2 48, ECR 41, EC2 34, ACM 15, RDS 7, STS 6, EKS 6. Examples: IAM `List*` accept out-of-range `MaxItems` and `Marker`; IAM, ELBv2 and ECR accept a tag without `Key`; EC2 `Describe*` accept out-of-range `MaxResults`; ECS `CreateCapacityProvider` accepts every enum and range break inside `autoScalingGroupProvider`; STS `GetSessionToken` accepts a `SerialNumber` and `TokenCode` below their minimum length. |
| A model-valid request is refused as invalid | 60 | Mostly deliberate limitations that say so: RDS `ModifyDBInstance` members, STS session policies, tags and MFA, IAM `PermissionsBoundary`, ECS `availabilityZoneRebalancing`. Others return a generic message whose cause is unresolved (ECS, EC2, EKS), or the model leaves out a value AWS itself requires, such as an RDS `AllocatedStorage` below the engine minimum. |
| A refusal uses an undeclared error code | 101 | ECR (89) and EKS (1) answer with `InvalidParameterValueException`; both models declare `InvalidParameterException`. IAM (11) answers `CreateGroup` and `Put*Policy` constraint breaks with `InvalidInput`, which those operations do not declare. |
Known limits of the sweep:
- Models mark conditionally required members optional, so an acceptance request refused with `MissingParameter` or `InvalidParameterCombination` is inconclusive, and so is every later refusal for that operation once its required-only request is refused.
- Filter names and pagination tokens cannot be generated validly, so `Filters`, `Marker` and `NextToken` get rejection requests only, and a refusal of one is inconclusive.
- ACM `RequestCertificate` sends no requests: its `DomainName` pattern uses lookahead, which Go's regexp cannot sample. The report lists such operations as `UNTESTED`.
- Constraints no request breaks are counted as `unbroken_constraints`: required URI labels, maximum lengths above the generated limit (such as IAM policy documents), map entry counts, and patterns no value of a valid length breaks.
- Most inconclusives are resource lookups that run before validation (`NoSuchEntity`, `*NotFound`), EC2 requests missing a conditionally required member (`MissingParameter`), EC2 subjects no daemon-lite answers (`InternalError`), and EKS operations whose backends are absent (`ServiceUnavailableException`, `NotImplementedException`).
- The EC2 query parser ignores parameters it does not know, which no generated request can reveal because every member it sends is modelled.
@@ -1,5 +1,6 @@
{
"promotedServices": [
"sts"
]
],
"promotedRequestServices": []
}
+29 -9
View File
@@ -20,12 +20,16 @@ const (
conformanceModeFail conformanceMode = "fail"
)
// conformancePolicy lists the services whose findings block the suite: their
// responses, and separately the generated requests they are sent.
type conformancePolicy struct {
promoted map[awsmodel.Service]bool
promoted map[awsmodel.Service]bool
requestPromoted map[awsmodel.Service]bool
}
type conformancePolicyFile struct {
PromotedServices []awsmodel.Service `json:"promotedServices"`
PromotedServices []awsmodel.Service `json:"promotedServices"`
PromotedRequestServices []awsmodel.Service `json:"promotedRequestServices"`
}
//go:embed conformance-promoted-services.json
@@ -39,21 +43,33 @@ func loadConformancePolicy() (conformancePolicy, error) {
return conformancePolicy{}, fmt.Errorf("parse promoted-services policy: %w", err)
}
promoted, err := promotedServiceSet("promotedServices", file.PromotedServices)
if err != nil {
return conformancePolicy{}, err
}
requestPromoted, err := promotedServiceSet("promotedRequestServices", file.PromotedRequestServices)
if err != nil {
return conformancePolicy{}, err
}
return conformancePolicy{promoted: promoted, requestPromoted: requestPromoted}, nil
}
func promotedServiceSet(field string, services []awsmodel.Service) (map[awsmodel.Service]bool, error) {
known := make(map[awsmodel.Service]bool)
for _, service := range awsmodel.Services() {
known[service] = true
}
policy := conformancePolicy{promoted: make(map[awsmodel.Service]bool, len(file.PromotedServices))}
for _, service := range file.PromotedServices {
set := make(map[awsmodel.Service]bool, len(services))
for _, service := range services {
if !known[service] {
return conformancePolicy{}, fmt.Errorf("promoted-services policy contains unknown service %q", service)
return nil, fmt.Errorf("promoted-services policy %s contains unknown service %q", field, service)
}
if policy.promoted[service] {
return conformancePolicy{}, fmt.Errorf("promoted-services policy contains duplicate service %q", service)
if set[service] {
return nil, fmt.Errorf("promoted-services policy %s contains duplicate service %q", field, service)
}
policy.promoted[service] = true
set[service] = true
}
return policy, nil
return set, nil
}
func conformancePolicyFor(services ...awsmodel.Service) conformancePolicy {
@@ -68,6 +84,10 @@ func (p conformancePolicy) isPromoted(service awsmodel.Service) bool {
return p.promoted[service]
}
func (p conformancePolicy) isRequestPromoted(service awsmodel.Service) bool {
return p.requestPromoted[service]
}
func (p conformancePolicy) services() []awsmodel.Service {
services := make([]awsmodel.Service, 0, len(p.promoted))
for service := range p.promoted {
@@ -15,6 +15,7 @@ func TestEmbeddedConformancePolicyPromotesSTS(t *testing.T) {
require.Equal(t, []awsmodel.Service{awsmodel.STS}, policy.services())
require.True(t, policy.isPromoted(awsmodel.STS))
require.False(t, policy.isPromoted(awsmodel.EC2))
require.False(t, policy.isRequestPromoted(awsmodel.STS), "request findings are promoted separately from response findings")
}
func TestConformanceModeFromEnvironment(t *testing.T) {
+89 -115
View File
@@ -5,8 +5,8 @@ package integration
import (
"context"
"encoding/json"
"errors"
"log/slog"
"reflect"
"testing"
"github.com/mulgadc/spinifex/spinifex/awserrors"
@@ -181,44 +181,10 @@ func StartDaemonLite(t *testing.T, gw *Gateway, opts ...DaemonLiteOption) *Daemo
return dl
}
// dispatch replicates daemon.handleNATSRequest's unmarshal -> service ->
// marshal -> respond envelope (daemon/daemon_handlers.go), so a subscribed
// service impl answers a NATS request exactly like the real daemon handler
// would, without needing an exported hook into the unexported daemon package.
// dispatch answers a NATS request with one service method, for subjects not
// named after the method.
func dispatch[I any, O any](msg *nats.Msg, serviceFn func(context.Context, *I, string) (*O, error)) {
ctx, span := utils.StartConsumerSpan(msg)
defer span.End()
accountID := utils.AccountIDFromMsg(msg)
input := new(I)
if errResp := utils.UnmarshalJsonPayload(input, msg.Data); errResp != nil {
utils.MarkSpanError(span, errors.New(awserrors.ErrorInvalidParameterValue))
if err := msg.Respond(errResp); err != nil {
slog.Error("dispatch: failed to respond to NATS request", "err", err)
}
return
}
output, err := serviceFn(ctx, input, accountID)
if err != nil {
utils.MarkSpanError(span, err)
if respErr := msg.Respond(utils.GenerateErrorPayload(awserrors.ValidErrorCode(err.Error()))); respErr != nil {
slog.Error("dispatch: failed to respond to NATS request", "err", respErr)
}
return
}
jsonResponse, err := json.Marshal(output)
if err != nil {
slog.Error("dispatch: failed to marshal response", "err", err)
if respErr := msg.Respond(utils.GenerateErrorPayload(awserrors.ErrorServerInternal)); respErr != nil {
slog.Error("dispatch: failed to respond to NATS request", "err", respErr)
}
return
}
if err := msg.Respond(jsonResponse); err != nil {
slog.Error("dispatch: failed to respond to NATS request", "err", err)
}
dispatchReflected(msg, reflect.ValueOf(serviceFn), false)
}
// sub registers a plain (non-queue-group) subscription and its t.Cleanup
@@ -231,83 +197,91 @@ func sub(t *testing.T, nc *nats.Conn, subject string, handler nats.MsgHandler) {
t.Cleanup(func() { _ = s.Unsubscribe() })
}
// subscribe wires every subject the in-scope ported tests (TestKeyPairs,
// TestTagManagement, TestRouteTableValidation, TestReplaceRouteConvergence,
// TestAccountScoping_*, TestSerialConsoleAccess) exercise, plus their
// supporting VPC/subnet/SG/IGW/EIGW/account-settings subjects, to the real
// service impls held on dl.
// subscribe wires every handler method of the service impls held on dl to its
// "ec2.<Method>" subject.
func (dl *DaemonLite) subscribe(t *testing.T, nc *nats.Conn) {
t.Helper()
// Key pairs.
sub(t, nc, "ec2.CreateKeyPair", func(m *nats.Msg) { dispatch(m, dl.Key.CreateKeyPair) })
sub(t, nc, "ec2.DeleteKeyPair", func(m *nats.Msg) { dispatch(m, dl.Key.DeleteKeyPair) })
sub(t, nc, "ec2.DescribeKeyPairs", func(m *nats.Msg) { dispatch(m, dl.Key.DescribeKeyPairs) })
sub(t, nc, "ec2.ImportKeyPair", func(m *nats.Msg) { dispatch(m, dl.Key.ImportKeyPair) })
// Tags. Unlike the live daemon's handleEC2CreateTags/handleEC2DeleteTags
// (daemon/daemon_handlers_tag.go), this dispatches straight to
// TagsServiceImpl and skips the instance-ID routing split and the
// recordTagMirrors projection onto owning resource records — both exist
// only to support instance/volume tagging, which is out of scope here
// and neither is observed by the ported TestTagManagement assertions (all
// of which read back through
// DescribeTags, the central tag store both paths write identically).
sub(t, nc, "ec2.CreateTags", func(m *nats.Msg) { dispatch(m, dl.Tags.CreateTags) })
sub(t, nc, "ec2.DeleteTags", func(m *nats.Msg) { dispatch(m, dl.Tags.DeleteTags) })
sub(t, nc, "ec2.DescribeTags", func(m *nats.Msg) { dispatch(m, dl.Tags.DescribeTags) })
// Route tables.
sub(t, nc, "ec2.CreateRouteTable", func(m *nats.Msg) { dispatch(m, dl.RouteTable.CreateRouteTable) })
sub(t, nc, "ec2.DeleteRouteTable", func(m *nats.Msg) { dispatch(m, dl.RouteTable.DeleteRouteTable) })
sub(t, nc, "ec2.DescribeRouteTables", func(m *nats.Msg) { dispatch(m, dl.RouteTable.DescribeRouteTables) })
sub(t, nc, "ec2.CreateRoute", func(m *nats.Msg) { dispatch(m, dl.RouteTable.CreateRoute) })
sub(t, nc, "ec2.DeleteRoute", func(m *nats.Msg) { dispatch(m, dl.RouteTable.DeleteRoute) })
sub(t, nc, "ec2.ReplaceRoute", func(m *nats.Msg) { dispatch(m, dl.RouteTable.ReplaceRoute) })
sub(t, nc, "ec2.AssociateRouteTable", func(m *nats.Msg) { dispatch(m, dl.RouteTable.AssociateRouteTable) })
sub(t, nc, "ec2.DisassociateRouteTable", func(m *nats.Msg) { dispatch(m, dl.RouteTable.DisassociateRouteTable) })
sub(t, nc, "ec2.ReplaceRouteTableAssociation", func(m *nats.Msg) { dispatch(m, dl.RouteTable.ReplaceRouteTableAssociation) })
// VPC / subnet / ENI / security group.
sub(t, nc, "ec2.CreateVpc", func(m *nats.Msg) { dispatch(m, dl.VPC.CreateVpc) })
sub(t, nc, "ec2.DeleteVpc", func(m *nats.Msg) { dispatch(m, dl.VPC.DeleteVpc) })
sub(t, nc, "ec2.DescribeVpcs", func(m *nats.Msg) { dispatch(m, dl.VPC.DescribeVpcs) })
sub(t, nc, "ec2.CreateSubnet", func(m *nats.Msg) { dispatch(m, dl.VPC.CreateSubnet) })
sub(t, nc, "ec2.DeleteSubnet", func(m *nats.Msg) { dispatch(m, dl.VPC.DeleteSubnet) })
sub(t, nc, "ec2.DescribeSubnets", func(m *nats.Msg) { dispatch(m, dl.VPC.DescribeSubnets) })
sub(t, nc, "ec2.ModifySubnetAttribute", func(m *nats.Msg) { dispatch(m, dl.VPC.ModifySubnetAttribute) })
sub(t, nc, "ec2.ModifyVpcAttribute", func(m *nats.Msg) { dispatch(m, dl.VPC.ModifyVpcAttribute) })
sub(t, nc, "ec2.DescribeVpcAttribute", func(m *nats.Msg) { dispatch(m, dl.VPC.DescribeVpcAttribute) })
sub(t, nc, "ec2.CreateNetworkInterface", func(m *nats.Msg) { dispatch(m, dl.VPC.CreateNetworkInterface) })
sub(t, nc, "ec2.DeleteNetworkInterface", func(m *nats.Msg) { dispatch(m, dl.VPC.DeleteNetworkInterface) })
sub(t, nc, "ec2.DescribeNetworkInterfaces", func(m *nats.Msg) { dispatch(m, dl.VPC.DescribeNetworkInterfaces) })
sub(t, nc, "ec2.ModifyNetworkInterfaceAttribute", func(m *nats.Msg) { dispatch(m, dl.VPC.ModifyNetworkInterfaceAttribute) })
sub(t, nc, "ec2.CreateSecurityGroup", func(m *nats.Msg) { dispatch(m, dl.VPC.CreateSecurityGroup) })
sub(t, nc, "ec2.DeleteSecurityGroup", func(m *nats.Msg) { dispatch(m, dl.VPC.DeleteSecurityGroup) })
sub(t, nc, "ec2.DescribeSecurityGroups", func(m *nats.Msg) { dispatch(m, dl.VPC.DescribeSecurityGroups) })
sub(t, nc, "ec2.DescribeSecurityGroupRules", func(m *nats.Msg) { dispatch(m, dl.VPC.DescribeSecurityGroupRules) })
sub(t, nc, "ec2.AuthorizeSecurityGroupIngress", func(m *nats.Msg) { dispatch(m, dl.VPC.AuthorizeSecurityGroupIngress) })
sub(t, nc, "ec2.AuthorizeSecurityGroupEgress", func(m *nats.Msg) { dispatch(m, dl.VPC.AuthorizeSecurityGroupEgress) })
sub(t, nc, "ec2.RevokeSecurityGroupIngress", func(m *nats.Msg) { dispatch(m, dl.VPC.RevokeSecurityGroupIngress) })
sub(t, nc, "ec2.RevokeSecurityGroupEgress", func(m *nats.Msg) { dispatch(m, dl.VPC.RevokeSecurityGroupEgress) })
// Internet gateways.
sub(t, nc, "ec2.CreateInternetGateway", func(m *nats.Msg) { dispatch(m, dl.IGW.CreateInternetGateway) })
sub(t, nc, "ec2.DeleteInternetGateway", func(m *nats.Msg) { dispatch(m, dl.IGW.DeleteInternetGateway) })
sub(t, nc, "ec2.DescribeInternetGateways", func(m *nats.Msg) { dispatch(m, dl.IGW.DescribeInternetGateways) })
sub(t, nc, "ec2.AttachInternetGateway", func(m *nats.Msg) { dispatch(m, dl.IGW.AttachInternetGateway) })
sub(t, nc, "ec2.DetachInternetGateway", func(m *nats.Msg) { dispatch(m, dl.IGW.DetachInternetGateway) })
// Egress-only internet gateways.
sub(t, nc, "ec2.CreateEgressOnlyInternetGateway", func(m *nats.Msg) { dispatch(m, dl.EIGW.CreateEgressOnlyInternetGateway) })
sub(t, nc, "ec2.DeleteEgressOnlyInternetGateway", func(m *nats.Msg) { dispatch(m, dl.EIGW.DeleteEgressOnlyInternetGateway) })
sub(t, nc, "ec2.DescribeEgressOnlyInternetGateways", func(m *nats.Msg) { dispatch(m, dl.EIGW.DescribeEgressOnlyInternetGateways) })
// Account settings.
sub(t, nc, "ec2.EnableEbsEncryptionByDefault", func(m *nats.Msg) { dispatch(m, dl.AccountSettings.EnableEbsEncryptionByDefault) })
sub(t, nc, "ec2.DisableEbsEncryptionByDefault", func(m *nats.Msg) { dispatch(m, dl.AccountSettings.DisableEbsEncryptionByDefault) })
sub(t, nc, "ec2.GetEbsEncryptionByDefault", func(m *nats.Msg) { dispatch(m, dl.AccountSettings.GetEbsEncryptionByDefault) })
sub(t, nc, "ec2.EnableSerialConsoleAccess", func(m *nats.Msg) { dispatch(m, dl.AccountSettings.EnableSerialConsoleAccess) })
sub(t, nc, "ec2.DisableSerialConsoleAccess", func(m *nats.Msg) { dispatch(m, dl.AccountSettings.DisableSerialConsoleAccess) })
sub(t, nc, "ec2.GetSerialConsoleAccessStatus", func(m *nats.Msg) { dispatch(m, dl.AccountSettings.GetSerialConsoleAccessStatus) })
// Tags dispatch straight to TagsServiceImpl, skipping the live daemon's
// instance-ID routing and tag mirroring, which only instance and volume
// tagging need; both paths write the central tag store DescribeTags reads.
for _, service := range []any{dl.Key, dl.Tags, dl.RouteTable, dl.VPC, dl.IGW, dl.EIGW, dl.AccountSettings} {
subscribeServiceMethods(t, nc, "ec2", service)
}
}
var (
contextType = reflect.TypeFor[context.Context]()
errorType = reflect.TypeFor[error]()
stringType = reflect.TypeFor[string]()
)
// subscribeServiceMethods subscribes each method shaped like a daemon NATS
// handler, (ctx, *Input, accountID[, principalARN]) (*Output, error), to
// "<prefix>.<Method>", so the subjects cannot drift from the service.
func subscribeServiceMethods(t *testing.T, nc *nats.Conn, prefix string, service any) {
t.Helper()
value := reflect.ValueOf(service)
for i := range value.NumMethod() {
method := value.Type().Method(i)
handler := value.Method(i)
withPrincipal, ok := daemonHandlerShape(handler.Type())
if !ok {
continue
}
sub(t, nc, prefix+"."+method.Name, func(msg *nats.Msg) {
dispatchReflected(msg, handler, withPrincipal)
})
}
}
func daemonHandlerShape(fn reflect.Type) (withPrincipal, ok bool) {
if fn.NumOut() != 2 || fn.Out(0).Kind() != reflect.Pointer || fn.Out(1) != errorType {
return false, false
}
switch fn.NumIn() {
case 3:
case 4:
if fn.In(3) != stringType {
return false, false
}
withPrincipal = true
default:
return false, false
}
return withPrincipal, fn.In(0) == contextType && fn.In(1).Kind() == reflect.Pointer && fn.In(2) == stringType
}
func dispatchReflected(msg *nats.Msg, handler reflect.Value, withPrincipal bool) {
ctx, span := utils.StartConsumerSpan(msg)
defer span.End()
ctx = utils.WithIdempotencyKey(ctx, utils.IdempotencyKeyFromMsg(msg))
input := reflect.New(handler.Type().In(1).Elem())
if errResp := utils.UnmarshalJsonPayload(input.Interface(), msg.Data); errResp != nil {
respond(msg, errResp)
return
}
args := []reflect.Value{reflect.ValueOf(ctx), input, reflect.ValueOf(utils.AccountIDFromMsg(msg))}
if withPrincipal {
args = append(args, reflect.ValueOf(utils.PrincipalARNFromMsg(msg)))
}
results := handler.Call(args)
if err, _ := results[1].Interface().(error); err != nil {
utils.MarkSpanError(span, err)
_, message, _ := awserrors.ResolveErrorDetail(err)
respond(msg, utils.GenerateErrorPayloadWithMessage(awserrors.ValidErrorCodeFromError(err), message))
return
}
payload, err := json.Marshal(results[0].Interface())
if err != nil {
respond(msg, utils.GenerateErrorPayload(awserrors.ErrorServerInternal))
return
}
respond(msg, payload)
}
func respond(msg *nats.Msg, payload []byte) {
if err := msg.Respond(payload); err != nil {
slog.Error("service daemon-lite: failed to respond to NATS request", "subject", msg.Subject, "err", err)
}
}
+3 -8
View File
@@ -7,7 +7,6 @@ import (
handlers_ec2_image "github.com/mulgadc/spinifex/spinifex/handlers/ec2/image"
"github.com/mulgadc/spinifex/spinifex/objectstore"
"github.com/nats-io/nats.go"
)
// testImageBucket is the bucket name StartImageDaemonLite's ImageServiceImpl
@@ -18,24 +17,20 @@ const testImageBucket = "integration-test-images"
// StartImageDaemonLite subscribes a real handlers_ec2_image.ImageServiceImpl
// — the same production code a live daemon runs (daemon/daemon_handlers_image.go)
// — to the ec2.DescribeImages/ec2.RegisterImage subjects, backed by a memory
// — to its ec2.* subjects, backed by a memory
// object store rather than a real Predastore. The store itself is returned
// too, so a test can seed fixture data (e.g. snapshot metadata RegisterImage
// requires) directly, without a round-trip through NATS.
//
// Deliberately separate from StartDaemonLite (like StartVolumeDaemonLite):
// only a test that actually exercises image registration/lookup should wire
// it. CreateImage/CopyImage/DeregisterImage/*ImageAttribute are not wired —
// no ported test needs them yet.
// only a test that actually exercises images should wire it.
func StartImageDaemonLite(t *testing.T, gw *Gateway) (*handlers_ec2_image.ImageServiceImpl, objectstore.ObjectStore) {
t.Helper()
store := objectstore.NewMemoryObjectStore()
svc := handlers_ec2_image.NewImageServiceImplWithStore(store, testImageBucket)
nc := gw.NATSConn
sub(t, nc, "ec2.DescribeImages", func(m *nats.Msg) { dispatch(m, svc.DescribeImages) })
sub(t, nc, "ec2.RegisterImage", func(m *nats.Msg) { dispatch(m, svc.RegisterImage) })
subscribeServiceMethods(t, gw.NATSConn, "ec2", svc)
return svc, store
}
@@ -7,7 +7,6 @@ import (
"github.com/mulgadc/spinifex/spinifex/config"
handlers_ec2_launchtemplate "github.com/mulgadc/spinifex/spinifex/handlers/ec2/launchtemplate"
"github.com/nats-io/nats.go"
"github.com/stretchr/testify/require"
)
@@ -31,14 +30,7 @@ func StartLaunchTemplateDaemonLite(t *testing.T, gw *Gateway) *handlers_ec2_laun
svc, err := handlers_ec2_launchtemplate.NewLaunchTemplateServiceImplWithNATS(t.Context(), cfg, gw.NATSConn)
require.NoError(t, err, "construct launch template service")
nc := gw.NATSConn
sub(t, nc, "ec2.CreateLaunchTemplate", func(m *nats.Msg) { dispatch(m, svc.CreateLaunchTemplate) })
sub(t, nc, "ec2.CreateLaunchTemplateVersion", func(m *nats.Msg) { dispatch(m, svc.CreateLaunchTemplateVersion) })
sub(t, nc, "ec2.DeleteLaunchTemplate", func(m *nats.Msg) { dispatch(m, svc.DeleteLaunchTemplate) })
sub(t, nc, "ec2.DeleteLaunchTemplateVersions", func(m *nats.Msg) { dispatch(m, svc.DeleteLaunchTemplateVersions) })
sub(t, nc, "ec2.ModifyLaunchTemplate", func(m *nats.Msg) { dispatch(m, svc.ModifyLaunchTemplate) })
sub(t, nc, "ec2.DescribeLaunchTemplates", func(m *nats.Msg) { dispatch(m, svc.DescribeLaunchTemplates) })
sub(t, nc, "ec2.DescribeLaunchTemplateVersions", func(m *nats.Msg) { dispatch(m, svc.DescribeLaunchTemplateVersions) })
subscribeServiceMethods(t, gw.NATSConn, "ec2", svc)
return svc
}
+5 -1
View File
@@ -55,12 +55,16 @@ func TestMain(m *testing.M) {
// the only point at which its cluster goroutines can be drained.
testpredastore.Stop()
report := suiteConformance.report(policy, mode)
report := suiteConformance.report(policy, mode) + "\n" + suiteRequestConformance.report(policy, mode)
fmt.Fprintln(os.Stderr, report)
if blocking := suiteConformance.blocking(policy, mode); blocking != 0 {
fmt.Fprintf(os.Stderr, "tests/integration: %d blocking AWS model conformance violation(s)\n", blocking)
code = 1
}
if blocking := suiteRequestConformance.blocking(policy, mode); blocking != 0 {
fmt.Fprintf(os.Stderr, "tests/integration: %d blocking AWS request conformance finding(s)\n", blocking)
code = 1
}
if reportPath := os.Getenv("AWS_MODEL_CONFORMANCE_REPORT"); reportPath != "" {
if err := os.MkdirAll(filepath.Dir(reportPath), 0o755); err != nil {
fmt.Fprintln(os.Stderr, "tests/integration: create conformance report directory:", err)
@@ -33,15 +33,8 @@ func StartPlacementGroupDaemonLite(t *testing.T, gw *Gateway) *handlers_ec2_plac
require.NoError(t, err, "construct placement group service")
nc := gw.NATSConn
sub(t, nc, "ec2.CreatePlacementGroup", func(m *nats.Msg) { dispatch(m, svc.CreatePlacementGroup) })
sub(t, nc, "ec2.DeletePlacementGroup", func(m *nats.Msg) { dispatch(m, svc.DeletePlacementGroup) })
sub(t, nc, "ec2.DescribePlacementGroups", func(m *nats.Msg) { dispatch(m, svc.DescribePlacementGroups) })
sub(t, nc, "ec2.ReserveSpreadNodes", func(m *nats.Msg) { dispatch(m, svc.ReserveSpreadNodes) })
sub(t, nc, "ec2.FinalizeSpreadInstances", func(m *nats.Msg) { dispatch(m, svc.FinalizeSpreadInstances) })
sub(t, nc, "ec2.ReleaseSpreadNodes", func(m *nats.Msg) { dispatch(m, svc.ReleaseSpreadNodes) })
subscribeServiceMethods(t, nc, "ec2", svc)
sub(t, nc, "ec2.RemoveInstanceFromPlacementGroup", func(m *nats.Msg) { dispatch(m, svc.RemoveInstance) })
sub(t, nc, "ec2.ReserveClusterNode", func(m *nats.Msg) { dispatch(m, svc.ReserveClusterNode) })
sub(t, nc, "ec2.FinalizeClusterInstances", func(m *nats.Msg) { dispatch(m, svc.FinalizeClusterInstances) })
return svc
}
+181
View File
@@ -0,0 +1,181 @@
//go:build integration
package integration
import (
"cmp"
"fmt"
"maps"
"slices"
"strings"
"sync"
"github.com/mulgadc/spinifex/internal/awsmodel"
)
// requestFinding is one generated request whose outcome disagrees with the
// model: an acceptance request rejected as invalid, a rejection request that
// succeeded, or one refused with an error the operation does not declare.
type requestFinding struct {
service awsmodel.Service
request awsmodel.RequestCase
result awsmodel.RequestResult
verdict awsmodel.Verdict
}
type requestServiceCounts struct {
operations int
requests int
verdicts map[awsmodel.Verdict]int
inconclusive map[string]int
skippedMembers int
unbroken int
// untested maps each operation that produced no request to the reason.
untested map[string]string
}
// requestCollector gathers the verdicts of the generated request sweep.
type requestCollector struct {
mu sync.Mutex
services map[awsmodel.Service]*requestServiceCounts
findings []requestFinding
}
func newRequestCollector() *requestCollector {
return &requestCollector{services: map[awsmodel.Service]*requestServiceCounts{}}
}
var suiteRequestConformance = newRequestCollector()
func (c *requestCollector) serviceLocked(service awsmodel.Service) *requestServiceCounts {
counts := c.services[service]
if counts == nil {
counts = &requestServiceCounts{verdicts: map[awsmodel.Verdict]int{}, inconclusive: map[string]int{}, untested: map[string]string{}}
c.services[service] = counts
}
return counts
}
func (c *requestCollector) recordOperation(service awsmodel.Service, operation string, plan awsmodel.RequestPlan) {
c.mu.Lock()
defer c.mu.Unlock()
counts := c.serviceLocked(service)
counts.operations++
counts.skippedMembers += len(plan.Skipped)
counts.unbroken += len(plan.Unbroken)
if len(plan.Cases) == 0 {
reasons := make([]string, len(plan.Skipped))
for i, skip := range plan.Skipped {
reasons[i] = skip.Path + ": " + skip.Reason
}
counts.untested[operation] = strings.Join(reasons, "; ")
}
}
func (c *requestCollector) record(service awsmodel.Service, request awsmodel.RequestCase, result awsmodel.RequestResult, verdict awsmodel.Verdict) {
c.mu.Lock()
defer c.mu.Unlock()
counts := c.serviceLocked(service)
counts.requests++
counts.verdicts[verdict]++
switch verdict {
case awsmodel.VerdictFinding, awsmodel.VerdictUndeclaredError:
c.findings = append(c.findings, requestFinding{service: service, request: request, result: result, verdict: verdict})
case awsmodel.VerdictInconclusive:
code := result.Code
if code == "" {
code = fmt.Sprintf("status %d", result.Status)
}
counts.inconclusive[code]++
}
}
func (c *requestCollector) blocking(policy conformancePolicy, mode conformanceMode) int {
if mode != conformanceModeFail {
return 0
}
c.mu.Lock()
defer c.mu.Unlock()
blocking := 0
for _, finding := range c.findings {
if policy.isRequestPromoted(finding.service) {
blocking++
}
}
return blocking
}
func (c *requestCollector) report(policy conformancePolicy, mode conformanceMode) string {
c.mu.Lock()
defer c.mu.Unlock()
var report strings.Builder
total := requestServiceCounts{verdicts: map[awsmodel.Verdict]int{}}
untested := 0
for _, counts := range c.services {
total.operations += counts.operations
total.requests += counts.requests
total.skippedMembers += counts.skippedMembers
total.unbroken += counts.unbroken
untested += len(counts.untested)
for verdict, count := range counts.verdicts {
total.verdicts[verdict] += count
}
}
fmt.Fprintf(&report, "AWS request conformance (%s): operations=%d untested_operations=%d requests=%d %s\n",
mode, total.operations, untested, total.requests, verdictSummary(&total))
for _, service := range slices.Sorted(maps.Keys(c.services)) {
counts := c.services[service]
fmt.Fprintf(&report, "REQUESTS %s operations=%d untested_operations=%d requests=%d %s promoted=%t\n",
service, counts.operations, len(counts.untested), counts.requests, verdictSummary(counts), policy.isRequestPromoted(service))
for _, operation := range slices.Sorted(maps.Keys(counts.untested)) {
fmt.Fprintf(&report, "UNTESTED %s %s: %s\n", service, operation, counts.untested[operation])
}
codes := slices.SortedFunc(maps.Keys(counts.inconclusive), func(a, b string) int {
return cmp.Or(cmp.Compare(counts.inconclusive[b], counts.inconclusive[a]), strings.Compare(a, b))
})
if len(codes) > 0 {
parts := make([]string, len(codes))
for i, code := range codes {
parts[i] = fmt.Sprintf("%s=%d", code, counts.inconclusive[code])
}
fmt.Fprintf(&report, "INCONCLUSIVE %s %s\n", service, strings.Join(parts, " "))
}
}
findings := slices.Clone(c.findings)
slices.SortFunc(findings, func(a, b requestFinding) int {
return cmp.Or(cmp.Compare(a.service, b.service), strings.Compare(a.request.Operation, b.request.Operation),
strings.Compare(a.request.Path, b.request.Path), strings.Compare(a.request.Detail, b.request.Detail))
})
for _, finding := range findings {
severity := "WARN"
if mode == conformanceModeFail && policy.isRequestPromoted(finding.service) {
severity = "FAIL"
}
request := finding.request
switch {
case finding.verdict == awsmodel.VerdictUndeclaredError:
fmt.Fprintf(&report, "%s %s %s %s %s (%s) refused with undeclared %s: %s\n",
severity, finding.service, request.Operation, request.Path, request.Constraint, request.Detail, finding.result.Code, finding.result.Message)
case request.Acceptance():
with := "with only required members"
if request.Member != "" {
with = "with " + request.Path + " set"
}
fmt.Fprintf(&report, "%s %s %s model-valid request %s rejected: %s: %s\n",
severity, finding.service, request.Operation, with, finding.result.Code, finding.result.Message)
default:
fmt.Fprintf(&report, "%s %s %s %s %s (%s) accepted\n",
severity, finding.service, request.Operation, request.Path, request.Constraint, request.Detail)
}
}
return strings.TrimSuffix(report.String(), "\n")
}
func verdictSummary(counts *requestServiceCounts) string {
return fmt.Sprintf("pass=%d findings=%d undeclared_errors=%d inconclusive=%d skipped_members=%d unbroken_constraints=%d",
counts.verdicts[awsmodel.VerdictPass], counts.verdicts[awsmodel.VerdictFinding], counts.verdicts[awsmodel.VerdictUndeclaredError],
counts.verdicts[awsmodel.VerdictInconclusive], counts.skippedMembers, counts.unbroken)
}
@@ -0,0 +1,38 @@
//go:build integration
package integration
import (
"testing"
"github.com/mulgadc/spinifex/internal/awsmodel"
"github.com/stretchr/testify/require"
)
func TestRequestConformanceBlocksOnlyRequestPromotedServices(t *testing.T) {
collector := newRequestCollector()
accepted := awsmodel.RequestCase{Operation: "ListRoles", Constraint: awsmodel.ConstraintRange, Path: "$.MaxItems", Detail: "above max 1000"}
collector.record(awsmodel.IAM, accepted, awsmodel.RequestResult{Status: 200}, awsmodel.VerdictFinding)
responseOnly := conformancePolicy{promoted: map[awsmodel.Service]bool{awsmodel.IAM: true}}
require.Zero(t, collector.blocking(responseOnly, conformanceModeFail), "response promotion does not promote requests")
requestPromoted := conformancePolicy{requestPromoted: map[awsmodel.Service]bool{awsmodel.IAM: true}}
require.Equal(t, 1, collector.blocking(requestPromoted, conformanceModeFail))
require.Zero(t, collector.blocking(requestPromoted, conformanceModeWarn))
require.Contains(t, collector.report(requestPromoted, conformanceModeFail), "FAIL iam ListRoles $.MaxItems range (above max 1000) accepted")
}
func TestRequestConformanceReportsUndeclaredErrorsAndUntestedOperations(t *testing.T) {
collector := newRequestCollector()
refused := awsmodel.RequestCase{Operation: "TagResource", Constraint: awsmodel.ConstraintLength, Path: "$.tags.{key}", Detail: "below min 1"}
collector.record(awsmodel.ECR, refused, awsmodel.RequestResult{Status: 400, Code: "InvalidParameterValueException", Message: "bad key"}, awsmodel.VerdictUndeclaredError)
collector.recordOperation(awsmodel.ACM, "RequestCertificate", awsmodel.RequestPlan{Skipped: []awsmodel.Skip{{Path: "$.DomainName", Reason: "pattern is not supported by Go regexp"}}})
policy := conformancePolicy{requestPromoted: map[awsmodel.Service]bool{awsmodel.ECR: true}}
require.Equal(t, 1, collector.blocking(policy, conformanceModeFail), "an undeclared error blocks like any finding")
report := collector.report(policy, conformanceModeFail)
require.Contains(t, report, "FAIL ecr TagResource $.tags.{key} length (below min 1) refused with undeclared InvalidParameterValueException: bad key")
require.Contains(t, report, "UNTESTED acm RequestCertificate: $.DomainName: pattern is not supported by Go regexp")
require.Contains(t, report, "operations=1 untested_operations=1 requests=0")
}
@@ -0,0 +1,138 @@
//go:build integration
package integration
import (
"bytes"
"errors"
"io"
"net"
"net/http"
"testing"
"time"
awscreds "github.com/aws/aws-sdk-go/aws/credentials"
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/mulgadc/spinifex/internal/awsmodel"
"github.com/mulgadc/spinifex/spinifex/gateway"
"github.com/stretchr/testify/require"
)
// requestTimeout bounds each generated request: a subject no daemon-lite
// answers can hold a request for the gateway's full NATS timeout.
const requestTimeout = 5 * time.Second
// slowRequest is the duration past which a request is logged, since a slow
// one usually waits on a NATS subject no daemon-lite answers.
const slowRequest = 500 * time.Millisecond
// TestRequestConformance sends every generated acceptance and rejection
// request for each implemented operation through the real gateway, with every
// daemon-lite wired, and records each verdict for the suite report.
func TestRequestConformance(t *testing.T) {
inventory := gateway.AWSOperationInventory()
for _, service := range awsmodel.Services() {
// Predastore serves S3, not the gateway.
if service == awsmodel.S3 {
continue
}
t.Run(string(service), func(t *testing.T) {
t.Parallel()
dispatch := inventory[string(service)]
coverage, err := awsmodel.CompareOperations(service, awsmodel.DispatchInventory{
Registered: dispatch.Registered,
Stubbed: dispatch.Stubbed,
Unsupported: dispatch.Unsupported,
})
require.NoError(t, err)
gw := startGateway(t, nil)
startRequestConformanceBackends(t, gw)
sender, err := newRequestSender(gw, service)
require.NoError(t, err)
for _, operation := range coverage.Implemented {
plan, err := awsmodel.GenerateRequests(service, operation, awsmodel.RequestOptions{AccountID: gw.AccountID, Region: testRegion})
require.NoError(t, err, operation)
suiteRequestConformance.recordOperation(service, operation, plan)
judge := plan.NewJudge()
for _, request := range plan.Cases {
started := time.Now()
result, err := sender.send(request)
require.NoError(t, err, "%s %s", operation, request.Path)
if elapsed := time.Since(started); elapsed > slowRequest {
t.Logf("slow request: %s %s %s took %dms (%d %s)", operation, request.Constraint, request.Path, elapsed.Milliseconds(), result.Status, result.Code)
}
suiteRequestConformance.record(service, request, result, judge.Judge(request, result))
}
}
})
}
}
// startRequestConformanceBackends wires every daemon-lite, so validation that
// lives behind NATS runs as it would on a live daemon.
func startRequestConformanceBackends(t *testing.T, gw *Gateway) {
t.Helper()
StartDaemonLite(t, gw)
StartImageDaemonLite(t, gw)
StartLaunchTemplateDaemonLite(t, gw)
StartPlacementGroupDaemonLite(t, gw)
StartSpotDaemonLite(t, gw)
StartECRDaemonLite(t, gw)
StartServiceDaemonLite(t, gw)
}
type requestSender struct {
endpoint string
service awsmodel.Service
signing string
signer *v4.Signer
client *http.Client
}
func newRequestSender(gw *Gateway, service awsmodel.Service) (*requestSender, error) {
model, err := awsmodel.Load(service)
if err != nil {
return nil, err
}
return &requestSender{
endpoint: gw.Server.URL,
service: service,
signing: model.Metadata().SigningName,
signer: v4.NewSigner(awscreds.NewStaticCredentials(testAccessKeyID, testSecretAccessKey, "")),
client: &http.Client{Timeout: requestTimeout},
}, nil
}
// send encodes, signs and sends one request. A request that times out is
// recorded with status 0, which judges as inconclusive; any other transport
// failure, such as a gateway panic closing the connection, is an error.
func (s *requestSender) send(request awsmodel.RequestCase) (awsmodel.RequestResult, error) {
encoded, err := awsmodel.EncodeRequest(s.service, request.Operation, request.Input)
if err != nil {
return awsmodel.RequestResult{}, err
}
httpRequest, err := http.NewRequest(encoded.Method, encoded.URL(s.endpoint), bytes.NewReader(encoded.Body))
if err != nil {
return awsmodel.RequestResult{}, err
}
for name, values := range encoded.Header {
httpRequest.Header[name] = values
}
if _, err := s.signer.Sign(httpRequest, bytes.NewReader(encoded.Body), s.signing, testRegion, time.Now()); err != nil {
return awsmodel.RequestResult{}, err
}
response, err := s.client.Do(httpRequest)
if netErr, ok := errors.AsType[net.Error](err); ok && netErr.Timeout() {
return awsmodel.RequestResult{Code: "Timeout"}, nil
}
if err != nil {
return awsmodel.RequestResult{}, err
}
defer func() { _ = response.Body.Close() }()
body, err := io.ReadAll(response.Body)
if err != nil {
return awsmodel.RequestResult{}, err
}
return awsmodel.DecodeRequestResult(s.service, response.StatusCode, response.Header, body), nil
}
+46
View File
@@ -0,0 +1,46 @@
//go:build integration
package integration
import (
"testing"
"github.com/mulgadc/spinifex/spinifex/config"
handlers_acm "github.com/mulgadc/spinifex/spinifex/handlers/acm"
handlers_ecs "github.com/mulgadc/spinifex/spinifex/handlers/ecs"
handlers_eks "github.com/mulgadc/spinifex/spinifex/handlers/eks"
handlers_elbv2 "github.com/mulgadc/spinifex/spinifex/handlers/elbv2"
handlers_iam "github.com/mulgadc/spinifex/spinifex/handlers/iam"
handlers_rds "github.com/mulgadc/spinifex/spinifex/handlers/rds"
"github.com/stretchr/testify/require"
)
// StartServiceDaemonLite subscribes the real ACM, ECS, EKS, ELBv2 and RDS
// services to their "<service>.<Method>" subjects. ECS and RDS run without
// their provisioning dependencies, so checks that need them do not run.
func StartServiceDaemonLite(t *testing.T, gw *Gateway) {
t.Helper()
nc := gw.NATSConn
cfg := &config.Config{AZ: testAZ, Region: testRegion}
masterKey, err := handlers_iam.GenerateMasterKey()
require.NoError(t, err)
acm, err := handlers_acm.NewACMServiceImplWithNATS(t.Context(), cfg, nc, masterKey)
require.NoError(t, err, "construct ACM service")
subscribeServiceMethods(t, nc, "acm", acm)
elbv2, err := handlers_elbv2.NewELBv2ServiceImplWithNATS(cfg, nc, masterKey)
require.NoError(t, err, "construct ELBv2 service")
t.Cleanup(elbv2.Close)
subscribeServiceMethods(t, nc, "elbv2", elbv2)
eks, err := handlers_eks.NewEKSServiceImpl(handlers_eks.EKSServiceDeps{
Config: cfg, NATSConn: nc, MasterKey: masterKey, Region: testRegion, HolderID: "integration-test-node", ClusterSize: 1,
})
require.NoError(t, err, "construct EKS service")
t.Cleanup(eks.Shutdown)
subscribeServiceMethods(t, nc, "eks", eks)
subscribeServiceMethods(t, nc, "ecs", handlers_ecs.NewService(nc, testRegion, ""))
subscribeServiceMethods(t, nc, "rds", handlers_rds.NewService(nc, testRegion))
}
+1 -5
View File
@@ -6,7 +6,6 @@ import (
"testing"
handlers_ec2_spotinstance "github.com/mulgadc/spinifex/spinifex/handlers/ec2/spotinstance"
"github.com/nats-io/nats.go"
"github.com/stretchr/testify/require"
)
@@ -29,10 +28,7 @@ func StartSpotDaemonLite(t *testing.T, gw *Gateway) *handlers_ec2_spotinstance.S
svc, err := handlers_ec2_spotinstance.NewSpotInstanceServiceImplWithNATS(t.Context(), nil, gw.NATSConn)
require.NoError(t, err, "construct spot instance service")
nc := gw.NATSConn
sub(t, nc, "ec2.PutSpotInstanceRequests", func(m *nats.Msg) { dispatch(m, svc.PutSpotInstanceRequests) })
sub(t, nc, "ec2.DescribeSpotInstanceRequests", func(m *nats.Msg) { dispatch(m, svc.DescribeSpotInstanceRequests) })
sub(t, nc, "ec2.CancelSpotInstanceRequests", func(m *nats.Msg) { dispatch(m, svc.CancelSpotInstanceRequests) })
subscribeServiceMethods(t, gw.NATSConn, "ec2", svc)
return svc
}
+4 -8
View File
@@ -13,7 +13,6 @@ import (
"github.com/mulgadc/spinifex/spinifex/objectstore"
"github.com/mulgadc/spinifex/spinifex/services/viperblockd"
testpredastore "github.com/mulgadc/spinifex/tests/fixtures/predastore"
"github.com/nats-io/nats.go"
"github.com/stretchr/testify/require"
)
@@ -35,7 +34,7 @@ const providerRequestTimeout = 60 * time.Second
// StartVolumeDaemonLite subscribes a real handlers_ec2_volume.VolumeServiceImpl
// — the same production code a live daemon runs (daemon/daemon_handlers_volume.go)
// — to the ec2.CreateVolume/DeleteVolume/DescribeVolumes subjects, backed by a
// — to its ec2.* subjects, backed by a
// real predastore daemon (testpredastore.Start) rather than the memory-backed
// stores DaemonLite's other resources use.
//
@@ -47,9 +46,8 @@ const providerRequestTimeout = 60 * time.Second
// Only a test that actually exercises volume storage should pay the shared
// predastore daemon's startup cost.
//
// Must be called before a test issues ec2.CreateVolume/DeleteVolume/
// DescribeVolumes; like StartECRDaemonLite it wires its own subjects
// independent of StartDaemonLite.
// Must be called before a test issues a volume request; like
// StartECRDaemonLite it wires its own subjects independent of StartDaemonLite.
func StartVolumeDaemonLite(t *testing.T, gw *Gateway) *handlers_ec2_volume.VolumeServiceImpl {
t.Helper()
@@ -89,9 +87,7 @@ func StartVolumeDaemonLite(t *testing.T, gw *Gateway) *handlers_ec2_volume.Volum
}, nc), "register provider subjects")
svc.SetEBSProvider(ebsprovider.NewNATSProvider(nc, providerRequestTimeout))
sub(t, nc, "ec2.CreateVolume", func(m *nats.Msg) { dispatch(m, svc.CreateVolume) })
sub(t, nc, "ec2.DeleteVolume", func(m *nats.Msg) { dispatch(m, svc.DeleteVolume) })
sub(t, nc, "ec2.DescribeVolumes", func(m *nats.Msg) { dispatch(m, svc.DescribeVolumes) })
subscribeServiceMethods(t, nc, "ec2", svc)
return svc
}