2022-09-14 15:50:29 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-19 14:22:55 +00:00
|
|
|
"encoding/json"
|
2022-09-14 15:50:29 +00:00
|
|
|
"fmt"
|
2022-10-19 14:22:55 +00:00
|
|
|
"io"
|
2023-01-10 12:16:30 +00:00
|
|
|
"net/http"
|
2022-09-14 15:50:29 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2023-01-10 12:16:30 +00:00
|
|
|
"path"
|
2022-09-14 15:50:29 +00:00
|
|
|
"path/filepath"
|
2022-12-12 13:31:06 +00:00
|
|
|
"strings"
|
2022-09-14 15:50:29 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-01-23 12:52:39 +00:00
|
|
|
_ "github.com/databricks/bricks/cmd/sync"
|
|
|
|
"github.com/databricks/bricks/libs/sync"
|
2023-01-10 12:16:30 +00:00
|
|
|
"github.com/databricks/bricks/libs/testfile"
|
2022-11-24 20:41:57 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go"
|
2023-01-10 12:16:30 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/client"
|
2022-09-14 15:50:29 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/repos"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/workspace"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-01-10 12:16:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-09-14 15:50:29 +00:00
|
|
|
)
|
|
|
|
|
2022-10-05 11:28:53 +00:00
|
|
|
// This test needs auth env vars to run.
|
|
|
|
// Please run using the deco env test or deco env shell
|
2023-01-10 12:16:30 +00:00
|
|
|
func setupRepo(t *testing.T, wsc *databricks.WorkspaceClient, ctx context.Context) (localRoot, remoteRoot string) {
|
2022-09-14 15:50:29 +00:00
|
|
|
me, err := wsc.CurrentUser.Me(ctx)
|
2023-01-24 14:06:59 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-14 15:50:29 +00:00
|
|
|
repoUrl := "https://github.com/shreyas-goenka/empty-repo.git"
|
|
|
|
repoPath := fmt.Sprintf("/Repos/%s/%s", me.UserName, RandomName("empty-repo-sync-integration-"))
|
|
|
|
|
|
|
|
repoInfo, err := wsc.Repos.Create(ctx, repos.CreateRepo{
|
|
|
|
Path: repoPath,
|
|
|
|
Url: repoUrl,
|
|
|
|
Provider: "gitHub",
|
|
|
|
})
|
2023-01-24 14:06:59 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-14 15:50:29 +00:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2022-09-27 16:58:55 +00:00
|
|
|
err := wsc.Repos.DeleteByRepoId(ctx, repoInfo.Id)
|
2022-09-14 15:50:29 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2022-10-05 11:28:53 +00:00
|
|
|
// clone public empty remote repo
|
2022-09-14 15:50:29 +00:00
|
|
|
tempDir := t.TempDir()
|
|
|
|
cmd := exec.Command("git", "clone", repoUrl)
|
|
|
|
cmd.Dir = tempDir
|
|
|
|
err = cmd.Run()
|
2023-01-24 14:06:59 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-14 15:50:29 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
localRoot = filepath.Join(tempDir, "empty-repo")
|
|
|
|
remoteRoot = repoPath
|
|
|
|
return localRoot, remoteRoot
|
|
|
|
}
|
2022-09-14 15:50:29 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
type assertSync struct {
|
|
|
|
t *testing.T
|
|
|
|
c *cobraTestRunner
|
|
|
|
w *databricks.WorkspaceClient
|
|
|
|
localRoot string
|
|
|
|
remoteRoot string
|
|
|
|
}
|
2022-09-14 15:50:29 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
func (a *assertSync) remoteDirContent(ctx context.Context, relativeDir string, expectedFiles []string) {
|
|
|
|
remoteDir := path.Join(a.remoteRoot, relativeDir)
|
|
|
|
a.c.Eventually(func() bool {
|
|
|
|
objects, err := a.w.Workspace.ListAll(ctx, workspace.List{
|
|
|
|
Path: remoteDir,
|
2022-09-14 15:50:29 +00:00
|
|
|
})
|
2023-01-10 12:16:30 +00:00
|
|
|
require.NoError(a.t, err)
|
|
|
|
return len(objects) == len(expectedFiles)
|
2022-10-05 11:28:53 +00:00
|
|
|
}, 30*time.Second, 5*time.Second)
|
2023-01-10 12:16:30 +00:00
|
|
|
objects, err := a.w.Workspace.ListAll(ctx, workspace.List{
|
|
|
|
Path: remoteDir,
|
2022-09-14 15:50:29 +00:00
|
|
|
})
|
2023-01-10 12:16:30 +00:00
|
|
|
require.NoError(a.t, err)
|
|
|
|
|
|
|
|
var actualFiles []string
|
2022-11-24 20:41:57 +00:00
|
|
|
for _, v := range objects {
|
2023-01-10 12:16:30 +00:00
|
|
|
actualFiles = append(actualFiles, v.Path)
|
2022-09-14 15:50:29 +00:00
|
|
|
}
|
2023-01-10 12:16:30 +00:00
|
|
|
|
|
|
|
assert.Len(a.t, actualFiles, len(expectedFiles))
|
|
|
|
for _, v := range expectedFiles {
|
|
|
|
assert.Contains(a.t, actualFiles, path.Join(a.remoteRoot, relativeDir, v))
|
2022-09-14 15:50:29 +00:00
|
|
|
}
|
2023-01-10 12:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *assertSync) remoteFileContent(ctx context.Context, relativePath string, expectedContent string) {
|
|
|
|
filePath := path.Join(a.remoteRoot, relativePath)
|
|
|
|
|
|
|
|
// Remove leading "/" so we can use it in the URL.
|
|
|
|
urlPath := fmt.Sprintf(
|
|
|
|
"/api/2.0/workspace-files/%s",
|
|
|
|
strings.TrimLeft(filePath, "/"),
|
|
|
|
)
|
|
|
|
|
|
|
|
apiClient, err := client.New(a.w.Config)
|
|
|
|
require.NoError(a.t, err)
|
|
|
|
|
|
|
|
var res []byte
|
|
|
|
a.c.Eventually(func() bool {
|
|
|
|
err = apiClient.Do(ctx, http.MethodGet, urlPath, nil, &res)
|
|
|
|
require.NoError(a.t, err)
|
|
|
|
actualContent := string(res)
|
|
|
|
return actualContent == expectedContent
|
2022-10-05 11:28:53 +00:00
|
|
|
}, 30*time.Second, 5*time.Second)
|
2022-09-14 15:50:29 +00:00
|
|
|
}
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
func (a *assertSync) objectType(ctx context.Context, relativePath string, expected string) {
|
|
|
|
path := path.Join(a.remoteRoot, relativePath)
|
|
|
|
|
|
|
|
a.c.Eventually(func() bool {
|
|
|
|
metadata, err := a.w.Workspace.GetStatusByPath(ctx, path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return metadata.ObjectType.String() == expected
|
|
|
|
}, 30*time.Second, 5*time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *assertSync) language(ctx context.Context, relativePath string, expected string) {
|
|
|
|
path := path.Join(a.remoteRoot, relativePath)
|
|
|
|
|
|
|
|
a.c.Eventually(func() bool {
|
|
|
|
metadata, err := a.w.Workspace.GetStatusByPath(ctx, path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return metadata.Language.String() == expected
|
|
|
|
}, 30*time.Second, 5*time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *assertSync) snapshotContains(files []string) {
|
|
|
|
snapshotPath := filepath.Join(a.localRoot, ".databricks/sync-snapshots", sync.GetFileName(a.w.Config.Host, a.remoteRoot))
|
|
|
|
assert.FileExists(a.t, snapshotPath)
|
2022-10-19 14:22:55 +00:00
|
|
|
|
|
|
|
var s *sync.Snapshot
|
|
|
|
f, err := os.Open(snapshotPath)
|
2023-01-10 12:16:30 +00:00
|
|
|
assert.NoError(a.t, err)
|
2022-10-19 14:22:55 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
bytes, err := io.ReadAll(f)
|
2023-01-10 12:16:30 +00:00
|
|
|
assert.NoError(a.t, err)
|
2022-10-19 14:22:55 +00:00
|
|
|
err = json.Unmarshal(bytes, &s)
|
2023-01-10 12:16:30 +00:00
|
|
|
assert.NoError(a.t, err)
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
assert.Equal(a.t, s.Host, a.w.Config.Host)
|
|
|
|
assert.Equal(a.t, s.RemotePath, a.remoteRoot)
|
|
|
|
for _, filePath := range files {
|
2022-10-19 14:22:55 +00:00
|
|
|
_, ok := s.LastUpdatedTimes[filePath]
|
2023-01-10 12:16:30 +00:00
|
|
|
assert.True(a.t, ok, fmt.Sprintf("%s not in snapshot file: %v", filePath, s.LastUpdatedTimes))
|
2022-10-19 14:22:55 +00:00
|
|
|
}
|
2023-01-10 12:16:30 +00:00
|
|
|
assert.Equal(a.t, len(files), len(s.LastUpdatedTimes))
|
2022-10-19 14:22:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
func TestAccFullFileSync(t *testing.T) {
|
2022-10-19 14:22:55 +00:00
|
|
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
|
|
|
|
2022-11-24 20:41:57 +00:00
|
|
|
wsc := databricks.Must(databricks.NewWorkspaceClient())
|
2022-10-19 14:22:55 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
localRepoPath, remoteRepoPath := setupRepo(t, wsc, ctx)
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
// Run `bricks sync` in the background.
|
|
|
|
t.Setenv("BRICKS_ROOT", localRepoPath)
|
2023-01-24 14:06:59 +00:00
|
|
|
c := NewCobraTestRunner(t, "sync", "--remote-path", remoteRepoPath, "--full", "--watch")
|
2023-01-10 12:16:30 +00:00
|
|
|
c.RunBackground()
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
assertSync := assertSync{
|
|
|
|
t: t,
|
|
|
|
c: c,
|
|
|
|
w: wsc,
|
|
|
|
localRoot: localRepoPath,
|
|
|
|
remoteRoot: remoteRepoPath,
|
|
|
|
}
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
// .gitkeep comes from cloning during repo setup
|
2023-01-11 10:50:27 +00:00
|
|
|
// .gitignore is created by the sync process to enforce .databricks is not
|
|
|
|
// synced
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{".gitkeep", ".gitignore"})
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
// New file
|
|
|
|
localFilePath := filepath.Join(localRepoPath, "foo.txt")
|
|
|
|
f := testfile.CreateFile(t, localFilePath)
|
|
|
|
defer f.Close(t)
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{"foo.txt", ".gitkeep", ".gitignore"})
|
|
|
|
assertSync.remoteFileContent(ctx, "foo.txt", "")
|
|
|
|
|
|
|
|
// Write to file
|
|
|
|
f.Overwrite(t, `{"statement": "Mi Gente"}`)
|
|
|
|
assertSync.remoteFileContent(ctx, "foo.txt", `{"statement": "Mi Gente"}`)
|
|
|
|
|
|
|
|
// Write again
|
|
|
|
f.Overwrite(t, `{"statement": "Young Dumb & Broke"}`)
|
|
|
|
assertSync.remoteFileContent(ctx, "foo.txt", `{"statement": "Young Dumb & Broke"}`)
|
|
|
|
|
|
|
|
// delete
|
|
|
|
f.Remove(t)
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{".gitkeep", ".gitignore"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccIncrementalFileSync(t *testing.T) {
|
|
|
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
|
|
|
|
|
|
|
wsc := databricks.Must(databricks.NewWorkspaceClient())
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
localRepoPath, remoteRepoPath := setupRepo(t, wsc, ctx)
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2022-12-09 10:47:06 +00:00
|
|
|
// Run `bricks sync` in the background.
|
2023-01-10 12:16:30 +00:00
|
|
|
t.Setenv("BRICKS_ROOT", localRepoPath)
|
2023-01-24 14:06:59 +00:00
|
|
|
c := NewCobraTestRunner(t, "sync", "--remote-path", remoteRepoPath, "--watch")
|
2022-12-09 10:47:06 +00:00
|
|
|
c.RunBackground()
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
assertSync := assertSync{
|
|
|
|
t: t,
|
|
|
|
c: c,
|
|
|
|
w: wsc,
|
|
|
|
localRoot: localRepoPath,
|
|
|
|
remoteRoot: remoteRepoPath,
|
2022-10-19 14:22:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
// .gitkeep comes from cloning during repo setup
|
2023-01-11 10:50:27 +00:00
|
|
|
// .gitignore is created by the sync process to enforce .databricks is not
|
|
|
|
// synced
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{".gitkeep", ".gitignore"})
|
2023-01-10 12:16:30 +00:00
|
|
|
|
|
|
|
// New file
|
|
|
|
localFilePath := filepath.Join(localRepoPath, "foo.txt")
|
|
|
|
f := testfile.CreateFile(t, localFilePath)
|
|
|
|
defer f.Close(t)
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{"foo.txt", ".gitkeep", ".gitignore"})
|
|
|
|
assertSync.remoteFileContent(ctx, "foo.txt", "")
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore", "foo.txt"})
|
|
|
|
|
|
|
|
// Write to file
|
|
|
|
f.Overwrite(t, `{"statement": "Mi Gente"}`)
|
|
|
|
assertSync.remoteFileContent(ctx, "foo.txt", `{"statement": "Mi Gente"}`)
|
|
|
|
|
|
|
|
// Write again
|
|
|
|
f.Overwrite(t, `{"statement": "Young Dumb & Broke"}`)
|
|
|
|
assertSync.remoteFileContent(ctx, "foo.txt", `{"statement": "Young Dumb & Broke"}`)
|
|
|
|
|
|
|
|
// delete
|
|
|
|
f.Remove(t)
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{".gitkeep", ".gitignore"})
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore"})
|
|
|
|
}
|
2022-10-19 14:22:55 +00:00
|
|
|
|
2023-01-10 12:16:30 +00:00
|
|
|
func TestAccNestedFolderSync(t *testing.T) {
|
|
|
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
|
|
|
|
|
|
|
wsc := databricks.Must(databricks.NewWorkspaceClient())
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
localRepoPath, remoteRepoPath := setupRepo(t, wsc, ctx)
|
|
|
|
|
|
|
|
// Run `bricks sync` in the background.
|
|
|
|
t.Setenv("BRICKS_ROOT", localRepoPath)
|
2023-01-24 14:06:59 +00:00
|
|
|
c := NewCobraTestRunner(t, "sync", "--remote-path", remoteRepoPath, "--watch")
|
2023-01-10 12:16:30 +00:00
|
|
|
c.RunBackground()
|
|
|
|
|
|
|
|
assertSync := assertSync{
|
|
|
|
t: t,
|
|
|
|
c: c,
|
|
|
|
w: wsc,
|
|
|
|
localRoot: localRepoPath,
|
|
|
|
remoteRoot: remoteRepoPath,
|
2022-10-19 14:22:55 +00:00
|
|
|
}
|
2023-01-10 12:16:30 +00:00
|
|
|
|
|
|
|
// .gitkeep comes from cloning during repo setup
|
2023-01-11 10:50:27 +00:00
|
|
|
// .gitignore is created by the sync process to enforce .databricks is not
|
|
|
|
// synced
|
|
|
|
assertSync.remoteDirContent(ctx, "/", []string{".gitkeep", ".gitignore"})
|
2023-01-10 12:16:30 +00:00
|
|
|
|
|
|
|
// New file
|
|
|
|
localFilePath := filepath.Join(localRepoPath, "dir1/dir2/dir3/foo.txt")
|
|
|
|
err := os.MkdirAll(filepath.Dir(localFilePath), 0o755)
|
2022-10-19 14:22:55 +00:00
|
|
|
assert.NoError(t, err)
|
2023-01-10 12:16:30 +00:00
|
|
|
f := testfile.CreateFile(t, localFilePath)
|
|
|
|
defer f.Close(t)
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{"dir1", ".gitkeep", ".gitignore"})
|
|
|
|
assertSync.remoteDirContent(ctx, "dir1", []string{"dir2"})
|
|
|
|
assertSync.remoteDirContent(ctx, "dir1/dir2", []string{"dir3"})
|
|
|
|
assertSync.remoteDirContent(ctx, "dir1/dir2/dir3", []string{"foo.txt"})
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore", filepath.FromSlash("dir1/dir2/dir3/foo.txt")})
|
|
|
|
|
|
|
|
// delete
|
|
|
|
f.Remove(t)
|
|
|
|
// directories are not cleaned up right now. This is not ideal
|
|
|
|
assertSync.remoteDirContent(ctx, "dir1/dir2/dir3", []string{})
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore"})
|
|
|
|
}
|
|
|
|
|
|
|
|
// sync does not clean up empty directories from the workspace file system.
|
|
|
|
// This is a check for the edge case when a user does the following:
|
|
|
|
//
|
|
|
|
// 1. Add file foo/bar.txt
|
|
|
|
// 2. Delete foo/bar.txt (including the directory)
|
|
|
|
// 3. Add file foo
|
|
|
|
//
|
|
|
|
// In the above scenario sync should delete the empty folder and add foo to the remote
|
|
|
|
// file system
|
|
|
|
func TestAccIncrementalFileOverwritesFolder(t *testing.T) {
|
|
|
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
|
|
|
|
|
|
|
wsc := databricks.Must(databricks.NewWorkspaceClient())
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
localRepoPath, remoteRepoPath := setupRepo(t, wsc, ctx)
|
|
|
|
|
|
|
|
// Run `bricks sync` in the background.
|
|
|
|
t.Setenv("BRICKS_ROOT", localRepoPath)
|
2023-01-24 14:06:59 +00:00
|
|
|
c := NewCobraTestRunner(t, "sync", "--remote-path", remoteRepoPath, "--watch")
|
2023-01-10 12:16:30 +00:00
|
|
|
c.RunBackground()
|
|
|
|
|
|
|
|
assertSync := assertSync{
|
|
|
|
t: t,
|
|
|
|
c: c,
|
|
|
|
w: wsc,
|
|
|
|
localRoot: localRepoPath,
|
|
|
|
remoteRoot: remoteRepoPath,
|
2022-10-19 14:22:55 +00:00
|
|
|
}
|
2023-01-10 12:16:30 +00:00
|
|
|
|
|
|
|
// create foo/bar.txt
|
|
|
|
localFilePath := filepath.Join(localRepoPath, "foo/bar.txt")
|
|
|
|
err := os.MkdirAll(filepath.Dir(localFilePath), 0o755)
|
2022-12-12 13:31:06 +00:00
|
|
|
assert.NoError(t, err)
|
2023-01-10 12:16:30 +00:00
|
|
|
f := testfile.CreateFile(t, localFilePath)
|
|
|
|
defer f.Close(t)
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{"foo", ".gitkeep", ".gitignore"})
|
|
|
|
assertSync.remoteDirContent(ctx, "foo", []string{"bar.txt"})
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore", filepath.FromSlash("foo/bar.txt")})
|
|
|
|
|
|
|
|
// delete foo/bar.txt
|
|
|
|
f.Remove(t)
|
|
|
|
os.Remove(filepath.Join(localRepoPath, "foo"))
|
|
|
|
assertSync.remoteDirContent(ctx, "foo", []string{})
|
|
|
|
assertSync.objectType(ctx, "foo", "DIRECTORY")
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore"})
|
|
|
|
|
|
|
|
f2 := testfile.CreateFile(t, filepath.Join(localRepoPath, "foo"))
|
|
|
|
defer f2.Close(t)
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{"foo", ".gitkeep", ".gitignore"})
|
|
|
|
assertSync.objectType(ctx, "foo", "FILE")
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore", "foo"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccIncrementalSyncPythonNotebookToFile(t *testing.T) {
|
|
|
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
|
|
|
|
|
|
|
wsc := databricks.Must(databricks.NewWorkspaceClient())
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
localRepoPath, remoteRepoPath := setupRepo(t, wsc, ctx)
|
|
|
|
|
|
|
|
// create python notebook
|
|
|
|
localFilePath := filepath.Join(localRepoPath, "foo.py")
|
|
|
|
f := testfile.CreateFile(t, localFilePath)
|
|
|
|
defer f.Close(t)
|
|
|
|
f.Overwrite(t, "# Databricks notebook source")
|
|
|
|
|
|
|
|
// Run `bricks sync` in the background.
|
|
|
|
t.Setenv("BRICKS_ROOT", localRepoPath)
|
2023-01-24 14:06:59 +00:00
|
|
|
c := NewCobraTestRunner(t, "sync", "--remote-path", remoteRepoPath, "--watch")
|
2023-01-10 12:16:30 +00:00
|
|
|
c.RunBackground()
|
|
|
|
|
|
|
|
assertSync := assertSync{
|
|
|
|
t: t,
|
|
|
|
c: c,
|
|
|
|
w: wsc,
|
|
|
|
localRoot: localRepoPath,
|
|
|
|
remoteRoot: remoteRepoPath,
|
2022-12-12 13:31:06 +00:00
|
|
|
}
|
2023-01-10 12:16:30 +00:00
|
|
|
|
|
|
|
// notebook was uploaded properly
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{"foo", ".gitkeep", ".gitignore"})
|
|
|
|
assertSync.objectType(ctx, "foo", "NOTEBOOK")
|
|
|
|
assertSync.language(ctx, "foo", "PYTHON")
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore", "foo.py"})
|
|
|
|
|
|
|
|
// convert to vanilla python file
|
|
|
|
f.Overwrite(t, "# No longer a python notebook")
|
|
|
|
assertSync.objectType(ctx, "foo.py", "FILE")
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{"foo.py", ".gitkeep", ".gitignore"})
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore", "foo.py"})
|
|
|
|
|
|
|
|
// delete the vanilla python file
|
|
|
|
f.Remove(t)
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{".gitkeep", ".gitignore"})
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccIncrementalSyncFileToPythonNotebook(t *testing.T) {
|
|
|
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
|
|
|
|
|
|
|
wsc := databricks.Must(databricks.NewWorkspaceClient())
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
localRepoPath, remoteRepoPath := setupRepo(t, wsc, ctx)
|
|
|
|
|
|
|
|
// Run `bricks sync` in the background.
|
|
|
|
t.Setenv("BRICKS_ROOT", localRepoPath)
|
2023-01-24 14:06:59 +00:00
|
|
|
c := NewCobraTestRunner(t, "sync", "--remote-path", remoteRepoPath, "--watch")
|
2023-01-10 12:16:30 +00:00
|
|
|
c.RunBackground()
|
|
|
|
|
|
|
|
assertSync := assertSync{
|
|
|
|
t: t,
|
|
|
|
c: c,
|
|
|
|
w: wsc,
|
|
|
|
localRoot: localRepoPath,
|
|
|
|
remoteRoot: remoteRepoPath,
|
2022-12-12 13:31:06 +00:00
|
|
|
}
|
2023-01-10 12:16:30 +00:00
|
|
|
|
|
|
|
// create vanilla python file
|
|
|
|
localFilePath := filepath.Join(localRepoPath, "foo.py")
|
|
|
|
f := testfile.CreateFile(t, localFilePath)
|
|
|
|
defer f.Close(t)
|
|
|
|
|
|
|
|
// assert file upload
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{"foo.py", ".gitkeep", ".gitignore"})
|
|
|
|
assertSync.objectType(ctx, "foo.py", "FILE")
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore", "foo.py"})
|
|
|
|
|
|
|
|
// convert to notebook
|
|
|
|
f.Overwrite(t, "# Databricks notebook source")
|
|
|
|
assertSync.objectType(ctx, "foo", "NOTEBOOK")
|
|
|
|
assertSync.language(ctx, "foo", "PYTHON")
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{"foo", ".gitkeep", ".gitignore"})
|
|
|
|
assertSync.snapshotContains([]string{".gitkeep", ".gitignore", "foo.py"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccIncrementalSyncPythonNotebookDelete(t *testing.T) {
|
|
|
|
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
|
|
|
|
|
|
|
|
wsc := databricks.Must(databricks.NewWorkspaceClient())
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
localRepoPath, remoteRepoPath := setupRepo(t, wsc, ctx)
|
|
|
|
|
|
|
|
// create python notebook
|
|
|
|
localFilePath := filepath.Join(localRepoPath, "foo.py")
|
|
|
|
f := testfile.CreateFile(t, localFilePath)
|
|
|
|
defer f.Close(t)
|
|
|
|
f.Overwrite(t, "# Databricks notebook source")
|
|
|
|
|
|
|
|
// Run `bricks sync` in the background.
|
|
|
|
t.Setenv("BRICKS_ROOT", localRepoPath)
|
2023-01-24 14:06:59 +00:00
|
|
|
c := NewCobraTestRunner(t, "sync", "--remote-path", remoteRepoPath, "--watch")
|
2023-01-10 12:16:30 +00:00
|
|
|
c.RunBackground()
|
|
|
|
|
|
|
|
assertSync := assertSync{
|
|
|
|
t: t,
|
|
|
|
c: c,
|
|
|
|
w: wsc,
|
|
|
|
localRoot: localRepoPath,
|
|
|
|
remoteRoot: remoteRepoPath,
|
2022-12-12 13:31:06 +00:00
|
|
|
}
|
2023-01-10 12:16:30 +00:00
|
|
|
|
|
|
|
// notebook was uploaded properly
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{"foo", ".gitkeep", ".gitignore"})
|
|
|
|
assertSync.objectType(ctx, "foo", "NOTEBOOK")
|
|
|
|
assertSync.language(ctx, "foo", "PYTHON")
|
|
|
|
|
|
|
|
// Delete notebook
|
|
|
|
f.Remove(t)
|
|
|
|
assertSync.remoteDirContent(ctx, "", []string{".gitkeep", ".gitignore"})
|
2022-10-19 14:22:55 +00:00
|
|
|
}
|