Skip to content

Commit

Permalink
aws/endpoints: Update unit tests use static data instead of live model (
Browse files Browse the repository at this point in the history
#4389)

Updates the SDK's endpoints model unit tests to ensure that unit tests
are performed on static data instead of the live model that can be
updated at any time. This change was done to ensure that upstream
sources of endpoint models making semantic changes do not cause unit
tests to break.
  • Loading branch information
jasdel committed May 5, 2022
1 parent a200c59 commit c1280f8
Show file tree
Hide file tree
Showing 6 changed files with 18,901 additions and 323 deletions.
6 changes: 6 additions & 0 deletions aws/endpoints/endpoints_test.go
Expand Up @@ -5,6 +5,12 @@ package endpoints

import "testing"

// ***************************************************************************
// All endpoint metadata is sourced from the testdata/endpoints.json file at
// test startup. Not the live endpoints model file. Update the testdata file
// for the tests to use the latest live model.
// ***************************************************************************

func TestEnumDefaultPartitions(t *testing.T) {
resolver := DefaultResolver()
enum, ok := resolver.(EnumPartitions)
Expand Down
10 changes: 9 additions & 1 deletion aws/endpoints/example_test.go
Expand Up @@ -13,6 +13,12 @@ import (
"github.com/aws/aws-sdk-go/service/sqs"
)

// ***************************************************************************
// All endpoint metadata is sourced from the testdata/endpoints.json file at
// test startup. Not the live endpoints model file. Update the testdata file
// for the tests to use the latest live model.
// ***************************************************************************

func ExampleEnumPartitions() {
resolver := endpoints.DefaultResolver()
partitions := resolver.(endpoints.EnumPartitions).Partitions()
Expand All @@ -31,7 +37,9 @@ func ExampleEnumPartitions() {
}

func ExampleResolverFunc() {
myCustomResolver := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
myCustomResolver := func(service, region string, optFns ...func(*endpoints.Options)) (
endpoints.ResolvedEndpoint, error,
) {
if service == endpoints.S3ServiceID {
return endpoints.ResolvedEndpoint{
URL: "s3.custom.endpoint.com",
Expand Down
49 changes: 49 additions & 0 deletions aws/endpoints/main_test.go
@@ -0,0 +1,49 @@
package endpoints

import (
"fmt"
"os"
"path/filepath"
"testing"
)

// Update all endpoints partition variables data to be the static testdata
// model instead of the dynamic live model. This ensures that endpoint tests
// are tested against static data, and will not break when the live
// endpoints.json model is updated.

func TestMain(m *testing.M) {
testdataFilename := filepath.Join("testdata", "endpoints.json")

testdataFile, err := os.Open(testdataFilename)
if err != nil {
panic(fmt.Sprintf("failed to open test endpoints model, %v", err))
}

resolver, err := DecodeModel(testdataFile)
if err != nil {
panic(fmt.Sprintf("failed to decode test endpoints model, %v", err))
}

partitions, ok := resolver.(partitions)
if !ok {
panic(fmt.Sprintf("expect %T resolver, got %T", partitions, resolver))
}

for _, p := range partitions {
switch p.ID {
case "aws":
awsPartition = p
case "aws-cn":
awscnPartition = p
case "aws-us-gov":
awsusgovPartition = p
case "aws-iso":
awsisoPartition = p
case "aws-iso-b":
awsisobPartition = p
default:
panic("unknown endpoints partition " + p.ID)
}
}
}

0 comments on commit c1280f8

Please sign in to comment.