Newer
Older
/*
* Copyright (c) 2019 InterDigital Communications, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package metricstore
import (
"fmt"
"testing"
log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
)
Kevin Di Lallo
committed
const metricStore1Name string = "metric-store-1"
const metricStore2Name string = "metric-store-2"
Kevin Di Lallo
committed
const metricStore3Name string = "metric-store-3"
const metricStoreInfluxAddr string = "http://localhost:30986"
const metricStoreRedisAddr string = "localhost:30380"
const metric1 = "metric1"
const tag1 = "tag1"
const tag2 = "tag2"
const field1 = "field1"
const field2 = "field2"
const field3 = "field3"
const field4 = "field4"
func TestMetricStoreNew(t *testing.T) {
fmt.Println("--- ", t.Name())
log.MeepTextLogInit(t.Name())
fmt.Println("Invalid Metric Store address")
ms, err := NewMetricStore("", "ExpectedFailure-InvalidStoreAddr", "")
if err == nil {
t.Errorf("Should report error on invalid store addr")
}
if ms != nil {
t.Errorf("Should have a nil metric store")
}
fmt.Println("Create valid Metric Store")
ms, err = NewMetricStore("", metricStoreInfluxAddr, metricStoreRedisAddr)
if err != nil {
t.Errorf("Unable to create Metric Store")
}
fmt.Println("Invoke API before setting store")
tags := map[string]string{tag1: "tag1", tag2: "tag2"}
fields := []string{field1, field2, field3, field4}
_, err = ms.GetInfluxMetric(metric1, tags, fields, "", 0)
if err == nil {
t.Errorf("API call should fail if no store is set")
}
metricList := make([]Metric, 1)
metric := &metricList[0]
metric.Name = NetMetName
metric.Tags = map[string]string{tag1: "tag1", tag2: "tag2"}
metric.Fields = map[string]interface{}{field1: true, field2: "val1", field3: 0, field4: 0.0}
err = ms.SetInfluxMetric(metricList)
if err == nil {
t.Errorf("API call should fail if no store is set")
}
fmt.Println("Set store")
err = ms.SetStore(metricStore1Name)
if err != nil {
t.Errorf("Unable to set Store")
}
fmt.Println("Set store2")
err = ms.SetStore(metricStore2Name)
if err != nil {
t.Errorf("Unable to set Store2")
}
fmt.Println("Reset store")
err = ms.SetStore("")
if err != nil {
t.Errorf("Unable to reset Store")
}
fmt.Println("Invoke API after resetting store")
tags = map[string]string{tag1: "tag1", tag2: "tag2"}
fields = []string{field1, field2, field3, field4}
_, err = ms.GetInfluxMetric(metric1, tags, fields, "", 0)
if err == nil {
t.Errorf("API call should fail if no store is set")
}
// t.Errorf("DONE")
}
func TestMetricStoreGetSetInflux(t *testing.T) {
fmt.Println("--- ", t.Name())
log.MeepTextLogInit(t.Name())
fmt.Println("Create valid Metric Store")
ms, err := NewMetricStore(metricStore1Name, metricStoreInfluxAddr, metricStoreRedisAddr)
if err != nil {
t.Errorf("Unable to create Metric Store")
}
fmt.Println("Flush store metrics")
ms.Flush()
fmt.Println("Get empty metric")
tags := map[string]string{tag1: "tag1", tag2: "tag2"}
fields := []string{field1, field2, field3, field4}
_, err = ms.GetInfluxMetric(metric1, tags, fields, "", 1)
t.Errorf("Net metric should not exist")
}
metricList := make([]Metric, 1)
metric := &metricList[0]
metric.Name = metric1
metric.Tags = map[string]string{tag1: "tag1", tag2: "tag2"}
metric.Fields = map[string]interface{}{field1: true, field2: "val1", field3: 0, field4: 0.0}
err = ms.SetInfluxMetric(metricList)
if err != nil {
metric.Tags = map[string]string{tag1: "tag1", tag2: "tag2"}
metric.Fields = map[string]interface{}{field1: false, field2: "val2", field3: 1, field4: 1.1}
err = ms.SetInfluxMetric(metricList)
Kevin Di Lallo
committed
if err != nil {
Kevin Di Lallo
committed
}
tags = map[string]string{tag1: "tag1", tag2: "tag2"}
fields = []string{field1, field2, field3, field4}
result, err := ms.GetInfluxMetric(metric1, tags, fields, "", 1)
if err != nil || len(result) != 1 {
t.Errorf("Failed to get metric")
if !validateMetric(result[0], false, "val2", 1, 1.1) {
t.Errorf("Invalid result")
Kevin Di Lallo
committed
}
tags = map[string]string{tag1: "tag1", tag2: "tag2"}
fields = []string{field1, field2, field3, field4}
result, err = ms.GetInfluxMetric(metric1, tags, fields, "", 0)
if err != nil || len(result) != 2 {
t.Errorf("Failed to get metric")
if !validateMetric(result[0], false, "val2", 1, 1.1) {
t.Errorf("Invalid result")
Kevin Di Lallo
committed
}
if !validateMetric(result[1], true, "val1", 0, 0.0) {
t.Errorf("Invalid result")
fmt.Println("Get all metrics from the last 10 seconds")
tags = map[string]string{tag1: "tag1", tag2: "tag2"}
fields = []string{field1, field2, field3, field4}
_, err = ms.GetInfluxMetric(metric1, tags, fields, "10s", 0)
if err != nil || len(result) != 2 {
t.Errorf("Failed to get metric")
Kevin Di Lallo
committed
}
if !validateMetric(result[0], false, "val2", 1, 1.1) {
t.Errorf("Invalid result")
if !validateMetric(result[1], true, "val1", 0, 0.0) {
t.Errorf("Invalid result")
Kevin Di Lallo
committed
}
fmt.Println("Get all metrics from the last millisecond (none)")
tags = map[string]string{tag1: "tag1", tag2: "tag2"}
fields = []string{field1, field2, field3, field4}
_, err = ms.GetInfluxMetric(metric1, tags, fields, "1ms", 0)
if err == nil {
t.Errorf("Net metric list should be empty")
Kevin Di Lallo
committed
Kevin Di Lallo
committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
func TestMetricStoreCopyInflux(t *testing.T) {
fmt.Println("--- ", t.Name())
log.MeepTextLogInit(t.Name())
fmt.Println("Create valid Metric Store")
ms, err := NewMetricStore(metricStore1Name, metricStoreInfluxAddr, metricStoreRedisAddr)
if err != nil {
t.Errorf("Unable to create Metric Store")
}
fmt.Println("Flush store metrics")
ms.Flush()
fmt.Println("Set metrics")
metricList := make([]Metric, 1)
metric := &metricList[0]
metric.Name = metric1
metric.Tags = map[string]string{tag1: "tag1", tag2: "tag2"}
metric.Fields = map[string]interface{}{field1: true, field2: "val1", field3: 0, field4: 0.0}
err = ms.SetInfluxMetric(metricList)
if err != nil {
t.Errorf("Failed to set metric")
}
metric.Tags = map[string]string{tag1: "tag1", tag2: "tag2"}
metric.Fields = map[string]interface{}{field1: false, field2: "val2", field3: 1, field4: 1.1}
err = ms.SetInfluxMetric(metricList)
if err != nil {
t.Errorf("Failed to set metric")
}
fmt.Println("Copy invalid")
err = ms.Copy("", metricStore3Name)
if err == nil {
t.Errorf("Database copy should fail")
}
err = ms.Copy(metricStore1Name, "")
if err == nil {
t.Errorf("Database copy should fail")
}
fmt.Println("Copy store")
err = ms.Copy(metricStore1Name, metricStore3Name)
if err != nil {
t.Errorf("Failed to copy database")
}
fmt.Println("Validate copied data")
fmt.Println("Set store")
err = ms.SetStore(metricStore3Name)
if err != nil {
t.Errorf("Unable to set Store")
}
fmt.Println("Get all metrics")
tags := map[string]string{tag1: "tag1", tag2: "tag2"}
fields := []string{field1, field2, field3, field4}
result, err := ms.GetInfluxMetric(metric1, tags, fields, "", 0)
if err != nil || len(result) != 2 {
t.Errorf("Failed to get metric")
}
if !validateMetric(result[0], false, "val2", 1, 1.1) {
t.Errorf("Invalid result")
}
if !validateMetric(result[1], true, "val1", 0, 0.0) {
t.Errorf("Invalid result")
}
}
func validateMetric(result map[string]interface{}, v1 bool, v2 string, v3 int32, v4 float64) bool {
if result[field1] != v1 {
fmt.Println("Invalid " + field1)
return false
if result[field2] != v2 {
fmt.Println("Invalid " + field2)
return false
if val, ok := result[field3].(json.Number); !ok || JsonNumToInt32(val) != v3 {
fmt.Println("Invalid " + field3)
return false
if val, ok := result[field4].(json.Number); !ok || JsonNumToFloat64(val) != v4 {
fmt.Println("Invalid " + field4)
return false
}
return true