AzureMonitorDatasource: Add bounds check to fix panics (#110879)
(azure-monitor-datasource): add bounds check to fix panics
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/kinds/dataquery"
|
||||
@@ -710,6 +711,19 @@ func TestLogAnalyticsCreateRequest(t *testing.T) {
|
||||
t.Errorf("Unexpected Body: %v", cmp.Diff(string(body), expectedBody))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("returns error for AppInsights traces query with empty resources", func(t *testing.T) {
|
||||
ds := AzureLogAnalyticsDatasource{}
|
||||
_, err := ds.createRequest(ctx, url, &AzureLogAnalyticsQuery{
|
||||
Resources: []string{}, // Empty resources
|
||||
Query: "traces",
|
||||
QueryType: dataquery.AzureQueryTypeAzureTraces,
|
||||
AppInsightsQuery: true,
|
||||
DashboardTime: false,
|
||||
})
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "no resources specified for Azure traces Application Insights query")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_executeQueryErrorWithDifferentLogAnalyticsCreds(t *testing.T) {
|
||||
@@ -826,3 +840,62 @@ func Test_exemplarsFeatureToggle(t *testing.T) {
|
||||
require.Error(t, err, "query type unsupported as azureMonitorPrometheusExemplars feature toggle is not enabled")
|
||||
})
|
||||
}
|
||||
|
||||
func TestAddTraceDataLinksToFields_EmptyResources(t *testing.T) {
|
||||
dsInfo := types.DatasourceInfo{
|
||||
Services: map[string]types.DatasourceService{
|
||||
"Azure Monitor": {},
|
||||
},
|
||||
JSONData: map[string]any{
|
||||
"azureLogAnalyticsSameAs": false,
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
queryJSON string
|
||||
expectedErrorString string
|
||||
}{
|
||||
{
|
||||
name: "empty resources array should return error",
|
||||
queryJSON: `{
|
||||
"queryType": "Azure Traces",
|
||||
"azureTraces": {
|
||||
"resources": [],
|
||||
"resultFormat": "table",
|
||||
"traceTypes": ["trace"]
|
||||
}
|
||||
}`,
|
||||
expectedErrorString: "no resources specified for Azure traces data link",
|
||||
},
|
||||
{
|
||||
name: "missing resources field should return error",
|
||||
queryJSON: `{
|
||||
"queryType": "Azure Traces",
|
||||
"azureTraces": {
|
||||
"resultFormat": "table",
|
||||
"traceTypes": ["trace"]
|
||||
}
|
||||
}`,
|
||||
expectedErrorString: "no resources specified for Azure traces data link",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
query := &AzureLogAnalyticsQuery{
|
||||
JSON: []byte(tt.queryJSON),
|
||||
QueryType: dataquery.AzureQueryTypeAzureTraces,
|
||||
ResultFormat: dataquery.ResultFormatTable,
|
||||
}
|
||||
|
||||
// Create a mock data frame
|
||||
frame := data.NewFrame("test")
|
||||
|
||||
err := addTraceDataLinksToFields(query, "https://portal.azure.com", frame, dsInfo)
|
||||
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), tt.expectedErrorString)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user