traefik/provider/dynamodb_test.go
Taylor Skinner 72e35af39f add dynamo
Signed-off-by: Taylor Skinner <tskinn12@gmail.com>

add some comments

Signed-off-by: Taylor Skinner <tskinn12@gmail.com>

update readmes

make test runnable

Signed-off-by: Taylor Skinner <tskinn12@gmail.com>

make test

squash! add dynamo

add glide.lock

format imports

gofmt

update glide.lock

fixes for review

golint

clean up and reorganize tests

add dynamodb integration test

remove default region. clean up tests. consistent docs

forgot the region is required

DRY

make validate

update readme and commit dependencies
2017-03-16 10:12:26 -06:00

140 lines
3.1 KiB
Go

package provider
import (
"errors"
"reflect"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/containous/traefik/types"
)
type mockDynamoDBCLient struct {
dynamodbiface.DynamoDBAPI
testWithError bool
}
var backend = &types.Backend{
HealthCheck: &types.HealthCheck{
URL: "/build",
},
Servers: map[string]types.Server{
"server1": {
URL: "http://test.traefik.io",
},
},
}
var frontend = &types.Frontend{
EntryPoints: []string{"http"},
Backend: "test.traefik.io",
Routes: map[string]types.Route{
"route1": {
Rule: "Host:test.traefik.io",
},
},
}
// ScanPages simulates a call to ScanPages (see https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#DynamoDB.ScanPages)
// by running the fn function twice and returning an item each time.
func (m *mockDynamoDBCLient) ScanPages(input *dynamodb.ScanInput, fn func(*dynamodb.ScanOutput, bool) bool) error {
if m.testWithError {
return errors.New("fake error")
}
attributeBackend, err := dynamodbattribute.Marshal(backend)
if err != nil {
return err
}
attributeFrontend, err := dynamodbattribute.Marshal(frontend)
if err != nil {
return err
}
fn(&dynamodb.ScanOutput{
Items: []map[string]*dynamodb.AttributeValue{
{
"id": &dynamodb.AttributeValue{
S: aws.String("test.traefik.io_backend"),
},
"name": &dynamodb.AttributeValue{
S: aws.String("test.traefik.io"),
},
"backend": attributeBackend,
},
},
}, false)
fn(&dynamodb.ScanOutput{
Items: []map[string]*dynamodb.AttributeValue{
{
"id": &dynamodb.AttributeValue{
S: aws.String("test.traefik.io_frontend"),
},
"name": &dynamodb.AttributeValue{
S: aws.String("test.traefik.io"),
},
"frontend": attributeFrontend,
},
},
}, true)
return nil
}
func TestLoadDynamoConfigSuccessful(t *testing.T) {
dbiface := &dynamoClient{
db: &mockDynamoDBCLient{
testWithError: false,
},
}
provider := DynamoDB{}
loadedConfig, err := provider.loadDynamoConfig(dbiface)
if err != nil {
t.Fatal(err)
}
expectedConfig := &types.Configuration{
Backends: map[string]*types.Backend{
"test.traefik.io": backend,
},
Frontends: map[string]*types.Frontend{
"test.traefik.io": frontend,
},
}
if !reflect.DeepEqual(loadedConfig, expectedConfig) {
t.Fatalf("Configurations did not match: %v %v", loadedConfig, expectedConfig)
}
}
func TestLoadDynamoConfigFailure(t *testing.T) {
dbiface := &dynamoClient{
db: &mockDynamoDBCLient{
testWithError: true,
},
}
provider := DynamoDB{}
_, err := provider.loadDynamoConfig(dbiface)
if err == nil {
t.Fatal("Expected error")
}
}
func TestCreateClientSuccessful(t *testing.T) {
provider := DynamoDB{
Region: "us-east-1",
}
_, err := provider.createClient()
if err != nil {
t.Fatal(err)
}
}
func TestCreateClientFailure(t *testing.T) {
provider := DynamoDB{}
_, err := provider.createClient()
if err == nil {
t.Fatal("Expected error")
}
}