2014-11-25 18:40:24 +00:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/testsuites"
|
2023-04-29 15:08:47 +00:00
|
|
|
"gopkg.in/check.v1"
|
2014-11-25 18:40:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-02-21 03:58:17 +00:00
|
|
|
envAccountName = "AZURE_STORAGE_ACCOUNT_NAME"
|
|
|
|
envAccountKey = "AZURE_STORAGE_ACCOUNT_KEY"
|
|
|
|
envContainer = "AZURE_STORAGE_CONTAINER"
|
|
|
|
envRealm = "AZURE_STORAGE_REALM"
|
|
|
|
envRootDirectory = "AZURE_ROOT_DIRECTORY"
|
2014-11-25 18:40:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
2023-04-29 15:08:47 +00:00
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
2014-11-25 18:40:24 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
var (
|
2020-02-21 03:58:17 +00:00
|
|
|
accountName string
|
|
|
|
accountKey string
|
|
|
|
container string
|
|
|
|
realm string
|
|
|
|
rootDirectory string
|
2014-11-25 18:40:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
config := []struct {
|
2020-02-21 03:58:17 +00:00
|
|
|
env string
|
|
|
|
value *string
|
|
|
|
missingOk bool
|
2014-11-25 18:40:24 +00:00
|
|
|
}{
|
2020-02-21 03:58:17 +00:00
|
|
|
{envAccountName, &accountName, false},
|
|
|
|
{envAccountKey, &accountKey, false},
|
|
|
|
{envContainer, &container, false},
|
|
|
|
{envRealm, &realm, false},
|
|
|
|
{envRootDirectory, &rootDirectory, true},
|
2014-11-25 18:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
missing := []string{}
|
|
|
|
for _, v := range config {
|
|
|
|
*v.value = os.Getenv(v.env)
|
2020-02-21 03:58:17 +00:00
|
|
|
if *v.value == "" && !v.missingOk {
|
2014-11-25 18:40:24 +00:00
|
|
|
missing = append(missing, v.env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
azureDriverConstructor := func() (storagedriver.StorageDriver, error) {
|
2020-02-21 03:58:17 +00:00
|
|
|
params := Parameters{
|
|
|
|
Container: container,
|
|
|
|
AccountName: accountName,
|
|
|
|
AccountKey: accountKey,
|
|
|
|
Realm: realm,
|
|
|
|
RootDirectory: rootDirectory,
|
|
|
|
}
|
|
|
|
return New(¶ms)
|
2014-11-25 18:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip Azure storage driver tests if environment variable parameters are not provided
|
|
|
|
skipCheck := func() string {
|
|
|
|
if len(missing) > 0 {
|
|
|
|
return fmt.Sprintf("Must set %s environment variables to run Azure tests", strings.Join(missing, ", "))
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-06-29 23:39:45 +00:00
|
|
|
testsuites.RegisterSuite(azureDriverConstructor, skipCheck)
|
2014-11-25 18:40:24 +00:00
|
|
|
}
|
2020-02-21 03:58:17 +00:00
|
|
|
|
|
|
|
func TestParamParsing(t *testing.T) {
|
|
|
|
expectErrors := []map[string]interface{}{
|
|
|
|
{},
|
|
|
|
{"accountname": "acc1"},
|
|
|
|
}
|
|
|
|
for _, parameters := range expectErrors {
|
|
|
|
if _, err := NewParameters(parameters); err == nil {
|
|
|
|
t.Fatalf("Expected an error for parameter set: %v", parameters)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
input := []map[string]interface{}{
|
|
|
|
{"accountname": "acc1", "accountkey": "k1", "container": "c1"},
|
|
|
|
{"accountname": "acc1", "container": "c1", "credentials": map[string]interface{}{"type": "default"}},
|
|
|
|
{"accountname": "acc1", "container": "c1", "credentials": map[string]interface{}{"type": "client_secret", "clientid": "c1", "tenantid": "t1", "secret": "s1"}},
|
|
|
|
}
|
|
|
|
expecteds := []Parameters{
|
|
|
|
{
|
|
|
|
Container: "c1", AccountName: "acc1", AccountKey: "k1",
|
|
|
|
Realm: "core.windows.net", ServiceURL: "https://acc1.blob.core.windows.net",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Container: "c1", AccountName: "acc1", Credentials: Credentials{Type: "default"},
|
|
|
|
Realm: "core.windows.net", ServiceURL: "https://acc1.blob.core.windows.net",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Container: "c1", AccountName: "acc1",
|
|
|
|
Credentials: Credentials{Type: "client_secret", ClientID: "c1", TenantID: "t1", Secret: "s1"},
|
|
|
|
Realm: "core.windows.net", ServiceURL: "https://acc1.blob.core.windows.net",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, expected := range expecteds {
|
|
|
|
actual, err := NewParameters(input[i])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse: %v", input[i])
|
|
|
|
}
|
|
|
|
if *actual != expected {
|
|
|
|
t.Fatalf("Expected: %v != %v", *actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|