1- package terraform
1+ package deploy
22
33import (
4- "context "
4+ "encoding/json "
55 "errors"
6+ "path/filepath"
67 "testing"
78
89 "github.com/databricks/cli/bundle"
910 "github.com/databricks/cli/bundle/config"
10- "github.com/databricks/cli/bundle/config/engine"
1111 "github.com/databricks/cli/bundle/config/resources"
12- "github.com/databricks/cli/internal/testutil "
12+ "github.com/databricks/cli/bundle/direct/dstate "
1313 "github.com/databricks/cli/libs/diag"
1414 "github.com/databricks/databricks-sdk-go/experimental/mocks"
1515 "github.com/databricks/databricks-sdk-go/service/dashboards"
@@ -51,21 +51,22 @@ func TestCheckDashboardsModifiedRemotely_NoDashboards(t *testing.T) {
5151 },
5252 }
5353
54- diags := bundle .Apply (t .Context (), b , CheckDashboardsModifiedRemotely (false , engine . EngineTerraform ))
54+ diags := bundle .Apply (t .Context (), b , CheckDashboardsModifiedRemotely (false ))
5555 assert .Empty (t , diags )
5656}
5757
5858func TestCheckDashboardsModifiedRemotely_FirstDeployment (t * testing.T ) {
5959 b := mockDashboardBundle (t )
60- diags := bundle .Apply (t .Context (), b , CheckDashboardsModifiedRemotely (false , engine .EngineTerraform ))
60+ openDashboardState (t , b , dstate .NewDatabase ("lineage" , 1 ))
61+ diags := bundle .Apply (t .Context (), b , CheckDashboardsModifiedRemotely (false ))
6162 assert .Empty (t , diags )
6263}
6364
6465func TestCheckDashboardsModifiedRemotely_ExistingStateNoChange (t * testing.T ) {
6566 ctx := t .Context ()
6667
6768 b := mockDashboardBundle (t )
68- writeFakeDashboardState (t , ctx , b )
69+ writeFakeDashboardState (t , b )
6970
7071 // Mock the call to the API.
7172 m := mocks .NewMockWorkspaceClient (t )
@@ -80,15 +81,15 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateNoChange(t *testing.T) {
8081 b .SetWorkpaceClient (m .WorkspaceClient )
8182
8283 // No changes, so no diags.
83- diags := bundle .Apply (ctx , b , CheckDashboardsModifiedRemotely (false , engine . EngineTerraform ))
84+ diags := bundle .Apply (ctx , b , CheckDashboardsModifiedRemotely (false ))
8485 assert .Empty (t , diags )
8586}
8687
8788func TestCheckDashboardsModifiedRemotely_ExistingStateChange (t * testing.T ) {
8889 ctx := t .Context ()
8990
9091 b := mockDashboardBundle (t )
91- writeFakeDashboardState (t , ctx , b )
92+ writeFakeDashboardState (t , b )
9293
9394 // Mock the call to the API.
9495 m := mocks .NewMockWorkspaceClient (t )
@@ -103,7 +104,7 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateChange(t *testing.T) {
103104 b .SetWorkpaceClient (m .WorkspaceClient )
104105
105106 // The dashboard has changed, so expect an error.
106- diags := bundle .Apply (ctx , b , CheckDashboardsModifiedRemotely (false , engine . EngineTerraform ))
107+ diags := bundle .Apply (ctx , b , CheckDashboardsModifiedRemotely (false ))
107108 if assert .Len (t , diags , 1 ) {
108109 assert .Equal (t , diag .Error , diags [0 ].Severity )
109110 assert .Equal (t , `dashboard "dash1" has been modified remotely` , diags [0 ].Summary )
@@ -114,7 +115,7 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateFailureToGet(t *testing.T)
114115 ctx := t .Context ()
115116
116117 b := mockDashboardBundle (t )
117- writeFakeDashboardState (t , ctx , b )
118+ writeFakeDashboardState (t , b )
118119
119120 // Mock the call to the API.
120121 m := mocks .NewMockWorkspaceClient (t )
@@ -126,7 +127,7 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateFailureToGet(t *testing.T)
126127 b .SetWorkpaceClient (m .WorkspaceClient )
127128
128129 // Unable to get the dashboard, so expect an error.
129- diags := bundle .Apply (ctx , b , CheckDashboardsModifiedRemotely (false , engine . EngineTerraform ))
130+ diags := bundle .Apply (ctx , b , CheckDashboardsModifiedRemotely (false ))
130131 if assert .Len (t , diags , 1 ) {
131132 assert .Equal (t , diag .Error , diags [0 ].Severity )
132133 assert .Equal (t , `failed to get dashboard "dash1"` , diags [0 ].Summary )
@@ -137,7 +138,7 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateChangePlanMode(t *testing.
137138 ctx := t .Context ()
138139
139140 b := mockDashboardBundle (t )
140- writeFakeDashboardState (t , ctx , b )
141+ writeFakeDashboardState (t , b )
141142
142143 // Mock the call to the API.
143144 m := mocks .NewMockWorkspaceClient (t )
@@ -152,64 +153,21 @@ func TestCheckDashboardsModifiedRemotely_ExistingStateChangePlanMode(t *testing.
152153 b .SetWorkpaceClient (m .WorkspaceClient )
153154
154155 // The dashboard has changed, but in plan mode expect a warning instead of an error.
155- diags := bundle .Apply (ctx , b , CheckDashboardsModifiedRemotely (true , engine . EngineTerraform ))
156+ diags := bundle .Apply (ctx , b , CheckDashboardsModifiedRemotely (true ))
156157 if assert .Len (t , diags , 1 ) {
157158 assert .Equal (t , diag .Warning , diags [0 ].Severity )
158159 assert .Equal (t , `dashboard "dash1" has been modified remotely` , diags [0 ].Summary )
159160 }
160161}
161162
162- func writeFakeDashboardState (t * testing.T , ctx context.Context , b * bundle.Bundle ) {
163- _ , path := b .StateFilenameTerraform (ctx )
164-
165- // Write fake state file.
166- testutil .WriteFile (t , path , `
167- {
168- "version": 4,
169- "terraform_version": "1.5.5",
170- "resources": [
171- {
172- "mode": "managed",
173- "type": "databricks_dashboard",
174- "name": "dash1",
175- "instances": [
176- {
177- "schema_version": 0,
178- "attributes": {
179- "etag": "1000",
180- "id": "id1"
181- }
182- }
183- ]
184- },
185- {
186- "mode": "managed",
187- "type": "databricks_job",
188- "name": "job",
189- "instances": [
190- {
191- "schema_version": 0,
192- "attributes": {
193- "id": "1234"
194- }
195- }
196- ]
197- },
198- {
199- "mode": "managed",
200- "type": "databricks_dashboard",
201- "name": "dash2",
202- "instances": [
203- {
204- "schema_version": 0,
205- "attributes": {
206- "etag": "1001",
207- "id": "id2"
208- }
209- }
210- ]
211- }
212- ]
213- }
214- ` )
163+ func openDashboardState (t * testing.T , b * bundle.Bundle , data dstate.Database ) {
164+ b .DeploymentBundle .StateDB .OpenWithData (filepath .Join (t .TempDir (), "resources.json" ), data )
165+ }
166+
167+ func writeFakeDashboardState (t * testing.T , b * bundle.Bundle ) {
168+ data := dstate .NewDatabase ("lineage" , 1 )
169+ data .State ["resources.dashboards.dash1" ] = dstate.ResourceEntry {ID : "id1" , State : json .RawMessage (`{"etag": "1000"}` )}
170+ data .State ["resources.jobs.job" ] = dstate.ResourceEntry {ID : "1234" , State : json .RawMessage (`{}` )}
171+ data .State ["resources.dashboards.dash2" ] = dstate.ResourceEntry {ID : "id2" , State : json .RawMessage (`{"etag": "1001"}` )}
172+ openDashboardState (t , b , data )
215173}
0 commit comments