VictoriaMetrics/lib/backup/azremote/azblob_test.go
Aliaksandr Valialkin 9b529c2742
lib/backup/azremote: follow-up for 5fd3aef549
- Mention that credentials can be configured via env variables at both vmbackup and vmrestore docs.

- Make clear that the AZURE_STORAGE_DOMAIN env var is optional at https://docs.victoriametrics.com/vmbackup/#providing-credentials-via-env-variables

- Use string literals as is for env variable names instead of indirecting them via string constants.
  This makes easier to read and understand the code. These environment variable names aren't going to change
  in the future, so there is no sense in hiding them under string constants with some other names.

- Refer to https://docs.victoriametrics.com/vmbackup/#providing-credentials-via-env-variables in error messages
  when auth creds are improperly configured. This should simplify figuring out how to fix the error.

- Simplify the code a bit at FS.newClient(), so it is easier to follow it now.
  While at it, remove the check when superflouos environment variables are set, since it is too fragile
  and it looks like it doesn't help properly configuring vmbackup / vmrestore.

- Remove envLookuper indirection - just use 'func(name string) (string, bool)' type inline.
  This simplifies code reading and understanding.

- Split TestFSInit() into TestFSInit_Failure() and TestFSInit_Success(). This simplifies the test code,
  so it should be easier to maintain in the future.

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6518
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5984
2024-07-17 17:55:39 +02:00

128 lines
2.8 KiB
Go

package azremote
import (
"strings"
"testing"
)
func TestCleanDirectory(t *testing.T) {
f := func(dir, exp string) {
t.Helper()
got := cleanDirectory(dir)
if got != exp {
t.Fatalf("expected dir %q, got %q", exp, got)
}
}
f("/foo/", "foo/")
f("//foo/", "foo/")
f("foo", "foo/")
}
func TestFSInit_Failure(t *testing.T) {
f := func(envArgs map[string]string, errStrExpected string) {
t.Helper()
fs := &FS{
Dir: "foo",
}
env := testEnv(envArgs)
fs.envLookupFunc = env.LookupEnv
err := fs.Init()
if err == nil {
t.Fatalf("expecting non-nil error")
}
errStr := err.Error()
if !strings.Contains(errStr, errStrExpected) {
t.Fatalf("expecting %q in the error %q", errStrExpected, errStr)
}
}
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")
}
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)
}
type testEnv map[string]string
func (e testEnv) LookupEnv(key string) (string, bool) {
val, ok := e[key]
return val, ok
}