bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/database/test/search_data_test.go (about) 1 package dbtest 2 3 import ( 4 "fmt" 5 "testing" 6 7 "bosun.org/opentsdb" 8 ) 9 10 func TestSearch_Metric_RoundTrip(t *testing.T) { 11 host := randString(5) 12 err := testData.Search().AddMetricForTag("host", host, "os.cpu", 42) 13 if err != nil { 14 t.Fatal(err) 15 } 16 17 metrics, err := testData.Search().GetMetricsForTag("host", host) 18 if err != nil { 19 t.Fatal(err) 20 } 21 if time, ok := metrics["os.cpu"]; !ok { 22 t.Fatal("Expected to find os.cpu. I didn't.") 23 } else if time != 42 { 24 t.Fatalf("Expected timestamp of 42. Got %d", time) 25 } 26 } 27 28 func TestSearch_MetricTagSets(t *testing.T) { 29 if err := testData.Search().AddMetricTagSet("services.status", "host=abc,service=def", 42); err != nil { 30 t.Fatal(err) 31 } 32 if err := testData.Search().AddMetricTagSet("os.cpu", "host=abc,service=def", 42); err != nil { 33 t.Fatal(err) 34 } 35 if err := testData.Search().AddMetricTagSet("services.status", "host=abc,service=ghi", 42); err != nil { 36 t.Fatal(err) 37 } 38 if err := testData.Search().AddMetricTagSet("services.status", "host=rrr,service=def", 42); err != nil { 39 t.Fatal(err) 40 } 41 tagsets, err := testData.Search().GetMetricTagSets("services.status", opentsdb.TagSet{"host": "abc"}) 42 if err != nil { 43 t.Fatal(err) 44 } 45 if len(tagsets) != 2 { 46 t.Fatalf("Expected 2 tagsets. Found %d.", len(tagsets)) 47 } 48 } 49 50 func TestSearch_MetricTagSets_Scan(t *testing.T) { 51 for i := int64(0); i < 10000; i++ { 52 if err := testData.Search().AddMetricTagSet("metric", fmt.Sprintf("host=abc%d", i), i); err != nil { 53 t.Fatal(err) 54 } 55 } 56 tagsets, err := testData.Search().GetMetricTagSets("metric", nil) 57 if err != nil { 58 t.Fatal(err) 59 } 60 if len(tagsets) != 10000 { 61 t.Fatalf("Expected 10000 tagsets. Found %d.", len(tagsets)) 62 } 63 }