mirror of https://github.com/status-im/consul.git
Adds tag filter tests.
This commit is contained in:
parent
14170535e7
commit
eefdb56d1e
|
@ -393,7 +393,8 @@ func (p *PreparedQuery) execute(query *structs.PreparedQuery,
|
|||
}
|
||||
|
||||
// tagFilter returns a list of nodes who satisfy the given tags. Nodes must have
|
||||
// ALL the given tags, and none of the forbidden tags (prefixed with !).
|
||||
// ALL the given tags, and NONE of the forbidden tags (prefixed with !). Note
|
||||
// for performance this modifies the original slice.
|
||||
func tagFilter(tags []string, nodes structs.CheckServiceNodes) structs.CheckServiceNodes {
|
||||
// Build up lists of required and disallowed tags.
|
||||
must, not := make([]string, 0), make([]string, 0)
|
||||
|
@ -413,9 +414,11 @@ func tagFilter(tags []string, nodes structs.CheckServiceNodes) structs.CheckServ
|
|||
|
||||
// Index the tags so lookups this way are cheaper.
|
||||
index := make(map[string]struct{})
|
||||
for _, tag := range node.Service.Tags {
|
||||
tag = strings.ToLower(tag)
|
||||
index[tag] = struct{}{}
|
||||
if node.Service != nil {
|
||||
for _, tag := range node.Service.Tags {
|
||||
tag = strings.ToLower(tag)
|
||||
index[tag] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// Bail if any of the required tags are missing.
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"net/rpc"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -1657,3 +1658,89 @@ func TestPreparedQuery_Execute_ForwardLeader(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparedQuery_tagFilter(t *testing.T) {
|
||||
testNodes := func() structs.CheckServiceNodes {
|
||||
return structs.CheckServiceNodes{
|
||||
structs.CheckServiceNode{
|
||||
Node: &structs.Node{Node: "node1"},
|
||||
Service: &structs.NodeService{Tags: []string{"foo"}},
|
||||
},
|
||||
structs.CheckServiceNode{
|
||||
Node: &structs.Node{Node: "node2"},
|
||||
Service: &structs.NodeService{Tags: []string{"foo", "BAR"}},
|
||||
},
|
||||
structs.CheckServiceNode{
|
||||
Node: &structs.Node{Node: "node3"},
|
||||
},
|
||||
structs.CheckServiceNode{
|
||||
Node: &structs.Node{Node: "node4"},
|
||||
Service: &structs.NodeService{Tags: []string{"foo", "baz"}},
|
||||
},
|
||||
structs.CheckServiceNode{
|
||||
Node: &structs.Node{Node: "node5"},
|
||||
Service: &structs.NodeService{Tags: []string{"foo", "zoo"}},
|
||||
},
|
||||
structs.CheckServiceNode{
|
||||
Node: &structs.Node{Node: "node6"},
|
||||
Service: &structs.NodeService{Tags: []string{"bar"}},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// This always sorts so that it's not annoying to compare after the swap
|
||||
// operations that the algorithm performs.
|
||||
stringify := func(nodes structs.CheckServiceNodes) string {
|
||||
var names []string
|
||||
for _, node := range nodes {
|
||||
names = append(names, node.Node.Node)
|
||||
}
|
||||
sort.Strings(names)
|
||||
return strings.Join(names, "|")
|
||||
}
|
||||
|
||||
ret := stringify(tagFilter([]string{}, testNodes()))
|
||||
if ret != "node1|node2|node3|node4|node5|node6" {
|
||||
t.Fatalf("bad: %s", ret)
|
||||
}
|
||||
|
||||
ret = stringify(tagFilter([]string{"foo"}, testNodes()))
|
||||
if ret != "node1|node2|node4|node5" {
|
||||
t.Fatalf("bad: %s", ret)
|
||||
}
|
||||
|
||||
ret = stringify(tagFilter([]string{"!foo"}, testNodes()))
|
||||
if ret != "node3|node6" {
|
||||
t.Fatalf("bad: %s", ret)
|
||||
}
|
||||
|
||||
ret = stringify(tagFilter([]string{"!foo", "bar"}, testNodes()))
|
||||
if ret != "node6" {
|
||||
t.Fatalf("bad: %s", ret)
|
||||
}
|
||||
|
||||
ret = stringify(tagFilter([]string{"!foo", "!bar"}, testNodes()))
|
||||
if ret != "node3" {
|
||||
t.Fatalf("bad: %s", ret)
|
||||
}
|
||||
|
||||
ret = stringify(tagFilter([]string{"nope"}, testNodes()))
|
||||
if ret != "" {
|
||||
t.Fatalf("bad: %s", ret)
|
||||
}
|
||||
|
||||
ret = stringify(tagFilter([]string{"bar"}, testNodes()))
|
||||
if ret != "node2|node6" {
|
||||
t.Fatalf("bad: %s", ret)
|
||||
}
|
||||
|
||||
ret = stringify(tagFilter([]string{"BAR"}, testNodes()))
|
||||
if ret != "node2|node6" {
|
||||
t.Fatalf("bad: %s", ret)
|
||||
}
|
||||
|
||||
ret = stringify(tagFilter([]string{"bAr"}, testNodes()))
|
||||
if ret != "node2|node6" {
|
||||
t.Fatalf("bad: %s", ret)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue