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-12 19:57:56 +00:00
|
|
|
func TestFSInit(t *testing.T) {
|
2024-07-10 11:06:27 +00:00
|
|
|
f := func(expErr string, params ...string) {
|
|
|
|
t.Helper()
|
2024-07-10 09:52:05 +00:00
|
|
|
|
2024-07-10 11:06:27 +00:00
|
|
|
env := make(testEnv)
|
|
|
|
for i := 0; i < len(params); i += 2 {
|
|
|
|
env[params[i]] = params[i+1]
|
|
|
|
}
|
2024-07-10 09:52:05 +00:00
|
|
|
|
2024-07-10 11:06:27 +00:00
|
|
|
fs := &FS{Dir: "foo"}
|
|
|
|
fs.env = env.LookupEnv
|
|
|
|
err := fs.Init()
|
|
|
|
if err != nil {
|
|
|
|
if expErr == "" {
|
|
|
|
t.Fatalf("unexpected error %v", err)
|
2024-07-10 09:52:05 +00:00
|
|
|
}
|
2024-07-10 11:06:27 +00:00
|
|
|
if !strings.Contains(err.Error(), expErr) {
|
|
|
|
t.Fatalf("expected error: \n%q, \ngot: \n%v", expErr, err)
|
2024-07-10 09:52:05 +00:00
|
|
|
}
|
2024-07-10 11:06:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if expErr != "" {
|
|
|
|
t.Fatalf("expected to have an error %q, instead got nil", expErr)
|
|
|
|
}
|
2024-07-10 09:52:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-10 11:06:27 +00:00
|
|
|
f("", envStorageAccCs, "BlobEndpoint=https://test.blob.core.windows.net/;SharedAccessSignature=")
|
|
|
|
f("", envStorageAcctName, "test", envStorageAccKey, "dGVhcG90Cg==")
|
|
|
|
f("", envStorageDefault, "true", envStorageAcctName, "test")
|
|
|
|
f("", envStorageAcctName, "test", envStorageAccKey, "dGVhcG90Cg==", envStorageDomain, "foo.bar")
|
2024-07-10 09:52:05 +00:00
|
|
|
|
2024-07-10 11:06:27 +00:00
|
|
|
f("failed to detect credentials for AZBlob")
|
|
|
|
f("failed to detect credentials for AZBlob", envStorageAcctName, "test")
|
|
|
|
f("failed to create Shared Key", envStorageAcctName, "", envStorageAccKey, "!")
|
|
|
|
f("connection string is either blank or malformed", envStorageAccCs, "")
|
|
|
|
f("failed to process credentials: only one of", envStorageAccCs, "teapot", envStorageAcctName, "test", envStorageAccKey, "dGVhcG90Cg==")
|
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
|
|
|
|
}
|