forked from kubeflow/model-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserving_environment_test.go
395 lines (317 loc) · 14.5 KB
/
serving_environment_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package core
import (
"context"
"github.com/kubeflow/model-registry/internal/apiutils"
"github.com/kubeflow/model-registry/internal/converter"
"github.com/kubeflow/model-registry/internal/ml_metadata/proto"
"github.com/kubeflow/model-registry/pkg/api"
"github.com/kubeflow/model-registry/pkg/openapi"
)
// SERVING ENVIRONMENT
func (suite *CoreTestSuite) TestCreateServingEnvironment() {
// create mode registry service
service := suite.setupModelRegistryService()
// register a new ServingEnvironment
eut := &openapi.ServingEnvironment{
Name: &entityName,
ExternalId: &entityExternalId,
Description: &entityDescription,
CustomProperties: &map[string]openapi.MetadataValue{
"myCustomProp": {
MetadataStringValue: converter.NewMetadataStringValue(myCustomProp),
},
},
}
// test
createdEntity, err := service.UpsertServingEnvironment(eut)
// checks
suite.Nilf(err, "error creating uut: %v", err)
suite.NotNilf(createdEntity.Id, "created uut should not have nil Id")
createdEntityId, _ := converter.StringToInt64(createdEntity.Id)
ctxById, err := suite.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{*createdEntityId},
})
suite.Nilf(err, "error retrieving context by type and name, not related to the test itself: %v", err)
ctx := ctxById.Contexts[0]
ctxId := converter.Int64ToString(ctx.Id)
suite.Equal(*createdEntity.Id, *ctxId, "returned id should match the mlmd one")
suite.Equal(entityName, *ctx.Name, "saved name should match the provided one")
suite.Equal(entityExternalId, *ctx.ExternalId, "saved external id should match the provided one")
suite.Equal(entityDescription, ctx.Properties["description"].GetStringValue(), "saved description should match the provided one")
suite.Equal(myCustomProp, ctx.CustomProperties["myCustomProp"].GetStringValue(), "saved myCustomProp custom property should match the provided one")
getAllResp, err := suite.mlmdClient.GetContexts(context.Background(), &proto.GetContextsRequest{})
suite.Nilf(err, "error retrieving all contexts, not related to the test itself: %v", err)
suite.Equal(1, len(getAllResp.Contexts), "there should be just one context saved in mlmd")
}
func (suite *CoreTestSuite) TestUpdateServingEnvironment() {
// create mode registry service
service := suite.setupModelRegistryService()
// register a new ServingEnvironment
eut := &openapi.ServingEnvironment{
Name: &entityName,
ExternalId: &entityExternalId,
CustomProperties: &map[string]openapi.MetadataValue{
"myCustomProp": {
MetadataStringValue: converter.NewMetadataStringValue(myCustomProp),
},
},
}
// test
createdEntity, err := service.UpsertServingEnvironment(eut)
// checks
suite.Nilf(err, "error creating uut: %v", err)
suite.NotNilf(createdEntity.Id, "created uut should not have nil Id")
createdEntityId, _ := converter.StringToInt64(createdEntity.Id)
// checks created entity matches original one except for Id
suite.Equal(*eut.Name, *createdEntity.Name, "returned entity should match the original one")
suite.Equal(*eut.ExternalId, *createdEntity.ExternalId, "returned entity external id should match the original one")
suite.Equal(*eut.CustomProperties, *createdEntity.CustomProperties, "returned entity custom props should match the original one")
// update existing entity
newExternalId := "newExternalId"
newCustomProp := "newCustomProp"
createdEntity.ExternalId = &newExternalId
(*createdEntity.CustomProperties)["myCustomProp"] = openapi.MetadataValue{
MetadataStringValue: converter.NewMetadataStringValue(newCustomProp),
}
// update the entity
createdEntity, err = service.UpsertServingEnvironment(createdEntity)
suite.Nilf(err, "error creating uut: %v", err)
// still one expected MLMD type
getAllResp, err := suite.mlmdClient.GetContexts(context.Background(), &proto.GetContextsRequest{})
suite.Nilf(err, "error retrieving all contexts, not related to the test itself: %v", err)
suite.Equal(1, len(getAllResp.Contexts), "there should be just one context saved in mlmd")
ctxById, err := suite.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{*createdEntityId},
})
suite.Nilf(err, "error retrieving context by type and name, not related to the test itself: %v", err)
ctx := ctxById.Contexts[0]
ctxId := converter.Int64ToString(ctx.Id)
suite.Equal(*createdEntity.Id, *ctxId, "returned entity id should match the mlmd one")
suite.Equal(entityName, *ctx.Name, "saved entity name should match the provided one")
suite.Equal(newExternalId, *ctx.ExternalId, "saved external id should match the provided one")
suite.Equal(newCustomProp, ctx.CustomProperties["myCustomProp"].GetStringValue(), "saved myCustomProp custom property should match the provided one")
// update the entity under test, keeping nil name
newExternalId = "newNewExternalId"
createdEntity.ExternalId = &newExternalId
createdEntity.Name = nil
createdEntity, err = service.UpsertServingEnvironment(createdEntity)
suite.Nilf(err, "error creating entity: %v", err)
// still one registered entity
getAllResp, err = suite.mlmdClient.GetContexts(context.Background(), &proto.GetContextsRequest{})
suite.Nilf(err, "error retrieving all contexts, not related to the test itself: %v", err)
suite.Equal(1, len(getAllResp.Contexts), "there should be just one context saved in mlmd")
ctxById, err = suite.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{*createdEntityId},
})
suite.Nilf(err, "error retrieving context by type and name, not related to the test itself: %v", err)
ctx = ctxById.Contexts[0]
ctxId = converter.Int64ToString(ctx.Id)
suite.Equal(*createdEntity.Id, *ctxId, "returned entity id should match the mlmd one")
suite.Equal(entityName, *ctx.Name, "saved entity name should match the provided one")
suite.Equal(newExternalId, *ctx.ExternalId, "saved external id should match the provided one")
suite.Equal(newCustomProp, ctx.CustomProperties["myCustomProp"].GetStringValue(), "saved myCustomProp custom property should match the provided one")
}
func (suite *CoreTestSuite) TestGetServingEnvironmentById() {
// create mode registry service
service := suite.setupModelRegistryService()
// register a new entity
eut := &openapi.ServingEnvironment{
Name: &entityName,
ExternalId: &entityExternalId,
CustomProperties: &map[string]openapi.MetadataValue{
"myCustomProp": {
MetadataStringValue: converter.NewMetadataStringValue(myCustomProp),
},
},
}
// test
createdEntity, err := service.UpsertServingEnvironment(eut)
// checks
suite.Nilf(err, "error creating eut: %v", err)
getEntityById, err := service.GetServingEnvironmentById(*createdEntity.Id)
suite.Nilf(err, "error getting eut by id %s: %v", *createdEntity.Id, err)
// checks created entity matches original one except for Id
suite.Equal(*eut.Name, *getEntityById.Name, "saved name should match the original one")
suite.Equal(*eut.ExternalId, *getEntityById.ExternalId, "saved external id should match the original one")
suite.Equal(*eut.CustomProperties, *getEntityById.CustomProperties, "saved custom props should match the original one")
}
func (suite *CoreTestSuite) TestGetServingEnvironmentByParamsWithNoResults() {
// create mode registry service
service := suite.setupModelRegistryService()
_, err := service.GetServingEnvironmentByParams(apiutils.Of("not-present"), nil)
suite.NotNil(err)
suite.Equal("no serving environments found for name=not-present, externalId=: not found", err.Error())
}
func (suite *CoreTestSuite) TestGetServingEnvironmentByParamsName() {
// create mode registry service
service := suite.setupModelRegistryService()
// register a new ServingEnvironment
eut := &openapi.ServingEnvironment{
Name: &entityName,
ExternalId: &entityExternalId,
}
createdEntity, err := service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
byName, err := service.GetServingEnvironmentByParams(&entityName, nil)
suite.Nilf(err, "error getting ServingEnvironment by name: %v", err)
suite.Equalf(*createdEntity.Id, *byName.Id, "the returned entity id should match the retrieved by name")
}
func (suite *CoreTestSuite) TestGetServingEnvironmentByParamsExternalId() {
// create mode registry service
service := suite.setupModelRegistryService()
// register a new ServingEnvironment
eut := &openapi.ServingEnvironment{
Name: &entityName,
ExternalId: &entityExternalId,
}
createdEntity, err := service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
byName, err := service.GetServingEnvironmentByParams(nil, &entityExternalId)
suite.Nilf(err, "error getting ServingEnvironment by external id: %v", err)
suite.Equalf(*createdEntity.Id, *byName.Id, "the returned entity id should match the retrieved by name")
}
func (suite *CoreTestSuite) TestGetServingEnvironmentByEmptyParams() {
// create mode registry service
service := suite.setupModelRegistryService()
// register a new ServingEnvironment
eut := &openapi.ServingEnvironment{
Name: &entityName,
ExternalId: &entityExternalId,
}
_, err := service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
_, err = service.GetServingEnvironmentByParams(nil, nil)
suite.NotNil(err)
suite.Equal("invalid parameters call, supply either name or externalId: bad request", err.Error())
}
func (suite *CoreTestSuite) TestGetServingEnvironmentsOrderedById() {
// create mode registry service
service := suite.setupModelRegistryService()
orderBy := "ID"
// register a new ServingEnvironment
eut := &openapi.ServingEnvironment{
Name: &entityName,
ExternalId: &entityExternalId,
}
_, err := service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
newName := "Pricingentity2"
newExternalId := "myExternalId2"
eut.Name = &newName
eut.ExternalId = &newExternalId
_, err = service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
newName = "Pricingentity3"
newExternalId = "myExternalId3"
eut.Name = &newName
eut.ExternalId = &newExternalId
_, err = service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
orderedById, err := service.GetServingEnvironments(api.ListOptions{
OrderBy: &orderBy,
SortOrder: &ascOrderDirection,
})
suite.Nilf(err, "error getting ServingEnvironment: %v", err)
suite.Equal(3, int(orderedById.Size))
for i := 0; i < int(orderedById.Size)-1; i++ {
suite.Less(*orderedById.Items[i].Id, *orderedById.Items[i+1].Id)
}
orderedById, err = service.GetServingEnvironments(api.ListOptions{
OrderBy: &orderBy,
SortOrder: &descOrderDirection,
})
suite.Nilf(err, "error getting ServingEnvironments: %v", err)
suite.Equal(3, int(orderedById.Size))
for i := 0; i < int(orderedById.Size)-1; i++ {
suite.Greater(*orderedById.Items[i].Id, *orderedById.Items[i+1].Id)
}
}
func (suite *CoreTestSuite) TestGetServingEnvironmentsOrderedByLastUpdate() {
// create mode registry service
service := suite.setupModelRegistryService()
orderBy := "LAST_UPDATE_TIME"
// register a new ServingEnvironment
eut := &openapi.ServingEnvironment{
Name: &entityName,
ExternalId: &entityExternalId,
}
firstEntity, err := service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
newName := "Pricingentity2"
newExternalId := "myExternalId2"
eut.Name = &newName
eut.ExternalId = &newExternalId
secondEntity, err := service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
newName = "Pricingentity3"
newExternalId = "myExternalId3"
eut.Name = &newName
eut.ExternalId = &newExternalId
thirdEntity, err := service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
// update second entity
secondEntity.ExternalId = nil
_, err = service.UpsertServingEnvironment(secondEntity)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
orderedById, err := service.GetServingEnvironments(api.ListOptions{
OrderBy: &orderBy,
SortOrder: &ascOrderDirection,
})
suite.Nilf(err, "error getting ServingEnvironments: %v", err)
suite.Equal(3, int(orderedById.Size))
suite.Equal(*firstEntity.Id, *orderedById.Items[0].Id)
suite.Equal(*thirdEntity.Id, *orderedById.Items[1].Id)
suite.Equal(*secondEntity.Id, *orderedById.Items[2].Id)
orderedById, err = service.GetServingEnvironments(api.ListOptions{
OrderBy: &orderBy,
SortOrder: &descOrderDirection,
})
suite.Nilf(err, "error getting ServingEnvironments: %v", err)
suite.Equal(3, int(orderedById.Size))
suite.Equal(*secondEntity.Id, *orderedById.Items[0].Id)
suite.Equal(*thirdEntity.Id, *orderedById.Items[1].Id)
suite.Equal(*firstEntity.Id, *orderedById.Items[2].Id)
}
func (suite *CoreTestSuite) TestGetServingEnvironmentsWithPageSize() {
// create mode registry service
service := suite.setupModelRegistryService()
pageSize := int32(1)
pageSize2 := int32(2)
entityName := "Pricingentity1"
entityExternalId := "myExternalId1"
// register a new ServingEnvironment
eut := &openapi.ServingEnvironment{
Name: &entityName,
ExternalId: &entityExternalId,
}
firstEntity, err := service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating registered entity: %v", err)
newName := "Pricingentity2"
newExternalId := "myExternalId2"
eut.Name = &newName
eut.ExternalId = &newExternalId
secondEntity, err := service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
newName = "Pricingentity3"
newExternalId = "myExternalId3"
eut.Name = &newName
eut.ExternalId = &newExternalId
thirdEntity, err := service.UpsertServingEnvironment(eut)
suite.Nilf(err, "error creating ServingEnvironment: %v", err)
truncatedList, err := service.GetServingEnvironments(api.ListOptions{
PageSize: &pageSize,
})
suite.Nilf(err, "error getting ServingEnvironments: %v", err)
suite.Equal(1, int(truncatedList.Size))
suite.NotEqual("", truncatedList.NextPageToken, "next page token should not be empty")
suite.Equal(*firstEntity.Id, *truncatedList.Items[0].Id)
truncatedList, err = service.GetServingEnvironments(api.ListOptions{
PageSize: &pageSize2,
NextPageToken: &truncatedList.NextPageToken,
})
suite.Nilf(err, "error getting ServingEnvironments: %v", err)
suite.Equal(2, int(truncatedList.Size))
suite.Equal("", truncatedList.NextPageToken, "next page token should be empty as list item returned")
suite.Equal(*secondEntity.Id, *truncatedList.Items[0].Id)
suite.Equal(*thirdEntity.Id, *truncatedList.Items[1].Id)
}