1
0
Fork 0

use provider-qualified name when recursing for chain

This commit is contained in:
mpl 2020-01-27 10:40:05 +01:00 committed by Traefiker Bot
parent 87044c54f4
commit 16288d171c
12 changed files with 70 additions and 62 deletions

View file

@ -0,0 +1,45 @@
package provider
import (
"context"
"strings"
"github.com/containous/traefik/v2/pkg/log"
)
type contextKey int
const (
key contextKey = iota
)
// AddInContext Adds the provider name in the context
func AddInContext(ctx context.Context, elementName string) context.Context {
parts := strings.Split(elementName, "@")
if len(parts) == 1 {
log.FromContext(ctx).Debugf("Could not find a provider for %s.", elementName)
return ctx
}
if name, ok := ctx.Value(key).(string); ok && name == parts[1] {
return ctx
}
return context.WithValue(ctx, key, parts[1])
}
// GetQualifiedName Gets the fully qualified name.
func GetQualifiedName(ctx context.Context, elementName string) string {
parts := strings.Split(elementName, "@")
if len(parts) == 1 {
if providerName, ok := ctx.Value(key).(string); ok {
return MakeQualifiedName(providerName, parts[0])
}
}
return elementName
}
// MakeQualifiedName Creates a qualified name for an element
func MakeQualifiedName(providerName string, elementName string) string {
return elementName + "@" + providerName
}

View file

@ -0,0 +1,115 @@
package provider
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAddProviderInContext(t *testing.T) {
testCases := []struct {
desc string
ctx context.Context
name string
expected string
}{
{
desc: "without provider information",
ctx: context.Background(),
name: "test",
expected: "",
},
{
desc: "provider name embedded in element name",
ctx: context.Background(),
name: "test@foo",
expected: "foo",
},
{
desc: "provider name in context",
ctx: context.WithValue(context.Background(), key, "foo"),
name: "test",
expected: "foo",
},
{
desc: "provider name in context and different provider name embedded in element name",
ctx: context.WithValue(context.Background(), key, "foo"),
name: "test@fii",
expected: "fii",
},
{
desc: "provider name in context and same provider name embedded in element name",
ctx: context.WithValue(context.Background(), key, "foo"),
name: "test@foo",
expected: "foo",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
newCtx := AddInContext(test.ctx, test.name)
var providerName string
if name, ok := newCtx.Value(key).(string); ok {
providerName = name
}
assert.Equal(t, test.expected, providerName)
})
}
}
func TestGetQualifiedName(t *testing.T) {
testCases := []struct {
desc string
ctx context.Context
name string
expected string
}{
{
desc: "empty name",
ctx: context.Background(),
name: "",
expected: "",
},
{
desc: "without provider",
ctx: context.Background(),
name: "test",
expected: "test",
},
{
desc: "with explicit provider",
ctx: context.Background(),
name: "test@foo",
expected: "test@foo",
},
{
desc: "with provider in context",
ctx: context.WithValue(context.Background(), key, "foo"),
name: "test",
expected: "test@foo",
},
{
desc: "with provider in context and explicit name",
ctx: context.WithValue(context.Background(), key, "foo"),
name: "test@fii",
expected: "test@fii",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
qualifiedName := GetQualifiedName(test.ctx, test.name)
assert.Equal(t, test.expected, qualifiedName)
})
}
}