forked from timescale/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in rrk/improve-test-coverage (pull request timescale#74)
Improve test coverage for InfluxDB loader Approved-by: Lee Hampton <leejhampton@gmail.com>
- Loading branch information
Showing
8 changed files
with
324 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
cmd/tsbs_generate_queries/databases/influx/devops_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package influx | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"bitbucket.org/440-labs/tsbs/query" | ||
) | ||
|
||
func TestDevopsGetHostWhereWithHostnames(t *testing.T) { | ||
cases := []struct { | ||
desc string | ||
hostnames []string | ||
want string | ||
}{ | ||
{ | ||
desc: "single host", | ||
hostnames: []string{"foo1"}, | ||
want: "(hostname = 'foo1')", | ||
}, | ||
{ | ||
desc: "multi host (2)", | ||
hostnames: []string{"foo1", "foo2"}, | ||
want: "(hostname = 'foo1' or hostname = 'foo2')", | ||
}, | ||
{ | ||
desc: "multi host (3)", | ||
hostnames: []string{"foo1", "foo2", "foo3"}, | ||
want: "(hostname = 'foo1' or hostname = 'foo2' or hostname = 'foo3')", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
d := NewDevops(time.Now(), time.Now(), 10) | ||
|
||
if got := d.getHostWhereWithHostnames(c.hostnames); got != c.want { | ||
t.Errorf("%s: incorrect output: got %s want %s", c.desc, got, c.want) | ||
} | ||
} | ||
} | ||
|
||
func TestDevopsGetSelectClausesAggMetrics(t *testing.T) { | ||
cases := []struct { | ||
desc string | ||
agg string | ||
metrics []string | ||
want string | ||
}{ | ||
{ | ||
desc: "single metric - max", | ||
agg: "max", | ||
metrics: []string{"foo"}, | ||
want: "max(foo)", | ||
}, | ||
{ | ||
desc: "multiple metric - max", | ||
agg: "max", | ||
metrics: []string{"foo", "bar"}, | ||
want: "max(foo),max(bar)", | ||
}, | ||
{ | ||
desc: "multiple metric - avg", | ||
agg: "avg", | ||
metrics: []string{"foo", "bar"}, | ||
want: "avg(foo),avg(bar)", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
d := NewDevops(time.Now(), time.Now(), 10) | ||
|
||
if got := strings.Join(d.getSelectClausesAggMetrics(c.agg, c.metrics), ","); got != c.want { | ||
t.Errorf("%s: incorrect output: got %s want %s", c.desc, got, c.want) | ||
} | ||
} | ||
} | ||
|
||
func TestDevopsFillInQuery(t *testing.T) { | ||
humanLabel := "this is my label" | ||
humanDesc := "and now my description" | ||
influxql := "SELECT * from cpu where usage_user > 90.0 and time < '2017-01-01'" | ||
d := NewDevops(time.Now(), time.Now(), 10) | ||
qi := d.GenerateEmptyQuery() | ||
q := qi.(*query.HTTP) | ||
if len(q.HumanLabel) != 0 { | ||
t.Errorf("empty query has non-zero length human label") | ||
} | ||
if len(q.HumanDescription) != 0 { | ||
t.Errorf("empty query has non-zero length human desc") | ||
} | ||
if len(q.Method) != 0 { | ||
t.Errorf("empty query has non-zero length method") | ||
} | ||
if len(q.Path) != 0 { | ||
t.Errorf("empty query has non-zero length path") | ||
} | ||
|
||
d.fillInQuery(q, humanLabel, humanDesc, influxql) | ||
if string(q.HumanLabel) != humanLabel { | ||
t.Errorf("filled query mislabeled: got %s want %s", string(q.HumanLabel), humanLabel) | ||
} | ||
if string(q.HumanDescription) != humanDesc { | ||
t.Errorf("filled query mis-described: got %s want %s", string(q.HumanDescription), humanDesc) | ||
} | ||
if string(q.Method) != "GET" { | ||
t.Errorf("filled query has wrong method: got %s want GET", string(q.Method)) | ||
} | ||
v := url.Values{} | ||
v.Set("q", influxql) | ||
encoded := v.Encode() | ||
if string(q.Path) != "/query?"+encoded { | ||
t.Errorf("filled query has wrong path: got %s want /query?%s", string(q.Path), encoded) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"sync" | ||
"testing" | ||
|
||
"bitbucket.org/440-labs/tsbs/load" | ||
) | ||
|
||
func TestBatch(t *testing.T) { | ||
bufPool = sync.Pool{ | ||
New: func() interface{} { | ||
return bytes.NewBuffer(make([]byte, 0, 4*1024*1024)) | ||
}, | ||
} | ||
f := &factory{} | ||
b := f.New().(*batch) | ||
if b.Len() != 0 { | ||
t.Errorf("batch not initialized with count 0") | ||
} | ||
p := &load.Point{ | ||
Data: []byte("tag1=tag1val,tag2=tag2val col1=0.0,col2=0.0 140"), | ||
} | ||
b.Append(p) | ||
if b.Len() != 1 { | ||
t.Errorf("batch count is not 1 after first append") | ||
} | ||
if b.rows != 1 { | ||
t.Errorf("batch row count is not 1 after first append") | ||
} | ||
if b.metrics != 2 { | ||
t.Errorf("batch metric count is not 2 after first append") | ||
} | ||
|
||
p = &load.Point{ | ||
Data: []byte("tag1=tag1val,tag2=tag2val col1=1.0,col2=1.0 190"), | ||
} | ||
b.Append(p) | ||
if b.Len() != 2 { | ||
t.Errorf("batch count is not 1 after first append") | ||
} | ||
if b.rows != 2 { | ||
t.Errorf("batch row count is not 1 after first append") | ||
} | ||
if b.metrics != 4 { | ||
t.Errorf("batch metric count is not 2 after first append") | ||
} | ||
|
||
p = &load.Point{ | ||
Data: []byte("bad_point"), | ||
} | ||
errMsg := "" | ||
fatal = func(f string, args ...interface{}) { | ||
errMsg = fmt.Sprintf(f, args...) | ||
} | ||
b.Append(p) | ||
if errMsg == "" { | ||
t.Errorf("batch append did not error with ill-formed point") | ||
} | ||
} | ||
|
||
func TestDecode(t *testing.T) { | ||
cases := []struct { | ||
desc string | ||
input string | ||
result []byte | ||
shouldFatal bool | ||
}{ | ||
{ | ||
desc: "correct input", | ||
input: "cpu,tag1=tag1text,tag2=tag2text col1=0.0,col2=0.0 140\n", | ||
result: []byte("cpu,tag1=tag1text,tag2=tag2text col1=0.0,col2=0.0 140"), | ||
}, | ||
{ | ||
desc: "correct input with extra", | ||
input: "cpu,tag1=tag1text,tag2=tag2text col1=0.0,col2=0.0 140\nextra_is_ignored", | ||
result: []byte("cpu,tag1=tag1text,tag2=tag2text col1=0.0,col2=0.0 140"), | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
br := bufio.NewReader(bytes.NewReader([]byte(c.input))) | ||
decoder := &decoder{scanner: bufio.NewScanner(br)} | ||
p := decoder.Decode(br) | ||
data := p.Data.([]byte) | ||
if !bytes.Equal(data, c.result) { | ||
t.Errorf("%s: incorrect result: got\n%v\nwant\n%v", c.desc, data, c.result) | ||
} | ||
} | ||
} | ||
|
||
func TestDecodeEOF(t *testing.T) { | ||
input := []byte("cpu,tag1=tag1text,tag2=tag2text col1=0.0,col2=0.0 140") | ||
br := bufio.NewReader(bytes.NewReader([]byte(input))) | ||
decoder := &decoder{scanner: bufio.NewScanner(br)} | ||
_ = decoder.Decode(br) | ||
// nothing left, should be EOF | ||
p := decoder.Decode(br) | ||
if p != nil { | ||
t.Errorf("expected p to be nil, got %v", p) | ||
} | ||
} |
Oops, something went wrong.