github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/block/azure/adapter_test.go (about)

     1  package azure_test
     2  
     3  import (
     4  	"context"
     5  	"net/url"
     6  	"regexp"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  	"github.com/treeverse/lakefs/pkg/block/azure"
    11  	"github.com/treeverse/lakefs/pkg/block/blocktest"
    12  	"github.com/treeverse/lakefs/pkg/block/params"
    13  )
    14  
    15  func TestAzureAdapter(t *testing.T) {
    16  	basePath, err := url.JoinPath(blockURL, containerName)
    17  	require.NoError(t, err)
    18  	localPath, err := url.JoinPath(basePath, "lakefs")
    19  	require.NoError(t, err)
    20  	externalPath, err := url.JoinPath(basePath, "external")
    21  	require.NoError(t, err)
    22  
    23  	adapter, err := azure.NewAdapter(context.Background(), params.Azure{
    24  		StorageAccount:   accountName,
    25  		StorageAccessKey: accountKey,
    26  		TestEndpointURL:  blockURL,
    27  		Domain:           domain,
    28  	})
    29  	require.NoError(t, err, "create new adapter")
    30  	blocktest.AdapterTest(t, adapter, localPath, externalPath)
    31  }
    32  
    33  func TestAdapterNamespace(t *testing.T) {
    34  	tests := []struct {
    35  		Name          string
    36  		Namespace     string
    37  		Success       bool
    38  		Domain        string
    39  		InvalidDomain bool
    40  	}{
    41  		{
    42  			Name:      "valid_https",
    43  			Namespace: "https://test.blob.core.windows.net/container1/repo1",
    44  			Success:   true,
    45  		},
    46  		{
    47  			Name: "valid_https_presign",
    48  			Namespace: "https://esti.blob.core.windows.net/esti-system-testing/11484/29538/coddo7tpocvujsvjhhjg/repo-coddo7tpocvujsvjhhj0/data/ggq2f8tpocvs73ftd1qg/" +
    49  				"coddo7tpocvs73ftd7u0?se=2024-04-13T19%3A40%3A19Z&sig=r%2BV3wR16PUG74U7wxZ1KknhRT%2BO2JqVJM0EVTuJbz8U%3D&ske=2024-04-13T20%3A40%3A19Z&" +
    50  				"skoid=d8193925-4108-40a0-8d76-0c6d3ceb1f61&sks=b&skt=2024-04-13T19%3A25%3A09Z&sktid=***&skv=2023-08-03&sp=raw&spr=https&sr=b&sv=2021-12-02",
    51  			Success: true,
    52  		},
    53  		{
    54  			Name:      "valid_https",
    55  			Namespace: "https://test.blob.core.windows.net/container1/repo1",
    56  			Success:   false,
    57  			Domain:    "blob.core.chinacloudapi.cn",
    58  		},
    59  		{
    60  			Name:      "valid_https_china",
    61  			Namespace: "https://test.blob.core.chinacloudapi.cn/container1/repo1",
    62  			Success:   false,
    63  		},
    64  		{
    65  			Name:      "valid_https_china",
    66  			Namespace: "https://test.blob.core.chinacloudapi.cn/container1/repo1",
    67  			Success:   true,
    68  			Domain:    "blob.core.chinacloudapi.cn",
    69  		},
    70  		{
    71  			Name:      "valid_http",
    72  			Namespace: "http://test.blob.core.windows.net/container1/repo1",
    73  			Success:   true,
    74  		},
    75  		{
    76  			Name:      "invalid_subdomain",
    77  			Namespace: "https://test.adls.core.windows.net/container1/repo1",
    78  			Success:   false,
    79  		},
    80  		{
    81  			Name:      "partial",
    82  			Namespace: "https://test.adls.core.windows.n",
    83  			Success:   false,
    84  		},
    85  		{
    86  			Name:      "s3",
    87  			Namespace: "s3://test/adls/core/windows/net",
    88  			Success:   false,
    89  		},
    90  		{
    91  			Name:      "invalid_string",
    92  			Namespace: "this is a bad string",
    93  			Success:   false,
    94  		},
    95  		{
    96  			Name:      "invalid_https_china_mix_1",
    97  			Namespace: "https://test.blob.core.chinacloudapi.net/container1/repo1",
    98  			Success:   false,
    99  		},
   100  		{
   101  			Name:      "invalid_https_china_mix_2",
   102  			Namespace: "https://test.blob.core.windows.cn/container1/repo1",
   103  			Success:   false,
   104  		},
   105  		{
   106  			Name:      "valid_gov_cloud",
   107  			Namespace: "https://test.blob.core.usgovcloudapi.net/container1/repo1",
   108  			Success:   true,
   109  			Domain:    "blob.core.usgovcloudapi.net",
   110  		},
   111  		{
   112  			Name:      "valid_gov_cloud_no_domain",
   113  			Namespace: "https://test.blob.core.usgovcloudapi.net/container1/repo1",
   114  			Success:   false,
   115  		},
   116  		{
   117  			Name:          "invalid_domain",
   118  			Namespace:     "https://test.blob.core.usgovcloudapi.net/container1/repo1",
   119  			Success:       false,
   120  			Domain:        "invalid_domain",
   121  			InvalidDomain: true,
   122  		},
   123  	}
   124  	for _, tt := range tests {
   125  		t.Run(tt.Name, func(t *testing.T) {
   126  			adapter, err := azure.NewAdapter(context.Background(), params.Azure{
   127  				StorageAccount:   accountName,
   128  				StorageAccessKey: accountKey,
   129  				TestEndpointURL:  blockURL,
   130  				Domain:           tt.Domain,
   131  			})
   132  			if tt.InvalidDomain {
   133  				require.ErrorIs(t, err, azure.ErrInvalidDomain)
   134  				return
   135  			}
   136  			require.NoError(t, err, "create new adapter")
   137  
   138  			namespaceInfo := adapter.GetStorageNamespaceInfo()
   139  			expr, err := regexp.Compile(namespaceInfo.ValidityRegex)
   140  			require.NoError(t, err)
   141  
   142  			require.Equal(t, tt.Success, expr.MatchString(tt.Namespace))
   143  		})
   144  	}
   145  }
   146  
   147  func TestAzureParseURL(t *testing.T) {
   148  	tests := []struct {
   149  		Name          string
   150  		Url           string
   151  		Account       string
   152  		Domain        string
   153  		InvalidDomain bool
   154  	}{
   155  		{
   156  			Name:    "valid_https",
   157  			Url:     "https://account1.blob.core.windows.net/container1/repo1",
   158  			Account: "account1",
   159  			Domain:  azure.BlobEndpointDefaultDomain,
   160  		},
   161  		{
   162  			Name: "valid_https_presign",
   163  			Url: "https://test.blob.core.windows.net/esti-system-testing/11484/29538/coddo7tpocvujsvjhhjg/repo-coddo7tpocvujsvjhhj0/data/ggq2f8tpocvs73ftd1qg/coddo7tpocvs73ftd7u0?" +
   164  				"se=2024-04-13T19%3A40%3A19Z&sig=r%2BV3wR16PUG74U7wxZ1KknhRT%2BO2JqVJM0EVTuJbz8U%3D&ske=2024-04-13T20%3A40%3A19Z&skoid=d8193925-4108-40a0-8d76-0c6d3ceb1f61&" +
   165  				"sks=b&skt=2024-04-13T19%3A25%3A09Z&sktid=***&skv=2023-08-03&sp=raw&spr=https&sr=b&sv=2021-12-02",
   166  			Account: "test",
   167  			Domain:  azure.BlobEndpointDefaultDomain,
   168  		},
   169  		{
   170  			Name:    "valid_china_https",
   171  			Url:     "https://china.blob.core.chinacloudapi.cn/container1/repo1",
   172  			Account: "china",
   173  			Domain:  azure.BlobEndpointChinaDomain,
   174  		},
   175  		{
   176  			Name:    "valid_usgov_https",
   177  			Url:     "https://mygov.blob.core.usgovcloudapi.net/container1/repo1",
   178  			Account: "mygov",
   179  			Domain:  azure.BlobEndpointUSGovDomain,
   180  		},
   181  		{
   182  			Name:    "valid_other_https",
   183  			Url:     "https://koala.blob.core.australiacloudapi.net/container1/repo1",
   184  			Account: "koala",
   185  			Domain:  "blob.core.australiacloudapi.net",
   186  		},
   187  		{
   188  			Name:          "invalid_http",
   189  			Url:           "http://mygov.blob.core.usgovcloudapi.net/container1/repo1",
   190  			InvalidDomain: true,
   191  		},
   192  		{
   193  			Name:          "invalid_url_s3",
   194  			Url:           "s3://example/bar/baz",
   195  			InvalidDomain: true,
   196  		},
   197  	}
   198  	for _, tt := range tests {
   199  		t.Run(tt.Name, func(t *testing.T) {
   200  			uri, err := url.Parse(tt.Url)
   201  			account, dom, err := azure.ParseURL(uri)
   202  			if tt.InvalidDomain {
   203  				require.ErrorIs(t, err, azure.ErrAzureInvalidURL)
   204  			} else {
   205  				require.NoError(t, err)
   206  				require.Equal(t, tt.Account, account)
   207  				require.Equal(t, tt.Domain, dom)
   208  			}
   209  		})
   210  	}
   211  }