2015-11-07 00:59:32 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-01-24 17:22:07 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2022-01-20 12:47:50 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2015-11-07 00:59:32 +00:00
|
|
|
)
|
|
|
|
|
2015-11-10 05:48:35 +00:00
|
|
|
func TestStateStore_PreparedQuery_isUUID(t *testing.T) {
|
|
|
|
cases := map[string]bool{
|
2018-11-07 10:16:03 +00:00
|
|
|
"": false,
|
|
|
|
"nope": false,
|
2015-11-10 05:48:35 +00:00
|
|
|
"f004177f-2c28-83b7-4229-eacc25fe55d1": true,
|
2015-11-17 06:57:47 +00:00
|
|
|
"F004177F-2C28-83B7-4229-EACC25FE55D1": true,
|
|
|
|
"x004177f-2c28-83b7-4229-eacc25fe55d1": false, // Bad hex
|
|
|
|
"f004177f-xc28-83b7-4229-eacc25fe55d1": false, // Bad hex
|
|
|
|
"f004177f-2c28-x3b7-4229-eacc25fe55d1": false, // Bad hex
|
|
|
|
"f004177f-2c28-83b7-x229-eacc25fe55d1": false, // Bad hex
|
|
|
|
"f004177f-2c28-83b7-4229-xacc25fe55d1": false, // Bad hex
|
2015-11-10 05:48:35 +00:00
|
|
|
" f004177f-2c28-83b7-4229-eacc25fe55d1": false, // Leading whitespace
|
|
|
|
"f004177f-2c28-83b7-4229-eacc25fe55d1 ": false, // Trailing whitespace
|
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
cases[testUUID()] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for str, expected := range cases {
|
|
|
|
if actual := isUUID(str); actual != expected {
|
|
|
|
t.Fatalf("bad: '%s'", str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 04:37:41 +00:00
|
|
|
func TestStateStore_PreparedQuerySet_PreparedQueryGet(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2015-11-07 00:59:32 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// Querying with no results returns nil.
|
2017-01-24 17:22:07 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
idx, res, err := s.PreparedQueryGet(ws, testUUID())
|
2015-11-07 00:59:32 +00:00
|
|
|
if idx != 0 || res != nil || err != nil {
|
|
|
|
t.Fatalf("expected (0, nil, nil), got: (%d, %#v, %#v)", idx, res, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inserting a query with empty ID is disallowed.
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQuerySet(1, &structs.PreparedQuery{}); err == nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("expected %#v, got: %#v", ErrMissingQueryID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index is not updated if nothing is saved.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 0 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
|
|
|
|
// Build a legit-looking query with the most basic options.
|
|
|
|
query := &structs.PreparedQuery{
|
Creates new "prepared-query" ACL type and new token capture behavior.
Prior to this change, prepared queries had the following behavior for
ACLs, which will need to change to support templates:
1. A management token, or a token with read access to the service being
queried needed to be provided in order to create a prepared query.
2. The token used to create the prepared query was stored with the query
in the state store and used to execute the query.
3. A management token, or the token used to create the query needed to be
supplied to perform and CRUD operations on an existing prepared query.
This was pretty subtle and complicated behavior, and won't work for
templates since the service name is computed at execution time. To solve
this, we introduce a new "prepared-query" ACL type, where the prefix
applies to the query name for static prepared query types and to the
prefix for template prepared query types.
With this change, the new behavior is:
1. A management token, or a token with "prepared-query" write access to
the query name or (soon) the given template prefix is required to do
any CRUD operations on a prepared query, or to list prepared queries
(the list is filtered by this ACL).
2. You will no longer need a management token to list prepared queries,
but you will only be able to see prepared queries that you have access
to (you get an empty list instead of permission denied).
3. When listing or getting a query, because it was easy to capture
management tokens given the past behavior, this will always blank out
the "Token" field (replacing the contents as <hidden>) for all tokens
unless a management token is supplied. Going forward, we should
discourage people from binding tokens for execution unless strictly
necessary.
4. No token will be captured by default when a prepared query is created.
If the user wishes to supply an execution token then can pass it in via
the "Token" field in the prepared query definition. Otherwise, this
field will default to empty.
5. At execution time, we will use the captured token if it exists with the
prepared query definition, otherwise we will use the token that's passed
in with the request, just like we do for other RPCs (or you can use the
agent's configured token for DNS).
6. Prepared queries with no name (accessible only by ID) will not require
ACLs to create or modify (execution time will depend on the service ACL
configuration). Our argument here is that these are designed to be
ephemeral and the IDs are as good as an ACL. Management tokens will be
able to list all of these.
These changes enable templates, but also enable delegation of authority to
manage the prepared query namespace.
2016-02-23 08:12:58 +00:00
|
|
|
ID: testUUID(),
|
|
|
|
Session: "nope",
|
2015-11-07 00:59:32 +00:00
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
Creates new "prepared-query" ACL type and new token capture behavior.
Prior to this change, prepared queries had the following behavior for
ACLs, which will need to change to support templates:
1. A management token, or a token with read access to the service being
queried needed to be provided in order to create a prepared query.
2. The token used to create the prepared query was stored with the query
in the state store and used to execute the query.
3. A management token, or the token used to create the query needed to be
supplied to perform and CRUD operations on an existing prepared query.
This was pretty subtle and complicated behavior, and won't work for
templates since the service name is computed at execution time. To solve
this, we introduce a new "prepared-query" ACL type, where the prefix
applies to the query name for static prepared query types and to the
prefix for template prepared query types.
With this change, the new behavior is:
1. A management token, or a token with "prepared-query" write access to
the query name or (soon) the given template prefix is required to do
any CRUD operations on a prepared query, or to list prepared queries
(the list is filtered by this ACL).
2. You will no longer need a management token to list prepared queries,
but you will only be able to see prepared queries that you have access
to (you get an empty list instead of permission denied).
3. When listing or getting a query, because it was easy to capture
management tokens given the past behavior, this will always blank out
the "Token" field (replacing the contents as <hidden>) for all tokens
unless a management token is supplied. Going forward, we should
discourage people from binding tokens for execution unless strictly
necessary.
4. No token will be captured by default when a prepared query is created.
If the user wishes to supply an execution token then can pass it in via
the "Token" field in the prepared query definition. Otherwise, this
field will default to empty.
5. At execution time, we will use the captured token if it exists with the
prepared query definition, otherwise we will use the token that's passed
in with the request, just like we do for other RPCs (or you can use the
agent's configured token for DNS).
6. Prepared queries with no name (accessible only by ID) will not require
ACLs to create or modify (execution time will depend on the service ACL
configuration). Our argument here is that these are designed to be
ephemeral and the IDs are as good as an ACL. Management tokens will be
able to list all of these.
These changes enable templates, but also enable delegation of authority to
manage the prepared query namespace.
2016-02-23 08:12:58 +00:00
|
|
|
// The set will still fail because the session is bogus.
|
2015-11-10 04:37:41 +00:00
|
|
|
err = s.PreparedQuerySet(1, query)
|
2019-11-25 17:07:04 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "invalid session") {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index is not updated if nothing is saved.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 0 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
|
Creates new "prepared-query" ACL type and new token capture behavior.
Prior to this change, prepared queries had the following behavior for
ACLs, which will need to change to support templates:
1. A management token, or a token with read access to the service being
queried needed to be provided in order to create a prepared query.
2. The token used to create the prepared query was stored with the query
in the state store and used to execute the query.
3. A management token, or the token used to create the query needed to be
supplied to perform and CRUD operations on an existing prepared query.
This was pretty subtle and complicated behavior, and won't work for
templates since the service name is computed at execution time. To solve
this, we introduce a new "prepared-query" ACL type, where the prefix
applies to the query name for static prepared query types and to the
prefix for template prepared query types.
With this change, the new behavior is:
1. A management token, or a token with "prepared-query" write access to
the query name or (soon) the given template prefix is required to do
any CRUD operations on a prepared query, or to list prepared queries
(the list is filtered by this ACL).
2. You will no longer need a management token to list prepared queries,
but you will only be able to see prepared queries that you have access
to (you get an empty list instead of permission denied).
3. When listing or getting a query, because it was easy to capture
management tokens given the past behavior, this will always blank out
the "Token" field (replacing the contents as <hidden>) for all tokens
unless a management token is supplied. Going forward, we should
discourage people from binding tokens for execution unless strictly
necessary.
4. No token will be captured by default when a prepared query is created.
If the user wishes to supply an execution token then can pass it in via
the "Token" field in the prepared query definition. Otherwise, this
field will default to empty.
5. At execution time, we will use the captured token if it exists with the
prepared query definition, otherwise we will use the token that's passed
in with the request, just like we do for other RPCs (or you can use the
agent's configured token for DNS).
6. Prepared queries with no name (accessible only by ID) will not require
ACLs to create or modify (execution time will depend on the service ACL
configuration). Our argument here is that these are designed to be
ephemeral and the IDs are as good as an ACL. Management tokens will be
able to list all of these.
These changes enable templates, but also enable delegation of authority to
manage the prepared query namespace.
2016-02-23 08:12:58 +00:00
|
|
|
// Now register the service and remove the bogus session.
|
2015-11-07 00:59:32 +00:00
|
|
|
testRegisterNode(t, s, 1, "foo")
|
|
|
|
testRegisterService(t, s, 2, "foo", "redis")
|
Creates new "prepared-query" ACL type and new token capture behavior.
Prior to this change, prepared queries had the following behavior for
ACLs, which will need to change to support templates:
1. A management token, or a token with read access to the service being
queried needed to be provided in order to create a prepared query.
2. The token used to create the prepared query was stored with the query
in the state store and used to execute the query.
3. A management token, or the token used to create the query needed to be
supplied to perform and CRUD operations on an existing prepared query.
This was pretty subtle and complicated behavior, and won't work for
templates since the service name is computed at execution time. To solve
this, we introduce a new "prepared-query" ACL type, where the prefix
applies to the query name for static prepared query types and to the
prefix for template prepared query types.
With this change, the new behavior is:
1. A management token, or a token with "prepared-query" write access to
the query name or (soon) the given template prefix is required to do
any CRUD operations on a prepared query, or to list prepared queries
(the list is filtered by this ACL).
2. You will no longer need a management token to list prepared queries,
but you will only be able to see prepared queries that you have access
to (you get an empty list instead of permission denied).
3. When listing or getting a query, because it was easy to capture
management tokens given the past behavior, this will always blank out
the "Token" field (replacing the contents as <hidden>) for all tokens
unless a management token is supplied. Going forward, we should
discourage people from binding tokens for execution unless strictly
necessary.
4. No token will be captured by default when a prepared query is created.
If the user wishes to supply an execution token then can pass it in via
the "Token" field in the prepared query definition. Otherwise, this
field will default to empty.
5. At execution time, we will use the captured token if it exists with the
prepared query definition, otherwise we will use the token that's passed
in with the request, just like we do for other RPCs (or you can use the
agent's configured token for DNS).
6. Prepared queries with no name (accessible only by ID) will not require
ACLs to create or modify (execution time will depend on the service ACL
configuration). Our argument here is that these are designed to be
ephemeral and the IDs are as good as an ACL. Management tokens will be
able to list all of these.
These changes enable templates, but also enable delegation of authority to
manage the prepared query namespace.
2016-02-23 08:12:58 +00:00
|
|
|
query.Session = ""
|
2015-11-07 00:59:32 +00:00
|
|
|
|
|
|
|
// This should go through.
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQuerySet(3, query); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 3 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if !watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
|
|
|
|
// Read it back out and verify it.
|
|
|
|
expected := &structs.PreparedQuery{
|
|
|
|
ID: query.ID,
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 3,
|
|
|
|
ModifyIndex: 3,
|
|
|
|
},
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
ws = memdb.NewWatchSet()
|
|
|
|
idx, actual, err := s.PreparedQueryGet(ws, query.ID)
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 3 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give it a name and set it again.
|
|
|
|
query.Name = "test-query"
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQuerySet(4, query); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 4 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if !watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
|
|
|
|
// Read it back and verify the data was updated as well as the index.
|
|
|
|
expected.Name = "test-query"
|
|
|
|
expected.ModifyIndex = 4
|
2017-01-24 17:22:07 +00:00
|
|
|
ws = memdb.NewWatchSet()
|
|
|
|
idx, actual, err = s.PreparedQueryGet(ws, query.ID)
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to tie it to a bogus session.
|
|
|
|
query.Session = testUUID()
|
2015-11-10 04:37:41 +00:00
|
|
|
err = s.PreparedQuerySet(5, query)
|
2015-11-07 00:59:32 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "invalid session") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index is not updated if nothing is saved.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 4 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
|
|
|
|
// Now make a session and try again.
|
|
|
|
session := &structs.Session{
|
|
|
|
ID: query.Session,
|
|
|
|
Node: "foo",
|
|
|
|
}
|
|
|
|
if err := s.SessionCreate(5, session); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQuerySet(6, query); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 6 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if !watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
|
|
|
|
// Read it back and verify the data was updated as well as the index.
|
|
|
|
expected.Session = query.Session
|
|
|
|
expected.ModifyIndex = 6
|
2017-01-24 17:22:07 +00:00
|
|
|
ws = memdb.NewWatchSet()
|
|
|
|
idx, actual, err = s.PreparedQueryGet(ws, query.ID)
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 6 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:04:51 +00:00
|
|
|
// Try to register a query with the same name and make sure it fails.
|
|
|
|
{
|
|
|
|
evil := &structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: query.Name,
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := s.PreparedQuerySet(7, evil)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "aliases an existing query name") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check to make sure it's not there.
|
2017-01-24 17:22:07 +00:00
|
|
|
idx, actual, err := s.PreparedQueryGet(nil, evil.ID)
|
2015-12-02 17:04:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 6 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
}
|
2015-12-02 17:04:51 +00:00
|
|
|
|
|
|
|
// Try to abuse the system by trying to register a query whose name
|
|
|
|
// aliases a real query ID.
|
|
|
|
{
|
|
|
|
evil := &structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: query.ID,
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := s.PreparedQuerySet(8, evil)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "aliases an existing query ID") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check to make sure it's not there.
|
2017-01-24 17:22:07 +00:00
|
|
|
idx, actual, err := s.PreparedQueryGet(nil, evil.ID)
|
2015-12-02 17:04:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 6 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-03 04:54:46 +00:00
|
|
|
// Try to register a template that squats on the existing query's name.
|
|
|
|
{
|
|
|
|
evil := &structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: query.Name,
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := s.PreparedQuerySet(8, evil)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "aliases an existing query name") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check to make sure it's not there.
|
2017-01-24 17:22:07 +00:00
|
|
|
idx, actual, err := s.PreparedQueryGet(nil, evil.ID)
|
2016-03-03 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 6 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-07 00:59:32 +00:00
|
|
|
// Index is not updated if nothing is saved.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 6 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2016-03-03 04:54:46 +00:00
|
|
|
|
|
|
|
// Turn the query into a template with an empty name.
|
|
|
|
query.Name = ""
|
|
|
|
query.Template = structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
}
|
|
|
|
if err := s.PreparedQuerySet(9, query); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 9 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if !watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2016-03-03 04:54:46 +00:00
|
|
|
|
|
|
|
// Read it back and verify the data was updated as well as the index.
|
|
|
|
expected.Name = ""
|
|
|
|
expected.Template = structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
}
|
|
|
|
expected.ModifyIndex = 9
|
2017-01-24 17:22:07 +00:00
|
|
|
ws = memdb.NewWatchSet()
|
|
|
|
idx, actual, err = s.PreparedQueryGet(ws, query.ID)
|
2016-03-03 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 9 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to register a template that squats on the empty prefix.
|
|
|
|
{
|
|
|
|
evil := &structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: "",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := s.PreparedQuerySet(10, evil)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "query template with an empty name already exists") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check to make sure it's not there.
|
2017-01-24 17:22:07 +00:00
|
|
|
idx, actual, err := s.PreparedQueryGet(nil, evil.ID)
|
2016-03-03 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 9 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give the query template a name.
|
|
|
|
query.Name = "prefix"
|
|
|
|
if err := s.PreparedQuerySet(11, query); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 11 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if !watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2016-03-03 04:54:46 +00:00
|
|
|
|
|
|
|
// Read it back and verify the data was updated as well as the index.
|
|
|
|
expected.Name = "prefix"
|
|
|
|
expected.ModifyIndex = 11
|
2017-01-24 17:22:07 +00:00
|
|
|
ws = memdb.NewWatchSet()
|
|
|
|
idx, actual, err = s.PreparedQueryGet(ws, query.ID)
|
2016-03-03 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 11 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to register a template that squats on the prefix.
|
|
|
|
{
|
|
|
|
evil := &structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: "prefix",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := s.PreparedQuerySet(12, evil)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "aliases an existing query name") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check to make sure it's not there.
|
2017-01-24 17:22:07 +00:00
|
|
|
idx, actual, err := s.PreparedQueryGet(nil, evil.ID)
|
2016-03-03 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 11 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to register a template that doesn't compile.
|
|
|
|
{
|
|
|
|
evil := &structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: "legit-prefix",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "${nope",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := s.PreparedQuerySet(13, evil)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "failed compiling template") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check to make sure it's not there.
|
2017-01-24 17:22:07 +00:00
|
|
|
idx, actual, err := s.PreparedQueryGet(nil, evil.ID)
|
2016-03-03 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 11 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
|
|
|
|
if watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 04:37:41 +00:00
|
|
|
func TestStateStore_PreparedQueryDelete(t *testing.T) {
|
2015-11-07 00:59:32 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// Set up our test environment.
|
|
|
|
testRegisterNode(t, s, 1, "foo")
|
|
|
|
testRegisterService(t, s, 2, "foo", "redis")
|
|
|
|
|
|
|
|
// Create a new query.
|
|
|
|
query := &structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting a query that doesn't exist should be a no-op.
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQueryDelete(3, query.ID); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index is not updated if nothing is saved.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 0 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now add the query to the data store.
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQuerySet(3, query); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 3 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read it back out and verify it.
|
|
|
|
expected := &structs.PreparedQuery{
|
|
|
|
ID: query.ID,
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 3,
|
|
|
|
ModifyIndex: 3,
|
|
|
|
},
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
idx, actual, err := s.PreparedQueryGet(ws, query.ID)
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 3 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now delete it.
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQueryDelete(4, query.ID); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 4 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if !watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
|
|
|
|
// Sanity check to make sure it's not there.
|
2017-01-24 17:22:07 +00:00
|
|
|
idx, actual, err = s.PreparedQueryGet(nil, query.ID)
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 20:07:43 +00:00
|
|
|
func TestStateStore_PreparedQueryResolve(t *testing.T) {
|
2015-11-07 00:59:32 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// Set up our test environment.
|
|
|
|
testRegisterNode(t, s, 1, "foo")
|
|
|
|
testRegisterService(t, s, 2, "foo", "redis")
|
|
|
|
|
|
|
|
// Create a new query.
|
|
|
|
query := &structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: "my-test-query",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to lookup a query that's not there using something that looks
|
|
|
|
// like a real ID.
|
2017-08-30 00:02:50 +00:00
|
|
|
idx, actual, err := s.PreparedQueryResolve(query.ID, structs.QuerySource{})
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to lookup a query that's not there using something that looks
|
|
|
|
// like a name
|
2017-08-30 00:02:50 +00:00
|
|
|
idx, actual, err = s.PreparedQueryResolve(query.Name, structs.QuerySource{})
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now actually insert the query.
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQuerySet(3, query); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2015-11-10 04:37:41 +00:00
|
|
|
if idx := s.maxIndex("prepared-queries"); idx != 3 {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read it back out using the ID and verify it.
|
|
|
|
expected := &structs.PreparedQuery{
|
|
|
|
ID: query.ID,
|
|
|
|
Name: "my-test-query",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 3,
|
|
|
|
ModifyIndex: 3,
|
|
|
|
},
|
|
|
|
}
|
2017-08-30 00:02:50 +00:00
|
|
|
idx, actual, err = s.PreparedQueryResolve(query.ID, structs.QuerySource{})
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 3 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read it back using the name and verify it again.
|
2017-08-30 00:02:50 +00:00
|
|
|
idx, actual, err = s.PreparedQueryResolve(query.Name, structs.QuerySource{})
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 3 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure an empty lookup is well-behaved if there are actual queries
|
|
|
|
// in the state store.
|
2017-08-30 00:02:50 +00:00
|
|
|
idx, actual, err = s.PreparedQueryResolve("", structs.QuerySource{})
|
2015-11-17 17:25:20 +00:00
|
|
|
if err != ErrMissingQueryID {
|
|
|
|
t.Fatalf("bad: %v ", err)
|
|
|
|
}
|
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
2015-11-07 00:59:32 +00:00
|
|
|
}
|
2016-03-03 04:54:46 +00:00
|
|
|
|
|
|
|
// Create two prepared query templates, one a longer prefix of the
|
|
|
|
// other.
|
|
|
|
tmpl1 := &structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: "prod-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "${name.suffix}",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := s.PreparedQuerySet(4, tmpl1); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
tmpl2 := &structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: "prod-redis",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
Regexp: "^prod-(.*)$",
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
2022-01-20 12:47:50 +00:00
|
|
|
Service: "${match(1)}-primary",
|
2016-03-03 04:54:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := s.PreparedQuerySet(5, tmpl2); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve the less-specific prefix.
|
|
|
|
expected = &structs.PreparedQuery{
|
|
|
|
ID: tmpl1.ID,
|
|
|
|
Name: "prod-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "mongodb",
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 4,
|
|
|
|
ModifyIndex: 4,
|
|
|
|
},
|
|
|
|
}
|
2017-08-30 00:02:50 +00:00
|
|
|
idx, actual, err = s.PreparedQueryResolve("prod-mongodb", structs.QuerySource{})
|
2016-03-03 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 5 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now resolve the more specific prefix.
|
|
|
|
expected = &structs.PreparedQuery{
|
|
|
|
ID: tmpl2.ID,
|
|
|
|
Name: "prod-redis",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
Regexp: "^prod-(.*)$",
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
2022-01-20 12:47:50 +00:00
|
|
|
Service: "redis-foobar-primary",
|
2016-03-03 04:54:46 +00:00
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 5,
|
|
|
|
ModifyIndex: 5,
|
|
|
|
},
|
|
|
|
}
|
2017-08-30 00:02:50 +00:00
|
|
|
idx, actual, err = s.PreparedQueryResolve("prod-redis-foobar", structs.QuerySource{})
|
2016-03-03 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 5 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve an exact-match prefix. The output of this one doesn't match a
|
|
|
|
// sensical service name, but it still renders.
|
|
|
|
expected = &structs.PreparedQuery{
|
|
|
|
ID: tmpl1.ID,
|
|
|
|
Name: "prod-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "",
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 4,
|
|
|
|
ModifyIndex: 4,
|
|
|
|
},
|
|
|
|
}
|
2017-08-30 00:02:50 +00:00
|
|
|
idx, actual, err = s.PreparedQueryResolve("prod-", structs.QuerySource{})
|
2016-03-03 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 5 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure you can't run a prepared query template by ID, since that
|
|
|
|
// makes no sense.
|
2017-08-30 00:02:50 +00:00
|
|
|
_, _, err = s.PreparedQueryResolve(tmpl1.ID, structs.QuerySource{})
|
2016-03-03 04:54:46 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "prepared query templates can only be resolved up by name") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 04:37:41 +00:00
|
|
|
func TestStateStore_PreparedQueryList(t *testing.T) {
|
2015-11-07 00:59:32 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
|
2015-11-15 05:59:23 +00:00
|
|
|
// Make sure nothing is returned for an empty query
|
2017-01-24 17:22:07 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
idx, actual, err := s.PreparedQueryList(ws)
|
2015-11-15 00:13:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2015-11-15 05:59:23 +00:00
|
|
|
if len(actual) != 0 {
|
2015-11-15 00:13:40 +00:00
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
|
2015-11-07 00:59:32 +00:00
|
|
|
// Set up our test environment.
|
|
|
|
testRegisterNode(t, s, 1, "foo")
|
|
|
|
testRegisterService(t, s, 2, "foo", "redis")
|
|
|
|
testRegisterService(t, s, 3, "foo", "mongodb")
|
|
|
|
|
|
|
|
// Create some queries.
|
|
|
|
queries := structs.PreparedQueries{
|
|
|
|
&structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: "alice",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: "bob",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "mongodb",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the sort order of the UUIDs before we create them so the
|
|
|
|
// order is deterministic.
|
|
|
|
queries[0].ID = "a" + queries[0].ID[1:]
|
|
|
|
queries[1].ID = "b" + queries[1].ID[1:]
|
|
|
|
|
|
|
|
// Now create the queries.
|
|
|
|
for i, query := range queries {
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQuerySet(uint64(4+i), query); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
if !watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
|
|
|
|
// Read it back and verify.
|
|
|
|
expected := structs.PreparedQueries{
|
|
|
|
&structs.PreparedQuery{
|
|
|
|
ID: queries[0].ID,
|
|
|
|
Name: "alice",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 4,
|
|
|
|
ModifyIndex: 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.PreparedQuery{
|
|
|
|
ID: queries[1].ID,
|
|
|
|
Name: "bob",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "mongodb",
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 5,
|
|
|
|
ModifyIndex: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-01-24 17:22:07 +00:00
|
|
|
idx, actual, err = s.PreparedQueryList(nil)
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 5 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 04:37:41 +00:00
|
|
|
func TestStateStore_PreparedQuery_Snapshot_Restore(t *testing.T) {
|
2015-11-07 00:59:32 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// Set up our test environment.
|
|
|
|
testRegisterNode(t, s, 1, "foo")
|
|
|
|
testRegisterService(t, s, 2, "foo", "redis")
|
|
|
|
testRegisterService(t, s, 3, "foo", "mongodb")
|
|
|
|
|
|
|
|
// Create some queries.
|
|
|
|
queries := structs.PreparedQueries{
|
|
|
|
&structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
|
|
|
Name: "alice",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.PreparedQuery{
|
|
|
|
ID: testUUID(),
|
2016-03-01 20:43:24 +00:00
|
|
|
Name: "bob-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
2015-11-07 00:59:32 +00:00
|
|
|
Service: structs.ServiceQuery{
|
2016-03-01 20:43:24 +00:00
|
|
|
Service: "${name.suffix}",
|
2015-11-07 00:59:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the sort order of the UUIDs before we create them so the
|
|
|
|
// order is deterministic.
|
|
|
|
queries[0].ID = "a" + queries[0].ID[1:]
|
|
|
|
queries[1].ID = "b" + queries[1].ID[1:]
|
|
|
|
|
|
|
|
// Now create the queries.
|
|
|
|
for i, query := range queries {
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQuerySet(uint64(4+i), query); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot the queries.
|
|
|
|
snap := s.Snapshot()
|
|
|
|
defer snap.Close()
|
|
|
|
|
|
|
|
// Alter the real state store.
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := s.PreparedQueryDelete(6, queries[0].ID); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the snapshot.
|
|
|
|
if idx := snap.LastIndex(); idx != 5 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
expected := structs.PreparedQueries{
|
|
|
|
&structs.PreparedQuery{
|
|
|
|
ID: queries[0].ID,
|
|
|
|
Name: "alice",
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "redis",
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 4,
|
|
|
|
ModifyIndex: 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.PreparedQuery{
|
|
|
|
ID: queries[1].ID,
|
2016-03-01 20:43:24 +00:00
|
|
|
Name: "bob-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
2015-11-07 00:59:32 +00:00
|
|
|
Service: structs.ServiceQuery{
|
2016-03-01 20:43:24 +00:00
|
|
|
Service: "${name.suffix}",
|
2015-11-07 00:59:32 +00:00
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 5,
|
|
|
|
ModifyIndex: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-02-26 08:25:44 +00:00
|
|
|
dump, err := snap.PreparedQueries()
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(dump, expected) {
|
|
|
|
t.Fatalf("bad: %v", dump)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the values into a new state store.
|
|
|
|
func() {
|
|
|
|
s := testStateStore(t)
|
|
|
|
restore := s.Restore()
|
|
|
|
for _, query := range dump {
|
2015-11-10 04:37:41 +00:00
|
|
|
if err := restore.PreparedQuery(query); err != nil {
|
2015-11-07 00:59:32 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
restore.Commit()
|
|
|
|
|
|
|
|
// Read the restored queries back out and verify that they
|
|
|
|
// match.
|
2017-01-24 17:22:07 +00:00
|
|
|
idx, actual, err := s.PreparedQueryList(nil)
|
2015-11-07 00:59:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 5 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
2016-03-01 20:43:24 +00:00
|
|
|
|
|
|
|
// Make sure the second query, which is a template, was compiled
|
|
|
|
// and can be resolved.
|
2017-08-30 00:02:50 +00:00
|
|
|
_, query, err := s.PreparedQueryResolve("bob-backwards-is-bob", structs.QuerySource{})
|
2016-03-01 20:43:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if query == nil {
|
|
|
|
t.Fatalf("should have resolved the query")
|
|
|
|
}
|
|
|
|
if query.Service.Service != "backwards-is-bob" {
|
|
|
|
t.Fatalf("bad: %s", query.Service.Service)
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
}()
|
|
|
|
}
|