2022-12-14 14:37:14 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-31 11:24:20 +00:00
|
|
|
"bytes"
|
2022-12-14 14:37:14 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-05-31 12:22:26 +00:00
|
|
|
"io/fs"
|
2022-12-14 14:37:14 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/filer"
|
2022-12-14 14:37:14 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go"
|
|
|
|
"github.com/databricks/databricks-sdk-go/apierr"
|
2023-05-31 11:24:20 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/files"
|
2022-12-14 14:37:14 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/workspace"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type filerTest struct {
|
|
|
|
*testing.T
|
|
|
|
filer.Filer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f filerTest) assertContents(ctx context.Context, name string, contents string) {
|
|
|
|
reader, err := f.Read(ctx, name)
|
|
|
|
if !assert.NoError(f, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:24:20 +00:00
|
|
|
var body bytes.Buffer
|
|
|
|
_, err = io.Copy(&body, reader)
|
2022-12-14 14:37:14 +00:00
|
|
|
if !assert.NoError(f, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:24:20 +00:00
|
|
|
assert.Equal(f, contents, body.String())
|
2022-12-14 14:37:14 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 11:24:20 +00:00
|
|
|
func runFilerReadWriteTest(t *testing.T, ctx context.Context, f filer.Filer) {
|
2023-05-31 09:11:17 +00:00
|
|
|
var err error
|
|
|
|
|
2022-12-14 14:37:14 +00:00
|
|
|
// Write should fail because the root path doesn't yet exist.
|
2023-01-05 11:03:31 +00:00
|
|
|
err = f.Write(ctx, "/foo/bar", strings.NewReader(`hello world`))
|
2022-12-14 14:37:14 +00:00
|
|
|
assert.True(t, errors.As(err, &filer.NoSuchDirectoryError{}))
|
2023-05-31 18:47:00 +00:00
|
|
|
assert.True(t, errors.Is(err, fs.ErrNotExist))
|
2022-12-14 14:37:14 +00:00
|
|
|
|
|
|
|
// Read should fail because the root path doesn't yet exist.
|
|
|
|
_, err = f.Read(ctx, "/foo/bar")
|
2023-05-31 11:24:20 +00:00
|
|
|
assert.True(t, errors.As(err, &filer.FileDoesNotExistError{}))
|
2023-05-31 18:47:00 +00:00
|
|
|
assert.True(t, errors.Is(err, fs.ErrNotExist))
|
2022-12-14 14:37:14 +00:00
|
|
|
|
|
|
|
// Write with CreateParentDirectories flag should succeed.
|
2023-01-05 11:03:31 +00:00
|
|
|
err = f.Write(ctx, "/foo/bar", strings.NewReader(`hello world`), filer.CreateParentDirectories)
|
2022-12-14 14:37:14 +00:00
|
|
|
assert.NoError(t, err)
|
2023-01-05 11:03:31 +00:00
|
|
|
filerTest{t, f}.assertContents(ctx, "/foo/bar", `hello world`)
|
2022-12-14 14:37:14 +00:00
|
|
|
|
|
|
|
// Write should fail because there is an existing file at the specified path.
|
2023-01-05 11:03:31 +00:00
|
|
|
err = f.Write(ctx, "/foo/bar", strings.NewReader(`hello universe`))
|
2022-12-14 14:37:14 +00:00
|
|
|
assert.True(t, errors.As(err, &filer.FileAlreadyExistsError{}))
|
2023-05-31 18:47:00 +00:00
|
|
|
assert.True(t, errors.Is(err, fs.ErrExist))
|
2022-12-14 14:37:14 +00:00
|
|
|
|
|
|
|
// Write with OverwriteIfExists should succeed.
|
2023-01-05 11:03:31 +00:00
|
|
|
err = f.Write(ctx, "/foo/bar", strings.NewReader(`hello universe`), filer.OverwriteIfExists)
|
2022-12-14 14:37:14 +00:00
|
|
|
assert.NoError(t, err)
|
2023-01-05 11:03:31 +00:00
|
|
|
filerTest{t, f}.assertContents(ctx, "/foo/bar", `hello universe`)
|
2022-12-14 14:37:14 +00:00
|
|
|
|
|
|
|
// Delete should fail if the file doesn't exist.
|
|
|
|
err = f.Delete(ctx, "/doesnt_exist")
|
2023-05-31 11:24:20 +00:00
|
|
|
assert.True(t, errors.As(err, &filer.FileDoesNotExistError{}))
|
2023-05-31 18:47:00 +00:00
|
|
|
assert.True(t, errors.Is(err, fs.ErrNotExist))
|
2022-12-14 14:37:14 +00:00
|
|
|
|
|
|
|
// Delete should succeed for file that does exist.
|
|
|
|
err = f.Delete(ctx, "/foo/bar")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2023-05-31 09:11:17 +00:00
|
|
|
|
2023-05-31 11:24:20 +00:00
|
|
|
func runFilerReadDirTest(t *testing.T, ctx context.Context, f filer.Filer) {
|
2023-05-31 09:11:17 +00:00
|
|
|
var err error
|
2023-05-31 12:22:26 +00:00
|
|
|
var info fs.FileInfo
|
2023-05-31 09:11:17 +00:00
|
|
|
|
|
|
|
// We start with an empty directory.
|
|
|
|
entries, err := f.ReadDir(ctx, ".")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, entries, 0)
|
|
|
|
|
|
|
|
// Write a file.
|
|
|
|
err = f.Write(ctx, "/hello.txt", strings.NewReader(`hello world`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Create a directory.
|
|
|
|
err = f.Mkdir(ctx, "/dir")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Write a file.
|
|
|
|
err = f.Write(ctx, "/dir/world.txt", strings.NewReader(`hello world`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Create a nested directory (check that it creates intermediate directories).
|
|
|
|
err = f.Mkdir(ctx, "/dir/a/b/c")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Expect an error if the path doesn't exist.
|
|
|
|
_, err = f.ReadDir(ctx, "/dir/a/b/c/d/e")
|
2023-05-31 11:24:20 +00:00
|
|
|
assert.True(t, errors.As(err, &filer.NoSuchDirectoryError{}), err)
|
2023-05-31 18:47:00 +00:00
|
|
|
assert.True(t, errors.Is(err, fs.ErrNotExist))
|
2023-05-31 09:11:17 +00:00
|
|
|
|
|
|
|
// Expect two entries in the root.
|
|
|
|
entries, err = f.ReadDir(ctx, ".")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, entries, 2)
|
2023-05-31 12:22:26 +00:00
|
|
|
assert.Equal(t, "dir", entries[0].Name())
|
|
|
|
assert.True(t, entries[0].IsDir())
|
|
|
|
assert.Equal(t, "hello.txt", entries[1].Name())
|
|
|
|
assert.False(t, entries[1].IsDir())
|
|
|
|
info, err = entries[1].Info()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Greater(t, info.ModTime().Unix(), int64(0))
|
2023-05-31 09:11:17 +00:00
|
|
|
|
|
|
|
// Expect two entries in the directory.
|
|
|
|
entries, err = f.ReadDir(ctx, "/dir")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, entries, 2)
|
2023-05-31 12:22:26 +00:00
|
|
|
assert.Equal(t, "a", entries[0].Name())
|
|
|
|
assert.True(t, entries[0].IsDir())
|
|
|
|
assert.Equal(t, "world.txt", entries[1].Name())
|
|
|
|
assert.False(t, entries[1].IsDir())
|
|
|
|
info, err = entries[1].Info()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Greater(t, info.ModTime().Unix(), int64(0))
|
2023-05-31 09:11:17 +00:00
|
|
|
|
|
|
|
// Expect a single entry in the nested path.
|
|
|
|
entries, err = f.ReadDir(ctx, "/dir/a/b")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, entries, 1)
|
2023-05-31 12:22:26 +00:00
|
|
|
assert.Equal(t, "c", entries[0].Name())
|
|
|
|
assert.True(t, entries[0].IsDir())
|
2023-05-31 09:11:17 +00:00
|
|
|
}
|
2023-05-31 11:24:20 +00:00
|
|
|
|
|
|
|
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-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) {
|
|
|
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
w := databricks.Must(databricks.NewWorkspaceClient())
|
|
|
|
tmpdir := temporaryWorkspaceDir(t, w)
|
|
|
|
f, err := filer.NewWorkspaceFilesClient(w, tmpdir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check if we can use this API here, skip test if we cannot.
|
|
|
|
_, err = f.Read(ctx, "we_use_this_call_to_test_if_this_api_is_enabled")
|
|
|
|
var aerr *apierr.APIError
|
|
|
|
if errors.As(err, &aerr) && aerr.StatusCode == http.StatusBadRequest {
|
|
|
|
t.Skip(aerr.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx, f
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccFilerWorkspaceFilesReadWrite(t *testing.T) {
|
|
|
|
ctx, f := setupWorkspaceFilesTest(t)
|
|
|
|
runFilerReadWriteTest(t, ctx, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccFilerWorkspaceFilesReadDir(t *testing.T) {
|
|
|
|
ctx, f := setupWorkspaceFilesTest(t)
|
|
|
|
runFilerReadDirTest(t, ctx, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func temporaryDbfsDir(t *testing.T, w *databricks.WorkspaceClient) string {
|
|
|
|
ctx := context.Background()
|
|
|
|
path := fmt.Sprintf("/tmp/%s", RandomName("integration-test-filer-dbfs-"))
|
|
|
|
|
|
|
|
// This call fails if the path already exists.
|
|
|
|
t.Logf("mkdir dbfs:%s", path)
|
|
|
|
err := w.Dbfs.MkdirsByPath(ctx, path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Remove test directory on test completion.
|
|
|
|
t.Cleanup(func() {
|
|
|
|
t.Logf("rm -rf dbfs:%s", path)
|
|
|
|
err := w.Dbfs.Delete(ctx, files.Delete{
|
|
|
|
Path: path,
|
|
|
|
Recursive: true,
|
|
|
|
})
|
|
|
|
if err == nil || apierr.IsMissing(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("unable to remove temporary dbfs directory %s: %#v", path, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupFilerDbfsTest(t *testing.T) (context.Context, filer.Filer) {
|
|
|
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
w := databricks.Must(databricks.NewWorkspaceClient())
|
|
|
|
tmpdir := temporaryDbfsDir(t, w)
|
|
|
|
f, err := filer.NewDbfsClient(w, tmpdir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return ctx, f
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccFilerDbfsReadWrite(t *testing.T) {
|
|
|
|
ctx, f := setupFilerDbfsTest(t)
|
|
|
|
runFilerReadWriteTest(t, ctx, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccFilerDbfsReadDir(t *testing.T) {
|
|
|
|
ctx, f := setupFilerDbfsTest(t)
|
|
|
|
runFilerReadDirTest(t, ctx, f)
|
|
|
|
}
|