Skip to content

Commit a02aed5

Browse files
committed
chore: Use aws client v2 in dynamodb tests
Signed-off-by: Javier Aliaga <[email protected]>
1 parent c30af7d commit a02aed5

File tree

4 files changed

+422
-541
lines changed

4 files changed

+422
-541
lines changed

common/aws/client.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package aws
2+
3+
/*
4+
Copyright 2025 The Dapr Authors
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
import (
19+
"context"
20+
21+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
22+
)
23+
24+
type DynamoDBClient interface {
25+
GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
26+
PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
27+
DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
28+
TransactWriteItems(ctx context.Context, params *dynamodb.TransactWriteItemsInput, optFns ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error)
29+
}

common/aws/client_mock.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package aws
2+
3+
/*
4+
Copyright 2025 The Dapr Authors
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
import (
19+
"context"
20+
21+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
22+
)
23+
24+
type MockDynamoDBClient struct {
25+
GetItemFn func(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
26+
PutItemFn func(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
27+
DeleteItemFn func(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
28+
TransactWriteItemsFn func(ctx context.Context, params *dynamodb.TransactWriteItemsInput, optFns ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error)
29+
}
30+
31+
func (m MockDynamoDBClient) GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error) {
32+
return m.GetItemFn(ctx, params, optFns...)
33+
}
34+
35+
func (m MockDynamoDBClient) PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error) {
36+
return m.PutItemFn(ctx, params, optFns...)
37+
}
38+
39+
func (m MockDynamoDBClient) DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error) {
40+
return m.DeleteItemFn(ctx, params, optFns...)
41+
}
42+
43+
func (m MockDynamoDBClient) TransactWriteItems(ctx context.Context, params *dynamodb.TransactWriteItemsInput, optFns ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error) {
44+
return m.TransactWriteItemsFn(ctx, params, optFns...)
45+
}

state/aws/dynamodb/dynamodb.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type StateStore struct {
4949
ttlAttributeName string
5050
partitionKey string
5151

52-
dynamodbClient *dynamodb.Client
52+
dynamodbClient awsCommon.DynamoDBClient
5353
}
5454

5555
type dynamoDBMetadata struct {
@@ -88,8 +88,12 @@ func NewDynamoDBStateStore(logger logger.Logger) state.Store {
8888
return s
8989
}
9090

91-
// Init does metadata and connection parsing.
9291
func (d *StateStore) Init(ctx context.Context, metadata state.Metadata) error {
92+
return d.InitWithOptions(ctx, metadata)
93+
}
94+
95+
// Init does metadata and connection parsing.
96+
func (d *StateStore) InitWithOptions(ctx context.Context, metadata state.Metadata, opts ...awsCommon.ConfigOption) error {
9397
meta, err := d.getDynamoDBMetadata(metadata)
9498
if err != nil {
9599
return err
@@ -107,13 +111,14 @@ func (d *StateStore) Init(ctx context.Context, metadata state.Metadata) error {
107111
SessionToken: meta.SessionToken,
108112
}
109113

110-
awsConfig, err := awsCommon.NewConfig(ctx, configOpts)
111-
if err != nil {
112-
return err
113-
}
114-
115-
d.dynamodbClient = dynamodb.NewFromConfig(awsConfig)
114+
if d.dynamodbClient == nil {
115+
awsConfig, err := awsCommon.NewConfig(ctx, configOpts, opts...)
116+
if err != nil {
117+
return err
118+
}
116119

120+
d.dynamodbClient = dynamodb.NewFromConfig(awsConfig)
121+
}
117122
d.table = meta.Table
118123
d.ttlAttributeName = meta.TTLAttributeName
119124
d.partitionKey = meta.PartitionKey

0 commit comments

Comments
 (0)