-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathresources_test.go
More file actions
345 lines (321 loc) · 12.7 KB
/
Copy pathresources_test.go
File metadata and controls
345 lines (321 loc) · 12.7 KB
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
package config
import (
"encoding/json"
"net/url"
"reflect"
"strings"
"testing"
"github.com/databricks/databricks-sdk-go/service/database"
"github.com/databricks/databricks-sdk-go/service/sql"
"github.com/databricks/databricks-sdk-go/service/workspace"
"github.com/databricks/databricks-sdk-go/service/serving"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/libs/workspaceurls"
"github.com/databricks/databricks-sdk-go/experimental/mocks"
"github.com/databricks/databricks-sdk-go/service/apps"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/databricks/databricks-sdk-go/service/ml"
"github.com/databricks/databricks-sdk-go/service/pipelines"
"github.com/databricks/databricks-sdk-go/service/postgres"
"github.com/databricks/databricks-sdk-go/service/vectorsearch"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)
// This test ensures that all resources have a custom marshaller and unmarshaller.
// This is required because DABs resources map to Databricks APIs, and they do so
// by embedding the corresponding Go SDK structs.
//
// Go SDK structs often implement custom marshalling and unmarshalling methods (based on the API specifics).
// If the Go SDK struct implements custom marshalling and unmarshalling and we do not
// for the resources at the top level, marshalling and unmarshalling operations will panic.
// Thus we will be overly cautious and ensure that all resources need a custom marshaller and unmarshaller.
//
// Why do we not assert this using an interface to assert MarshalJSON and UnmarshalJSON
// are implemented at the top level?
// If a method is implemented for an embedded struct, the top level struct will
// also have that method and satisfy the interface. This is why we cannot assert
// that the methods are implemented at the top level using an interface.
//
// Why don't we use reflection to assert that the methods are implemented at the
// top level?
// Same problem as above, the golang reflection package does not seem to provide
// a way to directly assert that MarshalJSON and UnmarshalJSON are implemented
// at the top level.
func TestCustomMarshallerIsImplemented(t *testing.T) {
rt := reflect.TypeFor[Resources]()
for field := range rt.Fields() {
// Fields in Resources are expected be of the form map[string]*resourceStruct
assert.Equal(t, reflect.Map, field.Type.Kind(), "Resource %s is not a map", field.Name)
kt := field.Type.Key()
assert.Equal(t, reflect.String, kt.Kind(), "Resource %s is not a map with string keys", field.Name)
vt := field.Type.Elem()
assert.Equal(t, reflect.Pointer, vt.Kind(), "Resource %s is not a map with pointer values", field.Name)
// Marshalling a resourceStruct will panic if resourceStruct does not have a custom marshaller
// This is because resourceStruct embeds a Go SDK struct that implements
// a custom marshaller.
// Eg: resource.Job implements MarshalJSON
v := reflect.Zero(vt.Elem()).Interface()
assert.NotPanics(t, func() {
_, err := json.Marshal(v)
assert.NoError(t, err)
}, "Resource %s does not have a custom marshaller", field.Name)
// Unmarshalling a *resourceStruct will panic if the resource does not have a custom unmarshaller
// This is because resourceStruct embeds a Go SDK struct that implements
// a custom unmarshaller.
// Eg: *resource.Job implements UnmarshalJSON
v = reflect.New(vt.Elem()).Interface()
assert.NotPanics(t, func() {
err := json.Unmarshal([]byte("{}"), v)
assert.NoError(t, err)
}, "Resource %s does not have a custom unmarshaller", field.Name)
}
}
func TestResourcesAllResourcesCompleteness(t *testing.T) {
r := Resources{}
rt := reflect.TypeFor[Resources]()
// Collect set of includes resource types
var types []string
for _, group := range r.AllResources() {
types = append(types, group.Description.PluralName)
}
for field := range rt.Fields() {
jsonTag := field.Tag.Get("json")
if idx := strings.Index(jsonTag, ","); idx != -1 {
jsonTag = jsonTag[:idx]
}
assert.Contains(t, types, jsonTag, "Field %s is missing in AllResources", field.Name)
}
}
func TestSupportedResources(t *testing.T) {
// Please add your resource to the SupportedResources() function in resources.go if you add a new resource.
actual := SupportedResources()
typ := reflect.TypeFor[Resources]()
for field := range typ.Fields() {
jsonTags := strings.Split(field.Tag.Get("json"), ",")
pluralName := jsonTags[0]
assert.Equal(t, actual[pluralName].PluralName, pluralName)
}
}
// Bundle resources whose InitializeURL() resolves via workspaceurls. When a
// pattern key or a bundle plural name drifts, ResourceURL returns "" and this
// test fails loudly instead of silently producing empty URLs in bundle summary.
func TestBundleResourcePluralNamesResolveInWorkspaceURLs(t *testing.T) {
withURLs := []string{
"alerts",
"apps",
"clusters",
"dashboards",
"experiments",
"jobs",
"models",
"model_serving_endpoints",
"pipelines",
"registered_models",
"sql_warehouses",
}
supported := SupportedResources()
for _, name := range withURLs {
_, ok := supported[name]
require.Truef(t, ok, "%q is not a bundle plural name, update SupportedResources or this test", name)
}
base := url.URL{Scheme: "https", Host: "example.com"}
for _, name := range withURLs {
got := workspaceurls.ResourceURL(base, name, "test-id")
assert.NotEmptyf(t, got, "workspaceurls.ResourceURL(%q) returned empty; pattern key renamed or alias missing", name)
}
}
func TestResourcesBindSupport(t *testing.T) {
supportedResources := &Resources{
Jobs: map[string]*resources.Job{
"my_job": {
JobSettings: jobs.JobSettings{},
},
},
Pipelines: map[string]*resources.Pipeline{
"my_pipeline": {
CreatePipeline: pipelines.CreatePipeline{},
},
},
Experiments: map[string]*resources.MlflowExperiment{
"my_experiment": {
CreateExperiment: ml.CreateExperiment{},
},
},
RegisteredModels: map[string]*resources.RegisteredModel{
"my_registered_model": {
CreateRegisteredModelRequest: catalog.CreateRegisteredModelRequest{},
},
},
Catalogs: map[string]*resources.Catalog{
"my_catalog": {
CreateCatalog: catalog.CreateCatalog{},
},
},
ExternalLocations: map[string]*resources.ExternalLocation{
"my_external_location": {
CreateExternalLocation: catalog.CreateExternalLocation{},
},
},
Schemas: map[string]*resources.Schema{
"my_schema": {
CreateSchema: catalog.CreateSchema{},
},
},
Clusters: map[string]*resources.Cluster{
"my_cluster": {},
},
Dashboards: map[string]*resources.Dashboard{
"my_dashboard": {},
},
Volumes: map[string]*resources.Volume{
"my_volume": {
CreateVolumeRequestContent: catalog.CreateVolumeRequestContent{},
},
},
Apps: map[string]*resources.App{
"my_app": {
App: apps.App{},
},
},
Alerts: map[string]*resources.Alert{
"my_alert": {
AlertV2: sql.AlertV2{},
},
},
QualityMonitors: map[string]*resources.QualityMonitor{
"my_quality_monitor": {
CreateMonitor: catalog.CreateMonitor{},
},
},
ModelServingEndpoints: map[string]*resources.ModelServingEndpoint{
"my_model_serving_endpoint": {
CreateServingEndpoint: serving.CreateServingEndpoint{},
},
},
SecretScopes: map[string]*resources.SecretScope{
"my_secret_scope": {
Name: "0",
},
},
SqlWarehouses: map[string]*resources.SqlWarehouse{
"my_sql_warehouse": {
CreateWarehouseRequest: sql.CreateWarehouseRequest{},
},
},
DatabaseInstances: map[string]*resources.DatabaseInstance{
"my_database_instance": {
DatabaseInstance: database.DatabaseInstance{},
},
},
DatabaseCatalogs: map[string]*resources.DatabaseCatalog{
"my_database_catalog": {
DatabaseCatalog: database.DatabaseCatalog{},
},
},
SyncedDatabaseTables: map[string]*resources.SyncedDatabaseTable{
"my_synced_database_table": {
SyncedDatabaseTable: database.SyncedDatabaseTable{},
},
},
PostgresProjects: map[string]*resources.PostgresProject{
"my_postgres_project": {
PostgresProjectConfig: resources.PostgresProjectConfig{
ProjectId: "my-postgres-project",
ProjectSpec: postgres.ProjectSpec{
DisplayName: "my_postgres_project",
},
},
},
},
PostgresBranches: map[string]*resources.PostgresBranch{
"my_postgres_branch": {
PostgresBranchConfig: resources.PostgresBranchConfig{
BranchId: "my-postgres-branch",
Parent: "projects/my-postgres-project",
},
},
},
PostgresEndpoints: map[string]*resources.PostgresEndpoint{
"my_postgres_endpoint": {
PostgresEndpointConfig: resources.PostgresEndpointConfig{
EndpointId: "my-postgres-endpoint",
Parent: "projects/my-postgres-project/branches/my-postgres-branch",
EndpointSpec: postgres.EndpointSpec{
EndpointType: postgres.EndpointTypeEndpointTypeReadWrite,
},
},
},
},
PostgresCatalogs: map[string]*resources.PostgresCatalog{
"my_postgres_catalog": {
PostgresCatalogConfig: resources.PostgresCatalogConfig{
CatalogId: "my_postgres_catalog",
},
},
},
PostgresSyncedTables: map[string]*resources.PostgresSyncedTable{
"my_postgres_synced_table": {
PostgresSyncedTableConfig: resources.PostgresSyncedTableConfig{
SyncedTableId: "catalog.schema.my_postgres_synced_table",
},
},
},
VectorSearchEndpoints: map[string]*resources.VectorSearchEndpoint{
"my_vector_search_endpoint": {
CreateEndpoint: vectorsearch.CreateEndpoint{
Name: "my_vector_search_endpoint",
EndpointType: vectorsearch.EndpointTypeStandard,
},
},
},
}
unbindableResources := map[string]bool{
"model": true,
}
ctx := t.Context()
m := mocks.NewMockWorkspaceClient(t)
m.GetMockJobsAPI().EXPECT().Get(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockPipelinesAPI().EXPECT().Get(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockExperimentsAPI().EXPECT().GetExperiment(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockRegisteredModelsAPI().EXPECT().Get(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockCatalogsAPI().EXPECT().GetByName(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockExternalLocationsAPI().EXPECT().GetByName(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockSchemasAPI().EXPECT().GetByFullName(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockClustersAPI().EXPECT().GetByClusterId(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockLakeviewAPI().EXPECT().Get(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockVolumesAPI().EXPECT().Read(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockAppsAPI().EXPECT().GetByName(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockAlertsV2API().EXPECT().GetAlertById(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockQualityMonitorsAPI().EXPECT().Get(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockServingEndpointsAPI().EXPECT().Get(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockSecretsAPI().EXPECT().ListScopesAll(mock.Anything).Return([]workspace.SecretScope{
{Name: "0"},
}, nil)
m.GetMockWarehousesAPI().EXPECT().GetById(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockDatabaseAPI().EXPECT().GetDatabaseInstance(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockDatabaseAPI().EXPECT().GetDatabaseCatalog(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockDatabaseAPI().EXPECT().GetSyncedDatabaseTable(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockPostgresAPI().EXPECT().GetProject(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockPostgresAPI().EXPECT().GetBranch(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockPostgresAPI().EXPECT().GetEndpoint(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockPostgresAPI().EXPECT().GetCatalog(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockPostgresAPI().EXPECT().GetSyncedTable(mock.Anything, mock.Anything).Return(nil, nil)
m.GetMockVectorSearchEndpointsAPI().EXPECT().GetEndpoint(mock.Anything, mock.Anything).Return(nil, nil)
allResources := supportedResources.AllResources()
for _, group := range allResources {
if len(group.Resources) == 0 && !unbindableResources[group.Description.SingularName] {
t.Fatalf("Expected at least one resource in group %s", group.Description)
}
for _, resource := range group.Resources {
// bind operation requires resource to be returned from FindResourceByConfigKey
r, err := supportedResources.FindResourceByConfigKey("my_" + resource.ResourceDescription().SingularName)
assert.NoError(t, err)
// bind operation requires Exists to return true
exists, err := r.Exists(ctx, m.WorkspaceClient, "0")
assert.NoError(t, err)
assert.True(t, exists)
}
}
}