|
5 | 5 | "testing" |
6 | 6 | "time" |
7 | 7 |
|
| 8 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
8 | 10 | "github.com/go-kit/log" |
9 | 11 | "github.com/gogo/protobuf/proto" |
10 | 12 | "github.com/pkg/errors" |
@@ -373,6 +375,57 @@ func Test_UpdateStaleData(t *testing.T) { |
373 | 375 |
|
374 | 376 | } |
375 | 377 |
|
| 378 | +func Test_GetInstance(t *testing.T) { |
| 379 | + equals := func(value string) types.Condition { |
| 380 | + return types.Condition{ |
| 381 | + ComparisonOperator: types.ComparisonOperatorEq, |
| 382 | + AttributeValueList: []types.AttributeValue{&types.AttributeValueMemberS{Value: value}}, |
| 383 | + } |
| 384 | + } |
| 385 | + var conditions map[string]types.Condition |
| 386 | + ddbClientMock := &mockDynamodb{ |
| 387 | + query: func(input *dynamodb.QueryInput) (*dynamodb.QueryOutput, error) { |
| 388 | + conditions = input.KeyConditions |
| 389 | + return &dynamodb.QueryOutput{}, nil |
| 390 | + }, |
| 391 | + } |
| 392 | + // The instrumentation layer and the real Query, down to the DynamoDB API. |
| 393 | + ddb := dynamodbInstrumentation{ |
| 394 | + kv: *newDynamodbClientMock("TEST", ddbClientMock, 0), |
| 395 | + ddbMetrics: newDynamoDbMetrics(prometheus.NewPedanticRegistry()), |
| 396 | + } |
| 397 | + codecMock := &CodecMock{} |
| 398 | + descMock := &DescMock{} |
| 399 | + c := NewClientMock(ddb, codecMock, TestLogger{}, prometheus.NewPedanticRegistry(), defaultPullTime, defaultBackoff) |
| 400 | + codecMock.On("DecodeMultiKey").Return(descMock, nil) |
| 401 | + |
| 402 | + value, err := c.GetInstance(context.TODO(), key, "ing-a") |
| 403 | + |
| 404 | + require.NoError(t, err) |
| 405 | + require.EqualValues(t, descMock, value) |
| 406 | + require.Equal(t, map[string]types.Condition{primaryKey: equals(key), sortKey: equals("ing-a")}, conditions) |
| 407 | + |
| 408 | + // The read saw one row, so the client keeps neither data nor a time for the whole key. |
| 409 | + require.Empty(t, c.staleData) |
| 410 | + require.True(t, c.LastUpdateTime(key).IsZero()) |
| 411 | + |
| 412 | + // A read of the whole key still has no condition on the sort key. |
| 413 | + _, err = c.Get(context.TODO(), key) |
| 414 | + require.NoError(t, err) |
| 415 | + require.Equal(t, map[string]types.Condition{primaryKey: equals(key)}, conditions) |
| 416 | +} |
| 417 | + |
| 418 | +func Test_GetInstance_EmptyInstanceID(t *testing.T) { |
| 419 | + ddbMock := NewDynamodbClientMock() |
| 420 | + c := NewClientMock(ddbMock, &CodecMock{}, TestLogger{}, prometheus.NewPedanticRegistry(), defaultPullTime, defaultBackoff) |
| 421 | + |
| 422 | + // An empty sort key would read every row of the key. |
| 423 | + _, err := c.GetInstance(context.TODO(), key, "") |
| 424 | + |
| 425 | + require.Error(t, err) |
| 426 | + ddbMock.AssertNotCalled(t, "Query") |
| 427 | +} |
| 428 | + |
376 | 429 | func Test_DynamodbKVWithTimeout(t *testing.T) { |
377 | 430 | ddbMock := NewDynamodbClientMock() |
378 | 431 | // Backend has delay of 5s while the client timeout is 1s. |
|
0 commit comments