[tests] check timestamp in tests (#177)

This commit is contained in:
Artem Navoiev 2019-09-08 19:48:38 +03:00 committed by Aliaksandr Valialkin
parent 4a2cd85b92
commit 4fd741f40d
7 changed files with 41 additions and 12 deletions

View File

@ -50,6 +50,12 @@ const (
testStorageInitTimeout = 10 * time.Second
)
const (
tplWordTime = "{TIME}"
tplQuotedWordTimeSeconds = `"{TIME_S}"`
tplQuotedWordTimeMillis = `"{TIME_MS}"`
)
var (
storagePath string
insertionTime = time.Now().UTC()
@ -68,6 +74,30 @@ type Row struct {
Timestamps []int64 `json:"timestamps"`
}
func (r *Row) UnmarshalJSON(b []byte) error {
type withoutInterface Row
var to withoutInterface
if err := json.Unmarshal(populateTimeTpl(b), &to); err != nil {
return err
}
*r = Row(to)
return nil
}
func populateTimeTpl(b []byte) []byte {
var (
tplTimeToQuotedMS = [2][]byte{[]byte(tplQuotedWordTimeMillis), []byte(fmt.Sprintf("%d", timeToMillis(insertionTime)))}
tpsTimeToQuotedS = [2][]byte{[]byte(tplQuotedWordTimeSeconds), []byte(fmt.Sprintf("%d", insertionTime.Unix()*1e3))}
)
tpls := [][2][]byte{
tplTimeToQuotedMS, tpsTimeToQuotedS,
}
for i := range tpls {
b = bytes.ReplaceAll(b, tpls[i][0], tpls[i][1])
}
return b
}
func TestMain(m *testing.M) {
setUp()
code := m.Run()
@ -142,7 +172,6 @@ func TestWriteRead(t *testing.T) {
t.Run("write", testWrite)
time.Sleep(1 * time.Second)
vmstorage.Stop()
// open storage after stop in write
vmstorage.InitWithoutMetrics()
t.Run("read", testRead)
@ -229,7 +258,7 @@ func readIn(readFor string, t *testing.T, timeStr string) []test {
s.noError(err)
item := test{}
s.noError(json.Unmarshal(b, &item))
item.Data = strings.Replace(item.Data, "{TIME}", timeStr, -1)
item.Data = strings.Replace(item.Data, tplWordTime, timeStr, -1)
tt = append(tt, item)
return nil
}))
@ -287,8 +316,8 @@ func rowContains(t *testing.T, rows, contains []Row) {
func removeIfFound(r Row, contains []Row) []Row {
for i, item := range contains {
// todo check time
if reflect.DeepEqual(r.Metric, item.Metric) && reflect.DeepEqual(r.Values, item.Values) {
if reflect.DeepEqual(r.Metric, item.Metric) && reflect.DeepEqual(r.Values, item.Values) &&
reflect.DeepEqual(r.Timestamps, item.Timestamps) {
contains[i] = contains[len(contains)-1]
return contains[:len(contains)-1]
}

View File

@ -3,6 +3,6 @@
"data": "graphite.foo.bar.baz;tag1=value1;tag2=value2 123 {TIME}",
"query": "/api/v1/export?match={__name__!=\"\"}",
"result": [
{"metric":{"__name__":"graphite.foo.bar.baz","tag1":"value1","tag2":"value2"},"values":[123]}
{"metric":{"__name__":"graphite.foo.bar.baz","tag1":"value1","tag2":"value2"},"values":[123], "timestamps": ["{TIME_S}"]}
]
}

View File

@ -3,7 +3,7 @@
"data": "measurement,tag1=value1,tag2=value2 field1=1.23,field2=123 {TIME}",
"query": "/api/v1/export?match={__name__!=\"\"}",
"result": [
{"metric":{"__name__":"measurement_field2","tag1":"value1","tag2":"value2"},"values":[123]},
{"metric":{"__name__":"measurement_field1","tag1":"value1","tag2":"value2"},"values":[1.23]}
{"metric":{"__name__":"measurement_field2","tag1":"value1","tag2":"value2"},"values":[123], "timestamps": ["{TIME_MS}"]},
{"metric":{"__name__":"measurement_field1","tag1":"value1","tag2":"value2"},"values":[1.23], "timestamps": ["{TIME_MS}"]}
]
}

View File

@ -3,6 +3,6 @@
"data": "put openstdb.foo.bar.baz {TIME} 123 tag1=value1 tag2=value2",
"query": "/api/v1/export?match={__name__!=\"\"}",
"result": [
{"metric":{"__name__":"openstdb.foo.bar.baz","tag1":"value1","tag2":"value2"},"values":[123]}
{"metric":{"__name__":"openstdb.foo.bar.baz","tag1":"value1","tag2":"value2"},"values":[123], "timestamps": ["{TIME_S}"]}
]
}

View File

@ -3,6 +3,6 @@
"data": "{\"metric\": \"opentsdbhttp.foo\", \"value\": 1001, \"timestamp\": {TIME}, \"tags\": {\"bar\":\"baz\", \"x\": \"y\"}}",
"query": "/api/v1/export?match={__name__!=\"\"}",
"result": [
{"metric":{"__name__":"opentsdbhttp.foo","bar":"baz","x":"y"},"values":[1001]}
{"metric":{"__name__":"opentsdbhttp.foo","bar":"baz","x":"y"},"values":[1001], "timestamps": ["{TIME_S}"]}
]
}

View File

@ -3,7 +3,7 @@
"data": "[{\"metric\": \"opentsdbhttp.multiline1\", \"value\": 1001, \"timestamp\": \"{TIME}\", \"tags\": {\"bar\":\"baz\", \"x\": \"y\"}}, {\"metric\": \"opentsdbhttp.multiline2\", \"value\": 1002, \"timestamp\": {TIME}}]",
"query": "/api/v1/export?match={__name__!=\"\"}",
"result": [
{"metric":{"__name__":"opentsdbhttp.multiline1","bar":"baz","x":"y"},"values":[1001]},
{"metric":{"__name__":"opentsdbhttp.multiline2"},"values":[1002]}
{"metric":{"__name__":"opentsdbhttp.multiline1","bar":"baz","x":"y"},"values":[1001], "timestamps": ["{TIME_S}"]},
{"metric":{"__name__":"opentsdbhttp.multiline2"},"values":[1002], "timestamps": ["{TIME_S}"]}
]
}

View File

@ -3,6 +3,6 @@
"data": "[{\"labels\":[{\"name\":\"__name__\",\"value\":\"prometheus.bar\"},{\"name\":\"baz\",\"value\":\"qux\"}],\"samples\":[{\"value\":100000,\"timestamp\":{TIME}}]}]",
"query": "/api/v1/export?match={__name__!=\"\"}",
"result": [
{"metric":{"__name__":"prometheus.bar","baz":"qux"},"values":[100000]}
{"metric":{"__name__":"prometheus.bar","baz":"qux"},"values":[100000], "timestamps": ["{TIME_MS}"]}
]
}