2023-06-12 12:05:56 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path"
|
2023-07-26 13:03:10 +00:00
|
|
|
"regexp"
|
2023-06-12 12:05:56 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/filer"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
func TestAccFsMkdir(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
for _, testCase := range fsTests {
|
|
|
|
tc := testCase
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
f, tmpDir := tc.setupFiler(t)
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// create directory "a"
|
|
|
|
stdout, stderr := RequireSuccessfulRun(t, "fs", "mkdir", path.Join(tmpDir, "a"))
|
|
|
|
assert.Equal(t, "", stderr.String())
|
|
|
|
assert.Equal(t, "", stdout.String())
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// assert directory "a" is created
|
|
|
|
info, err := f.Stat(context.Background(), "a")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "a", info.Name())
|
|
|
|
assert.Equal(t, true, info.IsDir())
|
|
|
|
})
|
|
|
|
}
|
2023-06-12 12:05:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
func TestAccFsMkdirCreatesIntermediateDirectories(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
for _, testCase := range fsTests {
|
|
|
|
tc := testCase
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f, tmpDir := tc.setupFiler(t)
|
|
|
|
|
|
|
|
// create directory "a/b/c"
|
|
|
|
stdout, stderr := RequireSuccessfulRun(t, "fs", "mkdir", path.Join(tmpDir, "a", "b", "c"))
|
|
|
|
assert.Equal(t, "", stderr.String())
|
|
|
|
assert.Equal(t, "", stdout.String())
|
|
|
|
|
|
|
|
// assert directory "a" is created
|
|
|
|
infoA, err := f.Stat(context.Background(), "a")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "a", infoA.Name())
|
|
|
|
assert.Equal(t, true, infoA.IsDir())
|
|
|
|
|
|
|
|
// assert directory "b" is created
|
|
|
|
infoB, err := f.Stat(context.Background(), "a/b")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "b", infoB.Name())
|
|
|
|
assert.Equal(t, true, infoB.IsDir())
|
|
|
|
|
|
|
|
// assert directory "c" is created
|
|
|
|
infoC, err := f.Stat(context.Background(), "a/b/c")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "c", infoC.Name())
|
|
|
|
assert.Equal(t, true, infoC.IsDir())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
func TestAccFsMkdirWhenDirectoryAlreadyExists(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
for _, testCase := range fsTests {
|
|
|
|
tc := testCase
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
f, tmpDir := tc.setupFiler(t)
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// create directory "a"
|
|
|
|
err := f.Mkdir(context.Background(), "a")
|
|
|
|
require.NoError(t, err)
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// assert run is successful without any errors
|
|
|
|
stdout, stderr := RequireSuccessfulRun(t, "fs", "mkdir", path.Join(tmpDir, "a"))
|
|
|
|
assert.Equal(t, "", stderr.String())
|
|
|
|
assert.Equal(t, "", stdout.String())
|
|
|
|
})
|
|
|
|
}
|
2023-06-12 12:05:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
func TestAccFsMkdirWhenFileExistsAtPath(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
t.Run("dbfs", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
f, tmpDir := setupDbfsFiler(t)
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// create file "hello"
|
|
|
|
err := f.Write(context.Background(), "hello", strings.NewReader("abc"))
|
|
|
|
require.NoError(t, err)
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// assert mkdir fails
|
|
|
|
_, _, err = RequireErrorRun(t, "fs", "mkdir", path.Join(tmpDir, "hello"))
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-10-16 12:50:17 +00:00
|
|
|
// Different cloud providers or cloud configurations return different errors.
|
|
|
|
regex := regexp.MustCompile(`(^|: )Path is a file: .*$|(^|: )Cannot create directory .* because .* is an existing file\.$|(^|: )mkdirs\(hadoopPath: .*, permission: rwxrwxrwx\): failed$|(^|: )"The specified path already exists.".*$`)
|
2024-02-20 16:14:37 +00:00
|
|
|
assert.Regexp(t, regex, err.Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("uc-volumes", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
f, tmpDir := setupUcVolumesFiler(t)
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// create file "hello"
|
|
|
|
err := f.Write(context.Background(), "hello", strings.NewReader("abc"))
|
|
|
|
require.NoError(t, err)
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// assert mkdir fails
|
|
|
|
_, _, err = RequireErrorRun(t, "fs", "mkdir", path.Join(tmpDir, "hello"))
|
2023-06-12 12:05:56 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
assert.ErrorAs(t, err, &filer.FileAlreadyExistsError{})
|
|
|
|
})
|
2023-06-12 12:05:56 +00:00
|
|
|
}
|