|
|
|
|
@ -6,7 +6,6 @@ import ( |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/expr" |
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
|
) |
|
|
|
|
@ -17,7 +16,6 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
alertQuery AlertQuery |
|
|
|
|
expectedIsExpression bool |
|
|
|
|
expectedDatasource string |
|
|
|
|
expectedDatasourceUID string |
|
|
|
|
expectedMaxPoints int64 |
|
|
|
|
expectedIntervalMS int64 |
|
|
|
|
err error |
|
|
|
|
@ -27,13 +25,12 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
alertQuery: AlertQuery{ |
|
|
|
|
RefID: "A", |
|
|
|
|
Model: json.RawMessage(`{ |
|
|
|
|
"datasourceUid": "-100", |
|
|
|
|
"queryType": "metricQuery", |
|
|
|
|
"extraParam": "some text" |
|
|
|
|
}`), |
|
|
|
|
DatasourceUID: "-100", |
|
|
|
|
}, |
|
|
|
|
expectedIsExpression: true, |
|
|
|
|
expectedDatasourceUID: expr.DatasourceUID, |
|
|
|
|
expectedMaxPoints: int64(defaultMaxDataPoints), |
|
|
|
|
expectedIntervalMS: int64(defaultIntervalMS), |
|
|
|
|
}, |
|
|
|
|
@ -42,13 +39,11 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
alertQuery: AlertQuery{ |
|
|
|
|
RefID: "A", |
|
|
|
|
Model: json.RawMessage(`{ |
|
|
|
|
"datasourceUid": "000000001", |
|
|
|
|
"queryType": "metricQuery", |
|
|
|
|
"extraParam": "some text" |
|
|
|
|
}`), |
|
|
|
|
}, |
|
|
|
|
expectedIsExpression: false, |
|
|
|
|
expectedDatasourceUID: "000000001", |
|
|
|
|
expectedMaxPoints: int64(defaultMaxDataPoints), |
|
|
|
|
expectedIntervalMS: int64(defaultIntervalMS), |
|
|
|
|
}, |
|
|
|
|
@ -57,14 +52,12 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
alertQuery: AlertQuery{ |
|
|
|
|
RefID: "A", |
|
|
|
|
Model: json.RawMessage(`{ |
|
|
|
|
"datasourceUid": "000000001", |
|
|
|
|
"queryType": "metricQuery", |
|
|
|
|
"maxDataPoints": 200, |
|
|
|
|
"extraParam": "some text" |
|
|
|
|
}`), |
|
|
|
|
}, |
|
|
|
|
expectedIsExpression: false, |
|
|
|
|
expectedDatasourceUID: "000000001", |
|
|
|
|
expectedMaxPoints: 200, |
|
|
|
|
expectedIntervalMS: int64(defaultIntervalMS), |
|
|
|
|
}, |
|
|
|
|
@ -73,14 +66,12 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
alertQuery: AlertQuery{ |
|
|
|
|
RefID: "A", |
|
|
|
|
Model: json.RawMessage(`{ |
|
|
|
|
"datasourceUid": "000000001", |
|
|
|
|
"queryType": "metricQuery", |
|
|
|
|
"maxDataPoints": "invalid", |
|
|
|
|
"extraParam": "some text" |
|
|
|
|
}`), |
|
|
|
|
}, |
|
|
|
|
expectedIsExpression: false, |
|
|
|
|
expectedDatasourceUID: "000000001", |
|
|
|
|
expectedMaxPoints: int64(defaultMaxDataPoints), |
|
|
|
|
expectedIntervalMS: int64(defaultIntervalMS), |
|
|
|
|
}, |
|
|
|
|
@ -89,14 +80,12 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
alertQuery: AlertQuery{ |
|
|
|
|
RefID: "A", |
|
|
|
|
Model: json.RawMessage(`{ |
|
|
|
|
"datasourceUid": "000000001", |
|
|
|
|
"queryType": "metricQuery", |
|
|
|
|
"maxDataPoints": 0, |
|
|
|
|
"extraParam": "some text" |
|
|
|
|
}`), |
|
|
|
|
}, |
|
|
|
|
expectedIsExpression: false, |
|
|
|
|
expectedDatasourceUID: "000000001", |
|
|
|
|
expectedMaxPoints: int64(defaultMaxDataPoints), |
|
|
|
|
expectedIntervalMS: int64(defaultIntervalMS), |
|
|
|
|
}, |
|
|
|
|
@ -105,14 +94,12 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
alertQuery: AlertQuery{ |
|
|
|
|
RefID: "A", |
|
|
|
|
Model: json.RawMessage(`{ |
|
|
|
|
"datasourceUid": "000000001", |
|
|
|
|
"queryType": "metricQuery", |
|
|
|
|
"intervalMs": 2000, |
|
|
|
|
"extraParam": "some text" |
|
|
|
|
}`), |
|
|
|
|
}, |
|
|
|
|
expectedIsExpression: false, |
|
|
|
|
expectedDatasourceUID: "000000001", |
|
|
|
|
expectedMaxPoints: int64(defaultMaxDataPoints), |
|
|
|
|
expectedIntervalMS: 2000, |
|
|
|
|
}, |
|
|
|
|
@ -121,14 +108,12 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
alertQuery: AlertQuery{ |
|
|
|
|
RefID: "A", |
|
|
|
|
Model: json.RawMessage(`{ |
|
|
|
|
"datasourceUid": "000000001", |
|
|
|
|
"queryType": "metricQuery", |
|
|
|
|
"intervalMs": "invalid", |
|
|
|
|
"extraParam": "some text"
|
|
|
|
|
}`), |
|
|
|
|
}, |
|
|
|
|
expectedIsExpression: false, |
|
|
|
|
expectedDatasourceUID: "000000001", |
|
|
|
|
expectedMaxPoints: int64(defaultMaxDataPoints), |
|
|
|
|
expectedIntervalMS: int64(defaultIntervalMS), |
|
|
|
|
}, |
|
|
|
|
@ -137,14 +122,12 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
alertQuery: AlertQuery{ |
|
|
|
|
RefID: "A", |
|
|
|
|
Model: json.RawMessage(`{ |
|
|
|
|
"datasourceUid": "000000001", |
|
|
|
|
"queryType": "metricQuery", |
|
|
|
|
"intervalMs": 0, |
|
|
|
|
"extraParam": "some text" |
|
|
|
|
}`), |
|
|
|
|
}, |
|
|
|
|
expectedIsExpression: false, |
|
|
|
|
expectedDatasourceUID: "000000001", |
|
|
|
|
expectedMaxPoints: int64(defaultMaxDataPoints), |
|
|
|
|
expectedIntervalMS: int64(defaultIntervalMS), |
|
|
|
|
}, |
|
|
|
|
@ -158,12 +141,6 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
assert.Equal(t, tc.expectedIsExpression, isExpression) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("can set datasource for expression", func(t *testing.T) { |
|
|
|
|
err := tc.alertQuery.setDatasource() |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, tc.expectedDatasourceUID, tc.alertQuery.DatasourceUID) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("can set queryType for expression", func(t *testing.T) { |
|
|
|
|
err := tc.alertQuery.setQueryType() |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
@ -189,13 +166,7 @@ func TestAlertQuery(t *testing.T) { |
|
|
|
|
err = json.Unmarshal(blob, &model) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
i, ok := model["datasourceUid"] |
|
|
|
|
require.True(t, ok) |
|
|
|
|
datasourceUID, ok := i.(string) |
|
|
|
|
require.True(t, ok) |
|
|
|
|
require.Equal(t, tc.expectedDatasourceUID, datasourceUID) |
|
|
|
|
|
|
|
|
|
i, ok = model["maxDataPoints"] |
|
|
|
|
i, ok := model["maxDataPoints"] |
|
|
|
|
require.True(t, ok) |
|
|
|
|
maxDataPoints, ok := i.(float64) |
|
|
|
|
require.True(t, ok) |
|
|
|
|
|