mirror of https://github.com/databricks/cli.git
helper wsfs function
This commit is contained in:
parent
289dd0db32
commit
fb5ea166ca
|
@ -15,7 +15,6 @@ import (
|
||||||
"github.com/databricks/databricks-sdk-go"
|
"github.com/databricks/databricks-sdk-go"
|
||||||
"github.com/databricks/databricks-sdk-go/apierr"
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
"github.com/databricks/databricks-sdk-go/service/files"
|
"github.com/databricks/databricks-sdk-go/service/files"
|
||||||
"github.com/databricks/databricks-sdk-go/service/workspace"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
@ -138,35 +137,10 @@ func runFilerReadDirTest(t *testing.T, ctx context.Context, f filer.Filer) {
|
||||||
assert.Len(t, entries, 1)
|
assert.Len(t, entries, 1)
|
||||||
assert.Equal(t, "c", entries[0].Name())
|
assert.Equal(t, "c", entries[0].Name())
|
||||||
assert.True(t, entries[0].IsDir())
|
assert.True(t, entries[0].IsDir())
|
||||||
}
|
|
||||||
|
|
||||||
func temporaryWorkspaceDir(t *testing.T, w *databricks.WorkspaceClient) string {
|
// TODO: split into a separate PR
|
||||||
ctx := context.Background()
|
_, err = f.ReadDir(ctx, "/hello.txt")
|
||||||
me, err := w.CurrentUser.Me(ctx)
|
assert.ErrorIs(t, err, filer.ErrNotADirectory)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
path := fmt.Sprintf("/Users/%s/%s", me.UserName, RandomName("integration-test-filer-wsfs-"))
|
|
||||||
|
|
||||||
// Ensure directory exists, but doesn't exist YET!
|
|
||||||
// Otherwise we could inadvertently remove a directory that already exists on cleanup.
|
|
||||||
t.Logf("mkdir %s", path)
|
|
||||||
err = w.Workspace.MkdirsByPath(ctx, path)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Remove test directory on test completion.
|
|
||||||
t.Cleanup(func() {
|
|
||||||
t.Logf("rm -rf %s", path)
|
|
||||||
err := w.Workspace.Delete(ctx, workspace.Delete{
|
|
||||||
Path: path,
|
|
||||||
Recursive: true,
|
|
||||||
})
|
|
||||||
if err == nil || apierr.IsMissing(err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
t.Logf("unable to remove temporary workspace directory %s: %#v", path, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
return path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupWorkspaceFilesTest(t *testing.T) (context.Context, filer.Filer) {
|
func setupWorkspaceFilesTest(t *testing.T) (context.Context, filer.Filer) {
|
||||||
|
@ -174,7 +148,7 @@ func setupWorkspaceFilesTest(t *testing.T) (context.Context, filer.Filer) {
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
w := databricks.Must(databricks.NewWorkspaceClient())
|
w := databricks.Must(databricks.NewWorkspaceClient())
|
||||||
tmpdir := temporaryWorkspaceDir(t, w)
|
tmpdir := TemporaryWorkspaceDir(t, w)
|
||||||
f, err := filer.NewWorkspaceFilesClient(w, tmpdir)
|
f, err := filer.NewWorkspaceFilesClient(w, tmpdir)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,9 @@ import (
|
||||||
|
|
||||||
"github.com/databricks/cli/cmd/root"
|
"github.com/databricks/cli/cmd/root"
|
||||||
_ "github.com/databricks/cli/cmd/version"
|
_ "github.com/databricks/cli/cmd/version"
|
||||||
|
"github.com/databricks/databricks-sdk-go"
|
||||||
|
"github.com/databricks/databricks-sdk-go/apierr"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/workspace"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
_ "github.com/databricks/cli/cmd/workspace"
|
_ "github.com/databricks/cli/cmd/workspace"
|
||||||
|
@ -176,3 +179,32 @@ func writeFile(t *testing.T, name string, body string) string {
|
||||||
f.Close()
|
f.Close()
|
||||||
return f.Name()
|
return f.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TemporaryWorkspaceDir(t *testing.T, w *databricks.WorkspaceClient) string {
|
||||||
|
ctx := context.Background()
|
||||||
|
me, err := w.CurrentUser.Me(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
path := fmt.Sprintf("/Users/%s/%s", me.UserName, RandomName("integration-test-wsfs-"))
|
||||||
|
|
||||||
|
// Ensure directory exists, but doesn't exist YET!
|
||||||
|
// Otherwise we could inadvertently remove a directory that already exists on cleanup.
|
||||||
|
t.Logf("mkdir %s", path)
|
||||||
|
err = w.Workspace.MkdirsByPath(ctx, path)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Remove test directory on test completion.
|
||||||
|
t.Cleanup(func() {
|
||||||
|
t.Logf("rm -rf %s", path)
|
||||||
|
err := w.Workspace.Delete(ctx, workspace.Delete{
|
||||||
|
Path: path,
|
||||||
|
Recursive: true,
|
||||||
|
})
|
||||||
|
if err == nil || apierr.IsMissing(err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Logf("unable to remove temporary workspace directory %s: %#v", path, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue