feat(storage): improve storage connectivity error handling and multi-instance support
Enhance error messaging for storage connectivity issues by sanitizing raw errors to prevent leaking sensitive information. Introduce a new utility function for consistent error handling across storage backend operations. Additionally, implement logic to support multi-instance storage backends, allowing for more flexible workspace configurations and improved reporting of active storage providers.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -13,6 +14,25 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// fakeStorageBackendRepo is a minimal StorageBackendRepository used to exercise
|
||||
// GetStorageEngineStatus' multi-instance awareness without a database.
|
||||
type fakeStorageBackendRepo struct{ backends []*types.StorageBackend }
|
||||
|
||||
func (f *fakeStorageBackendRepo) Create(context.Context, *types.StorageBackend) error {
|
||||
return nil
|
||||
}
|
||||
func (f *fakeStorageBackendRepo) GetByID(context.Context, uint64, string) (*types.StorageBackend, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (f *fakeStorageBackendRepo) List(context.Context, uint64) ([]*types.StorageBackend, error) {
|
||||
return f.backends, nil
|
||||
}
|
||||
func (f *fakeStorageBackendRepo) Update(context.Context, *types.StorageBackend) error { return nil }
|
||||
func (f *fakeStorageBackendRepo) Delete(context.Context, uint64, string) error { return nil }
|
||||
func (f *fakeStorageBackendRepo) FindLegacyAlias(context.Context, uint64, string) (*types.StorageBackend, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func TestGetStorageEngineStatus_IncludesOBS(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
t.Setenv(storageallowlist.AllowListEnv, "")
|
||||
@@ -90,3 +110,38 @@ func TestGetStorageEngineStatus_OBSConfiguredFromTenant(t *testing.T) {
|
||||
require.NotNil(t, obsStatus)
|
||||
assert.True(t, obsStatus.Available)
|
||||
}
|
||||
|
||||
// A workspace that configured COS only through the new multi-instance Storage
|
||||
// settings (storage_backends), with an empty legacy StorageEngineConfig, must
|
||||
// still be reported as available.
|
||||
func TestGetStorageEngineStatus_AvailableFromActiveBackend(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
t.Setenv(storageallowlist.AllowListEnv, "")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/system/storage-engine-status", nil)
|
||||
c.Set(types.TenantInfoContextKey.String(), &types.Tenant{ID: 42})
|
||||
|
||||
h := &SystemHandler{storageBackendRepo: &fakeStorageBackendRepo{backends: []*types.StorageBackend{
|
||||
{Provider: "cos", Status: types.StorageBackendStatusActive},
|
||||
{Provider: "s3", Status: types.StorageBackendStatusDisabled},
|
||||
}}}
|
||||
h.GetStorageEngineStatus(c)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
var resp struct {
|
||||
Data struct {
|
||||
Engines []StorageEngineStatusItem `json:"engines"`
|
||||
} `json:"data"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
|
||||
|
||||
status := map[string]bool{}
|
||||
for _, engine := range resp.Data.Engines {
|
||||
status[engine.Name] = engine.Available
|
||||
}
|
||||
assert.True(t, status["cos"], "active COS backend should be available")
|
||||
assert.False(t, status["s3"], "disabled S3 backend should not be available")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user