adds dynamodb helper

Signed-off-by: Roberto Rojas <robertojrojas@gmail.com>
This commit is contained in:
Roberto Rojas 2022-12-22 14:25:41 -05:00
parent 1eb684a0c7
commit 1a8c98bfc7
2 changed files with 129 additions and 4 deletions

View File

@ -0,0 +1,99 @@
/*
Copyright 2022 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package dynamoDBStorage_test
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
)
func createTestTables(tables []string) error {
svc := dynamoDBService()
for _, t := range tables {
if err := createTable(svc, t); err != nil {
return err
}
}
return nil
}
func deleteTestTables(tables []string) error {
svc := dynamoDBService()
for _, t := range tables {
if err := deleteTable(svc, t); err != nil {
return err
}
}
return nil
}
func dynamoDBService() dynamodbiface.DynamoDBAPI {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
return dynamodb.New(sess)
}
func createTable(svc dynamodbiface.DynamoDBAPI, tableName string) error {
key := "key"
attributeDefinitions := []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String(key),
AttributeType: aws.String("S"),
},
}
keySchema := []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String(key),
KeyType: aws.String("HASH"),
},
}
provisionedThroughput := &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(10),
WriteCapacityUnits: aws.Int64(10),
}
_, err := svc.CreateTable(&dynamodb.CreateTableInput{
AttributeDefinitions: attributeDefinitions,
KeySchema: keySchema,
ProvisionedThroughput: provisionedThroughput,
TableName: aws.String(tableName),
})
if err != nil {
return err
}
return svc.WaitUntilTableExists(&dynamodb.DescribeTableInput{
TableName: aws.String(tableName),
})
}
func deleteTable(svc dynamodbiface.DynamoDBAPI, tableName string) error {
_, err := svc.DeleteTable(&dynamodb.DeleteTableInput{
TableName: aws.String(tableName),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok && (aerr.Code() != dynamodb.ErrCodeResourceNotFoundException) {
return err
}
}
return nil
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2021 The Dapr Authors
Copyright 2022 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
@ -31,11 +31,21 @@ import (
)
const (
sidecarNamePrefix = "dynamodb-sidecar-"
certificationTestPrefix = ""
sidecarNamePrefix = "dynamodb-sidecar-"
key = "key"
)
// The following Test Tables names must match
// the values of the "table" metadata properties
// found inside each of the components/*/dynamodb.yaml files
var testTables = []string{
"cert-test-basic",
}
func TestAWSDynamoDBStorage(t *testing.T) {
setup(t)
defer teardown(t)
ports, err := dapr_testing.GetFreePorts(2)
assert.NoError(t, err)
@ -50,7 +60,7 @@ func TestAWSDynamoDBStorage(t *testing.T) {
}
defer client.Close()
stateKey := certificationTestPrefix + "key"
stateKey := key
stateValue := "certificationdata"
// save state, default options: strong, last-write
@ -93,3 +103,19 @@ func componentRuntimeOptions() []runtime.Option {
runtime.WithStates(stateRegistry),
}
}
func setup(t *testing.T) {
t.Logf("AWS DynamoDB CertificationTests setup (could take some time to create test tables)...")
if err := createTestTables(testTables); err != nil {
t.Error(err)
}
t.Logf("AWS DynamoDB CertificationTests setup...done!")
}
func teardown(t *testing.T) {
t.Logf("AWS DynamoDB CertificationTests teardown...")
if err := deleteTestTables(testTables); err != nil {
t.Error(err)
}
t.Logf("AWS DynamoDB CertificationTests teardown...done!")
}