|
| 1 | +package phases |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/databricks/cli/bundle" |
| 10 | + "github.com/databricks/cli/bundle/config" |
| 11 | + "github.com/databricks/cli/bundle/config/engine" |
| 12 | + "github.com/databricks/cli/bundle/direct/dstate" |
| 13 | + "github.com/databricks/cli/libs/env" |
| 14 | + "github.com/databricks/cli/libs/telemetry/protos" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func TestResourceTypeFromKey(t *testing.T) { |
| 20 | + cases := []struct { |
| 21 | + in string |
| 22 | + want string |
| 23 | + }{ |
| 24 | + {"resources.jobs.foo", "jobs"}, |
| 25 | + {"resources.pipelines.bar", "pipelines"}, |
| 26 | + {"resources.jobs.foo.permissions", "permissions"}, |
| 27 | + {"resources.secret_scopes.s.permissions", "permissions"}, |
| 28 | + {"not-a-state-key", ""}, |
| 29 | + {"resources.jobs", ""}, |
| 30 | + } |
| 31 | + for _, c := range cases { |
| 32 | + assert.Equal(t, c.want, resourceTypeFromKey(c.in), "key=%q", c.in) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestResolveDeployEngine(t *testing.T) { |
| 37 | + cases := []struct { |
| 38 | + name string |
| 39 | + configEng engine.EngineType |
| 40 | + envEng string |
| 41 | + want string |
| 42 | + }{ |
| 43 | + {"config wins over env", engine.EngineDirect, "terraform", "direct"}, |
| 44 | + {"env used when config unset", engine.EngineNotSet, "direct", "direct"}, |
| 45 | + {"default when neither set", engine.EngineNotSet, "", "terraform"}, |
| 46 | + } |
| 47 | + for _, c := range cases { |
| 48 | + t.Run(c.name, func(t *testing.T) { |
| 49 | + b := &bundle.Bundle{} |
| 50 | + b.Config.Bundle.Engine = c.configEng |
| 51 | + ctx := env.Set(t.Context(), engine.EnvVar, c.envEng) |
| 52 | + assert.Equal(t, c.want, resolveDeployEngine(ctx, b)) |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestStatHelpers(t *testing.T) { |
| 58 | + assert.Equal(t, int64(3), statMax([]int64{1, 2, 3})) |
| 59 | + assert.Equal(t, int64(2), statMean([]int64{1, 2, 3})) |
| 60 | + assert.Equal(t, int64(2), statMedian([]int64{1, 2, 3})) |
| 61 | + // Lower-middle for even count: sorted [1,2,3,4] -> index (4-1)/2 = 1 -> 2. |
| 62 | + assert.Equal(t, int64(2), statMedian([]int64{1, 2, 3, 4})) |
| 63 | + assert.Equal(t, int64(0), statMax(nil)) |
| 64 | + assert.Equal(t, int64(0), statMean(nil)) |
| 65 | + assert.Equal(t, int64(0), statMedian(nil)) |
| 66 | +} |
| 67 | + |
| 68 | +// directStateBundle writes a resources.json with the given per-key state blobs |
| 69 | +// and returns a bundle wired to read it via StateFilenameDirect. |
| 70 | +func directStateBundle(t *testing.T, state map[string]dstate.ResourceEntry) *bundle.Bundle { |
| 71 | + t.Helper() |
| 72 | + b := &bundle.Bundle{ |
| 73 | + BundleRootPath: t.TempDir(), |
| 74 | + Config: config.Root{ |
| 75 | + Bundle: config.Bundle{ |
| 76 | + Engine: engine.EngineDirect, |
| 77 | + Target: "default", |
| 78 | + }, |
| 79 | + Workspace: config.Workspace{ |
| 80 | + StatePath: "/Workspace/state", |
| 81 | + }, |
| 82 | + }, |
| 83 | + Target: &config.Target{}, |
| 84 | + } |
| 85 | + _, localPath := b.StateFilenameDirect(t.Context()) |
| 86 | + require.NoError(t, os.MkdirAll(filepath.Dir(localPath), 0o755)) |
| 87 | + raw, err := json.Marshal(dstate.Database{State: state}) |
| 88 | + require.NoError(t, err) |
| 89 | + require.NoError(t, os.WriteFile(localPath, raw, 0o600)) |
| 90 | + return b |
| 91 | +} |
| 92 | + |
| 93 | +func TestCollectResourcesMetadata_GroupsByTypeFromState(t *testing.T) { |
| 94 | + b := directStateBundle(t, map[string]dstate.ResourceEntry{ |
| 95 | + "resources.jobs.foo": {State: json.RawMessage(`{"name":"foo","x":1}`)}, // 20 |
| 96 | + "resources.jobs.bar": {State: json.RawMessage(`{"n":"bar"}`)}, // 11 |
| 97 | + "resources.jobs.foo.permissions": {State: json.RawMessage(`[]`)}, // 2 |
| 98 | + "resources.pipelines.qux": {State: json.RawMessage(`{"name":"qux"}`)}, // 14 |
| 99 | + }) |
| 100 | + |
| 101 | + md := collectResourcesMetadata(t.Context(), b) |
| 102 | + require.NotNil(t, md) |
| 103 | + assert.Equal(t, "direct", md.StateEngine) |
| 104 | + assert.Positive(t, md.StateFileSizeBytes) |
| 105 | + |
| 106 | + byType := make(map[string]protos.ResourceMetadata) |
| 107 | + for _, r := range md.Resources { |
| 108 | + byType[r.ResourceType] = r |
| 109 | + } |
| 110 | + assert.Equal(t, int64(2), byType["jobs"].Count) |
| 111 | + assert.Equal(t, int64(20), byType["jobs"].StateSizeMaxBytes) |
| 112 | + assert.Equal(t, int64(15), byType["jobs"].StateSizeMeanBytes) // (20+11)/2 |
| 113 | + assert.Equal(t, int64(11), byType["jobs"].StateSizeMedianBytes) |
| 114 | + assert.Equal(t, int64(1), byType["pipelines"].Count) |
| 115 | + assert.Equal(t, int64(1), byType["permissions"].Count) |
| 116 | +} |
| 117 | + |
| 118 | +func TestCollectResourcesMetadata_NilForTerraform(t *testing.T) { |
| 119 | + b := directStateBundle(t, map[string]dstate.ResourceEntry{ |
| 120 | + "resources.jobs.foo": {State: json.RawMessage(`{}`)}, |
| 121 | + }) |
| 122 | + b.Config.Bundle.Engine = engine.EngineTerraform |
| 123 | + assert.Nil(t, collectResourcesMetadata(t.Context(), b)) |
| 124 | +} |
| 125 | + |
| 126 | +func TestCollectResourcesMetadata_NilWhenNoStateFile(t *testing.T) { |
| 127 | + b := &bundle.Bundle{ |
| 128 | + BundleRootPath: t.TempDir(), |
| 129 | + Config: config.Root{ |
| 130 | + Bundle: config.Bundle{Engine: engine.EngineDirect, Target: "default"}, |
| 131 | + Workspace: config.Workspace{StatePath: "/Workspace/state"}, |
| 132 | + }, |
| 133 | + Target: &config.Target{}, |
| 134 | + } |
| 135 | + assert.Nil(t, collectResourcesMetadata(t.Context(), b)) |
| 136 | +} |
| 137 | + |
| 138 | +func TestCollectResourcesMetadata_NilOnMalformedState(t *testing.T) { |
| 139 | + b := &bundle.Bundle{ |
| 140 | + BundleRootPath: t.TempDir(), |
| 141 | + Config: config.Root{ |
| 142 | + Bundle: config.Bundle{Engine: engine.EngineDirect, Target: "default"}, |
| 143 | + Workspace: config.Workspace{StatePath: "/Workspace/state"}, |
| 144 | + }, |
| 145 | + Target: &config.Target{}, |
| 146 | + } |
| 147 | + _, localPath := b.StateFilenameDirect(t.Context()) |
| 148 | + require.NoError(t, os.MkdirAll(filepath.Dir(localPath), 0o755)) |
| 149 | + require.NoError(t, os.WriteFile(localPath, []byte("not json"), 0o600)) |
| 150 | + assert.Nil(t, collectResourcesMetadata(t.Context(), b)) |
| 151 | +} |
0 commit comments