func Test_TagIndex_DB(t *testing.T) { a := assert.New(t) db := newDatabase(t) if db == nil { return } defer cleanDatabase(t, db) if rows, err := db.GetMetricKeys("environment", "production"); err != nil { a.CheckError(err) } else { a.EqInt(len(rows), 0) } a.CheckError(db.AddToTagIndex("environment", "production", "a.b.c")) a.CheckError(db.AddToTagIndex("environment", "production", "d.e.f")) if rows, err := db.GetMetricKeys("environment", "production"); err != nil { a.CheckError(err) } else { a.EqInt(len(rows), 2) } a.CheckError(db.RemoveFromTagIndex("environment", "production", "a.b.c")) if rows, err := db.GetMetricKeys("environment", "production"); err != nil { a.CheckError(err) } else { a.EqInt(len(rows), 1) a.EqString(string(rows[0]), "d.e.f") } }
func TestTimeseries_MarshalJSON(t *testing.T) { for _, suite := range []struct { input Timeseries expected string }{ { Timeseries{ TagSet: ParseTagSet("foo=bar"), Values: []float64{0, 1, -1, math.NaN()}, }, `{"tagset":{"foo":"bar"},"values":[0,1,-1,null]}`, }, { Timeseries{ TagSet: NewTagSet(), Values: []float64{0, 1, -1, math.NaN()}, }, `{"tagset":{},"values":[0,1,-1,null]}`, }, } { a := assert.New(t).Contextf("expected=%s", suite.expected) encoded, err := json.Marshal(suite.input) a.CheckError(err) a.Eq(string(encoded), suite.expected) } }
func TestTagSet_ParseTagSet(t *testing.T) { a := assert.New(t) a.EqString(ParseTagSet("foo=bar").Serialize(), "foo=bar") a.EqString(ParseTagSet("a=1,b=2").Serialize(), "a=1,b=2") a.EqString(ParseTagSet("a\\,b=1").Serialize(), "a\\,b=1") a.EqString(ParseTagSet("a\\=b=1").Serialize(), "a\\=b=1") }
func TestTimerange(t *testing.T) { for _, suite := range []struct { Start int64 End int64 Resolution int64 ExpectedValid bool ExpectedSlots int }{ // valid cases {0, 0, 1, true, 1}, {0, 1, 1, true, 2}, {0, 100, 1, true, 101}, {0, 100, 5, true, 21}, // invalid cases {100, 0, 1, false, 0}, {0, 100, 6, false, 0}, {0, 100, 200, false, 0}, } { a := assert.New(t).Contextf("input=%d:%d:%d", suite.Start, suite.End, suite.Resolution, ) timerange, err := NewTimerange(suite.Start, suite.End, suite.Resolution) a.EqBool(err == nil, suite.ExpectedValid) if !suite.ExpectedValid { continue } a.EqInt(timerange.Slots(), suite.ExpectedSlots) } }
func TestFunctionName(t *testing.T) { a := assert.New(t) a.EqString(functionName(0), "TestFunctionName") first, second := testFunction1() a.EqString(first, "testFunction1") a.EqString(second, "TestFunctionName") }
func Test_MetricName_GetTagSet_DB(t *testing.T) { a := assert.New(t) db := newDatabase(t) if db == nil { return } defer cleanDatabase(t, db) if _, err := db.GetTagSet("sample"); err == nil { t.Errorf("Cassandra should error on fetching nonexistent metric") } metricNamesTests := []struct { addTest bool metricName string tagString string expectedTags map[string][]string // { metricName: [ tags ] } }{ {true, "sample", "foo=bar1", map[string][]string{ "sample": []string{"foo=bar1"}, }}, {true, "sample", "foo=bar2", map[string][]string{ "sample": []string{"foo=bar1", "foo=bar2"}, }}, {true, "sample2", "foo=bar2", map[string][]string{ "sample": []string{"foo=bar1", "foo=bar2"}, "sample2": []string{"foo=bar2"}, }}, {false, "sample2", "foo=bar2", map[string][]string{ "sample": []string{"foo=bar1", "foo=bar2"}, }}, {false, "sample", "foo=bar1", map[string][]string{ "sample": []string{"foo=bar2"}, }}, } for _, c := range metricNamesTests { if c.addTest { a.CheckError(db.AddMetricName(api.MetricKey(c.metricName), api.ParseTagSet(c.tagString))) } else { a.CheckError(db.RemoveMetricName(api.MetricKey(c.metricName), api.ParseTagSet(c.tagString))) } for k, v := range c.expectedTags { if tags, err := db.GetTagSet(api.MetricKey(k)); err != nil { t.Errorf("Error fetching tags") } else { stringTags := make([]string, len(tags)) for i, tag := range tags { stringTags[i] = tag.Serialize() } a.EqInt(len(stringTags), len(v)) sort.Sort(sort.StringSlice(stringTags)) sort.Sort(sort.StringSlice(v)) a.Eq(stringTags, v) } } } }
func TestCompile_Good(t *testing.T) { a := assert.New(t) _, err := Compile(RawRule{ Pattern: "prefix.%foo%", MetricKeyPattern: "test-metric", }) a.CheckError(err) }
func TestTagSet_Serialize(t *testing.T) { a := assert.New(t) a.EqString(NewTagSet().Serialize(), "") ts := NewTagSet() ts["dc"] = "sjc1b" ts["env"] = "production" a.EqString(ts.Serialize(), "dc=sjc1b,env=production") }
func checkConversionErrorCode(t *testing.T, err error, expected ConversionErrorCode) { casted, ok := err.(ConversionError) if !ok { t.Errorf("Invalid Error type") return } a := assert.New(t) a.EqInt(int(casted.Code()), int(expected)) }
func TestCompile(t *testing.T) { for _, row := range inputs { a := assert.New(t).Contextf(row) p := Parser{Buffer: row} p.Init() a.CheckError(p.Parse()) p.Execute() testParserResult(a, p) } }
func TestUnescapeLiteral(t *testing.T) { a := assert.New(t) a.EqString(unescapeLiteral("'foo'"), "foo") a.EqString(unescapeLiteral("foo"), "foo") a.EqString(unescapeLiteral("nodes.cpu.io"), "nodes.cpu.io") a.EqString(unescapeLiteral(`"hello"`), `hello`) a.EqString(unescapeLiteral(`"\"hello\""`), `"hello"`) a.EqString(unescapeLiteral(`'\"hello\"'`), `"hello"`) a.EqString(unescapeLiteral("\"\\`\""), "`") }
func TestTagSet_Serialize_Escape(t *testing.T) { a := assert.New(t) ts := NewTagSet() ts["weird=key=1"] = "weird,value" ts["weird=key=2"] = "weird\\value" a.EqString(ts.Serialize(), "weird\\=key\\=1=weird\\,value,weird\\=key\\=2=weird\\\\value") parsed := ParseTagSet(ts.Serialize()) a.EqInt(len(parsed), 2) a.EqString(parsed["weird=key=1"], "weird,value") a.EqString(parsed["weird=key=2"], "weird\\value") }
func Test_Registry_Default(t *testing.T) { a := assert.New(t) sr := StandardRegistry{mapping: make(map[string]function.MetricFunction)} a.Eq(sr.All(), []string{}) if err := sr.Register(function.MetricFunction{Name: "foo", Compute: dummyCompute}); err != nil { a.CheckError(err) } if err := sr.Register(function.MetricFunction{Name: "bar", Compute: dummyCompute}); err != nil { a.CheckError(err) } a.Eq(sr.All(), []string{"bar", "foo"}) }
func TestLoadYAML_Invalid(t *testing.T) { a := assert.New(t) rawYAML := ` rules - pattern: foo.bar.baz.%tag% metric_key: abc regex: {} ` ruleSet, err := LoadYAML([]byte(rawYAML)) checkRuleErrorCode(a, err, InvalidYaml) a.EqInt(len(ruleSet.Rules), 0) }
func TestRandom(t *testing.T) { a := assert.New(t) expected := []string{"Apple", "apple", "file2", "file22", "file90", "file99", "file100", "Zoo", "zoo"} test := []string{"Apple", "apple", "file2", "file22", "file90", "file99", "file100", "Zoo", "zoo"} Sort(test) a.Eq(test, expected) for i := 0; i < 1000; i++ { testShuffle(test) a := a.Contextf("input: %+v", test) Sort(test) a.Eq(test, expected) } }
func TestTimerange_MarshalJSON(t *testing.T) { for _, suite := range []struct { input Timerange expected string }{ {Timerange{0, 100, 10}, `{"start":0,"end":100,"resolution":10}`}, {Timerange{100, 10000, 50}, `{"start":100,"end":10000,"resolution":50}`}, } { a := assert.New(t).Contextf("expected=%s", suite.expected) encoded, err := json.Marshal(suite.input) a.CheckError(err) a.Eq(string(encoded), suite.expected) } }
func Test_FetchCounter(t *testing.T) { c := NewFetchCounter(10) a := assert.New(t) a.EqInt(c.Current(), 0) a.EqInt(c.Limit(), 10) a.EqBool(c.Consume(5), true) a.EqInt(c.Current(), 5) a.EqBool(c.Consume(4), true) a.EqInt(c.Current(), 9) a.EqBool(c.Consume(1), true) a.EqInt(c.Current(), 10) a.EqBool(c.Consume(1), false) a.EqInt(c.Current(), 11) }
func TestNaturalSort(t *testing.T) { a := assert.New(t) expected := []string{"Apple", "apple", "file2", "file22", "file90", "file99", "file100", "Zoo", "zoo"} tests := [][]string{ {"Apple", "apple", "file2", "file90", "file99", "file100", "Zoo", "zoo", "file22"}, {"Zoo", "Apple", "apple", "file100", "file2", "file90", "file99", "zoo", "file22"}, {"file2", "file90", "apple", "Zoo", "file100", "file22", "file99", "zoo", "Apple"}, } Sort([]string{}) // check that no panic occurs for _, test := range tests { Sort(test) a.Eq(test, expected) } }
func Test_GetAllMetrics_API(t *testing.T) { a := assert.New(t) cassandra, context := newCassandraAPI(t) defer cleanAPI(t, cassandra) a.CheckError(cassandra.AddMetric(api.TaggedMetric{ "metric.a", api.ParseTagSet("foo=a"), }, context)) a.CheckError(cassandra.AddMetric(api.TaggedMetric{ "metric.a", api.ParseTagSet("foo=b"), }, context)) a.CheckError(cassandra.AddMetrics([]api.TaggedMetric{ { "metric.c", api.TagSet{ "bar": "cat", }, }, { "metric.d", api.TagSet{ "bar": "dog", }, }, { "metric.e", api.TagSet{ "bar": "cat", }, }, }, context)) keys, err := cassandra.GetAllMetrics(context) a.CheckError(err) sort.Sort(api.MetricKeys(keys)) a.Eq(keys, []api.MetricKey{"metric.a", "metric.c", "metric.d", "metric.e"}) a.CheckError(cassandra.AddMetric(api.TaggedMetric{ "metric.b", api.ParseTagSet("foo=c"), }, context)) a.CheckError(cassandra.AddMetric(api.TaggedMetric{ "metric.b", api.ParseTagSet("foo=c"), }, context)) keys, err = cassandra.GetAllMetrics(context) a.CheckError(err) sort.Sort(api.MetricKeys(keys)) a.Eq(keys, []api.MetricKey{"metric.a", "metric.b", "metric.c", "metric.d", "metric.e"}) }
func TestToGraphiteName(t *testing.T) { a := assert.New(t) rule, err := Compile(RawRule{ Pattern: "prefix.%foo%", MetricKeyPattern: "test-metric", }) a.CheckError(err) tm := api.TaggedMetric{ MetricKey: "test-metric", TagSet: api.ParseTagSet("foo=fooValue"), } reversed, err := rule.ToGraphiteName(tm) a.CheckError(err) a.EqString(string(reversed), "prefix.fooValue") }
func TestLoadYAML(t *testing.T) { a := assert.New(t) rawYAML := ` rules: - pattern: foo.bar.baz.%tag% metric_key: abc regex: {} ` ruleSet, err := LoadYAML([]byte(rawYAML)) a.CheckError(err) a.EqInt(len(ruleSet.Rules), 1) a.EqString(string(ruleSet.Rules[0].raw.MetricKeyPattern), "abc") a.Eq(ruleSet.Rules[0].graphitePatternTags, []string{"tag"}) }
func TestCompile_Error(t *testing.T) { for _, test := range []struct { rawRule RawRule expectedCode RuleErrorCode }{ {RawRule{Pattern: "prefix.%foo%", MetricKeyPattern: ""}, InvalidMetricKey}, {RawRule{Pattern: "prefix.%foo%abc%", MetricKeyPattern: "test-metric"}, InvalidPattern}, {RawRule{Pattern: "", MetricKeyPattern: "test-metric"}, InvalidPattern}, {RawRule{Pattern: "prefix.%foo%.%foo%", MetricKeyPattern: "test-metric"}, InvalidPattern}, {RawRule{Pattern: "prefix.%foo%.abc.%%", MetricKeyPattern: "test-metric"}, InvalidPattern}, {RawRule{Pattern: "prefix.%foo%", MetricKeyPattern: "test-metric", Regex: map[string]string{"foo": "(bar)"}}, InvalidCustomRegex}, } { _, err := Compile(test.rawRule) a := assert.New(t).Contextf("%s", test.rawRule.Pattern) checkRuleErrorCode(a, err, test.expectedCode) } }
func TestTimeseries_Downsample(t *testing.T) { a := assert.New(t) for _, suite := range []struct { input []float64 inputRange Timerange newRange Timerange sampler func([]float64) float64 expected []float64 }{ {[]float64{1, 2, 3, 4, 5}, Timerange{0, 4, 1}, Timerange{0, 4, 2}, max, []float64{2, 4, 5}}, } { tagset := ParseTagSet("key=value") ts := Timeseries{suite.input, tagset} sampled, err := ts.Downsample(suite.inputRange, suite.newRange, suite.sampler) a.CheckError(err) a.Eq(sampled.Values, suite.expected) } }
func TestCommand_Describe(t *testing.T) { fakeAPI := mocks.NewFakeMetricMetadataAPI() fakeAPI.AddPairWithoutGraphite(api.TaggedMetric{"series_0", api.ParseTagSet("dc=west,env=production,host=a")}, emptyGraphiteName) fakeAPI.AddPairWithoutGraphite(api.TaggedMetric{"series_0", api.ParseTagSet("dc=west,env=staging,host=b")}, emptyGraphiteName) fakeAPI.AddPairWithoutGraphite(api.TaggedMetric{"series_0", api.ParseTagSet("dc=east,env=production,host=c")}, emptyGraphiteName) fakeAPI.AddPairWithoutGraphite(api.TaggedMetric{"series_0", api.ParseTagSet("dc=east,env=staging,host=d")}, emptyGraphiteName) for _, test := range []struct { query string metricmetadata api.MetricMetadataAPI expected map[string][]string }{ {"describe series_0", fakeAPI, map[string][]string{"dc": {"east", "west"}, "env": {"production", "staging"}, "host": {"a", "b", "c", "d"}}}, {"describe`series_0`", fakeAPI, map[string][]string{"dc": {"east", "west"}, "env": {"production", "staging"}, "host": {"a", "b", "c", "d"}}}, {"describe series_0 where dc='west'", fakeAPI, map[string][]string{"dc": {"west"}, "env": {"production", "staging"}, "host": {"a", "b"}}}, {"describe`series_0`where(dc='west')", fakeAPI, map[string][]string{"dc": {"west"}, "env": {"production", "staging"}, "host": {"a", "b"}}}, {"describe series_0 where dc='west' or env = 'production'", fakeAPI, map[string][]string{"dc": {"east", "west"}, "env": {"production", "staging"}, "host": {"a", "b", "c"}}}, {"describe series_0 where`dc`='west'or`env`='production'", fakeAPI, map[string][]string{"dc": {"east", "west"}, "env": {"production", "staging"}, "host": {"a", "b", "c"}}}, {"describe series_0 where dc='west' or env = 'production' and doesnotexist = ''", fakeAPI, map[string][]string{"dc": {"west"}, "env": {"production", "staging"}, "host": {"a", "b"}}}, {"describe series_0 where env = 'production' and doesnotexist = '' or dc = 'west'", fakeAPI, map[string][]string{"dc": {"west"}, "env": {"production", "staging"}, "host": {"a", "b"}}}, {"describe series_0 where (dc='west' or env = 'production') and doesnotexist = ''", fakeAPI, map[string][]string{}}, {"describe series_0 where(dc='west' or env = 'production')and`doesnotexist` = ''", fakeAPI, map[string][]string{}}, } { a := assert.New(t).Contextf("query=%s", test.query) command, err := Parse(test.query) if err != nil { a.Errorf("Unexpected error while parsing") continue } a.EqString(command.Name(), "describe") fakeTimeseriesStorage := mocks.FakeTimeseriesStorageAPI{} rawResult, err := command.Execute(ExecutionContext{ TimeseriesStorageAPI: fakeTimeseriesStorage, MetricMetadataAPI: test.metricmetadata, FetchLimit: 1000, Timeout: 0, OptimizationConfiguration: optimize.NewOptimizationConfiguration(), }) a.CheckError(err) a.Eq(rawResult, test.expected) } }
func Test_ScalarExpression(t *testing.T) { timerangeA, err := api.NewTimerange(0, 10, 2) if err != nil { t.Fatalf("invalid timerange used for testcase") return } for _, test := range []struct { expr scalarExpression timerange api.Timerange expectedSeries []api.Timeseries }{ { scalarExpression{5}, timerangeA, []api.Timeseries{ api.Timeseries{ Values: []float64{5.0, 5.0, 5.0, 5.0, 5.0, 5.0}, TagSet: api.NewTagSet(), }, }, }, } { a := assert.New(t).Contextf("%+v", test) result, err := evaluateToSeriesList(test.expr, &function.EvaluationContext{ TimeseriesStorageAPI: FakeBackend{}, Timerange: test.timerange, SampleMethod: api.SampleMean, FetchLimit: function.NewFetchCounter(1000), Registry: registry.Default(), }) if err != nil { t.Fatalf("failed to convert number into serieslist") } a.EqInt(len(result.Series), len(test.expectedSeries)) for i := 0; i < len(result.Series); i++ { a.Eq(result.Series[i].Values, test.expectedSeries[i].Values) } } }
func Test_TagIndex_API(t *testing.T) { a := assert.New(t) cassandra, context := newCassandraAPI(t) defer cleanAPI(t, cassandra) if rows, err := cassandra.GetMetricsForTag("environment", "production", context); err != nil { a.CheckError(err) } else { a.EqInt(len(rows), 0) } a.CheckError(cassandra.AddMetric(api.TaggedMetric{ "a.b.c", api.TagSet{ "environment": "production", }, }, context)) a.CheckError(cassandra.AddMetric(api.TaggedMetric{ "d.e.f", api.TagSet{ "environment": "production", }, }, context)) if rows, err := cassandra.GetMetricsForTag("environment", "production", context); err != nil { a.CheckError(err) } else { a.EqInt(len(rows), 2) } a.CheckError(cassandra.RemoveMetric(api.TaggedMetric{ "a.b.c", api.TagSet{ "environment": "production", }, }, context)) if rows, err := cassandra.GetMetricsForTag("environment", "production", context); err != nil { a.CheckError(err) } else { a.EqInt(len(rows), 1) a.EqString(string(rows[0]), "d.e.f") } }
func TestMatchRule_FilterTag(t *testing.T) { a := assert.New(t) rule, err := Compile(RawRule{ Pattern: "prefix.%foo%.%bar%", MetricKeyPattern: "test-metric.%bar%", }) a.CheckError(err) originalName := "prefix.fooValue.barValue" matcher, matched := rule.MatchRule(originalName) if !matched { t.Errorf("Expected matching but didn't occur") return } a.EqString(string(matcher.MetricKey), "test-metric.barValue") a.Eq(matcher.TagSet, api.TagSet(map[string]string{"foo": "fooValue"})) // perform the reverse. reversed, err := rule.ToGraphiteName(matcher) a.CheckError(err) a.EqString(string(reversed), originalName) }
func Test_GetAllMetrics(t *testing.T) { a := assert.New(t) db := newDatabase(t) if db == nil { return } defer cleanDatabase(t, db) a.CheckError(db.AddMetricName("metric.a", api.ParseTagSet("foo=a"))) a.CheckError(db.AddMetricName("metric.a", api.ParseTagSet("foo=b"))) keys, err := db.GetAllMetrics() a.CheckError(err) sort.Sort(api.MetricKeys(keys)) a.Eq(keys, []api.MetricKey{"metric.a"}) a.CheckError(db.AddMetricName("metric.b", api.ParseTagSet("foo=c"))) a.CheckError(db.AddMetricName("metric.b", api.ParseTagSet("foo=c"))) keys, err = db.GetAllMetrics() a.CheckError(err) sort.Sort(api.MetricKeys(keys)) a.Eq(keys, []api.MetricKey{"metric.a", "metric.b"}) }
func TestToGraphiteName_Error(t *testing.T) { a := assert.New(t) rule, err := Compile(RawRule{ Pattern: "prefix.%foo%", MetricKeyPattern: "test-metric", }) a.CheckError(err) reversed, err := rule.ToGraphiteName(api.TaggedMetric{ MetricKey: "test-metric", TagSet: api.ParseTagSet(""), }) checkConversionErrorCode(t, err, MissingTag) a.EqString(string(reversed), "") reversed, err = rule.ToGraphiteName(api.TaggedMetric{ MetricKey: "test-metric-foo", TagSet: api.ParseTagSet("foo=fooValue"), }) checkConversionErrorCode(t, err, CannotInterpolate) a.EqString(string(reversed), "") }
func Test_Registry_Error(t *testing.T) { for _, suite := range []struct { Name string Function function.MetricFunction }{ {"empty name", function.MetricFunction{Name: "", Compute: dummyCompute}}, {"duplicate name", function.MetricFunction{Name: "existing", Compute: dummyCompute}}, {"no compute", function.MetricFunction{Name: "notexisting", Compute: nil}}, } { a := assert.New(t).Contextf("%s", suite.Name) // set up the standard registry sr := StandardRegistry{mapping: make(map[string]function.MetricFunction)} if err := sr.Register(function.MetricFunction{Name: "existing", Compute: dummyCompute}); err != nil { a.CheckError(err) return } if err := sr.Register(suite.Function); err == nil { a.Errorf("Expected error, but got none.") } } }