Files
grafana/pkg/tsdb/azuremonitor/azure-resource-graph-datasource_test.go
shuotli 71fd0981ca AzureMonitor: Add Azure Resource Graph (#33293)
* Add Azure Resource Graph in Azure Plugin

* fix lodash import

* Fix mock queries

* use "backend" sdk

* Address comments

* add converter for object type

* Query error should cause 400 & apply template var

* fix backend test & add documentation

* update image

* Address comments

* marshal body from map

* use interpolated query instead of raw query

* fix test

* filter out empty queries

* fix go linting problem

* use new query field language name

* improve variable tests

* add better tests for interpolate variable

Co-authored-by: joshhunt <josh@trtr.co>
Co-authored-by: Erik Sundell <erik.sundell87@gmail.com>
2021-05-19 10:31:27 +02:00

76 lines
2.2 KiB
Go

package azuremonitor
import (
"fmt"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/stretchr/testify/require"
)
func TestBuildingAzureResourceGraphQueries(t *testing.T) {
datasource := &AzureResourceGraphDatasource{}
fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
tests := []struct {
name string
queryModel []plugins.DataSubQuery
timeRange plugins.DataTimeRange
azureResourceGraphQueries []*AzureResourceGraphQuery
Err require.ErrorAssertionFunc
}{
{
name: "Query with macros should be interpolated",
timeRange: plugins.DataTimeRange{
From: fmt.Sprintf("%v", fromStart.Unix()*1000),
To: fmt.Sprintf("%v", fromStart.Add(34*time.Minute).Unix()*1000),
},
queryModel: []plugins.DataSubQuery{
{
DataSource: &models.DataSource{
JsonData: simplejson.NewFromAny(map[string]interface{}{}),
},
Model: simplejson.NewFromAny(map[string]interface{}{
"queryType": "Azure Resource Graph",
"azureResourceGraph": map[string]interface{}{
"query": "resources | where $__contains(name,'res1','res2')",
"resultFormat": "table",
},
}),
RefID: "A",
},
},
azureResourceGraphQueries: []*AzureResourceGraphQuery{
{
RefID: "A",
ResultFormat: "table",
URL: "",
Model: simplejson.NewFromAny(map[string]interface{}{
"azureResourceGraph": map[string]interface{}{
"query": "resources | where $__contains(name,'res1','res2')",
"resultFormat": "table",
},
}),
InterpolatedQuery: "resources | where ['name'] in ('res1','res2')",
},
},
Err: require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
queries, err := datasource.buildQueries(tt.queryModel, tt.timeRange)
tt.Err(t, err)
if diff := cmp.Diff(tt.azureResourceGraphQueries, queries, cmpopts.IgnoreUnexported(simplejson.Json{})); diff != "" {
t.Errorf("Result mismatch (-want +got):\n%s", diff)
}
})
}
}