github.com/DerekStrickland/consul@v1.4.5/agent/cache-types/intention_match_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 TestIntentionMatch(t *testing.T) {
    14  	require := require.New(t)
    15  	rpc := TestRPC(t)
    16  	defer rpc.AssertExpectations(t)
    17  	typ := &IntentionMatch{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.IndexedIntentionMatches
    22  	rpc.On("RPC", "Intention.Match", mock.Anything, mock.Anything).Return(nil).
    23  		Run(func(args mock.Arguments) {
    24  			req := args.Get(1).(*structs.IntentionQueryRequest)
    25  			require.Equal(uint64(24), req.MinQueryIndex)
    26  			require.Equal(1*time.Second, req.MaxQueryTime)
    27  
    28  			reply := args.Get(2).(*structs.IndexedIntentionMatches)
    29  			reply.Index = 48
    30  			resp = reply
    31  		})
    32  
    33  	// Fetch
    34  	result, err := typ.Fetch(cache.FetchOptions{
    35  		MinIndex: 24,
    36  		Timeout:  1 * time.Second,
    37  	}, &structs.IntentionQueryRequest{Datacenter: "dc1"})
    38  	require.NoError(err)
    39  	require.Equal(cache.FetchResult{
    40  		Value: resp,
    41  		Index: 48,
    42  	}, result)
    43  }
    44  
    45  func TestIntentionMatch_badReqType(t *testing.T) {
    46  	require := require.New(t)
    47  	rpc := TestRPC(t)
    48  	defer rpc.AssertExpectations(t)
    49  	typ := &IntentionMatch{RPC: rpc}
    50  
    51  	// Fetch
    52  	_, err := typ.Fetch(cache.FetchOptions{}, cache.TestRequest(
    53  		t, cache.RequestInfo{Key: "foo", MinIndex: 64}))
    54  	require.Error(err)
    55  	require.Contains(err.Error(), "wrong type")
    56  
    57  }