Skip to content

Commit 8044f95

Browse files
Add -workspace-tmp-dir option to acceptance tests (#3498)
## Changes The—workspace-tmp-dir option makes the acceptance testing framework run the test using the workspace file system instead of the local file mount. ## Why This option is necessary to simulate DABs in the workspace, since it runs the Databricks CLI on the workspace file system. ## Tests Manually. I've been using this function to run a couple of test runs in my other PR: #3453
1 parent abcbbee commit 8044f95

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

acceptance/acceptance_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ import (
3737
)
3838

3939
var (
40-
KeepTmp bool
41-
NoRepl bool
42-
VerboseTest bool = os.Getenv("VERBOSE_TEST") != ""
43-
Tail bool
44-
Forcerun bool
45-
LogRequests bool
46-
LogConfig bool
47-
SkipLocal bool
48-
UseVersion string
40+
KeepTmp bool
41+
NoRepl bool
42+
VerboseTest bool = os.Getenv("VERBOSE_TEST") != ""
43+
Tail bool
44+
Forcerun bool
45+
LogRequests bool
46+
LogConfig bool
47+
SkipLocal bool
48+
UseVersion string
49+
WorkspaceTmpDir bool
4950
)
5051

5152
// In order to debug CLI running under acceptance test, search for TestInprocessMode and update
@@ -67,6 +68,10 @@ func init() {
6768
flag.BoolVar(&LogConfig, "logconfig", false, "Log merged for each test case")
6869
flag.BoolVar(&SkipLocal, "skiplocal", false, "Skip tests that are enabled to run on Local")
6970
flag.StringVar(&UseVersion, "useversion", "", "Download previously released version of CLI and use it to run the tests")
71+
72+
// DABs in the workspace runs on the workspace file system. This flags does the same for acceptance tests
73+
// to simulate an identical environment.
74+
flag.BoolVar(&WorkspaceTmpDir, "workspace-tmp-dir", false, "Run tests on the workspace file system (For DBR testing).")
7075
}
7176

7277
const (
@@ -486,6 +491,15 @@ func runTest(t *testing.T,
486491
tmpDir, err = os.MkdirTemp(tempDirBase, "")
487492
require.NoError(t, err)
488493
t.Logf("Created directory: %s", tmpDir)
494+
} else if WorkspaceTmpDir {
495+
// If the test is being run on DBR, auth is already configured
496+
// by the dbr_runner notebook by reading a token from the notebook context and
497+
// setting DATABRICKS_TOKEN and DATABRICKS_HOST environment variables.
498+
_, _, tmpDir = workspaceTmpDir(t.Context(), t)
499+
500+
// Run DBR tests on the workspace file system to mimic usage from
501+
// DABs in the workspace.
502+
t.Logf("Running DBR tests on %s", tmpDir)
489503
} else {
490504
tmpDir = t.TempDir()
491505
}

acceptance/dbr_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package acceptance_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
"time"
8+
9+
"github.com/databricks/cli/libs/filer"
10+
"github.com/databricks/databricks-sdk-go"
11+
"github.com/databricks/databricks-sdk-go/service/workspace"
12+
"github.com/google/uuid"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func workspaceTmpDir(ctx context.Context, t *testing.T) (*databricks.WorkspaceClient, filer.Filer, string) {
18+
w, err := databricks.NewWorkspaceClient()
19+
require.NoError(t, err)
20+
21+
currentUser, err := w.CurrentUser.Me(ctx)
22+
require.NoError(t, err)
23+
24+
timestamp := time.Now().Format("2006-01-02T15:04:05Z")
25+
tmpDir := fmt.Sprintf(
26+
"/Workspace/Users/%s/acceptance/%s/%s",
27+
currentUser.UserName,
28+
timestamp,
29+
uuid.New().String(),
30+
)
31+
32+
t.Cleanup(func() {
33+
err := w.Workspace.Delete(ctx, workspace.Delete{
34+
Path: tmpDir,
35+
Recursive: true,
36+
})
37+
assert.NoError(t, err)
38+
})
39+
40+
err = w.Workspace.MkdirsByPath(ctx, tmpDir)
41+
require.NoError(t, err)
42+
43+
f, err := filer.NewWorkspaceFilesClient(w, tmpDir)
44+
require.NoError(t, err)
45+
46+
return w, f, tmpDir
47+
}

0 commit comments

Comments
 (0)