Skip to content

Commit

Permalink
move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meeech committed Sep 15, 2023
1 parent 3ae30f9 commit ca837a8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 83 deletions.
83 changes: 0 additions & 83 deletions metricproviders/datadog/datadogV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,86 +460,3 @@ func TestRunSuiteV2(t *testing.T) {

}
}

func TestValidateIncomingProps(t *testing.T) {
tests := []struct {
name string
metric *v1alpha1.DatadogMetric
expectedErrorMessage string
}{
{
name: "query and queries missing",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v1",
Query: "",
Queries: nil,
},
expectedErrorMessage: "Must have either a query or queries",
},
{
name: "both query and queries",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v1",
Query: "foo",
Queries: map[string]string{"a": "how much wood could a wood chuck chuck"},
},
expectedErrorMessage: "Cannot have both a query and queries",
},
{
name: "queries with v1 api",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v1",
Queries: map[string]string{"a": "how much wood could a wood chuck chuck"},
},
expectedErrorMessage: "Query is empty. API Version v1 only supports using the query parameter in your Analysis Template.",
},
{
name: "formula/queries with wrong apiVersion",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v1",
Queries: map[string]string{"a": "how much wood could a wood chuck chuck"},
Formula: "a + b",
},
expectedErrorMessage: "Query is empty. API Version v1 only supports using the query parameter in your Analysis Template.",
},
{
name: "formula without queries",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v1",
Formula: "foo / bar",
Query: "foo",
},
expectedErrorMessage: "Formula are only valid when queries are set",
},
{
name: "valid simple query with v2",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v2",
Query: "foo",
},
expectedErrorMessage: "",
},
{
name: "valid queries with v2",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v2",
Query: "",
Queries: map[string]string{"a": "how much wood could a wood chuck chuck"},
Formula: "a + b",
},
expectedErrorMessage: "",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := validateIncomingProps(test.metric)
if test.expectedErrorMessage != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), test.expectedErrorMessage)
} else {
assert.NoError(t, err)
}
})
}
}
84 changes: 84 additions & 0 deletions metricproviders/datadog/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"testing"

"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
"github.com/stretchr/testify/assert"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -53,3 +54,86 @@ func TestDatadogSpecDefaults(t *testing.T) {
assert.Equal(t, "\"5m\"", defaultInterval, "Default version should be v1")
})
}

func TestValidateIncomingProps(t *testing.T) {
tests := []struct {
name string
metric *v1alpha1.DatadogMetric
expectedErrorMessage string
}{
{
name: "query and queries missing",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v1",
Query: "",
Queries: nil,
},
expectedErrorMessage: "Must have either a query or queries",
},
{
name: "both query and queries",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v1",
Query: "foo",
Queries: map[string]string{"a": "how much wood could a wood chuck chuck"},
},
expectedErrorMessage: "Cannot have both a query and queries",
},
{
name: "queries with v1 api",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v1",
Queries: map[string]string{"a": "how much wood could a wood chuck chuck"},
},
expectedErrorMessage: "Query is empty. API Version v1 only supports using the query parameter in your Analysis Template.",
},
{
name: "formula/queries with wrong apiVersion",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v1",
Queries: map[string]string{"a": "how much wood could a wood chuck chuck"},
Formula: "a + b",
},
expectedErrorMessage: "Query is empty. API Version v1 only supports using the query parameter in your Analysis Template.",
},
{
name: "formula without queries",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v1",
Formula: "foo / bar",
Query: "foo",
},
expectedErrorMessage: "Formula are only valid when queries are set",
},
{
name: "valid simple query with v2",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v2",
Query: "foo",
},
expectedErrorMessage: "",
},
{
name: "valid queries with v2",
metric: &v1alpha1.DatadogMetric{
ApiVersion: "v2",
Query: "",
Queries: map[string]string{"a": "how much wood could a wood chuck chuck"},
Formula: "a + b",
},
expectedErrorMessage: "",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := validateIncomingProps(test.metric)
if test.expectedErrorMessage != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), test.expectedErrorMessage)
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit ca837a8

Please sign in to comment.