2022-11-18 09:57:31 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2023-08-11 12:28:05 +00:00
|
|
|
"context"
|
2022-11-18 09:57:31 +00:00
|
|
|
"os"
|
2022-11-30 13:40:41 +00:00
|
|
|
"path/filepath"
|
2022-11-18 09:57:31 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
"github.com/databricks/cli/bundle/env"
|
2022-11-18 09:57:31 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadNotExists(t *testing.T) {
|
2023-08-11 12:28:05 +00:00
|
|
|
b, err := Load(context.Background(), "/doesntexist")
|
2022-11-18 09:57:31 +00:00
|
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
assert.Nil(t, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadExists(t *testing.T) {
|
2023-08-11 12:28:05 +00:00
|
|
|
b, err := Load(context.Background(), "./tests/basic")
|
2022-11-18 09:57:31 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, "basic", b.Config.Bundle.Name)
|
|
|
|
}
|
2022-11-30 13:40:41 +00:00
|
|
|
|
|
|
|
func TestBundleCacheDir(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
2022-11-30 13:40:41 +00:00
|
|
|
projectDir := t.TempDir()
|
2023-07-18 10:16:34 +00:00
|
|
|
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
|
2022-11-30 13:40:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
f1.Close()
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
bundle, err := Load(ctx, projectDir)
|
2022-11-30 13:40:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
// Artificially set target.
|
|
|
|
// This is otherwise done by [mutators.SelectTarget].
|
|
|
|
bundle.Config.Bundle.Target = "default"
|
2022-11-30 13:40:41 +00:00
|
|
|
|
2023-06-21 07:53:54 +00:00
|
|
|
// unset env variable in case it's set
|
|
|
|
t.Setenv("DATABRICKS_BUNDLE_TMP", "")
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
cacheDir, err := bundle.CacheDir(ctx)
|
2023-06-21 07:53:54 +00:00
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
// format is <CWD>/.databricks/bundle/<target>
|
2023-06-21 07:53:54 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, filepath.Join(projectDir, ".databricks", "bundle", "default"), cacheDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleCacheDirOverride(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
ctx := context.Background()
|
2023-06-21 07:53:54 +00:00
|
|
|
projectDir := t.TempDir()
|
|
|
|
bundleTmpDir := t.TempDir()
|
2023-07-18 10:16:34 +00:00
|
|
|
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
|
2023-06-21 07:53:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
f1.Close()
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
bundle, err := Load(ctx, projectDir)
|
2023-06-21 07:53:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
// Artificially set target.
|
|
|
|
// This is otherwise done by [mutators.SelectTarget].
|
|
|
|
bundle.Config.Bundle.Target = "default"
|
2023-06-21 07:53:54 +00:00
|
|
|
|
|
|
|
// now we expect to use 'bundleTmpDir' instead of CWD/.databricks/bundle
|
|
|
|
t.Setenv("DATABRICKS_BUNDLE_TMP", bundleTmpDir)
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
cacheDir, err := bundle.CacheDir(ctx)
|
2023-06-21 07:53:54 +00:00
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
// format is <DATABRICKS_BUNDLE_TMP>/<target>
|
2022-11-30 13:40:41 +00:00
|
|
|
assert.NoError(t, err)
|
2023-06-21 07:53:54 +00:00
|
|
|
assert.Equal(t, filepath.Join(bundleTmpDir, "default"), cacheDir)
|
2022-11-30 13:40:41 +00:00
|
|
|
}
|
2023-01-27 15:57:39 +00:00
|
|
|
|
|
|
|
func TestBundleMustLoadSuccess(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, "./tests/basic")
|
2023-08-11 12:28:05 +00:00
|
|
|
b, err := MustLoad(context.Background())
|
2023-01-27 15:57:39 +00:00
|
|
|
require.NoError(t, err)
|
2023-04-12 14:17:13 +00:00
|
|
|
assert.Equal(t, "tests/basic", filepath.ToSlash(b.Config.Path))
|
2023-01-27 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleMustLoadFailureWithEnv(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, "./tests/doesntexist")
|
2023-08-11 12:28:05 +00:00
|
|
|
_, err := MustLoad(context.Background())
|
2023-01-27 15:57:39 +00:00
|
|
|
require.Error(t, err, "not a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleMustLoadFailureIfNotFound(t *testing.T) {
|
|
|
|
chdir(t, t.TempDir())
|
2023-08-11 12:28:05 +00:00
|
|
|
_, err := MustLoad(context.Background())
|
2023-01-27 15:57:39 +00:00
|
|
|
require.Error(t, err, "unable to find bundle root")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleTryLoadSuccess(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, "./tests/basic")
|
2023-08-11 12:28:05 +00:00
|
|
|
b, err := TryLoad(context.Background())
|
2023-01-27 15:57:39 +00:00
|
|
|
require.NoError(t, err)
|
2023-04-12 14:17:13 +00:00
|
|
|
assert.Equal(t, "tests/basic", filepath.ToSlash(b.Config.Path))
|
2023-01-27 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleTryLoadFailureWithEnv(t *testing.T) {
|
2023-09-11 08:18:43 +00:00
|
|
|
t.Setenv(env.RootVariable, "./tests/doesntexist")
|
2023-08-11 12:28:05 +00:00
|
|
|
_, err := TryLoad(context.Background())
|
2023-01-27 15:57:39 +00:00
|
|
|
require.Error(t, err, "not a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBundleTryLoadOkIfNotFound(t *testing.T) {
|
|
|
|
chdir(t, t.TempDir())
|
2023-08-11 12:28:05 +00:00
|
|
|
b, err := TryLoad(context.Background())
|
2023-01-27 15:57:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, b)
|
|
|
|
}
|