-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_test.go
185 lines (170 loc) · 6.63 KB
/
metrics_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metrics
import (
"context"
"errors"
"fmt"
"net/http"
"testing"
"github.com/google/go-cmp/cmp"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
)
const (
hyperdiskBalanced = "hyperdisk-balanced"
)
func CreateDiskWithConfidentialCompute(confidentialCompute bool, diskType string) *gce.CloudDisk {
return gce.CloudDiskFromV1(&compute.Disk{
EnableConfidentialCompute: confidentialCompute,
Type: diskType,
})
}
func CreateDiskWithStoragePool(storagePool string, diskType string) *gce.CloudDisk {
return gce.CloudDiskFromV1(&compute.Disk{
StoragePool: storagePool,
Type: diskType,
})
}
func TestGetMetricParameters(t *testing.T) {
testCases := []struct {
name string
disk *gce.CloudDisk
expectedEnableConfidentialCompute string
expectedDiskType string
expectedEnableStoragePools string
}{
{
name: "test disk with enableConfidentialCompute=false",
disk: CreateDiskWithConfidentialCompute(false, hyperdiskBalanced),
expectedEnableConfidentialCompute: "false",
expectedDiskType: hyperdiskBalanced,
expectedEnableStoragePools: "false",
},
{
name: "test disk with enableConfidentialCompute=true",
disk: CreateDiskWithConfidentialCompute(true, hyperdiskBalanced),
expectedEnableConfidentialCompute: "true",
expectedDiskType: hyperdiskBalanced,
expectedEnableStoragePools: "false",
},
{
name: "test disk with storage pool projects/my-project/zone/us-central1-a/storagePools/sp1",
disk: CreateDiskWithStoragePool("projects/my-project/zone/us-central1-a/storagePools/sp1", hyperdiskBalanced),
expectedEnableConfidentialCompute: "false",
expectedDiskType: hyperdiskBalanced,
expectedEnableStoragePools: "true",
},
{
name: "test disk with no storage pool",
disk: CreateDiskWithStoragePool("", hyperdiskBalanced),
expectedEnableConfidentialCompute: "false",
expectedDiskType: hyperdiskBalanced,
expectedEnableStoragePools: "false",
},
{
name: "test nil disk",
disk: nil,
expectedEnableConfidentialCompute: DefaultEnableConfidentialCompute,
expectedDiskType: DefaultDiskTypeForMetric,
expectedEnableStoragePools: DefaultEnableStoragePools,
},
}
for _, tc := range testCases {
t.Logf("Running test: %v", tc.name)
diskType, confidentialCompute, enableStoragePools := GetMetricParameters(tc.disk)
if confidentialCompute != tc.expectedEnableConfidentialCompute {
t.Fatalf("Got confidentialCompute value %q expected %q", confidentialCompute, tc.expectedEnableConfidentialCompute)
}
if diskType != tc.expectedDiskType {
t.Fatalf("Got diskType value %q expected %q", diskType, tc.expectedDiskType)
}
if enableStoragePools != tc.expectedEnableStoragePools {
t.Fatalf("Got enableStoragePools value %q expected %q", enableStoragePools, tc.expectedEnableStoragePools)
}
}
}
func TestErrorCodeLabelValue(t *testing.T) {
testCases := []struct {
name string
operationErr error
wantErrorCode string
}{
{
name: "Not googleapi.Error",
operationErr: errors.New("I am not a googleapi.Error"),
wantErrorCode: "Internal",
},
{
name: "User error",
operationErr: &googleapi.Error{Code: http.StatusBadRequest, Message: "User error with bad request"},
wantErrorCode: "InvalidArgument",
},
{
name: "googleapi.Error but not a user error",
operationErr: &googleapi.Error{Code: http.StatusInternalServerError, Message: "Internal error"},
wantErrorCode: "Internal",
},
{
name: "context canceled error",
operationErr: context.Canceled,
wantErrorCode: "Canceled",
},
{
name: "context deadline exceeded error",
operationErr: context.DeadlineExceeded,
wantErrorCode: "DeadlineExceeded",
},
{
name: "status error with Aborted error code",
operationErr: status.Error(codes.Aborted, "aborted error"),
wantErrorCode: "Aborted",
},
{
name: "user multiattach error",
operationErr: fmt.Errorf("The disk resource 'projects/foo/disk/bar' is already being used by 'projects/foo/instances/1'"),
wantErrorCode: "InvalidArgument",
},
{
name: "TemporaryError that wraps googleapi error",
operationErr: common.NewTemporaryError(codes.Unavailable, &googleapi.Error{Code: http.StatusBadRequest, Message: "User error with bad request"}),
wantErrorCode: "InvalidArgument",
},
{
name: "TemporaryError that wraps fmt.Errorf, which wraps googleapi error",
operationErr: common.NewTemporaryError(codes.Aborted, fmt.Errorf("got error: %w", &googleapi.Error{Code: http.StatusBadRequest, Message: "User error with bad request"})),
wantErrorCode: "InvalidArgument",
},
{
name: "TemporaryError that wraps status error",
operationErr: common.NewTemporaryError(codes.Aborted, status.Error(codes.InvalidArgument, "User error with bad request")),
wantErrorCode: "InvalidArgument",
},
{
name: "TemporaryError that wraps multiattach error",
operationErr: common.NewTemporaryError(codes.Unavailable, fmt.Errorf("The disk resource 'projects/foo/disk/bar' is already being used by 'projects/foo/instances/1'")),
wantErrorCode: "InvalidArgument",
},
}
for _, tc := range testCases {
t.Logf("Running test: %v", tc.name)
errCode := errorCodeLabelValue(tc.operationErr)
if diff := cmp.Diff(tc.wantErrorCode, errCode); diff != "" {
t.Errorf("%s: -want err, +got err\n%s", tc.name, diff)
}
}
}