github.com/Jeffail/benthos/v3@v3.65.0/lib/test/integration/cache/dynamodb_test.go (about) 1 package cache 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/Jeffail/benthos/v3/internal/integration" 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/aws/credentials" 13 "github.com/aws/aws-sdk-go/aws/session" 14 "github.com/aws/aws-sdk-go/service/dynamodb" 15 "github.com/ory/dockertest/v3" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func createTable(ctx context.Context, t testing.TB, dynamoPort, id string) error { 21 endpoint := fmt.Sprintf("http://localhost:%v", dynamoPort) 22 23 table := id 24 hashKey := "id" 25 26 client := dynamodb.New(session.Must(session.NewSession(&aws.Config{ 27 Credentials: credentials.NewStaticCredentials("xxxxx", "xxxxx", "xxxxx"), 28 Endpoint: aws.String(endpoint), 29 Region: aws.String("us-east-1"), 30 }))) 31 32 ta, err := client.DescribeTable(&dynamodb.DescribeTableInput{ 33 TableName: aws.String(table), 34 }) 35 if err != nil { 36 if aerr, ok := err.(awserr.Error); !ok || aerr.Code() != dynamodb.ErrCodeResourceNotFoundException { 37 return err 38 } 39 } 40 41 if ta != nil && ta.Table != nil && ta.Table.TableStatus != nil && *ta.Table.TableStatus == dynamodb.TableStatusActive { 42 return nil 43 } 44 45 t.Logf("Creating table: %v\n", table) 46 client.CreateTable(&dynamodb.CreateTableInput{ 47 AttributeDefinitions: []*dynamodb.AttributeDefinition{ 48 { 49 AttributeName: aws.String(hashKey), 50 AttributeType: aws.String("S"), 51 }, 52 }, 53 KeySchema: []*dynamodb.KeySchemaElement{ 54 { 55 AttributeName: aws.String(hashKey), 56 KeyType: aws.String(dynamodb.KeyTypeHash), 57 }, 58 }, 59 ProvisionedThroughput: &dynamodb.ProvisionedThroughput{ 60 ReadCapacityUnits: aws.Int64(5), 61 WriteCapacityUnits: aws.Int64(5), 62 }, 63 TableName: aws.String(table), 64 }) 65 66 // wait for table to exist 67 return client.WaitUntilTableExistsWithContext(ctx, &dynamodb.DescribeTableInput{ 68 TableName: aws.String(table), 69 }) 70 } 71 72 var _ = registerIntegrationTest("dynamodb", func(t *testing.T) { 73 t.Parallel() 74 75 pool, err := dockertest.NewPool("") 76 require.NoError(t, err) 77 78 pool.MaxWait = time.Second * 30 79 80 resource, err := pool.RunWithOptions(&dockertest.RunOptions{ 81 Repository: "peopleperhour/dynamodb", 82 ExposedPorts: []string{"8000/tcp"}, 83 }) 84 require.NoError(t, err) 85 t.Cleanup(func() { 86 assert.NoError(t, pool.Purge(resource)) 87 }) 88 89 resource.Expire(900) 90 require.NoError(t, pool.Retry(func() error { 91 return createTable(context.Background(), t, resource.GetPort("8000/tcp"), "poketable") 92 })) 93 94 template := ` 95 cache_resources: 96 - label: testcache 97 aws_dynamodb: 98 endpoint: http://localhost:$PORT 99 region: us-east-1 100 consistent_read: true 101 data_key: data 102 hash_key: id 103 table: $ID 104 credentials: 105 id: xxxxx 106 secret: xxxxx 107 token: xxxxx 108 ` 109 suite := integration.CacheTests( 110 integration.CacheTestOpenClose(), 111 integration.CacheTestMissingKey(), 112 integration.CacheTestDoubleAdd(), 113 integration.CacheTestDelete(), 114 integration.CacheTestGetAndSet(50), 115 ) 116 suite.Run( 117 t, template, 118 integration.CacheTestOptPort(resource.GetPort("8000/tcp")), 119 integration.CacheTestOptPreTest(func(t testing.TB, ctx context.Context, testID string, vars *integration.CacheTestConfigVars) { 120 require.NoError(t, createTable(ctx, t, resource.GetPort("8000/tcp"), testID)) 121 }), 122 ) 123 })