mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 18:53:43 -04:00
* plugin/metadata: add metadata plugin * plugin/metadata: Add MD struct, refactor code, fix doc * plugin/metadata: simplify metadata key * plugin/metadata: improve setup_test * Support of metadata by rewrite plugin. Move calculated variables to metadata. * Move variables from metadata to pkg, add UTs, READMEs change, metadata small fixes * Add client port validation to variables_test * plugin/metadata: improve README * plugin/metadata: rename methods * plugin/metadata: Update Metadataer interface, update doc, cosmetic code changes * plugin/metadata: move colllisions check to OnStartup(). Fix default variables metadataer. * plugin/metadata: Fix comment for method setValue * plugin/metadata: change variables order to fix linter warning * plugin/metadata: rename Metadataer to Provider
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package metadata
|
|
|
|
import (
|
|
"context"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestMD(t *testing.T) {
|
|
tests := []struct {
|
|
addValues map[string]interface{}
|
|
expectedValues map[string]interface{}
|
|
}{
|
|
{
|
|
// Add initial metadata key/vals
|
|
map[string]interface{}{"key1": "val1", "key2": 2},
|
|
map[string]interface{}{"key1": "val1", "key2": 2},
|
|
},
|
|
{
|
|
// Add additional key/vals.
|
|
map[string]interface{}{"key3": 3, "key4": 4.5},
|
|
map[string]interface{}{"key1": "val1", "key2": 2, "key3": 3, "key4": 4.5},
|
|
},
|
|
}
|
|
|
|
// Using one same md and ctx for all test cases
|
|
ctx := context.TODO()
|
|
md, ctx := newMD(ctx)
|
|
|
|
for i, tc := range tests {
|
|
for k, v := range tc.addValues {
|
|
md.setValue(k, v)
|
|
}
|
|
if !reflect.DeepEqual(tc.expectedValues, map[string]interface{}(md)) {
|
|
t.Errorf("Test %d: Expected %v but got %v", i, tc.expectedValues, md)
|
|
}
|
|
|
|
// Make sure that MD is recieved from context successfullly
|
|
mdFromContext, ok := FromContext(ctx)
|
|
if !ok {
|
|
t.Errorf("Test %d: MD is not recieved from the context", i)
|
|
}
|
|
if !reflect.DeepEqual(md, mdFromContext) {
|
|
t.Errorf("Test %d: MD recieved from context differs from initial. Initial: %v, from context: %v", i, md, mdFromContext)
|
|
}
|
|
}
|
|
}
|