VictoriaMetrics/lib/backup/azremote/azblob_test.go
hagen1778 7370f84b97
lib/bakcup/azremote: follow-up after 5fd3aef549
Simplify tests by converting them to f-tests.

5fd3aef549
Signed-off-by: hagen1778 <roman@victoriametrics.com>
(cherry picked from commit 03e4c5c19c)
2024-07-10 15:17:06 +02:00

65 lines
1.7 KiB
Go

package azremote
import (
"strings"
"testing"
)
func Test_cleanDirectory(t *testing.T) {
f := func(dir, exp string) {
t.Helper()
got := cleanDirectory(dir)
if got != exp {
t.Errorf("expected dir %q, got %q", exp, got)
}
}
f("/foo/", "foo/")
f("//foo/", "foo/")
f("foo", "foo/")
}
func Test_FSInit(t *testing.T) {
f := func(expErr string, params ...string) {
t.Helper()
env := make(testEnv)
for i := 0; i < len(params); i += 2 {
env[params[i]] = params[i+1]
}
fs := &FS{Dir: "foo"}
fs.env = env.LookupEnv
err := fs.Init()
if err != nil {
if expErr == "" {
t.Fatalf("unexpected error %v", err)
}
if !strings.Contains(err.Error(), expErr) {
t.Fatalf("expected error: \n%q, \ngot: \n%v", expErr, err)
}
return
}
if expErr != "" {
t.Fatalf("expected to have an error %q, instead got nil", expErr)
}
}
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")
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==")
}
type testEnv map[string]string
func (e testEnv) LookupEnv(key string) (string, bool) {
val, ok := e[key]
return val, ok
}