Completes non-ACL version of apply test.
This commit is contained in:
parent
bc0fba43d8
commit
76a55eed53
|
@ -276,7 +276,7 @@ func (c *consulFSM) applyPreparedQueryOperation(buf []byte, index uint64) interf
|
|||
defer metrics.MeasureSince([]string{"consul", "fsm", "prepared-query", string(req.Op)}, time.Now())
|
||||
switch req.Op {
|
||||
case structs.PreparedQueryCreate, structs.PreparedQueryUpdate:
|
||||
return c.state.PreparedQuerySet(index, &req.Query)
|
||||
return c.state.PreparedQuerySet(index, req.Query)
|
||||
case structs.PreparedQueryDelete:
|
||||
return c.state.PreparedQueryDelete(index, req.Query.ID)
|
||||
default:
|
||||
|
|
|
@ -79,7 +79,7 @@ func (p *PreparedQuery) Apply(args *structs.PreparedQueryRequest, reply *string)
|
|||
// Parse the query and prep it for the state store.
|
||||
switch args.Op {
|
||||
case structs.PreparedQueryCreate, structs.PreparedQueryUpdate:
|
||||
if err := parseQuery(&args.Query); err != nil {
|
||||
if err := parseQuery(args.Query); err != nil {
|
||||
return fmt.Errorf("Invalid prepared query: %v", err)
|
||||
}
|
||||
|
||||
|
@ -182,7 +182,7 @@ func parseDNS(dns *structs.QueryDNSOptions) error {
|
|||
}
|
||||
|
||||
// Lookup returns a single prepared query by ID or name.
|
||||
func (p *PreparedQuery) Lookup(args *structs.PreparedQuerySpecificRequest, reply *structs.IndexedPreparedQuery) error {
|
||||
func (p *PreparedQuery) Lookup(args *structs.PreparedQuerySpecificRequest, reply *structs.IndexedPreparedQueries) error {
|
||||
if done, err := p.srv.forward("PreparedQuery.Lookup", args, args, reply); done {
|
||||
return err
|
||||
}
|
||||
|
@ -206,12 +206,17 @@ func (p *PreparedQuery) Lookup(args *structs.PreparedQuerySpecificRequest, reply
|
|||
return err
|
||||
}
|
||||
|
||||
if (query.Token != args.Token) && (acl != nil && !acl.QueryModify()) {
|
||||
if (query != nil) && (query.Token != args.Token) && (acl != nil && !acl.QueryModify()) {
|
||||
p.srv.logger.Printf("[WARN] consul.prepared_query: Request to lookup prepared query '%s' denied because ACL didn't match ACL used to create the query, and a management token wasn't supplied", args.QueryIDOrName)
|
||||
return permissionDeniedErr
|
||||
}
|
||||
|
||||
reply.Index, reply.Query = index, query
|
||||
reply.Index = index
|
||||
if query != nil {
|
||||
reply.Queries = structs.PreparedQueries{query}
|
||||
} else {
|
||||
reply.Queries = nil
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package consul
|
|||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
@ -43,7 +44,7 @@ func TestPreparedQuery_Apply(t *testing.T) {
|
|||
arg := structs.PreparedQueryRequest{
|
||||
Datacenter: "dc1",
|
||||
Op: structs.PreparedQueryCreate,
|
||||
Query: structs.PreparedQuery{
|
||||
Query: &structs.PreparedQuery{
|
||||
Service: structs.ServiceQuery{
|
||||
Service: "redis",
|
||||
},
|
||||
|
@ -91,13 +92,62 @@ func TestPreparedQuery_Apply(t *testing.T) {
|
|||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
// Capture the new ID and make the op an update. This should go through.
|
||||
arg.Op = structs.PreparedQueryUpdate
|
||||
// Capture the ID and read the query back to verify.
|
||||
arg.Query.ID = reply
|
||||
{
|
||||
req := &structs.PreparedQuerySpecificRequest{
|
||||
Datacenter: "dc1",
|
||||
QueryIDOrName: arg.Query.ID,
|
||||
}
|
||||
var resp structs.IndexedPreparedQueries
|
||||
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Lookup", req, &resp); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
if len(resp.Queries) != 1 {
|
||||
t.Fatalf("bad: %v", resp)
|
||||
}
|
||||
actual := resp.Queries[0]
|
||||
if resp.Index != actual.ModifyIndex {
|
||||
t.Fatalf("bad index: %d", resp.Index)
|
||||
}
|
||||
actual.CreateIndex, actual.ModifyIndex = 0, 0
|
||||
if !reflect.DeepEqual(actual, arg.Query) {
|
||||
t.Fatalf("bad: %v", actual)
|
||||
}
|
||||
}
|
||||
|
||||
// Make the op an update. This should go through now that we have an ID.
|
||||
arg.Op = structs.PreparedQueryUpdate
|
||||
arg.Query.Service.Failover.NearestN = 2
|
||||
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Apply", &arg, &reply); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
// Read back again to verify the update worked.
|
||||
{
|
||||
req := &structs.PreparedQuerySpecificRequest{
|
||||
Datacenter: "dc1",
|
||||
QueryIDOrName: arg.Query.ID,
|
||||
}
|
||||
var resp structs.IndexedPreparedQueries
|
||||
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Lookup", req, &resp); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
if len(resp.Queries) != 1 {
|
||||
t.Fatalf("bad: %v", resp)
|
||||
}
|
||||
actual := resp.Queries[0]
|
||||
if resp.Index != actual.ModifyIndex {
|
||||
t.Fatalf("bad index: %d", resp.Index)
|
||||
}
|
||||
actual.CreateIndex, actual.ModifyIndex = 0, 0
|
||||
if !reflect.DeepEqual(actual, arg.Query) {
|
||||
t.Fatalf("bad: %v", actual)
|
||||
}
|
||||
}
|
||||
|
||||
// Give a bogus op and make sure it fails.
|
||||
arg.Op = "nope"
|
||||
err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Apply", &arg, &reply)
|
||||
|
@ -113,16 +163,6 @@ func TestPreparedQuery_Apply(t *testing.T) {
|
|||
t.Fatalf("bad: %v", err)
|
||||
}
|
||||
|
||||
// Sanity check - make sure there's one PQ in there.
|
||||
var queries structs.IndexedPreparedQueries
|
||||
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.List",
|
||||
&structs.DCSpecificRequest{Datacenter: "dc1"}, &queries); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if len(queries.Queries) != 1 {
|
||||
t.Fatalf("bad: %v", queries)
|
||||
}
|
||||
|
||||
// Now change the op to delete; the bad query field should be ignored
|
||||
// because all we care about for a delete op is the ID.
|
||||
arg.Op = structs.PreparedQueryDelete
|
||||
|
@ -130,12 +170,19 @@ func TestPreparedQuery_Apply(t *testing.T) {
|
|||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
// Make sure there are no longer any queries.
|
||||
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.List",
|
||||
&structs.DCSpecificRequest{Datacenter: "dc1"}, &queries); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if len(queries.Queries) != 0 {
|
||||
t.Fatalf("bad: %v", queries)
|
||||
// Verify that this query is deleted.
|
||||
{
|
||||
req := &structs.PreparedQuerySpecificRequest{
|
||||
Datacenter: "dc1",
|
||||
QueryIDOrName: arg.Query.ID,
|
||||
}
|
||||
var resp structs.IndexedPreparedQueries
|
||||
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Lookup", req, &resp); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
if len(resp.Queries) != 0 {
|
||||
t.Fatalf("bad: %v", resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,11 +83,6 @@ type PreparedQuery struct {
|
|||
|
||||
type PreparedQueries []*PreparedQuery
|
||||
|
||||
type IndexedPreparedQuery struct {
|
||||
Query *PreparedQuery
|
||||
QueryMeta
|
||||
}
|
||||
|
||||
type IndexedPreparedQueries struct {
|
||||
Queries PreparedQueries
|
||||
QueryMeta
|
||||
|
@ -105,7 +100,7 @@ const (
|
|||
type PreparedQueryRequest struct {
|
||||
Datacenter string
|
||||
Op PreparedQueryOp
|
||||
Query PreparedQuery
|
||||
Query *PreparedQuery
|
||||
WriteRequest
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue