-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix "bundle init" when run from Databricks #1744
Changes from all commits
662234f
8d78809
6585a75
62b2451
08b6b10
90d6490
e51037f
02745de
4c9bf79
694413b
4cd9b17
84d1bbf
6bf59ff
49c6ed6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,14 @@ | ||||||||||||||
package runtime | ||||||||||||||
|
||||||||||||||
import ( | ||||||||||||||
"context" | ||||||||||||||
|
||||||||||||||
"github.com/databricks/cli/libs/env" | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
const envDatabricksRuntimeVersion = "DATABRICKS_RUNTIME_VERSION" | ||||||||||||||
|
||||||||||||||
func RunsOnDatabricks(ctx context.Context) bool { | ||||||||||||||
value, ok := env.Lookup(ctx, envDatabricksRuntimeVersion) | ||||||||||||||
return value != "" && ok | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need the value to be non-empty, then you don't need to check if it exists (the OK). You can use Lines 51 to 56 in a4ba0bb
|
||||||||||||||
} | ||||||||||||||
fjakobs marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package runtime | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRunsOnDatabricks(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
t.Setenv("DATABRICKS_RUNTIME_VERSION", "") | ||
assert.False(t, RunsOnDatabricks(ctx)) | ||
|
||
t.Setenv("DATABRICKS_RUNTIME_VERSION", "14.3") | ||
assert.True(t, RunsOnDatabricks(ctx)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,11 @@ import ( | |
"runtime" | ||
"testing" | ||
|
||
"github.com/databricks/databricks-sdk-go/experimental/mocks" | ||
"github.com/databricks/databricks-sdk-go/service/workspace" | ||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/databricks/cli/cmd/root" | ||
"github.com/databricks/cli/libs/filer" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
@@ -17,6 +22,7 @@ func testInMemoryFile(t *testing.T, perm fs.FileMode) { | |
tmpDir := t.TempDir() | ||
|
||
f := &inMemoryFile{ | ||
ctx: context.Background(), | ||
dstPath: &destinationPath{ | ||
root: tmpDir, | ||
relPath: "a/b/c", | ||
|
@@ -109,3 +115,37 @@ func TestTemplateCopyFilePersistToDiskForWindows(t *testing.T) { | |
// fs.FileMode values we can use for different operating systems. | ||
testCopyFile(t, 0666) | ||
} | ||
|
||
func TestShouldUseImportNotebook(t *testing.T) { | ||
ctx := context.Background() | ||
data := []byte("# Databricks notebook source\n print('hello')") | ||
|
||
assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar", data)) | ||
assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar.ipynb", data)) | ||
assert.False(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar", data)) | ||
assert.False(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar.ipynb", data)) | ||
|
||
t.Setenv("DATABRICKS_RUNTIME_VERSION", "14.3") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the difference? I see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With If it broke then either 1) you weren't capturing the returned |
||
assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar", data)) | ||
assert.False(t, shouldUseImportNotebook(ctx, "./foo/bar.ipynb", data)) | ||
assert.False(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar", data)) | ||
assert.True(t, shouldUseImportNotebook(ctx, "/Workspace/foo/bar.py", data)) | ||
} | ||
|
||
func TestImportNotebook(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
m := mocks.NewMockWorkspaceClient(t) | ||
ctx = root.SetWorkspaceClient(ctx, m.WorkspaceClient) | ||
|
||
workspaceApi := m.GetMockWorkspaceAPI() | ||
workspaceApi.EXPECT().Import(mock.Anything, workspace.Import{ | ||
Content: "cXdlcnR5", // base64 of "qwerty" | ||
Format: "AUTO", | ||
Overwrite: false, | ||
Path: "/Workspace/foo/bar.ipynb", | ||
}).Return(nil) | ||
|
||
err := importNotebook(ctx, "/Workspace/foo/bar.ipynb", []byte("qwerty")) | ||
assert.NoError(t, err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for a common lib function. I'd expect this to also check for the existence of /Workspace though to avoid false positives. There may be all kinds of reasons why customers set the env var locally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer not to test for
/Workspace
as it makes the code using it harder to testThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, testing is a pain :( We can leave it out, but then we should at least emit a debug log message whenever there's a match. There will be false positives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing is not an issue as long as you don't inline these expectations.
You can store a bool on the context that stores whether or not we're running on DBR (see
bundle/context.go
for inspiration). In tests, you mark it as always true or always false depending on what you want to test. For the real CLI run, you run a routine that performs the actual detection and stores it in the context.For the check itself, it can look at
/proc/mounts
for the workspace fuse mount, it can check for/.fuse-mounts
, it can check for/databricks
, and I'm sure there are a couple other stable ways to determine this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented this approach in #1889.
Besides the environment variable, it also checks for the presence of a
/databricks
directory.