Skip to content

Commit

Permalink
Add tests for Config.DataTable processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed Apr 23, 2021
1 parent c41927b commit 6bf90a3
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 54 deletions.
118 changes: 64 additions & 54 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,6 @@ func ReadConfig(filename string) (*Config, error) {
}
}

if cfg.ClickHouse.DataTableLegacy != "" {
cfg.DataTable = append(cfg.DataTable, DataTable{
Table: cfg.ClickHouse.DataTableLegacy,
RollupConf: cfg.ClickHouse.RollupConfLegacy,
})
}

err = IndexUseReversesValidate(cfg.ClickHouse.IndexUseReverses)
if err != nil {
return nil, err
Expand All @@ -373,53 +366,9 @@ func ReadConfig(filename string) (*Config, error) {
}
}

for i := 0; i < len(cfg.DataTable); i++ {
if cfg.DataTable[i].TargetMatchAny != "" {
r, err := regexp.Compile(cfg.DataTable[i].TargetMatchAny)
if err != nil {
return nil, err
}
cfg.DataTable[i].TargetMatchAnyRegexp = r
}

if cfg.DataTable[i].TargetMatchAll != "" {
r, err := regexp.Compile(cfg.DataTable[i].TargetMatchAll)
if err != nil {
return nil, err
}
cfg.DataTable[i].TargetMatchAllRegexp = r
}

rdp := cfg.DataTable[i].RollupDefaultPrecision
rdf := cfg.DataTable[i].RollupDefaultFunction
if cfg.DataTable[i].RollupConf == "auto" || cfg.DataTable[i].RollupConf == "" {
table := cfg.DataTable[i].Table
if cfg.DataTable[i].RollupAutoTable != "" {
table = cfg.DataTable[i].RollupAutoTable
}

cfg.DataTable[i].Rollup, err = rollup.NewAuto(cfg.ClickHouse.Url, table, time.Minute, rdp, rdf)
} else if cfg.DataTable[i].RollupConf == "none" {
cfg.DataTable[i].Rollup, err = rollup.NewDefault(rdp, rdf)
} else {
cfg.DataTable[i].Rollup, err = rollup.NewXMLFile(cfg.DataTable[i].RollupConf, rdp, rdf)
}

if err != nil {
return nil, err
}

if len(cfg.DataTable[i].Context) == 0 {
cfg.DataTable[i].ContextMap = knownDataTableContext
} else {
cfg.DataTable[i].ContextMap = make(map[string]bool)
for _, c := range cfg.DataTable[i].Context {
if !knownDataTableContext[c] {
return nil, fmt.Errorf("unknown context %#v", c)
}
cfg.DataTable[i].ContextMap[c] = true
}
}
err = cfg.ProcessDataTables()
if err != nil {
return nil, err
}

// compute prometheus external url
Expand All @@ -443,3 +392,64 @@ func ReadConfig(filename string) (*Config, error) {

return cfg, nil
}

// ProcessDataTables checks if legacy `data`-table config is used, compiles regexps for `target-match-any` and `target-match-all`
// parameters, sets the rollup configuration and proper context.
func (c *Config) ProcessDataTables() (err error) {
if c.ClickHouse.DataTableLegacy != "" {
c.DataTable = append(c.DataTable, DataTable{
Table: c.ClickHouse.DataTableLegacy,
RollupConf: c.ClickHouse.RollupConfLegacy,
})
}

for i := 0; i < len(c.DataTable); i++ {
if c.DataTable[i].TargetMatchAny != "" {
r, err := regexp.Compile(c.DataTable[i].TargetMatchAny)
if err != nil {
return err
}
c.DataTable[i].TargetMatchAnyRegexp = r
}

if c.DataTable[i].TargetMatchAll != "" {
r, err := regexp.Compile(c.DataTable[i].TargetMatchAll)
if err != nil {
return err
}
c.DataTable[i].TargetMatchAllRegexp = r
}

rdp := c.DataTable[i].RollupDefaultPrecision
rdf := c.DataTable[i].RollupDefaultFunction
if c.DataTable[i].RollupConf == "auto" || c.DataTable[i].RollupConf == "" {
table := c.DataTable[i].Table
if c.DataTable[i].RollupAutoTable != "" {
table = c.DataTable[i].RollupAutoTable
}

c.DataTable[i].Rollup, err = rollup.NewAuto(c.ClickHouse.Url, table, time.Minute, rdp, rdf)
} else if c.DataTable[i].RollupConf == "none" {
c.DataTable[i].Rollup, err = rollup.NewDefault(rdp, rdf)
} else {
c.DataTable[i].Rollup, err = rollup.NewXMLFile(c.DataTable[i].RollupConf, rdp, rdf)
}

if err != nil {
return err
}

if len(c.DataTable[i].Context) == 0 {
c.DataTable[i].ContextMap = knownDataTableContext
} else {
c.DataTable[i].ContextMap = make(map[string]bool)
for _, ctx := range c.DataTable[i].Context {
if !knownDataTableContext[ctx] {
return fmt.Errorf("unknown context %#v", ctx)
}
c.DataTable[i].ContextMap[ctx] = true
}
}
}
return nil
}
215 changes: 215 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package config

import (
"fmt"
"io/fs"
"regexp"
"regexp/syntax"
"syscall"
"testing"

"github.com/stretchr/testify/assert"
)

func TestProcessDataTables(t *testing.T) {
type in struct {
table DataTable
tableLegacy string
}
type out struct {
tables []DataTable
err error
}
type ctx map[string]bool

regexpCompileWrapper := func(re string) *regexp.Regexp {
r, _ := regexp.Compile(re)
return r
}

tests := []struct {
name string
in in
out out
}{
{
name: "legacy table only",
in: in{
tableLegacy: "graphite.data",
},
out: out{
[]DataTable{
{
Table: "graphite.data",
RollupConf: "auto",
ContextMap: ctx{"graphite": true, "prometheus": true},
},
},
nil,
},
},
{
name: "legacy and normal tables",
in: in{
table: DataTable{Table: "graphite.new_data"},
tableLegacy: "graphite.data",
},
out: out{
[]DataTable{
{
Table: "graphite.new_data",
ContextMap: ctx{"graphite": true, "prometheus": true},
},
{
Table: "graphite.data",
RollupConf: "auto",
ContextMap: ctx{"graphite": true, "prometheus": true},
},
},
nil,
},
},
{
name: "fail to compile TargetMatchAll",
in: in{
table: DataTable{Table: "graphite.data", TargetMatchAll: "[2223"},
},
out: out{
[]DataTable{{Table: "graphite.data", TargetMatchAll: "[2223"}},
&syntax.Error{Code: syntax.ErrMissingBracket, Expr: "[2223"},
},
},
{
name: "fail to compile TargetMatchAny",
in: in{
table: DataTable{Table: "graphite.data", TargetMatchAny: "[2223"},
},
out: out{
[]DataTable{{Table: "graphite.data", TargetMatchAny: "[2223"}},
&syntax.Error{Code: syntax.ErrMissingBracket, Expr: "[2223"},
},
},
{
name: "fail to compile TargetMatchAny",
in: in{
table: DataTable{Table: "graphite.data", TargetMatchAny: "[2223"},
},
out: out{
[]DataTable{{Table: "graphite.data", TargetMatchAny: "[2223"}},
&syntax.Error{Code: syntax.ErrMissingBracket, Expr: "[2223"},
},
},
{
name: "fail to read xml rollup",
in: in{
table: DataTable{Table: "graphite.data", RollupConf: "/some/file/that/does/not/hopefully/exists/on/the/disk"},
},
out: out{
[]DataTable{{Table: "graphite.data", RollupConf: "/some/file/that/does/not/hopefully/exists/on/the/disk"}},
&fs.PathError{Op: "open", Path: "/some/file/that/does/not/hopefully/exists/on/the/disk", Err: syscall.ENOENT},
},
},
{
name: "unknown context",
in: in{
table: DataTable{Table: "graphite.data", Context: []string{"unexpected"}},
},
out: out{
[]DataTable{
{
Table: "graphite.data",
Context: []string{"unexpected"},
ContextMap: ctx{},
},
},
fmt.Errorf("unknown context \"unexpected\""),
},
},
{
name: "check all works",
in: in{
table: DataTable{
Table: "graphite.data",
Reverse: true,
TargetMatchAll: "^.*[asdf][.].*",
TargetMatchAny: "^.*{a|s|d|f}[.].*",
RollupConf: "none",
RollupDefaultFunction: "any",
RollupDefaultPrecision: 61,
RollupUseReverted: true,
Context: []string{"prometheus"},
},
tableLegacy: "table",
},
out: out{
[]DataTable{
{
Table: "graphite.data",
Reverse: true,
TargetMatchAll: "^.*[asdf][.].*",
TargetMatchAny: "^.*{a|s|d|f}[.].*",
TargetMatchAllRegexp: regexpCompileWrapper("^.*[asdf][.].*"),
TargetMatchAnyRegexp: regexpCompileWrapper("^.*{a|s|d|f}[.].*"),
RollupConf: "none",
RollupDefaultFunction: "any",
RollupDefaultPrecision: 61,
RollupUseReverted: true,
Context: []string{"prometheus"},
ContextMap: ctx{"prometheus": true},
},
{
Table: "table",
RollupConf: "auto",
ContextMap: ctx{"graphite": true, "prometheus": true},
},
},
nil,
},
},
{
name: "unknown context",
in: in{
table: DataTable{Table: "graphite.data", Context: []string{"unexpected"}},
},
out: out{
[]DataTable{
{
Table: "graphite.data",
Context: []string{"unexpected"},
ContextMap: ctx{},
},
},
fmt.Errorf("unknown context \"unexpected\""),
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfg := New()
if test.in.table.Table != "" {
cfg.DataTable = []DataTable{test.in.table}
}
if test.in.tableLegacy != "" {
cfg.ClickHouse.DataTableLegacy = test.in.tableLegacy
}
err := cfg.ProcessDataTables()
if err != nil {
assert.Equal(t, test.out.err, err)
return
}
assert.Equal(t, len(test.out.tables), len(cfg.DataTable))
// it's difficult to check rollup.Rollup because Rules.updated field
// We explicitly don't check it here
for i := range cfg.DataTable {
test.out.tables[i].Rollup = nil
cfg.DataTable[i].Rollup = nil
}
assert.Equal(t, test.out.tables, cfg.DataTable)
})
}
}

func TestKnownDataTableContext(t *testing.T) {
assert.Equal(t, map[string]bool{ContextGraphite: true, ContextPrometheus: true}, knownDataTableContext)
}

0 comments on commit 6bf90a3

Please sign in to comment.