2024-03-18 14:41:58 +00:00
|
|
|
package deploy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/internal/build"
|
|
|
|
"github.com/databricks/cli/internal/testutil"
|
2024-06-17 09:48:52 +00:00
|
|
|
"github.com/databricks/cli/libs/fileset"
|
|
|
|
"github.com/databricks/cli/libs/vfs"
|
2024-03-18 14:41:58 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/iam"
|
2024-07-16 10:01:58 +00:00
|
|
|
"github.com/google/uuid"
|
2024-03-18 14:41:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-06-17 09:48:52 +00:00
|
|
|
func setupBundleForStateUpdate(t *testing.T) *bundle.Bundle {
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
|
|
|
testutil.Touch(t, tmpDir, "test1.py")
|
|
|
|
testutil.TouchNotebook(t, tmpDir, "test2.py")
|
|
|
|
|
2024-08-19 15:15:14 +00:00
|
|
|
files, err := fileset.New(vfs.MustNew(tmpDir)).Files()
|
2024-06-17 09:48:52 +00:00
|
|
|
require.NoError(t, err)
|
2024-03-18 14:41:58 +00:00
|
|
|
|
2024-06-17 09:48:52 +00:00
|
|
|
return &bundle.Bundle{
|
2024-09-27 10:03:05 +00:00
|
|
|
BundleRootPath: tmpDir,
|
2024-03-18 14:41:58 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "default",
|
|
|
|
},
|
|
|
|
Workspace: config.Workspace{
|
|
|
|
StatePath: "/state",
|
|
|
|
FilePath: "/files",
|
|
|
|
CurrentUser: &config.User{
|
|
|
|
User: &iam.User{
|
|
|
|
UserName: "test-user",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-06-17 09:48:52 +00:00
|
|
|
Files: files,
|
2024-03-18 14:41:58 +00:00
|
|
|
}
|
2024-06-17 09:48:52 +00:00
|
|
|
}
|
2024-03-18 14:41:58 +00:00
|
|
|
|
2024-06-17 09:48:52 +00:00
|
|
|
func TestStateUpdate(t *testing.T) {
|
|
|
|
s := &stateUpdate{}
|
2024-03-18 14:41:58 +00:00
|
|
|
|
2024-06-17 09:48:52 +00:00
|
|
|
b := setupBundleForStateUpdate(t)
|
2024-03-18 14:41:58 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(ctx, b, s)
|
|
|
|
require.NoError(t, diags.Error())
|
2024-03-18 14:41:58 +00:00
|
|
|
|
|
|
|
// Check that the state file was updated.
|
|
|
|
state, err := load(ctx, b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, int64(1), state.Seq)
|
2024-06-17 09:48:52 +00:00
|
|
|
require.Equal(t, state.Files, Filelist{
|
|
|
|
{
|
|
|
|
LocalPath: "test1.py",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
LocalPath: "test2.py",
|
|
|
|
IsNotebook: true,
|
|
|
|
},
|
|
|
|
})
|
2024-03-18 14:41:58 +00:00
|
|
|
require.Equal(t, build.GetInfo().Version, state.CliVersion)
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags = bundle.Apply(ctx, b, s)
|
|
|
|
require.NoError(t, diags.Error())
|
2024-03-18 14:41:58 +00:00
|
|
|
|
|
|
|
// Check that the state file was updated again.
|
|
|
|
state, err = load(ctx, b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, int64(2), state.Seq)
|
2024-06-17 09:48:52 +00:00
|
|
|
require.Equal(t, state.Files, Filelist{
|
|
|
|
{
|
|
|
|
LocalPath: "test1.py",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
LocalPath: "test2.py",
|
|
|
|
IsNotebook: true,
|
|
|
|
},
|
|
|
|
})
|
2024-03-18 14:41:58 +00:00
|
|
|
require.Equal(t, build.GetInfo().Version, state.CliVersion)
|
2024-07-16 10:01:58 +00:00
|
|
|
|
|
|
|
// Valid non-empty UUID is generated.
|
|
|
|
require.NotEqual(t, uuid.Nil, state.ID)
|
2024-03-18 14:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateUpdateWithExistingState(t *testing.T) {
|
|
|
|
s := &stateUpdate{}
|
|
|
|
|
2024-06-17 09:48:52 +00:00
|
|
|
b := setupBundleForStateUpdate(t)
|
2024-03-18 14:41:58 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Create an existing state file.
|
|
|
|
statePath, err := getPathToStateFile(ctx, b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
state := &DeploymentState{
|
|
|
|
Version: DeploymentStateVersion,
|
|
|
|
Seq: 10,
|
|
|
|
CliVersion: build.GetInfo().Version,
|
|
|
|
Files: []File{
|
|
|
|
{
|
|
|
|
LocalPath: "bar/t1.py",
|
|
|
|
},
|
|
|
|
},
|
2024-07-16 10:01:58 +00:00
|
|
|
ID: uuid.MustParse("123e4567-e89b-12d3-a456-426614174000"),
|
2024-03-18 14:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(state)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.WriteFile(statePath, data, 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(ctx, b, s)
|
|
|
|
require.NoError(t, diags.Error())
|
2024-03-18 14:41:58 +00:00
|
|
|
|
|
|
|
// Check that the state file was updated.
|
|
|
|
state, err = load(ctx, b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, int64(11), state.Seq)
|
2024-06-17 09:48:52 +00:00
|
|
|
require.Equal(t, state.Files, Filelist{
|
|
|
|
{
|
|
|
|
LocalPath: "test1.py",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
LocalPath: "test2.py",
|
|
|
|
IsNotebook: true,
|
|
|
|
},
|
|
|
|
})
|
2024-03-18 14:41:58 +00:00
|
|
|
require.Equal(t, build.GetInfo().Version, state.CliVersion)
|
2024-07-16 10:01:58 +00:00
|
|
|
|
|
|
|
// Existing UUID is not overwritten.
|
|
|
|
require.Equal(t, uuid.MustParse("123e4567-e89b-12d3-a456-426614174000"), state.ID)
|
2024-03-18 14:41:58 +00:00
|
|
|
}
|