open-consul/agent/consul/prepared_query/template_test.go
Frank Schroeder cd837b0b18 pkg refactor
command/agent/*                  -> agent/*
    command/consul/*                 -> agent/consul/*
    command/agent/command{,_test}.go -> command/agent{,_test}.go
    command/base/command.go          -> command/base.go
    command/base/*                   -> command/*
    commands.go                      -> command/commands.go

The script which did the refactor is:

(
	cd $GOPATH/src/github.com/hashicorp/consul
	git mv command/agent/command.go command/agent.go
	git mv command/agent/command_test.go command/agent_test.go
	git mv command/agent/flag_slice_value{,_test}.go command/
	git mv command/agent .
	git mv command/base/command.go command/base.go
	git mv command/base/config_util{,_test}.go command/
	git mv commands.go command/
	git mv consul agent
	rmdir command/base/

	gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
	gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
	gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
	gsed -i -e 's|package main|package command|' command/commands.go

	gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
	gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
	gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
	gsed -i -e 's|base\.||' command/commands.go
	gsed -i -e 's|command\.||' command/commands.go

	gsed -i -e 's|command|c|' main.go
	gsed -i -e 's|range Commands|range command.Commands|' main.go
	gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go

	gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
	gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
	gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
	gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go

	gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
	gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
	gsed -i -e 's|base.Command|BaseCommand|' command/*.go
	gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
	gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
	gsed -i -e 's|base\.||' command/*_test.go

	gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
	gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go

	gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
	gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
	gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
	gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
	gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
	gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go

	gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go

	gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
	gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
	gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
	gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
	gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go

	gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
	gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
	gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
	gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
	gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
	gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
	gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
	gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
	gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
	gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go

	gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
	gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
	gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
	gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
	gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go

	gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go

	gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile

	gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go

	# fix imports
	f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
	gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
	goimports -w $f

	f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
	gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
	goimports -w $f

	goimports -w command/*.go main.go
)
2017-06-10 18:52:45 +02:00

300 lines
6.7 KiB
Go

package prepared_query
import (
"reflect"
"strings"
"testing"
"github.com/hashicorp/consul/agent/consul/structs"
"github.com/mitchellh/copystructure"
)
var (
// 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{
Name: "hello",
Template: structs.QueryTemplateOptions{
Type: structs.QueryTemplateTypeNamePrefixMatch,
Regexp: "^hello-(.*)-(.*)$",
},
Service: structs.ServiceQuery{
Service: "${name.full}",
Failover: structs.QueryDatacenterOptions{
Datacenters: []string{
"${name.full}",
"${name.prefix}",
"${name.suffix}",
"${match(0)}",
"${match(1)}",
"${match(2)}",
},
},
Tags: []string{
"${name.full}",
"${name.prefix}",
"${name.suffix}",
"${match(0)}",
"${match(1)}",
"${match(2)}",
},
NodeMeta: map[string]string{
"foo": "${name.prefix}",
"bar": "${match(0)}",
"baz": "${match(1)}",
},
},
}
// 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}",
Failover: structs.QueryDatacenterOptions{
Datacenters: []string{
"dc1",
"dc2",
"dc3",
},
},
},
}
)
func compileBench(b *testing.B, query *structs.PreparedQuery) {
for i := 0; i < b.N; i++ {
_, err := Compile(query)
if err != nil {
b.Fatalf("err: %v", err)
}
}
}
func renderBench(b *testing.B, query *structs.PreparedQuery) {
compiled, err := Compile(query)
if err != nil {
b.Fatalf("err: %v", err)
}
for i := 0; i < b.N; i++ {
_, err := compiled.Render("hello-bench-mark")
if err != nil {
b.Fatalf("err: %v", err)
}
}
}
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)
}
func TestTemplate_Compile(t *testing.T) {
// 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}"
query.Service.Tags = []string{"${match(1)}"}
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.
actual, err := ct.Render("hellothere")
if err != nil {
t.Fatalf("err: %v", err)
}
// See if it rendered correctly.
expected := &structs.PreparedQuery{
Template: structs.QueryTemplateOptions{
Type: structs.QueryTemplateTypeNamePrefixMatch,
Regexp: "^(hello)there$",
},
Service: structs.ServiceQuery{
Service: "hellothere",
Tags: []string{
"hello",
},
},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
// 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)
}
actual, err := ct.Render("unused")
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(actual, query) {
t.Fatalf("bad: %#v", actual)
}
}
// Try all the variables and functions.
query := &structs.PreparedQuery{
Name: "hello-",
Template: structs.QueryTemplateOptions{
Type: structs.QueryTemplateTypeNamePrefixMatch,
Regexp: "^(.*?)-(.*?)-(.*)$",
},
Service: structs.ServiceQuery{
Service: "${name.prefix} xxx ${name.full} xxx ${name.suffix}",
Tags: []string{
"${match(-1)}",
"${match(0)}",
"${match(1)}",
"${match(2)}",
"${match(3)}",
"${match(4)}",
"${40 + 2}",
},
NodeMeta: map[string]string{"foo": "${match(1)}"},
},
}
ct, err := Compile(query)
if err != nil {
t.Fatalf("err: %v", err)
}
// Run a case that matches the regexp.
{
actual, err := ct.Render("hello-foo-bar-none")
if err != nil {
t.Fatalf("err: %v", err)
}
expected := &structs.PreparedQuery{
Name: "hello-",
Template: structs.QueryTemplateOptions{
Type: structs.QueryTemplateTypeNamePrefixMatch,
Regexp: "^(.*?)-(.*?)-(.*)$",
},
Service: structs.ServiceQuery{
Service: "hello- xxx hello-foo-bar-none xxx foo-bar-none",
Tags: []string{
"",
"hello-foo-bar-none",
"hello",
"foo",
"bar-none",
"",
"42",
},
NodeMeta: map[string]string{"foo": "hello"},
},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
// Run a case that doesn't match the regexp
{
actual, err := ct.Render("hello-nope")
if err != nil {
t.Fatalf("err: %v", err)
}
expected := &structs.PreparedQuery{
Name: "hello-",
Template: structs.QueryTemplateOptions{
Type: structs.QueryTemplateTypeNamePrefixMatch,
Regexp: "^(.*?)-(.*?)-(.*)$",
},
Service: structs.ServiceQuery{
Service: "hello- xxx hello-nope xxx nope",
Tags: []string{
"",
"",
"",
"",
"",
"",
"42",
},
NodeMeta: map[string]string{"foo": ""},
},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
}