-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathconvert_job_test.go
More file actions
375 lines (350 loc) · 9.16 KB
/
Copy pathconvert_job_test.go
File metadata and controls
375 lines (350 loc) · 9.16 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
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
package tfdyn
import (
"reflect"
"slices"
"strings"
"testing"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/bundle/internal/tf/schema"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/dyn/convert"
"github.com/databricks/cli/libs/textutil"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConvertJob(t *testing.T) {
src := resources.Job{
JobSettings: jobs.JobSettings{
Name: "my job",
JobClusters: []jobs.JobCluster{
{
JobClusterKey: "key",
NewCluster: compute.ClusterSpec{
SparkVersion: "10.4.x-scala2.12",
},
},
},
GitSource: &jobs.GitSource{
GitProvider: jobs.GitProviderGitHub,
GitUrl: "https://github.com/foo/bar",
},
Parameters: []jobs.JobParameterDefinition{
{
Name: "param1",
Default: "default1",
},
{
Name: "param2",
Default: "default2",
},
},
Tasks: []jobs.Task{
{
TaskKey: "task_key_b",
JobClusterKey: "job_cluster_key_b",
Libraries: []compute.Library{
{
Pypi: &compute.PythonPyPiLibrary{
Package: "package",
},
},
{
Whl: "/path/to/my.whl",
},
},
},
{
TaskKey: "task_key_a",
JobClusterKey: "job_cluster_key_a",
},
{
TaskKey: "task_key_c",
JobClusterKey: "job_cluster_key_c",
},
{
Description: "missing task key 😱",
},
},
},
Permissions: []resources.JobPermission{
{
Level: "CAN_VIEW",
UserName: "jane@doe.com",
},
},
}
vin, err := convert.FromTyped(src, dyn.NilValue)
require.NoError(t, err)
ctx := t.Context()
out := schema.NewResources()
err = jobConverter{}.Convert(ctx, "my_job", vin, out)
require.NoError(t, err)
// Assert equality on the job
assert.Equal(t, map[string]any{
"name": "my job",
"job_cluster": []any{
map[string]any{
"job_cluster_key": "key",
"new_cluster": map[string]any{
"spark_version": "10.4.x-scala2.12",
},
},
},
"git_source": map[string]any{
"provider": "gitHub",
"url": "https://github.com/foo/bar",
},
"parameter": []any{
map[string]any{
"name": "param1",
"default": "default1",
},
map[string]any{
"name": "param2",
"default": "default2",
},
},
"task": []any{
map[string]any{
"description": "missing task key 😱",
},
map[string]any{
"task_key": "task_key_a",
"job_cluster_key": "job_cluster_key_a",
},
map[string]any{
"task_key": "task_key_b",
"job_cluster_key": "job_cluster_key_b",
"library": []any{
map[string]any{
"pypi": map[string]any{
"package": "package",
},
},
map[string]any{
"whl": "/path/to/my.whl",
},
},
},
map[string]any{
"task_key": "task_key_c",
"job_cluster_key": "job_cluster_key_c",
},
},
}, out.Job["my_job"])
// Assert equality on the permissions
assert.Equal(t, &schema.ResourcePermissions{
JobId: "${databricks_job.my_job.id}",
AccessControl: []schema.ResourcePermissionsAccessControl{
{
PermissionLevel: "CAN_VIEW",
UserName: "jane@doe.com",
},
},
}, out.Permissions["job_my_job"])
}
func TestConvertJobApplyPolicyDefaultValues(t *testing.T) {
src := resources.Job{
JobSettings: jobs.JobSettings{
Name: "my job",
JobClusters: []jobs.JobCluster{
{
JobClusterKey: "key",
NewCluster: compute.ClusterSpec{
ApplyPolicyDefaultValues: true,
PolicyId: "policy_id",
GcpAttributes: &compute.GcpAttributes{
Availability: "SPOT",
LocalSsdCount: 2,
},
},
},
{
JobClusterKey: "key2",
NewCluster: compute.ClusterSpec{
ApplyPolicyDefaultValues: true,
PolicyId: "policy_id2",
CustomTags: map[string]string{
"key": "value",
},
InitScripts: []compute.InitScriptInfo{
{
Workspace: &compute.WorkspaceStorageInfo{
Destination: "/Workspace/path/to/init_script1",
},
},
{
Workspace: &compute.WorkspaceStorageInfo{
Destination: "/Workspace/path/to/init_script2",
},
},
},
SparkConf: map[string]string{
"key": "value",
},
SparkEnvVars: map[string]string{
"key": "value",
},
SshPublicKeys: []string{
"ssh-rsa 1234",
},
},
},
{
JobClusterKey: "key3",
NewCluster: compute.ClusterSpec{
ApplyPolicyDefaultValues: true,
SparkVersion: "16.4.x-scala2.12",
},
},
},
},
}
vin, err := convert.FromTyped(src, dyn.NilValue)
require.NoError(t, err)
ctx := t.Context()
out := schema.NewResources()
err = jobConverter{}.Convert(ctx, "my_job", vin, out)
require.NoError(t, err)
assert.Equal(t, map[string]any{
"name": "my job",
"job_cluster": []any{
map[string]any{
"job_cluster_key": "key",
"new_cluster": map[string]any{
"__apply_policy_default_values_allow_list": []any{
"apply_policy_default_values",
"gcp_attributes.availability",
"gcp_attributes.local_ssd_count",
"policy_id",
},
"apply_policy_default_values": true,
"policy_id": "policy_id",
"gcp_attributes": map[string]any{
"availability": "SPOT",
"local_ssd_count": int64(2),
},
},
},
map[string]any{
"job_cluster_key": "key2",
"new_cluster": map[string]any{
"__apply_policy_default_values_allow_list": []any{
"apply_policy_default_values",
"custom_tags",
"init_scripts",
"policy_id",
"spark_conf",
"spark_env_vars",
"ssh_public_keys",
},
"apply_policy_default_values": true,
"policy_id": "policy_id2",
"custom_tags": map[string]any{
"key": "value",
},
"init_scripts": []any{
map[string]any{
"workspace": map[string]any{
"destination": "/Workspace/path/to/init_script1",
},
},
map[string]any{
"workspace": map[string]any{
"destination": "/Workspace/path/to/init_script2",
},
},
},
"spark_conf": map[string]any{
"key": "value",
},
"spark_env_vars": map[string]any{
"key": "value",
},
"ssh_public_keys": []any{
"ssh-rsa 1234",
},
},
},
map[string]any{
"job_cluster_key": "key3",
"new_cluster": map[string]any{
"apply_policy_default_values": true,
"spark_version": "16.4.x-scala2.12",
},
},
},
}, out.Job["my_job"])
}
// TestSupportedTypeTasksComplete verifies that supportedTypeTasks includes all task types with a Source field.
func TestSupportedTypeTasksComplete(t *testing.T) {
// Use reflection to find all task types that have a Source field
taskType := reflect.TypeFor[jobs.Task]()
var tasksWithSource []string
for field := range taskType.Fields() {
// Skip non-task fields (like DependsOn, Libraries, etc.)
if !strings.HasSuffix(field.Name, "Task") {
continue
}
// Get the type of the task field (e.g., *NotebookTask)
taskFieldType := field.Type
if taskFieldType.Kind() == reflect.Ptr {
taskFieldType = taskFieldType.Elem()
}
if taskFieldType.Kind() != reflect.Struct {
continue
}
// Recursively search for Source fields in this task type
// We only search one level deep to catch nested Source fields like sql_task.file
taskName := textutil.CamelToSnakeCase(field.Name)
sourcePaths := findSourceFieldsShallow(taskFieldType)
for _, path := range sourcePaths {
if path == "" {
tasksWithSource = append(tasksWithSource, taskName)
} else {
tasksWithSource = append(tasksWithSource, taskName+"."+path)
}
}
}
// Verify that all tasks with Source fields are in supportedTypeTasks
slices.Sort(tasksWithSource)
sortedSupported := make([]string, len(supportedTypeTasks))
copy(sortedSupported, supportedTypeTasks)
slices.Sort(sortedSupported)
assert.Equal(t, sortedSupported, tasksWithSource,
"supportedTypeTasks must include all task types with a Source field. "+
"If this test fails, update supportedTypeTasks in convert_job.go")
}
// findSourceFieldsShallow searches for Source fields in a struct type, going only one level deep.
// Returns a list of paths to Source fields (e.g., "" for direct Source, "file" for sql_task.file).
func findSourceFieldsShallow(t reflect.Type) []string {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return nil
}
var paths []string
for field := range t.Fields() {
// Check if this field is named "Source"
if field.Name == "Source" {
paths = append(paths, "")
continue
}
// Only search one level deep in nested structs
fieldType := field.Type
if fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
}
if fieldType.Kind() == reflect.Struct {
// Check if the nested struct has a Source field
if _, hasSource := fieldType.FieldByName("Source"); hasSource {
fieldName := textutil.CamelToSnakeCase(field.Name)
paths = append(paths, fieldName)
}
}
}
return paths
}