// Copyright 2023 The Gitea Authors and MarketAlly. All rights reserved. // SPDX-License-Identifier: MIT package storage import ( "io" "os" "strings" "testing" "code.gitcaddy.com/server/v3/modules/setting" "github.com/stretchr/testify/assert" ) // azureBlobTestConfig returns the Azure Blob storage config for tests. // Returns nil if Azurite is not available (skip the test). func azureBlobTestConfig() *setting.Storage { return &setting.Storage{ AzureBlobConfig: setting.AzureBlobStorageConfig{ // https://learn.microsoft.com/azure/storage/common/storage-use-azurite?tabs=visual-studio-code#ip-style-url Endpoint: "http://devstoreaccount1.azurite.local:10000", // https://learn.microsoft.com/azure/storage/common/storage-use-azurite?tabs=visual-studio-code#well-known-storage-account-and-key AccountName: "devstoreaccount1", AccountKey: "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==", Container: "test", }, } } // skipIfNoAzurite skips the test if Azurite service is not available. // In CI, we always skip since Azurite is not available in our runner environment. func skipIfNoAzurite(t *testing.T) { t.Helper() if os.Getenv("CI") != "" { t.Skip("azureBlobStorage requires Azurite service which is not available in CI") } } func TestAzureBlobStorageIterator(t *testing.T) { skipIfNoAzurite(t) cfg := azureBlobTestConfig() // Try to create storage to verify Azurite is available s, err := NewStorage(setting.AzureBlobStorageType, cfg) if err != nil { t.Skipf("azureBlobStorage not available: %v", err) } // Clean up the test storage _ = s.Delete("a/1.txt") _ = s.Delete("ab/1.txt") _ = s.Delete("b/1.txt") _ = s.Delete("b/2.txt") _ = s.Delete("b/3.txt") _ = s.Delete("b/x 4.txt") testStorageIterator(t, setting.AzureBlobStorageType, cfg) } func TestAzureBlobStoragePath(t *testing.T) { m := &AzureBlobStorage{cfg: &setting.AzureBlobStorageConfig{BasePath: ""}} assert.Empty(t, m.buildAzureBlobPath("/")) assert.Empty(t, m.buildAzureBlobPath(".")) assert.Equal(t, "a", m.buildAzureBlobPath("/a")) assert.Equal(t, "a/b", m.buildAzureBlobPath("/a/b/")) m = &AzureBlobStorage{cfg: &setting.AzureBlobStorageConfig{BasePath: "/"}} assert.Empty(t, m.buildAzureBlobPath("/")) assert.Empty(t, m.buildAzureBlobPath(".")) assert.Equal(t, "a", m.buildAzureBlobPath("/a")) assert.Equal(t, "a/b", m.buildAzureBlobPath("/a/b/")) m = &AzureBlobStorage{cfg: &setting.AzureBlobStorageConfig{BasePath: "/base"}} assert.Equal(t, "base", m.buildAzureBlobPath("/")) assert.Equal(t, "base", m.buildAzureBlobPath(".")) assert.Equal(t, "base/a", m.buildAzureBlobPath("/a")) assert.Equal(t, "base/a/b", m.buildAzureBlobPath("/a/b/")) m = &AzureBlobStorage{cfg: &setting.AzureBlobStorageConfig{BasePath: "/base/"}} assert.Equal(t, "base", m.buildAzureBlobPath("/")) assert.Equal(t, "base", m.buildAzureBlobPath(".")) assert.Equal(t, "base/a", m.buildAzureBlobPath("/a")) assert.Equal(t, "base/a/b", m.buildAzureBlobPath("/a/b/")) } func Test_azureBlobObject(t *testing.T) { skipIfNoAzurite(t) s, err := NewStorage(setting.AzureBlobStorageType, azureBlobTestConfig()) if err != nil { t.Skipf("azureBlobStorage not available: %v", err) } data := "Q2xTckt6Y1hDOWh0" _, err = s.Save("test.txt", strings.NewReader(data), int64(len(data))) assert.NoError(t, err) obj, err := s.Open("test.txt") assert.NoError(t, err) offset, err := obj.Seek(2, io.SeekStart) assert.NoError(t, err) assert.EqualValues(t, 2, offset) buf1 := make([]byte, 3) read, err := obj.Read(buf1) assert.NoError(t, err) assert.Equal(t, 3, read) assert.Equal(t, data[2:5], string(buf1)) offset, err = obj.Seek(-5, io.SeekEnd) assert.NoError(t, err) assert.EqualValues(t, len(data)-5, offset) buf2 := make([]byte, 4) read, err = obj.Read(buf2) assert.NoError(t, err) assert.Equal(t, 4, read) assert.Equal(t, data[11:15], string(buf2)) assert.NoError(t, obj.Close()) assert.NoError(t, s.Delete("test.txt")) }