|
| 1 | +package adapter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 8 | + "github.com/aws/aws-sdk-go-v2/config" |
| 9 | + "github.com/aws/aws-sdk-go-v2/credentials" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 11 | + ddbTypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestDynamoDB_AdminListTables_Empty exercises the SigV4-bypass admin |
| 16 | +// entrypoint on a server that has no Dynamo tables. The expected shape |
| 17 | +// is an empty (non-nil) slice so the admin JSON response stays a valid |
| 18 | +// array rather than `null`, matching the design doc 4.3 contract. |
| 19 | +func TestDynamoDB_AdminListTables_Empty(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + nodes, _, _ := createNode(t, 1) |
| 22 | + defer shutdown(nodes) |
| 23 | + |
| 24 | + got, err := nodes[0].dynamoServer.AdminListTables(context.Background()) |
| 25 | + require.NoError(t, err) |
| 26 | + require.Empty(t, got) |
| 27 | +} |
| 28 | + |
| 29 | +// TestDynamoDB_AdminListTables_Sorted verifies that the admin entrypoint |
| 30 | +// returns table names in lexicographic order, matching the listTables |
| 31 | +// HTTP handler so the two admin views (SigV4 and bypass) cannot drift. |
| 32 | +func TestDynamoDB_AdminListTables_Sorted(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + nodes, _, _ := createNode(t, 1) |
| 35 | + defer shutdown(nodes) |
| 36 | + |
| 37 | + client := newDynamoClient(t, nodes[0].dynamoAddress) |
| 38 | + ctx := context.Background() |
| 39 | + |
| 40 | + for _, name := range []string{"zeta", "alpha", "mu"} { |
| 41 | + _, err := client.CreateTable(ctx, &dynamodb.CreateTableInput{ |
| 42 | + TableName: aws.String(name), |
| 43 | + BillingMode: ddbTypes.BillingModePayPerRequest, |
| 44 | + AttributeDefinitions: []ddbTypes.AttributeDefinition{ |
| 45 | + {AttributeName: aws.String("pk"), AttributeType: ddbTypes.ScalarAttributeTypeS}, |
| 46 | + }, |
| 47 | + KeySchema: []ddbTypes.KeySchemaElement{ |
| 48 | + {AttributeName: aws.String("pk"), KeyType: ddbTypes.KeyTypeHash}, |
| 49 | + }, |
| 50 | + }) |
| 51 | + require.NoError(t, err) |
| 52 | + } |
| 53 | + |
| 54 | + got, err := nodes[0].dynamoServer.AdminListTables(ctx) |
| 55 | + require.NoError(t, err) |
| 56 | + require.Equal(t, []string{"alpha", "mu", "zeta"}, got) |
| 57 | +} |
| 58 | + |
| 59 | +// TestDynamoDB_AdminDescribeTable_Missing checks the (nil, false, nil) |
| 60 | +// "not found" contract — admin callers must be able to tell a missing |
| 61 | +// table apart from a storage error without sniffing sentinels. |
| 62 | +func TestDynamoDB_AdminDescribeTable_Missing(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + nodes, _, _ := createNode(t, 1) |
| 65 | + defer shutdown(nodes) |
| 66 | + |
| 67 | + summary, exists, err := nodes[0].dynamoServer.AdminDescribeTable(context.Background(), "absent") |
| 68 | + require.NoError(t, err) |
| 69 | + require.False(t, exists) |
| 70 | + require.Nil(t, summary) |
| 71 | +} |
| 72 | + |
| 73 | +// TestDynamoDB_AdminDescribeTable_Composite covers the simple-key happy |
| 74 | +// path: a table with hash + range key and no GSIs. The admin summary |
| 75 | +// must mirror the schema's primary key fields exactly. |
| 76 | +func TestDynamoDB_AdminDescribeTable_Composite(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + nodes, _, _ := createNode(t, 1) |
| 79 | + defer shutdown(nodes) |
| 80 | + |
| 81 | + client := newDynamoClient(t, nodes[0].dynamoAddress) |
| 82 | + ctx := context.Background() |
| 83 | + |
| 84 | + _, err := client.CreateTable(ctx, &dynamodb.CreateTableInput{ |
| 85 | + TableName: aws.String("orders"), |
| 86 | + BillingMode: ddbTypes.BillingModePayPerRequest, |
| 87 | + AttributeDefinitions: []ddbTypes.AttributeDefinition{ |
| 88 | + {AttributeName: aws.String("customer"), AttributeType: ddbTypes.ScalarAttributeTypeS}, |
| 89 | + {AttributeName: aws.String("orderID"), AttributeType: ddbTypes.ScalarAttributeTypeS}, |
| 90 | + }, |
| 91 | + KeySchema: []ddbTypes.KeySchemaElement{ |
| 92 | + {AttributeName: aws.String("customer"), KeyType: ddbTypes.KeyTypeHash}, |
| 93 | + {AttributeName: aws.String("orderID"), KeyType: ddbTypes.KeyTypeRange}, |
| 94 | + }, |
| 95 | + }) |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + summary, exists, err := nodes[0].dynamoServer.AdminDescribeTable(ctx, "orders") |
| 99 | + require.NoError(t, err) |
| 100 | + require.True(t, exists) |
| 101 | + require.NotNil(t, summary) |
| 102 | + require.Equal(t, "orders", summary.Name) |
| 103 | + require.Equal(t, "customer", summary.PartitionKey) |
| 104 | + require.Equal(t, "orderID", summary.SortKey) |
| 105 | + require.NotZero(t, summary.Generation) |
| 106 | + require.Empty(t, summary.GlobalSecondaryIndexes) |
| 107 | +} |
| 108 | + |
| 109 | +// TestDynamoDB_AdminDescribeTable_GSI_SortedDeterministic exercises the |
| 110 | +// GSI projection path. Two indexes are added in deliberately reversed |
| 111 | +// alphabetical order to confirm summaryFromSchema's Sort.Strings call — |
| 112 | +// without it, map iteration order would produce a flaky output. |
| 113 | +func TestDynamoDB_AdminDescribeTable_GSI_SortedDeterministic(t *testing.T) { |
| 114 | + t.Parallel() |
| 115 | + nodes, _, _ := createNode(t, 1) |
| 116 | + defer shutdown(nodes) |
| 117 | + |
| 118 | + client := newDynamoClient(t, nodes[0].dynamoAddress) |
| 119 | + ctx := context.Background() |
| 120 | + |
| 121 | + _, err := client.CreateTable(ctx, &dynamodb.CreateTableInput{ |
| 122 | + TableName: aws.String("threads"), |
| 123 | + BillingMode: ddbTypes.BillingModePayPerRequest, |
| 124 | + AttributeDefinitions: []ddbTypes.AttributeDefinition{ |
| 125 | + {AttributeName: aws.String("threadId"), AttributeType: ddbTypes.ScalarAttributeTypeS}, |
| 126 | + {AttributeName: aws.String("status"), AttributeType: ddbTypes.ScalarAttributeTypeS}, |
| 127 | + {AttributeName: aws.String("owner"), AttributeType: ddbTypes.ScalarAttributeTypeS}, |
| 128 | + {AttributeName: aws.String("createdAt"), AttributeType: ddbTypes.ScalarAttributeTypeS}, |
| 129 | + }, |
| 130 | + KeySchema: []ddbTypes.KeySchemaElement{ |
| 131 | + {AttributeName: aws.String("threadId"), KeyType: ddbTypes.KeyTypeHash}, |
| 132 | + }, |
| 133 | + GlobalSecondaryIndexes: []ddbTypes.GlobalSecondaryIndex{ |
| 134 | + { |
| 135 | + IndexName: aws.String("zStatusIndex"), |
| 136 | + KeySchema: []ddbTypes.KeySchemaElement{ |
| 137 | + {AttributeName: aws.String("status"), KeyType: ddbTypes.KeyTypeHash}, |
| 138 | + {AttributeName: aws.String("createdAt"), KeyType: ddbTypes.KeyTypeRange}, |
| 139 | + }, |
| 140 | + Projection: &ddbTypes.Projection{ProjectionType: ddbTypes.ProjectionTypeAll}, |
| 141 | + }, |
| 142 | + { |
| 143 | + IndexName: aws.String("aOwnerIndex"), |
| 144 | + KeySchema: []ddbTypes.KeySchemaElement{ |
| 145 | + {AttributeName: aws.String("owner"), KeyType: ddbTypes.KeyTypeHash}, |
| 146 | + }, |
| 147 | + Projection: &ddbTypes.Projection{ProjectionType: ddbTypes.ProjectionTypeKeysOnly}, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }) |
| 151 | + require.NoError(t, err) |
| 152 | + |
| 153 | + summary, exists, err := nodes[0].dynamoServer.AdminDescribeTable(ctx, "threads") |
| 154 | + require.NoError(t, err) |
| 155 | + require.True(t, exists) |
| 156 | + require.NotNil(t, summary) |
| 157 | + require.Equal(t, "threadId", summary.PartitionKey) |
| 158 | + require.Empty(t, summary.SortKey) |
| 159 | + |
| 160 | + require.Len(t, summary.GlobalSecondaryIndexes, 2) |
| 161 | + // Names sorted lexicographically: "aOwnerIndex" < "zStatusIndex". |
| 162 | + require.Equal(t, "aOwnerIndex", summary.GlobalSecondaryIndexes[0].Name) |
| 163 | + require.Equal(t, "owner", summary.GlobalSecondaryIndexes[0].PartitionKey) |
| 164 | + require.Empty(t, summary.GlobalSecondaryIndexes[0].SortKey) |
| 165 | + require.Equal(t, string(ddbTypes.ProjectionTypeKeysOnly), summary.GlobalSecondaryIndexes[0].ProjectionType) |
| 166 | + |
| 167 | + require.Equal(t, "zStatusIndex", summary.GlobalSecondaryIndexes[1].Name) |
| 168 | + require.Equal(t, "status", summary.GlobalSecondaryIndexes[1].PartitionKey) |
| 169 | + require.Equal(t, "createdAt", summary.GlobalSecondaryIndexes[1].SortKey) |
| 170 | + require.Equal(t, string(ddbTypes.ProjectionTypeAll), summary.GlobalSecondaryIndexes[1].ProjectionType) |
| 171 | +} |
| 172 | + |
| 173 | +func newDynamoClient(t *testing.T, address string) *dynamodb.Client { |
| 174 | + t.Helper() |
| 175 | + cfg, err := config.LoadDefaultConfig(context.Background(), |
| 176 | + config.WithRegion("us-west-2"), |
| 177 | + config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("dummy", "dummy", "")), |
| 178 | + ) |
| 179 | + require.NoError(t, err) |
| 180 | + return dynamodb.NewFromConfig(cfg, func(o *dynamodb.Options) { |
| 181 | + o.BaseEndpoint = aws.String("http://" + address) |
| 182 | + }) |
| 183 | +} |
0 commit comments