2017-04-17 10:50:02 +00:00
|
|
|
package dynamodb
|
2017-03-09 01:53:34 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2017-06-01 20:10:19 +00:00
|
|
|
type mockDynamoDBClient struct {
|
2017-03-09 01:53:34 +00:00
|
|
|
dynamodbiface.DynamoDBAPI
|
|
|
|
testWithError bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var backend = &types.Backend{
|
|
|
|
HealthCheck: &types.HealthCheck{
|
2017-04-11 15:10:46 +00:00
|
|
|
Path: "/build",
|
2017-03-09 01:53:34 +00:00
|
|
|
},
|
|
|
|
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.
|
2017-06-01 20:10:19 +00:00
|
|
|
func (m *mockDynamoDBClient) ScanPages(input *dynamodb.ScanInput, fn func(*dynamodb.ScanOutput, bool) bool) error {
|
2017-03-09 01:53:34 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:25:29 +00:00
|
|
|
func TestBuildConfigurationSuccessful(t *testing.T) {
|
2017-03-09 01:53:34 +00:00
|
|
|
dbiface := &dynamoClient{
|
2017-06-01 20:10:19 +00:00
|
|
|
db: &mockDynamoDBClient{
|
2017-03-09 01:53:34 +00:00
|
|
|
testWithError: false,
|
|
|
|
},
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := Provider{}
|
2017-12-02 18:25:29 +00:00
|
|
|
loadedConfig, err := provider.buildConfiguration(dbiface)
|
2017-03-09 01:53:34 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:25:29 +00:00
|
|
|
func TestBuildConfigurationFailure(t *testing.T) {
|
2017-03-09 01:53:34 +00:00
|
|
|
dbiface := &dynamoClient{
|
2017-06-01 20:10:19 +00:00
|
|
|
db: &mockDynamoDBClient{
|
2017-03-09 01:53:34 +00:00
|
|
|
testWithError: true,
|
|
|
|
},
|
|
|
|
}
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := Provider{}
|
2017-12-02 18:25:29 +00:00
|
|
|
_, err := provider.buildConfiguration(dbiface)
|
2017-03-09 01:53:34 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateClientSuccessful(t *testing.T) {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := Provider{
|
2017-03-09 01:53:34 +00:00
|
|
|
Region: "us-east-1",
|
|
|
|
}
|
|
|
|
_, err := provider.createClient()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateClientFailure(t *testing.T) {
|
2017-04-17 10:50:02 +00:00
|
|
|
provider := Provider{}
|
2017-03-09 01:53:34 +00:00
|
|
|
_, err := provider.createClient()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error")
|
|
|
|
}
|
|
|
|
}
|