2020-06-30 10:47:13 -05:00
|
|
|
package cloudmonitoring
|
2018-09-24 09:02:35 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-10-08 07:46:35 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2021-01-06 13:42:48 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-09-24 09:02:35 -05:00
|
|
|
)
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
func TestExecutor_parseToAnnotations(t *testing.T) {
|
2021-02-25 10:29:17 -06:00
|
|
|
d, err := loadTestFile("./test-data/2-series-response-no-agg.json")
|
2021-01-06 13:42:48 -06:00
|
|
|
require.NoError(t, err)
|
2021-02-25 10:29:17 -06:00
|
|
|
require.Len(t, d.TimeSeries, 3)
|
2021-01-06 13:42:48 -06:00
|
|
|
|
2021-10-08 07:46:35 -05:00
|
|
|
res := &backend.DataResponse{}
|
2018-09-24 09:02:35 -05:00
|
|
|
|
2022-11-08 08:41:17 -06:00
|
|
|
err = parseToAnnotations("anno", res, d, "atitle {{metric.label.instance_name}} {{metric.value}}",
|
2021-11-11 06:38:04 -06:00
|
|
|
"atext {{resource.label.zone}}")
|
2021-01-06 13:42:48 -06:00
|
|
|
require.NoError(t, err)
|
2018-09-27 08:17:35 -05:00
|
|
|
|
2022-05-19 15:52:52 -05:00
|
|
|
require.Len(t, res.Frames, 1)
|
|
|
|
assert.Equal(t, "time", res.Frames[0].Fields[0].Name)
|
2021-10-08 07:46:35 -05:00
|
|
|
assert.Equal(t, "title", res.Frames[0].Fields[1].Name)
|
|
|
|
assert.Equal(t, "tags", res.Frames[0].Fields[2].Name)
|
|
|
|
assert.Equal(t, "text", res.Frames[0].Fields[3].Name)
|
2022-05-19 15:52:52 -05:00
|
|
|
assert.Equal(t, 9, res.Frames[0].Fields[0].Len())
|
|
|
|
assert.Equal(t, 9, res.Frames[0].Fields[1].Len())
|
|
|
|
assert.Equal(t, 9, res.Frames[0].Fields[2].Len())
|
|
|
|
assert.Equal(t, 9, res.Frames[0].Fields[3].Len())
|
2021-02-25 10:29:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloudMonitoringExecutor_parseToAnnotations_emptyTimeSeries(t *testing.T) {
|
2021-10-08 07:46:35 -05:00
|
|
|
res := &backend.DataResponse{}
|
2021-02-25 10:29:17 -06:00
|
|
|
|
|
|
|
response := cloudMonitoringResponse{
|
|
|
|
TimeSeries: []timeSeries{},
|
|
|
|
}
|
|
|
|
|
2022-11-08 08:41:17 -06:00
|
|
|
err := parseToAnnotations("anno", res, response, "atitle", "atext")
|
2021-02-25 10:29:17 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-19 15:52:52 -05:00
|
|
|
require.Len(t, res.Frames, 1)
|
|
|
|
assert.Equal(t, "time", res.Frames[0].Fields[0].Name)
|
|
|
|
assert.Equal(t, "title", res.Frames[0].Fields[1].Name)
|
|
|
|
assert.Equal(t, "tags", res.Frames[0].Fields[2].Name)
|
|
|
|
assert.Equal(t, "text", res.Frames[0].Fields[3].Name)
|
|
|
|
assert.Equal(t, 0, res.Frames[0].Fields[0].Len())
|
|
|
|
assert.Equal(t, 0, res.Frames[0].Fields[1].Len())
|
|
|
|
assert.Equal(t, 0, res.Frames[0].Fields[2].Len())
|
|
|
|
assert.Equal(t, 0, res.Frames[0].Fields[3].Len())
|
2021-02-25 10:29:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloudMonitoringExecutor_parseToAnnotations_noPointsInSeries(t *testing.T) {
|
2021-10-08 07:46:35 -05:00
|
|
|
res := &backend.DataResponse{}
|
2021-02-25 10:29:17 -06:00
|
|
|
|
|
|
|
response := cloudMonitoringResponse{
|
|
|
|
TimeSeries: []timeSeries{
|
|
|
|
{Points: nil},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-08 08:41:17 -06:00
|
|
|
err := parseToAnnotations("anno", res, response, "atitle", "atext")
|
2021-02-25 10:29:17 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-19 15:52:52 -05:00
|
|
|
require.Len(t, res.Frames, 1)
|
|
|
|
assert.Equal(t, "time", res.Frames[0].Fields[0].Name)
|
|
|
|
assert.Equal(t, "title", res.Frames[0].Fields[1].Name)
|
|
|
|
assert.Equal(t, "tags", res.Frames[0].Fields[2].Name)
|
|
|
|
assert.Equal(t, "text", res.Frames[0].Fields[3].Name)
|
|
|
|
assert.Equal(t, 0, res.Frames[0].Fields[0].Len())
|
|
|
|
assert.Equal(t, 0, res.Frames[0].Fields[1].Len())
|
|
|
|
assert.Equal(t, 0, res.Frames[0].Fields[2].Len())
|
|
|
|
assert.Equal(t, 0, res.Frames[0].Fields[3].Len())
|
2018-09-24 09:02:35 -05:00
|
|
|
}
|