2024-07-10 09:52:05 +00:00
|
|
|
package azremote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-07-12 19:57:56 +00:00
|
|
|
func TestCleanDirectory(t *testing.T) {
|
2024-07-10 11:06:27 +00:00
|
|
|
f := func(dir, exp string) {
|
|
|
|
t.Helper()
|
2024-07-12 19:57:56 +00:00
|
|
|
|
2024-07-10 11:06:27 +00:00
|
|
|
got := cleanDirectory(dir)
|
|
|
|
if got != exp {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expected dir %q, got %q", exp, got)
|
2024-07-10 11:06:27 +00:00
|
|
|
}
|
2024-07-10 09:52:05 +00:00
|
|
|
}
|
2024-07-12 19:57:56 +00:00
|
|
|
|
2024-07-10 11:06:27 +00:00
|
|
|
f("/foo/", "foo/")
|
|
|
|
f("//foo/", "foo/")
|
|
|
|
f("foo", "foo/")
|
2024-07-10 09:52:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 15:42:20 +00:00
|
|
|
func TestFSInit_Failure(t *testing.T) {
|
|
|
|
f := func(envArgs map[string]string, errStrExpected string) {
|
2024-07-10 11:06:27 +00:00
|
|
|
t.Helper()
|
2024-07-10 09:52:05 +00:00
|
|
|
|
2024-07-17 15:42:20 +00:00
|
|
|
fs := &FS{
|
|
|
|
Dir: "foo",
|
2024-07-10 11:06:27 +00:00
|
|
|
}
|
2024-07-17 15:42:20 +00:00
|
|
|
env := testEnv(envArgs)
|
|
|
|
fs.envLookupFunc = env.LookupEnv
|
2024-07-10 09:52:05 +00:00
|
|
|
|
2024-07-10 11:06:27 +00:00
|
|
|
err := fs.Init()
|
2024-07-17 15:42:20 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
2024-07-10 11:06:27 +00:00
|
|
|
}
|
2024-07-17 15:42:20 +00:00
|
|
|
errStr := err.Error()
|
|
|
|
if !strings.Contains(errStr, errStrExpected) {
|
|
|
|
t.Fatalf("expecting %q in the error %q", errStrExpected, errStr)
|
2024-07-10 11:06:27 +00:00
|
|
|
}
|
2024-07-10 09:52:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 15:42:20 +00:00
|
|
|
var envArgs map[string]string
|
|
|
|
|
|
|
|
f(envArgs, "failed to detect credentials for AZBlob")
|
|
|
|
|
|
|
|
envArgs = map[string]string{
|
|
|
|
"AZURE_STORAGE_ACCOUNT_NAME": "test",
|
|
|
|
}
|
|
|
|
f(envArgs, "failed to detect credentials for AZBlob")
|
|
|
|
|
|
|
|
envArgs = map[string]string{
|
|
|
|
"AZURE_STORAGE_ACCOUNT_NAME": "",
|
|
|
|
"AZURE_STORAGE_ACCOUNT_KEY": "!",
|
|
|
|
}
|
|
|
|
f(envArgs, "missing AZURE_STORAGE_ACCOUNT_NAME")
|
|
|
|
|
|
|
|
envArgs = map[string]string{
|
|
|
|
"AZURE_STORAGE_ACCOUNT_NAME": "foo",
|
|
|
|
"AZURE_STORAGE_ACCOUNT_KEY": "!",
|
|
|
|
}
|
|
|
|
f(envArgs, "failed to create Shared Key credentials")
|
|
|
|
|
|
|
|
envArgs = map[string]string{
|
|
|
|
"AZURE_STORAGE_ACCOUNT_CONNECTION_STRING": "foobar",
|
|
|
|
}
|
|
|
|
f(envArgs, "connection string is either blank or malformed")
|
|
|
|
|
|
|
|
envArgs = map[string]string{
|
|
|
|
"AZURE_STORAGE_ACCOUNT_CONNECTION_STRING": "teapot",
|
|
|
|
"AZURE_STORAGE_ACCOUNT_NAME": "test",
|
|
|
|
"AZURE_STORAGE_ACCOUNT_KEY": "dGVhcG90Cg==",
|
|
|
|
}
|
|
|
|
f(envArgs, "connection string is either blank or malformed")
|
|
|
|
|
|
|
|
envArgs = map[string]string{
|
|
|
|
"AZURE_USE_DEFAULT_CREDENTIAL": "true",
|
|
|
|
}
|
|
|
|
f(envArgs, "missing AZURE_STORAGE_ACCOUNT_NAME")
|
|
|
|
}
|
2024-07-10 09:52:05 +00:00
|
|
|
|
2024-07-17 15:42:20 +00:00
|
|
|
func TestFSInit_Success(t *testing.T) {
|
|
|
|
f := func(envArgs map[string]string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
fs := &FS{
|
|
|
|
Dir: "foo",
|
|
|
|
}
|
|
|
|
env := testEnv(envArgs)
|
|
|
|
fs.envLookupFunc = env.LookupEnv
|
|
|
|
|
|
|
|
err := fs.Init()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error at fs.Init(): %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
envArgs := map[string]string{
|
|
|
|
"AZURE_STORAGE_ACCOUNT_CONNECTION_STRING": "BlobEndpoint=https://test.blob.core.windows.net/;SharedAccessSignature=",
|
|
|
|
}
|
|
|
|
f(envArgs)
|
|
|
|
|
|
|
|
envArgs = map[string]string{
|
|
|
|
"AZURE_STORAGE_ACCOUNT_NAME": "test",
|
|
|
|
"AZURE_STORAGE_ACCOUNT_KEY": "dGVhcG90Cg==",
|
|
|
|
}
|
|
|
|
f(envArgs)
|
|
|
|
|
|
|
|
envArgs = map[string]string{
|
|
|
|
"AZURE_USE_DEFAULT_CREDENTIAL": "true",
|
|
|
|
"AZURE_STORAGE_ACCOUNT_NAME": "test",
|
|
|
|
}
|
|
|
|
f(envArgs)
|
|
|
|
|
|
|
|
envArgs = map[string]string{
|
|
|
|
"AZURE_STORAGE_ACCOUNT_NAME": "test",
|
|
|
|
"AZURE_STORAGE_ACCOUNT_KEY": "dGVhcG90Cg==",
|
|
|
|
"AZURE_STORAGE_DOMAIN": "foo.bar",
|
|
|
|
}
|
|
|
|
f(envArgs)
|
2024-07-10 09:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testEnv map[string]string
|
|
|
|
|
|
|
|
func (e testEnv) LookupEnv(key string) (string, bool) {
|
|
|
|
val, ok := e[key]
|
|
|
|
return val, ok
|
|
|
|
}
|