github.com/DerekStrickland/consul@v1.4.5/agent/cache-types/health_services_test.go (about) 1 package cachetype 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/hashicorp/consul/agent/cache" 8 "github.com/hashicorp/consul/agent/structs" 9 "github.com/stretchr/testify/mock" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestHealthServices(t *testing.T) { 14 require := require.New(t) 15 rpc := TestRPC(t) 16 defer rpc.AssertExpectations(t) 17 typ := &HealthServices{RPC: rpc} 18 19 // Expect the proper RPC call. This also sets the expected value 20 // since that is return-by-pointer in the arguments. 21 var resp *structs.IndexedCheckServiceNodes 22 rpc.On("RPC", "Health.ServiceNodes", mock.Anything, mock.Anything).Return(nil). 23 Run(func(args mock.Arguments) { 24 req := args.Get(1).(*structs.ServiceSpecificRequest) 25 require.Equal(uint64(24), req.QueryOptions.MinQueryIndex) 26 require.Equal(1*time.Second, req.QueryOptions.MaxQueryTime) 27 require.Equal("web", req.ServiceName) 28 require.True(req.AllowStale) 29 30 reply := args.Get(2).(*structs.IndexedCheckServiceNodes) 31 reply.Nodes = []structs.CheckServiceNode{ 32 {Service: &structs.NodeService{Tags: req.ServiceTags}}, 33 } 34 reply.QueryMeta.Index = 48 35 resp = reply 36 }) 37 38 // Fetch 39 resultA, err := typ.Fetch(cache.FetchOptions{ 40 MinIndex: 24, 41 Timeout: 1 * time.Second, 42 }, &structs.ServiceSpecificRequest{ 43 Datacenter: "dc1", 44 ServiceName: "web", 45 ServiceTags: []string{"tag1", "tag2"}, 46 }) 47 require.NoError(err) 48 require.Equal(cache.FetchResult{ 49 Value: resp, 50 Index: 48, 51 }, resultA) 52 } 53 54 func TestHealthServices_badReqType(t *testing.T) { 55 require := require.New(t) 56 rpc := TestRPC(t) 57 defer rpc.AssertExpectations(t) 58 typ := &HealthServices{RPC: rpc} 59 60 // Fetch 61 _, err := typ.Fetch(cache.FetchOptions{}, cache.TestRequest( 62 t, cache.RequestInfo{Key: "foo", MinIndex: 64})) 63 require.Error(err) 64 require.Contains(err.Error(), "wrong type") 65 66 }