Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Commit

Permalink
modify existing tests for more aggregates
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaysngupta committed Nov 5, 2019
1 parent a3fc695 commit cc5658f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
15 changes: 13 additions & 2 deletions hack/e2e-scripts/configure-queue-metrics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ cp deploy/externalmetric.yaml deploy/externalmetric.yaml.copy
sed -i 's|sb-external-ns|'${SERVICEBUS_NAMESPACE}'|g' deploy/externalmetric.yaml
sed -i 's|sb-external-example|'${SERVICEBUS_RESOURCE_GROUP}'|g' deploy/externalmetric.yaml
sed -i 's|externalq|'${SERVICEBUS_QUEUE_NAME}'|g' deploy/externalmetric.yaml
kubectl apply -f deploy/externalmetric.yaml

rm deploy/externalmetric.yaml; mv deploy/externalmetric.yaml.copy deploy/externalmetric.yaml
AGGREGATE_TYPE=( "Average" "Maximum" "Minimum" "Total" )
# supported aggregates /~https://github.com/Azure/azure-sdk-for-go/blob/0acfc1d1083d148a606d380143176e218d437728/services/preview/monitor/mgmt/2018-03-01/insights/models.go#L38
for aggregate in "${AGGREGATE_TYPE[@]}"
do
filePath="deploy/${aggregate}.externalmetric.yaml"
echo "Creating ${aggregate} external metric with file: $filePath"
cp deploy/externalmetric.yaml $filePath
sed -i 's|Total|'${aggregate}'|g' $filePath

kubectl apply -f $filePath
done

rm deploy/*externalmetric.yaml; mv deploy/externalmetric.yaml.copy deploy/externalmetric.yaml
52 changes: 36 additions & 16 deletions pkg/azure/externalmetrics/monitor_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAzureMonitorIfFailedResponseGetError(t *testing.T) {

client := newMonitorClient("", monitorClient)

request := newAzureMonitorMetricRequest()
request := newAzureMonitorMetricRequest(insights.Average)
_, err := client.GetAzureMetric(request)

if err == nil {
Expand All @@ -44,28 +44,46 @@ func TestAzureMonitorIfFailedResponseGetError(t *testing.T) {
}

func TestAzureMonitorIfValidRequestGetResult(t *testing.T) {
response := makeAzureMonitorResponse(15)
monitorClient := newFakeMonitorClient(response, nil)
aggregateList := []insights.AggregationType{
insights.Average,
insights.Minimum,
insights.Maximum,
insights.Total,
}

client := newMonitorClient("", monitorClient)
for _, agg := range aggregateList {
response := makeAzureMonitorResponse(agg, 15)
monitorClient := newFakeMonitorClient(response, nil)

request := newAzureMonitorMetricRequest()
metricResponse, err := client.GetAzureMetric(request)
client := newMonitorClient("", monitorClient)

if err != nil {
t.Errorf("error after processing got: %v, want nil", err)
}
request := newAzureMonitorMetricRequest(agg)
metricResponse, err := client.GetAzureMetric(request)

if metricResponse.Total != 15 {
t.Errorf("metricResponse.Total = %v, want = %v", metricResponse.Total, 15)
if err != nil {
t.Errorf("error after processing got: %v, want nil", err)
}

if metricResponse.Value != 15 {
t.Errorf("metricresponse.Value = %v, want = %v", metricResponse.Value, 15)
}
}
}

func makeAzureMonitorResponse(value float64) insights.Response {
func makeAzureMonitorResponse(aggregateType insights.AggregationType, value float64) insights.Response {
// create metric value
mv := insights.MetricValue{
Total: &value,
mv := insights.MetricValue{}
switch aggregateType {
case insights.Average:
mv.Average = &value
case insights.Minimum:
mv.Minimum = &value
case insights.Maximum:
mv.Maximum = &value
case insights.Total:
mv.Total = &value
}

metricValues := []insights.MetricValue{}
metricValues = append(metricValues, mv)

Expand All @@ -77,8 +95,10 @@ func makeAzureMonitorResponse(value float64) insights.Response {
timeseries = append(timeseries, te)

// create metric
aType := string(aggregateType)
metric := insights.Metric{
Timeseries: &timeseries,
Type: &aType,
}
metrics := []insights.Metric{}
metrics = append(metrics, metric)
Expand All @@ -90,7 +110,7 @@ func makeAzureMonitorResponse(value float64) insights.Response {
return response
}

func newAzureMonitorMetricRequest() AzureExternalMetricRequest {
func newAzureMonitorMetricRequest(aggregationType insights.AggregationType) AzureExternalMetricRequest {
return AzureExternalMetricRequest{
ResourceGroup: "ResourceGroup",
ResourceName: "ResourceName",
Expand All @@ -99,7 +119,7 @@ func newAzureMonitorMetricRequest() AzureExternalMetricRequest {
SubscriptionID: "SubscriptionID",
MetricName: "MetricName",
Filter: "Filter",
Aggregation: "Aggregation",
Aggregation: string(aggregationType),
Timespan: "PT10",
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func TestIfValidRequestGetResult(t *testing.T) {
t.Errorf("error after processing got: %v, want nil", err)
}

if metricResponse.Total != 15 {
t.Errorf("metricResponse.Total = %v, want = %v", metricResponse.Total, 15)
if metricResponse.Value != 15 {
t.Errorf("metricResponse.Total = %v, want = %v", metricResponse.Value, 15)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/provider_external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ type fakeAzureExternalClientFactory struct {
func (f fakeAzureExternalClientFactory) GetAzureExternalMetricClient(clientType string) (client externalmetrics.AzureExternalMetricClient, err error) {
fakeClient := fakeAzureMonitorClient{
err: nil,
result: externalmetrics.AzureExternalMetricResponse{Total: 15},
result: externalmetrics.AzureExternalMetricResponse{Value: 15},
}

return fakeClient, nil
Expand Down

0 comments on commit cc5658f

Please sign in to comment.