2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-02-26 08:25:19 +00:00
|
|
|
package prepared_query
|
|
|
|
|
|
|
|
import (
|
2016-02-27 04:03:15 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2016-02-26 08:25:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-04-10 12:28:27 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2016-02-27 04:03:15 +00:00
|
|
|
"github.com/mitchellh/copystructure"
|
2016-02-26 08:25:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-02-27 04:03:15 +00:00
|
|
|
// bigBench is a test query that uses all the features of templates, not
|
|
|
|
// in a realistic way, but in a complete way.
|
|
|
|
bigBench = &structs.PreparedQuery{
|
2016-02-26 08:25:19 +00:00
|
|
|
Name: "hello",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
2017-07-27 05:46:43 +00:00
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
Regexp: "^hello-(.*)-(.*)$",
|
|
|
|
RemoveEmptyTags: true,
|
2016-02-26 08:25:19 +00:00
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "${name.full}",
|
2022-07-22 13:14:43 +00:00
|
|
|
Failover: structs.QueryFailoverOptions{
|
2016-02-27 04:03:15 +00:00
|
|
|
Datacenters: []string{
|
|
|
|
"${name.full}",
|
|
|
|
"${name.prefix}",
|
|
|
|
"${name.suffix}",
|
|
|
|
"${match(0)}",
|
|
|
|
"${match(1)}",
|
|
|
|
"${match(2)}",
|
2017-08-30 00:02:50 +00:00
|
|
|
"${agent.segment}",
|
2016-02-27 04:03:15 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-10 12:28:27 +00:00
|
|
|
IgnoreCheckIDs: []types.CheckID{
|
|
|
|
"${name.full}",
|
|
|
|
"${name.prefix}",
|
|
|
|
"${name.suffix}",
|
|
|
|
"${match(0)}",
|
|
|
|
"${match(1)}",
|
|
|
|
"${match(2)}",
|
|
|
|
"${agent.segment}",
|
|
|
|
},
|
2016-02-27 04:03:15 +00:00
|
|
|
Tags: []string{
|
|
|
|
"${name.full}",
|
|
|
|
"${name.prefix}",
|
|
|
|
"${name.suffix}",
|
|
|
|
"${match(0)}",
|
|
|
|
"${match(1)}",
|
|
|
|
"${match(2)}",
|
2017-08-30 00:02:50 +00:00
|
|
|
"${agent.segment}",
|
2016-02-27 04:03:15 +00:00
|
|
|
},
|
2017-01-18 21:23:33 +00:00
|
|
|
NodeMeta: map[string]string{
|
2017-01-23 23:53:45 +00:00
|
|
|
"foo": "${name.prefix}",
|
|
|
|
"bar": "${match(0)}",
|
|
|
|
"baz": "${match(1)}",
|
2017-08-30 00:02:50 +00:00
|
|
|
"zoo": "${agent.segment}",
|
2017-01-18 21:23:33 +00:00
|
|
|
},
|
2016-02-27 04:03:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// smallBench is a small prepared query just for doing geo failover. This
|
|
|
|
// is a minimal, useful configuration.
|
|
|
|
smallBench = &structs.PreparedQuery{
|
|
|
|
Name: "",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "${name.full}",
|
2022-07-22 13:14:43 +00:00
|
|
|
Failover: structs.QueryFailoverOptions{
|
2016-02-27 04:03:15 +00:00
|
|
|
Datacenters: []string{
|
|
|
|
"dc1",
|
|
|
|
"dc2",
|
|
|
|
"dc3",
|
|
|
|
},
|
|
|
|
},
|
2016-02-26 08:25:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-02-27 04:03:15 +00:00
|
|
|
func compileBench(b *testing.B, query *structs.PreparedQuery) {
|
2016-02-26 08:25:19 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-02-27 04:03:15 +00:00
|
|
|
_, err := Compile(query)
|
2016-02-26 08:25:19 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:03:15 +00:00
|
|
|
func renderBench(b *testing.B, query *structs.PreparedQuery) {
|
|
|
|
compiled, err := Compile(query)
|
2016-02-26 08:25:19 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-08-30 00:02:50 +00:00
|
|
|
_, err := compiled.Render("hello-bench-mark", structs.QuerySource{})
|
2016-02-26 08:25:19 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:03:15 +00:00
|
|
|
func BenchmarkTemplate_CompileSmall(b *testing.B) {
|
|
|
|
compileBench(b, smallBench)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTemplate_CompileBig(b *testing.B) {
|
|
|
|
compileBench(b, bigBench)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTemplate_RenderSmall(b *testing.B) {
|
|
|
|
renderBench(b, smallBench)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTemplate_RenderBig(b *testing.B) {
|
|
|
|
renderBench(b, bigBench)
|
|
|
|
}
|
|
|
|
|
2016-02-26 08:25:19 +00:00
|
|
|
func TestTemplate_Compile(t *testing.T) {
|
2016-02-27 04:03:15 +00:00
|
|
|
// Start with an empty query that's not even a template.
|
|
|
|
query := &structs.PreparedQuery{}
|
|
|
|
_, err := Compile(query)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Bad Template") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if IsTemplate(query) {
|
|
|
|
t.Fatalf("should not be a template")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make it a basic template, keeping a copy before we compile.
|
|
|
|
query.Template.Type = structs.QueryTemplateTypeNamePrefixMatch
|
|
|
|
query.Template.Regexp = "^(hello)there$"
|
|
|
|
query.Service.Service = "${name.full}"
|
2018-04-10 12:28:27 +00:00
|
|
|
query.Service.IgnoreCheckIDs = []types.CheckID{"${match(1)}", "${agent.segment}"}
|
2017-08-30 00:02:50 +00:00
|
|
|
query.Service.Tags = []string{"${match(1)}", "${agent.segment}"}
|
2016-02-27 04:03:15 +00:00
|
|
|
backup, err := copystructure.Copy(query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
ct, err := Compile(query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !IsTemplate(query) {
|
|
|
|
t.Fatalf("should be a template")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a sanity check render on it.
|
2017-08-30 00:02:50 +00:00
|
|
|
actual, err := ct.Render("hellothere", structs.QuerySource{Segment: "segment-foo"})
|
2016-02-27 04:03:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if it rendered correctly.
|
|
|
|
expected := &structs.PreparedQuery{
|
2016-02-26 08:25:19 +00:00
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
2016-02-27 04:03:15 +00:00
|
|
|
Regexp: "^(hello)there$",
|
2016-02-26 08:25:19 +00:00
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
2016-02-27 04:03:15 +00:00
|
|
|
Service: "hellothere",
|
2018-04-10 12:28:27 +00:00
|
|
|
IgnoreCheckIDs: []types.CheckID{
|
|
|
|
"hello",
|
|
|
|
"segment-foo",
|
|
|
|
},
|
2016-02-27 04:03:15 +00:00
|
|
|
Tags: []string{
|
|
|
|
"hello",
|
2017-08-30 00:02:50 +00:00
|
|
|
"segment-foo",
|
2016-02-27 04:03:15 +00:00
|
|
|
},
|
2016-02-26 08:25:19 +00:00
|
|
|
},
|
|
|
|
}
|
2016-02-27 04:03:15 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
2016-02-26 08:25:19 +00:00
|
|
|
|
2016-02-27 04:03:15 +00:00
|
|
|
// Prove that it didn't alter the definition we compiled.
|
|
|
|
if !reflect.DeepEqual(query, backup.(*structs.PreparedQuery)) {
|
|
|
|
t.Fatalf("bad: %#v", query)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try a bad HIL interpolation (syntax error).
|
|
|
|
query.Service.Service = "${name.full"
|
|
|
|
_, err = Compile(query)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Bad format") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try a bad HIL interpolation (syntax ok but unknown variable).
|
|
|
|
query.Service.Service = "${name.nope}"
|
|
|
|
_, err = Compile(query)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "unknown variable") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try a bad regexp.
|
|
|
|
query.Template.Regexp = "^(nope$"
|
|
|
|
query.Service.Service = "${name.full}"
|
|
|
|
_, err = Compile(query)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Bad Regexp") {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplate_Render(t *testing.T) {
|
|
|
|
// Try a noop template that is all static.
|
|
|
|
{
|
|
|
|
query := &structs.PreparedQuery{
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "hellothere",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ct, err := Compile(query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-30 00:02:50 +00:00
|
|
|
actual, err := ct.Render("unused", structs.QuerySource{})
|
2016-02-27 04:03:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, query) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
2016-02-26 08:25:19 +00:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:03:15 +00:00
|
|
|
// Try all the variables and functions.
|
|
|
|
query := &structs.PreparedQuery{
|
|
|
|
Name: "hello-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
Regexp: "^(.*?)-(.*?)-(.*)$",
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
2017-08-30 00:02:50 +00:00
|
|
|
Service: "${name.prefix} xxx ${name.full} xxx ${name.suffix} xxx ${agent.segment}",
|
2016-02-27 04:03:15 +00:00
|
|
|
Tags: []string{
|
2016-03-07 21:32:32 +00:00
|
|
|
"${match(-1)}",
|
2016-02-27 04:03:15 +00:00
|
|
|
"${match(0)}",
|
|
|
|
"${match(1)}",
|
|
|
|
"${match(2)}",
|
|
|
|
"${match(3)}",
|
|
|
|
"${match(4)}",
|
|
|
|
"${40 + 2}",
|
|
|
|
},
|
2017-01-23 23:53:45 +00:00
|
|
|
NodeMeta: map[string]string{"foo": "${match(1)}"},
|
2016-02-27 04:03:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
ct, err := Compile(query)
|
2016-02-26 08:25:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:03:15 +00:00
|
|
|
// Run a case that matches the regexp.
|
|
|
|
{
|
2017-08-30 00:02:50 +00:00
|
|
|
actual, err := ct.Render("hello-foo-bar-none", structs.QuerySource{Segment: "segment-bar"})
|
2016-02-27 04:03:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected := &structs.PreparedQuery{
|
|
|
|
Name: "hello-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
Regexp: "^(.*?)-(.*?)-(.*)$",
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
2017-08-30 00:02:50 +00:00
|
|
|
Service: "hello- xxx hello-foo-bar-none xxx foo-bar-none xxx segment-bar",
|
2016-02-27 04:03:15 +00:00
|
|
|
Tags: []string{
|
2016-03-07 21:32:32 +00:00
|
|
|
"",
|
2016-02-27 04:03:15 +00:00
|
|
|
"hello-foo-bar-none",
|
|
|
|
"hello",
|
|
|
|
"foo",
|
|
|
|
"bar-none",
|
|
|
|
"",
|
|
|
|
"42",
|
|
|
|
},
|
2017-01-23 23:53:45 +00:00
|
|
|
NodeMeta: map[string]string{"foo": "hello"},
|
2016-02-27 04:03:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a case that doesn't match the regexp
|
|
|
|
{
|
2017-08-30 00:02:50 +00:00
|
|
|
actual, err := ct.Render("hello-nope", structs.QuerySource{Segment: "segment-bar"})
|
2016-02-27 04:03:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected := &structs.PreparedQuery{
|
|
|
|
Name: "hello-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
Regexp: "^(.*?)-(.*?)-(.*)$",
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
2017-08-30 00:02:50 +00:00
|
|
|
Service: "hello- xxx hello-nope xxx nope xxx segment-bar",
|
2016-02-27 04:03:15 +00:00
|
|
|
Tags: []string{
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2016-03-07 21:32:32 +00:00
|
|
|
"",
|
2016-02-27 04:03:15 +00:00
|
|
|
"42",
|
|
|
|
},
|
2017-01-23 23:53:45 +00:00
|
|
|
NodeMeta: map[string]string{"foo": ""},
|
2016-02-27 04:03:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
2017-07-27 05:46:43 +00:00
|
|
|
|
|
|
|
// Try all the variables and functions, removing empty tags.
|
|
|
|
query = &structs.PreparedQuery{
|
|
|
|
Name: "hello-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
Regexp: "^(.*?)-(.*?)-(.*)$",
|
|
|
|
RemoveEmptyTags: true,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
2017-08-30 00:02:50 +00:00
|
|
|
Service: "${name.prefix} xxx ${name.full} xxx ${name.suffix} xxx ${agent.segment}",
|
2017-07-27 05:46:43 +00:00
|
|
|
Tags: []string{
|
|
|
|
"${match(-1)}",
|
|
|
|
"${match(0)}",
|
|
|
|
"${match(1)}",
|
|
|
|
"${match(2)}",
|
|
|
|
"${match(3)}",
|
|
|
|
"${match(4)}",
|
|
|
|
"${40 + 2}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ct, err = Compile(query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a case that matches the regexp, removing empty tags.
|
|
|
|
{
|
2017-08-30 00:02:50 +00:00
|
|
|
actual, err := ct.Render("hello-foo-bar-none", structs.QuerySource{Segment: "segment-baz"})
|
2017-07-27 05:46:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected := &structs.PreparedQuery{
|
|
|
|
Name: "hello-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
Regexp: "^(.*?)-(.*?)-(.*)$",
|
|
|
|
RemoveEmptyTags: true,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
2017-08-30 00:02:50 +00:00
|
|
|
Service: "hello- xxx hello-foo-bar-none xxx foo-bar-none xxx segment-baz",
|
2017-07-27 05:46:43 +00:00
|
|
|
Tags: []string{
|
|
|
|
"hello-foo-bar-none",
|
|
|
|
"hello",
|
|
|
|
"foo",
|
|
|
|
"bar-none",
|
|
|
|
"42",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad:\n%#v\nexpected:\n%#v\n", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a case that doesn't match the regexp, removing empty tags.
|
|
|
|
{
|
2017-08-30 00:02:50 +00:00
|
|
|
actual, err := ct.Render("hello-nope", structs.QuerySource{Segment: "segment-baz"})
|
2017-07-27 05:46:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected := &structs.PreparedQuery{
|
|
|
|
Name: "hello-",
|
|
|
|
Template: structs.QueryTemplateOptions{
|
|
|
|
Type: structs.QueryTemplateTypeNamePrefixMatch,
|
|
|
|
Regexp: "^(.*?)-(.*?)-(.*)$",
|
|
|
|
RemoveEmptyTags: true,
|
|
|
|
},
|
|
|
|
Service: structs.ServiceQuery{
|
2017-08-30 00:02:50 +00:00
|
|
|
Service: "hello- xxx hello-nope xxx nope xxx segment-baz",
|
2017-07-27 05:46:43 +00:00
|
|
|
Tags: []string{
|
|
|
|
"42",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 08:25:19 +00:00
|
|
|
}
|