Skip to content

Commit e490e07

Browse files
authored
Fixed cleaning up artifact path .internal folder (#2572)
## Changes Fixed cleaning up artifact path .internal folder ## Why Due to the bug in the code (we can't delete the root folder using filers) we never cleaned up .internal folder where artifacts are uploaded to. ## Tests Added regression test
1 parent 3229dd7 commit e490e07

File tree

10 files changed

+111
-67
lines changed

10 files changed

+111
-67
lines changed

NEXT_CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
## Release v0.246.0
44

5+
### Notable Changes
6+
Previously ".internal" folder under artifact_path was not cleaned up as expected. In this release this behaviour is fixed and now DABs cleans up this folder before uploading artifacts to it.
7+
58
### CLI
69

710
### Bundles
11+
* Fixed cleaning up artifact path .internal folder ([#2572](https://github.com/databricks/cli/pull/2572))
812

913
### API Changes

acceptance/bundle/artifacts/whl_explicit/output.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,24 @@ my_test_code/dist/my_test_code-0.0.1-py3-none-any.whl
3232
>>> jq .path
3333
"/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/python-wheel/default/artifacts/.internal/my_test_code-0.0.1-py3-none-any.whl"
3434
"/api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/python-wheel/default/files/my_test_code/dist/my_test_code-0.0.1-py3-none-any.whl"
35+
36+
=== Expecting delete request to artifact_path/.internal folder
37+
>>> jq -s .[] | select(.path=="/api/2.0/workspace/delete") | select(.body.path | test(".*/artifacts/.internal")) out.requests.txt
38+
{
39+
"method": "POST",
40+
"path": "/api/2.0/workspace/delete",
41+
"body": {
42+
"path": "/Workspace/Users/[USERNAME]/.bundle/python-wheel/default/artifacts/.internal",
43+
"recursive": true
44+
}
45+
}
46+
47+
=== Expecting mkdirs request to artifact_path/.internal folder
48+
>>> jq -s .[] | select(.path=="/api/2.0/workspace/mkdirs") | select(.body.path | test(".*/artifacts/.internal")) out.requests.txt
49+
{
50+
"method": "POST",
51+
"path": "/api/2.0/workspace/mkdirs",
52+
"body": {
53+
"path": "/Workspace/Users/[USERNAME]/.bundle/python-wheel/default/artifacts/.internal"
54+
}
55+
}

acceptance/bundle/artifacts/whl_explicit/script

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ trace jq -s '.[] | select(.path=="/api/2.1/jobs/create") | .body.tasks' out.requ
88
title "Expecting 1 wheel to be uploaded"
99
trace jq .path < out.requests.txt | grep import | grep whl | sort
1010

11+
title "Expecting delete request to artifact_path/.internal folder"
12+
trace jq -s '.[] | select(.path=="/api/2.0/workspace/delete") | select(.body.path | test(".*/artifacts/.internal"))' out.requests.txt
13+
14+
title "Expecting mkdirs request to artifact_path/.internal folder"
15+
trace jq -s '.[] | select(.path=="/api/2.0/workspace/mkdirs") | select(.body.path | test(".*/artifacts/.internal"))' out.requests.txt
16+
17+
1118
rm out.requests.txt

bundle/artifacts/upload.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ func (m *cleanUp) Name() string {
2121
}
2222

2323
func (m *cleanUp) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
24-
client, uploadPath, diags := libraries.GetFilerForLibraries(ctx, b)
24+
client, uploadPath, diags := libraries.GetFilerForLibrariesCleanup(ctx, b)
2525
if diags.HasError() {
2626
return diags
2727
}
2828

2929
// We intentionally ignore the error because it is not critical to the deployment
30-
err := client.Delete(ctx, ".", filer.DeleteRecursively)
30+
err := client.Delete(ctx, libraries.InternalDirName, filer.DeleteRecursively)
3131
if err != nil {
3232
log.Debugf(ctx, "failed to delete %s: %v", uploadPath, err)
3333
}
3434

35-
err = client.Mkdir(ctx, ".")
35+
err = client.Mkdir(ctx, libraries.InternalDirName)
3636
if err != nil {
3737
return diag.Errorf("unable to create directory for %s: %v", uploadPath, err)
3838
}

bundle/libraries/filer.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package libraries
22

33
import (
44
"context"
5+
"path"
56

67
"github.com/databricks/cli/bundle"
78
"github.com/databricks/cli/libs/diag"
@@ -22,11 +23,28 @@ func GetFilerForLibraries(ctx context.Context, b *bundle.Bundle) (filer.Filer, s
2223
return nil, "", diag.Errorf("remote artifact path not configured")
2324
}
2425

26+
uploadPath := path.Join(b.Config.Workspace.ArtifactPath, InternalDirName)
27+
28+
switch {
29+
case IsVolumesPath(artifactPath):
30+
return filerForVolume(b, uploadPath)
31+
32+
default:
33+
return filerForWorkspace(b, uploadPath)
34+
}
35+
}
36+
37+
func GetFilerForLibrariesCleanup(ctx context.Context, b *bundle.Bundle) (filer.Filer, string, diag.Diagnostics) {
38+
artifactPath := b.Config.Workspace.ArtifactPath
39+
if artifactPath == "" {
40+
return nil, "", diag.Errorf("remote artifact path not configured")
41+
}
42+
2543
switch {
2644
case IsVolumesPath(artifactPath):
27-
return filerForVolume(b)
45+
return filerForVolume(b, b.Config.Workspace.ArtifactPath)
2846

2947
default:
30-
return filerForWorkspace(b)
48+
return filerForWorkspace(b, b.Config.Workspace.ArtifactPath)
3149
}
3250
}

bundle/libraries/filer_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/databricks/cli/bundle"
88
"github.com/databricks/cli/bundle/config"
99
"github.com/databricks/cli/libs/filer"
10+
databrickscfg "github.com/databricks/databricks-sdk-go/config"
11+
"github.com/databricks/databricks-sdk-go/experimental/mocks"
1012
"github.com/stretchr/testify/assert"
1113
"github.com/stretchr/testify/require"
1214
)
@@ -20,13 +22,37 @@ func TestGetFilerForLibrariesValidWsfs(t *testing.T) {
2022
},
2123
}
2224

25+
m := mocks.NewMockWorkspaceClient(t)
26+
m.WorkspaceClient.Config = &databrickscfg.Config{}
27+
b.SetWorkpaceClient(m.WorkspaceClient)
28+
2329
client, uploadPath, diags := GetFilerForLibraries(context.Background(), b)
2430
require.NoError(t, diags.Error())
2531
assert.Equal(t, "/foo/bar/artifacts/.internal", uploadPath)
2632

2733
assert.IsType(t, &filer.WorkspaceFilesClient{}, client)
2834
}
2935

36+
func TestGetFilerForLibrariesCleanupValidWsfs(t *testing.T) {
37+
b := &bundle.Bundle{
38+
Config: config.Root{
39+
Workspace: config.Workspace{
40+
ArtifactPath: "/foo/bar/artifacts",
41+
},
42+
},
43+
}
44+
45+
m := mocks.NewMockWorkspaceClient(t)
46+
m.WorkspaceClient.Config = &databrickscfg.Config{}
47+
b.SetWorkpaceClient(m.WorkspaceClient)
48+
49+
client, uploadPath, diags := GetFilerForLibrariesCleanup(context.Background(), b)
50+
require.NoError(t, diags.Error())
51+
assert.Equal(t, "/foo/bar/artifacts", uploadPath)
52+
53+
assert.IsType(t, &filer.WorkspaceFilesClient{}, client)
54+
}
55+
3056
func TestGetFilerForLibrariesValidUcVolume(t *testing.T) {
3157
b := &bundle.Bundle{
3258
Config: config.Root{
@@ -36,20 +62,48 @@ func TestGetFilerForLibrariesValidUcVolume(t *testing.T) {
3662
},
3763
}
3864

65+
m := mocks.NewMockWorkspaceClient(t)
66+
m.WorkspaceClient.Config = &databrickscfg.Config{}
67+
b.SetWorkpaceClient(m.WorkspaceClient)
68+
3969
client, uploadPath, diags := GetFilerForLibraries(context.Background(), b)
4070
require.NoError(t, diags.Error())
4171
assert.Equal(t, "/Volumes/main/my_schema/my_volume/.internal", uploadPath)
4272

4373
assert.IsType(t, &filer.FilesClient{}, client)
4474
}
4575

76+
func TestGetFilerForLibrariesCleanupValidUcVolume(t *testing.T) {
77+
b := &bundle.Bundle{
78+
Config: config.Root{
79+
Workspace: config.Workspace{
80+
ArtifactPath: "/Volumes/main/my_schema/my_volume",
81+
},
82+
},
83+
}
84+
85+
m := mocks.NewMockWorkspaceClient(t)
86+
m.WorkspaceClient.Config = &databrickscfg.Config{}
87+
b.SetWorkpaceClient(m.WorkspaceClient)
88+
89+
client, uploadPath, diags := GetFilerForLibrariesCleanup(context.Background(), b)
90+
require.NoError(t, diags.Error())
91+
assert.Equal(t, "/Volumes/main/my_schema/my_volume", uploadPath)
92+
93+
assert.IsType(t, &filer.FilesClient{}, client)
94+
}
95+
4696
func TestGetFilerForLibrariesRemotePathNotSet(t *testing.T) {
4797
b := &bundle.Bundle{
4898
Config: config.Root{
4999
Workspace: config.Workspace{},
50100
},
51101
}
52102

103+
m := mocks.NewMockWorkspaceClient(t)
104+
m.WorkspaceClient.Config = &databrickscfg.Config{}
105+
b.SetWorkpaceClient(m.WorkspaceClient)
106+
53107
_, _, diags := GetFilerForLibraries(context.Background(), b)
54108
require.EqualError(t, diags.Error(), "remote artifact path not configured")
55109
}

bundle/libraries/filer_volume.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package libraries
22

33
import (
4-
"path"
5-
64
"github.com/databricks/cli/bundle"
75
"github.com/databricks/cli/libs/diag"
86
"github.com/databricks/cli/libs/filer"
97
)
108

11-
func filerForVolume(b *bundle.Bundle) (filer.Filer, string, diag.Diagnostics) {
9+
func filerForVolume(b *bundle.Bundle, uploadPath string) (filer.Filer, string, diag.Diagnostics) {
1210
w := b.WorkspaceClient()
13-
uploadPath := path.Join(b.Config.Workspace.ArtifactPath, InternalDirName)
1411
f, err := filer.NewFilesClient(w, uploadPath)
1512
return f, uploadPath, diag.FromErr(err)
1613
}

bundle/libraries/filer_volume_test.go

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package libraries
22

33
import (
4-
"path"
5-
64
"github.com/databricks/cli/bundle"
75
"github.com/databricks/cli/libs/diag"
86
"github.com/databricks/cli/libs/filer"
97
)
108

11-
func filerForWorkspace(b *bundle.Bundle) (filer.Filer, string, diag.Diagnostics) {
12-
uploadPath := path.Join(b.Config.Workspace.ArtifactPath, InternalDirName)
9+
func filerForWorkspace(b *bundle.Bundle, uploadPath string) (filer.Filer, string, diag.Diagnostics) {
1310
f, err := filer.NewWorkspaceFilesClient(b.WorkspaceClient(), uploadPath)
1411
return f, uploadPath, diag.FromErr(err)
1512
}

bundle/libraries/filer_workspace_test.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)