Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Eftimie <[email protected]>
  • Loading branch information
alexef committed Jul 18, 2024
1 parent b967b7e commit c9a5326
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions metricproviders/datadog/datadogV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func newQueryProviderInterval10m() v1alpha1.MetricProvider {
}
}

func newQueryProviderSumAggregator() v1alpha1.MetricProvider {
return v1alpha1.MetricProvider{
Datadog: &v1alpha1.DatadogMetric{
Query: "avg:kubernetes.cpu.user.total{*}",
Interval: "5m",
Aggregator: "sum",
ApiVersion: "v2",
},
}
}
func TestRunSuiteV2(t *testing.T) {
const expectedApiKey = "0123456789abcdef0123456789abcdef"
const expectedAppKey = "0123456789abcdef0123456789abcdef01234567"
Expand All @@ -68,6 +78,7 @@ func TestRunSuiteV2(t *testing.T) {
expectedValue string
expectedPhase v1alpha1.AnalysisPhase
expectedErrorMessage string
expectedAggregator string
useEnvVarForKeys bool
}{
{
Expand Down Expand Up @@ -252,6 +263,21 @@ func TestRunSuiteV2(t *testing.T) {
expectedPhase: v1alpha1.AnalysisPhaseSuccessful,
useEnvVarForKeys: false,
},

{
webServerStatus: 200,
webServerResponse: `{"data": {"attributes": {"columns": [ {"values": [0.006121378742186943]}]}}}`,
metric: v1alpha1.Metric{
Name: "success with default and data",
SuccessCondition: "default(result, 0) < 0.05",
Provider: newQueryProviderSumAggregator(),
},
expectedIntervalSeconds: 300,
expectedValue: "0.006121378742186943",
expectedPhase: v1alpha1.AnalysisPhaseSuccessful,
expectedAggregator: "sum",
useEnvVarForKeys: false,
},
}

// Run
Expand Down Expand Up @@ -298,6 +324,16 @@ func TestRunSuiteV2(t *testing.T) {
t.Errorf("\nExpected formula but no Formulas in request: %+v", actualFormulas)
}
}
// Check query aggregation being set
expectedAggregator := test.expectedAggregator
if expectedAggregator == "" {
expectedAggregator = "last"
}
for _, query := range actualQueries {
if query["aggregator"] != expectedAggregator {
t.Errorf("\naggregator expected %s but got %s", expectedAggregator, query["aggregator"])
}
}

if actualFrom != (unixNow()-test.expectedIntervalSeconds)*1000 {
t.Errorf("\nfrom %d expected be equal to %d", actualFrom, (unixNow()-test.expectedIntervalSeconds)*1000)
Expand Down

0 comments on commit c9a5326

Please sign in to comment.