Skip to content

Commit 4caa8da

Browse files
committed
Add unit test for mount error type
1 parent 6c3322f commit 4caa8da

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

pkg/metrics/metrics.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,9 @@ func (mm *MetricsManager) RecordOperationErrorMetrics(
116116
}
117117

118118
func (mm *MetricsManager) RecordMountErrorMetric(fs_format string, err error) {
119-
errType := "OK"
120-
mntErr := &mount.MountError{}
121-
if errors.As(err, mntErr) {
122-
errType = string(mntErr.Type)
123-
}
119+
errType := mountErrorType(err)
124120
mountErrorMetric.WithLabelValues(pdcsiDriverName, fs_format, errType).Inc()
125-
126-
klog.Infof("Recorded mount error type: %q", mntErr.Type)
121+
klog.Infof("Recorded mount error type: %q", errType)
127122
}
128123

129124
func (mm *MetricsManager) EmmitProcessStartTime() error {
@@ -198,3 +193,16 @@ func errorCodeLabelValue(operationErr error) string {
198193
}
199194
return err
200195
}
196+
197+
func mountErrorType(err error) string {
198+
if err == nil {
199+
return "OK"
200+
}
201+
202+
mntErr := &mount.MountError{}
203+
if !errors.As(err, mntErr) {
204+
return "UnknownError"
205+
}
206+
207+
return string(mntErr.Type)
208+
}

pkg/metrics/metrics_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"google.golang.org/api/googleapi"
3030
"google.golang.org/grpc/codes"
3131
"google.golang.org/grpc/status"
32+
"k8s.io/mount-utils"
3233

3334
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
3435
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
@@ -186,3 +187,35 @@ func TestErrorCodeLabelValue(t *testing.T) {
186187
}
187188
}
188189
}
190+
191+
func TestMountOperationError(t *testing.T) {
192+
testCases := []struct {
193+
name string
194+
err error
195+
want string
196+
}{
197+
{
198+
name: "no error",
199+
want: "OK",
200+
},
201+
{
202+
name: "unknown error",
203+
err: fmt.Errorf("fake error"),
204+
want: "UnknownError",
205+
},
206+
{
207+
name: "mount error",
208+
err: mount.NewMountError(mount.FormatFailed, "file system format failed"),
209+
want: string(mount.FormatFailed),
210+
},
211+
}
212+
213+
for _, tc := range testCases {
214+
t.Run(tc.name, func(t *testing.T) {
215+
got := mountErrorType(tc.err)
216+
if diff := cmp.Diff(tc.want, got); diff != "" {
217+
t.Errorf("%s: -want err, +got err\n%s", tc.name, diff)
218+
}
219+
})
220+
}
221+
}

0 commit comments

Comments
 (0)