|
| 1 | +package migrate_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/databricks/cli/bundle/migrate" |
| 8 | + "github.com/databricks/cli/libs/structs/structaccess" |
| 9 | + "github.com/databricks/cli/libs/structs/structpath" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// state with src job having int and bool fields set. |
| 15 | +func testState() migrate.TFStateAttrs { |
| 16 | + return migrate.TFStateAttrs{ |
| 17 | + "databricks_job": { |
| 18 | + "src": json.RawMessage(`{ |
| 19 | + "id": "111", |
| 20 | + "max_concurrent_runs": 4, |
| 21 | + "always_running": true |
| 22 | + }`), |
| 23 | + "dst": json.RawMessage(`{ |
| 24 | + "id": "222", |
| 25 | + "max_concurrent_runs": 4, |
| 26 | + "always_running": true |
| 27 | + }`), |
| 28 | + }, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// TestResolveFieldRefInt proves that when Method B (template evaluation) wins for |
| 33 | +// an int field, the returned string value is still usable: structaccess.Set must |
| 34 | +// parse it back to int and not error. |
| 35 | +func TestResolveFieldRefInt(t *testing.T) { |
| 36 | + state := testState() |
| 37 | + // Remove dst from state so Method A fails and Method B must be used. |
| 38 | + delete(state["databricks_job"], "dst") |
| 39 | + |
| 40 | + ctx := t.Context() |
| 41 | + fieldPath, err := structpath.ParsePath("max_concurrent_runs") |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + value, err := migrate.ResolveFieldRef(ctx, state, "jobs", "dst", fieldPath, |
| 45 | + "${resources.jobs.src.max_concurrent_runs}") |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + // Method B succeeds: returns string "4". Verify Set converts it to int. |
| 49 | + type target struct { |
| 50 | + MaxConcurrentRuns int `json:"max_concurrent_runs"` |
| 51 | + } |
| 52 | + s := &target{} |
| 53 | + err = structaccess.Set(s, fieldPath, value) |
| 54 | + assert.NoError(t, err, "Set should parse string %q into int field", value) |
| 55 | + assert.Equal(t, 4, s.MaxConcurrentRuns) |
| 56 | +} |
| 57 | + |
| 58 | +// TestResolveFieldRefBool is the same for a bool field. |
| 59 | +func TestResolveFieldRefBool(t *testing.T) { |
| 60 | + state := testState() |
| 61 | + delete(state["databricks_job"], "dst") |
| 62 | + |
| 63 | + ctx := t.Context() |
| 64 | + fieldPath, err := structpath.ParsePath("always_running") |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + value, err := migrate.ResolveFieldRef(ctx, state, "jobs", "dst", fieldPath, |
| 68 | + "${resources.jobs.src.always_running}") |
| 69 | + require.NoError(t, err) |
| 70 | + |
| 71 | + type target struct { |
| 72 | + AlwaysRunning bool `json:"always_running"` |
| 73 | + } |
| 74 | + s := &target{} |
| 75 | + err = structaccess.Set(s, fieldPath, value) |
| 76 | + assert.NoError(t, err, "Set should parse string %q into bool field", value) |
| 77 | + assert.Equal(t, true, s.AlwaysRunning) |
| 78 | +} |
0 commit comments