|
5 | 5 |
|
6 | 6 | "github.com/databricks/cli/bundle/config" |
7 | 7 | "github.com/databricks/cli/libs/dyn" |
| 8 | + "github.com/databricks/cli/libs/dyn/dynvar" |
8 | 9 | "github.com/databricks/cli/libs/logdiag" |
9 | 10 | "github.com/stretchr/testify/assert" |
10 | 11 | "github.com/stretchr/testify/require" |
@@ -37,3 +38,97 @@ resources: |
37 | 38 | require.NoError(t, err) |
38 | 39 | assert.Equal(t, "${resources.jobs.base.schedule}", viaref.MustString()) |
39 | 40 | } |
| 41 | + |
| 42 | +func TestSetDefaultSkipsPureReferenceAboveKey(t *testing.T) { |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + config string |
| 46 | + pattern string |
| 47 | + set string |
| 48 | + ref string |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "job_cluster_new_cluster", |
| 52 | + config: ` |
| 53 | +resources: |
| 54 | + jobs: |
| 55 | + base: |
| 56 | + job_clusters: |
| 57 | + - job_cluster_key: main |
| 58 | + new_cluster: |
| 59 | + workload_type: |
| 60 | + clients: |
| 61 | + jobs: false |
| 62 | + viaref: |
| 63 | + job_clusters: |
| 64 | + - job_cluster_key: main |
| 65 | + new_cluster: ${resources.jobs.base.job_clusters[0].new_cluster} |
| 66 | +`, |
| 67 | + pattern: "resources.jobs.*.job_clusters[*].new_cluster.workload_type.clients.notebooks", |
| 68 | + set: "resources.jobs.base.job_clusters[0].new_cluster.workload_type.clients.notebooks", |
| 69 | + ref: "resources.jobs.viaref.job_clusters[0].new_cluster", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "cluster_workload_type", |
| 73 | + config: ` |
| 74 | +resources: |
| 75 | + clusters: |
| 76 | + base: |
| 77 | + workload_type: |
| 78 | + clients: |
| 79 | + jobs: false |
| 80 | + viaref: |
| 81 | + workload_type: ${resources.clusters.base.workload_type} |
| 82 | +`, |
| 83 | + pattern: "resources.clusters.*.workload_type.clients.notebooks", |
| 84 | + set: "resources.clusters.base.workload_type.clients.notebooks", |
| 85 | + ref: "resources.clusters.viaref.workload_type", |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tc := range tests { |
| 90 | + t.Run(tc.name, func(t *testing.T) { |
| 91 | + root, diags := config.LoadFromBytes("databricks.yml", []byte(tc.config)) |
| 92 | + require.Empty(t, diags) |
| 93 | + b := &Bundle{Config: *root} |
| 94 | + |
| 95 | + ctx := logdiag.InitContext(t.Context()) |
| 96 | + logdiag.SetCollect(ctx, true) |
| 97 | + SetDefault(ctx, b, tc.pattern, true) |
| 98 | + require.Empty(t, logdiag.FlushCollected(ctx)) |
| 99 | + |
| 100 | + v := b.Config.Value() |
| 101 | + set, err := dyn.GetByPath(v, dyn.MustPathFromString(tc.set)) |
| 102 | + require.NoError(t, err) |
| 103 | + assert.True(t, set.MustBool()) |
| 104 | + |
| 105 | + ref, err := dyn.GetByPath(v, dyn.MustPathFromString(tc.ref)) |
| 106 | + require.NoError(t, err) |
| 107 | + assert.True(t, dynvar.IsPureVariableReference(ref.MustString())) |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestSetDefaultErrorHasFullPath(t *testing.T) { |
| 113 | + // Built directly because loading the config would drop a wrong-kind workload_type. |
| 114 | + v := dyn.V(map[string]dyn.Value{ |
| 115 | + "resources": dyn.V(map[string]dyn.Value{ |
| 116 | + "jobs": dyn.V(map[string]dyn.Value{ |
| 117 | + "job": dyn.V(map[string]dyn.Value{ |
| 118 | + "job_clusters": dyn.V([]dyn.Value{ |
| 119 | + dyn.V(map[string]dyn.Value{ |
| 120 | + "new_cluster": dyn.V(map[string]dyn.Value{ |
| 121 | + "workload_type": dyn.V("not a map"), |
| 122 | + }), |
| 123 | + }), |
| 124 | + }), |
| 125 | + }), |
| 126 | + }), |
| 127 | + }), |
| 128 | + }) |
| 129 | + |
| 130 | + pattern := dyn.MustPatternFromString("resources.jobs.*.job_clusters[*].new_cluster.workload_type.clients") |
| 131 | + m := SetDefaultMutator(pattern, "notebooks", true).(*setDefault) |
| 132 | + _, err := m.apply(v, dyn.EmptyPath, m.pattern) |
| 133 | + assert.EqualError(t, err, `resources.jobs.job.job_clusters[0].new_cluster.workload_type: expected a map to index "clients", found string`) |
| 134 | +} |
0 commit comments