github.imxd.top/hashicorp/consul@v1.4.5/agent/consul/prepared_query/walk_test.go (about) 1 package prepared_query 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "sort" 9 10 "github.com/hashicorp/consul/agent/structs" 11 ) 12 13 func TestWalk_ServiceQuery(t *testing.T) { 14 var actual []string 15 fn := func(path string, v reflect.Value) error { 16 actual = append(actual, fmt.Sprintf("%s:%s", path, v.String())) 17 return nil 18 } 19 20 service := &structs.ServiceQuery{ 21 Service: "the-service", 22 Failover: structs.QueryDatacenterOptions{ 23 Datacenters: []string{"dc1", "dc2"}, 24 }, 25 Near: "_agent", 26 Tags: []string{"tag1", "tag2", "tag3"}, 27 NodeMeta: map[string]string{"foo": "bar", "role": "server"}, 28 } 29 if err := walk(service, fn); err != nil { 30 t.Fatalf("err: %v", err) 31 } 32 33 expected := []string{ 34 ".Failover.Datacenters[0]:dc1", 35 ".Failover.Datacenters[1]:dc2", 36 ".Near:_agent", 37 ".NodeMeta[foo]:bar", 38 ".NodeMeta[role]:server", 39 ".Service:the-service", 40 ".Tags[0]:tag1", 41 ".Tags[1]:tag2", 42 ".Tags[2]:tag3", 43 } 44 sort.Strings(actual) 45 if !reflect.DeepEqual(actual, expected) { 46 t.Fatalf("bad: %#v", actual) 47 } 48 } 49 50 func TestWalk_Visitor_Errors(t *testing.T) { 51 fn := func(path string, v reflect.Value) error { 52 return fmt.Errorf("bad") 53 } 54 55 service := &structs.ServiceQuery{} 56 err := walk(service, fn) 57 if err == nil || err.Error() != "bad" { 58 t.Fatalf("bad: %#v", err) 59 } 60 }