-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests for instrument package and addressed review comments
- Loading branch information
1 parent
e5cb64a
commit b850d3a
Showing
16 changed files
with
912 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package instrument | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/codes" | ||
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/status" | ||
"github.com/gardener/machine-controller-manager/pkg/util/provider/metrics" | ||
. "github.com/onsi/gomega" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
) | ||
|
||
var ( | ||
testErr = errors.New("test-error") | ||
defaultErrorCode = strconv.Itoa(int(codes.Internal)) | ||
testStatusErr = status.New(codes.InvalidArgument, "test-status-error") | ||
) | ||
|
||
const serviceName = "test-service" | ||
|
||
func TestAPIMetricRecorderFn(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
err error | ||
}{ | ||
{"assert that function captures failed API request count when the error is not nil", testErr}, | ||
{"assert that function captures successful API request count when the error is nil", nil}, | ||
} | ||
g := NewWithT(t) | ||
reg := prometheus.NewRegistry() | ||
g.Expect(reg.Register(metrics.APIRequestCount)).To(Succeed()) | ||
g.Expect(reg.Register(metrics.APIFailedRequestCount)).To(Succeed()) | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
defer metrics.APIRequestCount.Reset() | ||
defer metrics.APIFailedRequestCount.Reset() | ||
_ = deferredMetricsRecorderInvoker(tc.err != nil, false, AZAPIMetricRecorderFn) | ||
if tc.err != nil { | ||
g.Expect(testutil.CollectAndCount(metrics.APIFailedRequestCount)).To(Equal(1)) | ||
g.Expect(testutil.ToFloat64(metrics.APIFailedRequestCount.WithLabelValues(prometheusProviderLabelValue, serviceName))).To(Equal(float64(1))) | ||
} else { | ||
g.Expect(testutil.CollectAndCount(metrics.APIRequestCount)).To(Equal(1)) | ||
g.Expect(testutil.ToFloat64(metrics.APIRequestCount.WithLabelValues(prometheusProviderLabelValue, serviceName))).To(Equal(float64(1))) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDriverAPIMetricRecorderFn(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
err error | ||
}{ | ||
{"assert that function captures failed driver API request with default error code for internal error when there is an error", testErr}, | ||
{"assert that function captures failed driver API request with error code from status.Status on error", testStatusErr}, | ||
{"assert that function captures successful driver API request count when the error is nil", nil}, | ||
} | ||
g := NewWithT(t) | ||
reg := prometheus.NewRegistry() | ||
g.Expect(reg.Register(metrics.DriverFailedAPIRequests)).To(Succeed()) | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
defer metrics.DriverFailedAPIRequests.Reset() | ||
_ = deferredMetricsRecorderInvoker(tc.err != nil, isStatusErr(tc.err), DriverAPIMetricRecorderFn) | ||
if tc.err != nil { | ||
expectedErrCode := getExpectedErrorCode(tc.err) | ||
g.Expect(testutil.CollectAndCount(metrics.DriverFailedAPIRequests)).To(Equal(1)) | ||
g.Expect(testutil.ToFloat64(metrics.DriverFailedAPIRequests.WithLabelValues(prometheusProviderLabelValue, serviceName, expectedErrCode))).To(Equal(float64(1))) | ||
} else { | ||
g.Expect(testutil.CollectAndCount(metrics.DriverFailedAPIRequests)).To(Equal(0)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func isStatusErr(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
var statusErr *status.Status | ||
return errors.As(err, &statusErr) | ||
} | ||
|
||
func getExpectedErrorCode(err error) string { | ||
if err == nil { | ||
return "" | ||
} | ||
var statusErr *status.Status | ||
if errors.As(err, &statusErr) { | ||
return strconv.Itoa(int(statusErr.Code())) | ||
} else { | ||
return defaultErrorCode | ||
} | ||
} | ||
|
||
type recorderFn func(string, *error) func() | ||
|
||
func deferredMetricsRecorderInvoker(shouldReturnErr bool, isStatusErr bool, fn recorderFn) (err error) { | ||
defer fn(serviceName, &err)() | ||
if shouldReturnErr { | ||
if isStatusErr { | ||
err = testStatusErr | ||
} else { | ||
err = testErr | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
vendor/github.com/prometheus/client_golang/prometheus/testutil/lint.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.