|
|
|
@ -32,6 +32,7 @@ func TestPreparedQuery_Apply(t *testing.T) {
|
|
|
|
|
Datacenter: "dc1", |
|
|
|
|
Op: structs.PreparedQueryCreate, |
|
|
|
|
Query: &structs.PreparedQuery{ |
|
|
|
|
Name: "test", |
|
|
|
|
Service: structs.ServiceQuery{ |
|
|
|
|
Service: "redis", |
|
|
|
|
}, |
|
|
|
@ -515,6 +516,7 @@ func TestPreparedQuery_Apply_ForwardLeader(t *testing.T) {
|
|
|
|
|
Datacenter: "dc1", |
|
|
|
|
Op: structs.PreparedQueryCreate, |
|
|
|
|
Query: &structs.PreparedQuery{ |
|
|
|
|
Name: "test", |
|
|
|
|
Service: structs.ServiceQuery{ |
|
|
|
|
Service: "redis", |
|
|
|
|
}, |
|
|
|
@ -531,53 +533,77 @@ func TestPreparedQuery_Apply_ForwardLeader(t *testing.T) {
|
|
|
|
|
func TestPreparedQuery_parseQuery(t *testing.T) { |
|
|
|
|
query := &structs.PreparedQuery{} |
|
|
|
|
|
|
|
|
|
err := parseQuery(query) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Must provide a Service") { |
|
|
|
|
err := parseQuery(query, true) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Must be bound to a session") { |
|
|
|
|
t.Fatalf("bad: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.Service.Service = "foo" |
|
|
|
|
if err := parseQuery(query); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.Token = redactedToken |
|
|
|
|
err = parseQuery(query) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Bad Token") { |
|
|
|
|
query.Session = "adf4238a-882b-9ddc-4a9d-5b6758e4159e" |
|
|
|
|
err = parseQuery(query, true) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Must provide a Service") { |
|
|
|
|
t.Fatalf("bad: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.Token = "adf4238a-882b-9ddc-4a9d-5b6758e4159e" |
|
|
|
|
if err := parseQuery(query); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
query.Session = "" |
|
|
|
|
query.Template.Type = "some-kind-of-template" |
|
|
|
|
err = parseQuery(query, true) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Must provide a Service") { |
|
|
|
|
t.Fatalf("bad: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.Service.Failover.NearestN = -1 |
|
|
|
|
err = parseQuery(query) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Bad NearestN") { |
|
|
|
|
query.Template.Type = "" |
|
|
|
|
err = parseQuery(query, false) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Must provide a Service") { |
|
|
|
|
t.Fatalf("bad: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.Service.Failover.NearestN = 3 |
|
|
|
|
if err := parseQuery(query); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
} |
|
|
|
|
// None of the rest of these care about version 8 ACL enforcement.
|
|
|
|
|
for _, version8 := range []bool{true, false} { |
|
|
|
|
query = &structs.PreparedQuery{} |
|
|
|
|
query.Session = "adf4238a-882b-9ddc-4a9d-5b6758e4159e" |
|
|
|
|
query.Service.Service = "foo" |
|
|
|
|
if err := parseQuery(query, version8); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.DNS.TTL = "two fortnights" |
|
|
|
|
err = parseQuery(query) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Bad DNS TTL") { |
|
|
|
|
t.Fatalf("bad: %v", err) |
|
|
|
|
} |
|
|
|
|
query.Token = redactedToken |
|
|
|
|
err = parseQuery(query, version8) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Bad Token") { |
|
|
|
|
t.Fatalf("bad: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.DNS.TTL = "-3s" |
|
|
|
|
err = parseQuery(query) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "must be >=0") { |
|
|
|
|
t.Fatalf("bad: %v", err) |
|
|
|
|
} |
|
|
|
|
query.Token = "adf4238a-882b-9ddc-4a9d-5b6758e4159e" |
|
|
|
|
if err := parseQuery(query, version8); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.DNS.TTL = "3s" |
|
|
|
|
if err := parseQuery(query); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
query.Service.Failover.NearestN = -1 |
|
|
|
|
err = parseQuery(query, version8) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Bad NearestN") { |
|
|
|
|
t.Fatalf("bad: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.Service.Failover.NearestN = 3 |
|
|
|
|
if err := parseQuery(query, version8); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.DNS.TTL = "two fortnights" |
|
|
|
|
err = parseQuery(query, version8) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Bad DNS TTL") { |
|
|
|
|
t.Fatalf("bad: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.DNS.TTL = "-3s" |
|
|
|
|
err = parseQuery(query, version8) |
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "must be >=0") { |
|
|
|
|
t.Fatalf("bad: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query.DNS.TTL = "3s" |
|
|
|
|
if err := parseQuery(query, version8); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -917,9 +943,25 @@ func TestPreparedQuery_Get(t *testing.T) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a session.
|
|
|
|
|
var session string |
|
|
|
|
{ |
|
|
|
|
req := structs.SessionRequest{ |
|
|
|
|
Datacenter: "dc1", |
|
|
|
|
Op: structs.SessionCreate, |
|
|
|
|
Session: structs.Session{ |
|
|
|
|
Node: s1.config.NodeName, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &req, &session); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Now update the query to take away its name.
|
|
|
|
|
query.Op = structs.PreparedQueryUpdate |
|
|
|
|
query.Query.Name = "" |
|
|
|
|
query.Query.Session = session |
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "PreparedQuery.Apply", &query, &reply); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
} |
|
|
|
@ -1170,9 +1212,25 @@ func TestPreparedQuery_List(t *testing.T) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a session.
|
|
|
|
|
var session string |
|
|
|
|
{ |
|
|
|
|
req := structs.SessionRequest{ |
|
|
|
|
Datacenter: "dc1", |
|
|
|
|
Op: structs.SessionCreate, |
|
|
|
|
Session: structs.Session{ |
|
|
|
|
Node: s1.config.NodeName, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &req, &session); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Now take away the query name.
|
|
|
|
|
query.Op = structs.PreparedQueryUpdate |
|
|
|
|
query.Query.Name = "" |
|
|
|
|
query.Query.Session = session |
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "PreparedQuery.Apply", &query, &reply); err != nil { |
|
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
} |
|
|
|
@ -1451,6 +1509,7 @@ func TestPreparedQuery_Execute(t *testing.T) {
|
|
|
|
|
Datacenter: "dc1", |
|
|
|
|
Op: structs.PreparedQueryCreate, |
|
|
|
|
Query: &structs.PreparedQuery{ |
|
|
|
|
Name: "test", |
|
|
|
|
Service: structs.ServiceQuery{ |
|
|
|
|
Service: "foo", |
|
|
|
|
}, |
|
|
|
@ -2314,6 +2373,7 @@ func TestPreparedQuery_Execute_ForwardLeader(t *testing.T) {
|
|
|
|
|
Datacenter: "dc1", |
|
|
|
|
Op: structs.PreparedQueryCreate, |
|
|
|
|
Query: &structs.PreparedQuery{ |
|
|
|
|
Name: "test", |
|
|
|
|
Service: structs.ServiceQuery{ |
|
|
|
|
Service: "redis", |
|
|
|
|
}, |
|
|
|
@ -2577,6 +2637,7 @@ func (m *mockQueryServer) ForwardDC(method, dc string, args interface{}, reply i
|
|
|
|
|
|
|
|
|
|
func TestPreparedQuery_queryFailover(t *testing.T) { |
|
|
|
|
query := &structs.PreparedQuery{ |
|
|
|
|
Name: "test", |
|
|
|
|
Service: structs.ServiceQuery{ |
|
|
|
|
Failover: structs.QueryDatacenterOptions{ |
|
|
|
|
NearestN: 0, |
|
|
|
|