agent/cache: ConnectCA roots caching type

This commit is contained in:
Mitchell Hashimoto 2018-04-08 14:45:55 +01:00
parent 975be337a9
commit b0db5657c4
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 75 additions and 2 deletions

View File

@ -1,6 +1,5 @@
package cache
/*
import (
"fmt"
@ -36,4 +35,3 @@ func (c *TypeCARoot) Fetch(opts FetchOptions, req Request) (FetchResult, error)
result.Index = reply.QueryMeta.Index
return result, nil
}
*/

55
agent/cache/type_connect_ca_test.go vendored Normal file
View File

@ -0,0 +1,55 @@
package cache
import (
"testing"
"time"
"github.com/hashicorp/consul/agent/structs"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestTypeCARoot(t *testing.T) {
require := require.New(t)
rpc := TestRPC(t)
defer rpc.AssertExpectations(t)
typ := &TypeCARoot{RPC: rpc}
// Expect the proper RPC call. This also sets the expected value
// since that is return-by-pointer in the arguments.
var resp *structs.IndexedCARoots
rpc.On("RPC", "ConnectCA.Roots", mock.Anything, mock.Anything).Return(nil).
Run(func(args mock.Arguments) {
req := args.Get(1).(*structs.DCSpecificRequest)
require.Equal(uint64(24), req.QueryOptions.MinQueryIndex)
require.Equal(1*time.Second, req.QueryOptions.MaxQueryTime)
reply := args.Get(2).(*structs.IndexedCARoots)
reply.QueryMeta.Index = 48
resp = reply
})
// Fetch
result, err := typ.Fetch(FetchOptions{
MinIndex: 24,
Timeout: 1 * time.Second,
}, &structs.DCSpecificRequest{Datacenter: "dc1"})
require.Nil(err)
require.Equal(FetchResult{
Value: resp,
Index: 48,
}, result)
}
func TestTypeCARoot_badReqType(t *testing.T) {
require := require.New(t)
rpc := TestRPC(t)
defer rpc.AssertExpectations(t)
typ := &TypeCARoot{RPC: rpc}
// Fetch
_, err := typ.Fetch(FetchOptions{}, TestRequest(t, "foo", 64))
require.NotNil(err)
require.Contains(err.Error(), "wrong type")
}

View File

@ -6,6 +6,7 @@ import (
"math/rand"
"reflect"
"regexp"
"strconv"
"strings"
"time"
@ -14,6 +15,7 @@ import (
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/serf/coordinate"
"github.com/mitchellh/hashstructure"
)
type MessageType uint8
@ -276,6 +278,24 @@ func (r *DCSpecificRequest) RequestDatacenter() string {
return r.Datacenter
}
func (r *DCSpecificRequest) CacheKey() string {
// To calculate the cache key we only hash the node filters. The
// datacenter is handled by the cache framework. The other fields are
// not, but should not be used in any cache types.
v, err := hashstructure.Hash(r.NodeMetaFilters, nil)
if err != nil {
// Empty string means do not cache. If we have an error we should
// just forward along to the server.
return ""
}
return strconv.FormatUint(v, 10)
}
func (r *DCSpecificRequest) CacheMinIndex() uint64 {
return r.QueryOptions.MinQueryIndex
}
// ServiceSpecificRequest is used to query about a specific service
type ServiceSpecificRequest struct {
Datacenter string