mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-04 10:29:28 +03:00
a0c26f6870
Should BucketExists (HeadBucket) fail because of an error related to
the connection rather than the existence of the bucket, no information
is available and the admin is left guessing.
https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadBucket.html
> This action is useful to determine if a bucket exists and you have
> permission to access it. The action returns a 200 OK if the bucket
> exists and you have permission to access it.
>
> If the bucket does not exist or you do not have permission to access
> it, the HEAD request returns a generic 400 Bad Request, 403
> Forbidden or 404 Not Found code. A message body is not included, so
> you cannot determine the exception beyond these error codes.
GetBucketVersioning is used instead and exclusively dedicated to
asserting if using the connection does not return a BadRequest.
If it does the NewMinioStorage logs an error and returns. Otherwise
it keeps going knowing that BucketExists is not going to fail for
reasons unrelated to the existence of the bucket and the permissions
to access it.
(cherry picked from commit de59924605
)
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package storage
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/test"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMinioStorageIterator(t *testing.T) {
|
|
if os.Getenv("CI") == "" {
|
|
t.Skip("minioStorage not present outside of CI")
|
|
return
|
|
}
|
|
testStorageIterator(t, setting.MinioStorageType, &setting.Storage{
|
|
MinioConfig: setting.MinioStorageConfig{
|
|
Endpoint: "minio:9000",
|
|
AccessKeyID: "123456",
|
|
SecretAccessKey: "12345678",
|
|
Bucket: "gitea",
|
|
Location: "us-east-1",
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestS3StorageBadRequest(t *testing.T) {
|
|
if os.Getenv("CI") == "" {
|
|
t.Skip("S3Storage not present outside of CI")
|
|
return
|
|
}
|
|
lc, cleanup := test.NewLogChecker("bad-request")
|
|
lc.StopMark("S3 storage connection failure")
|
|
defer cleanup()
|
|
cfg := &setting.Storage{
|
|
MinioConfig: setting.MinioStorageConfig{
|
|
Endpoint: "minio:9000",
|
|
AccessKeyID: "123456",
|
|
SecretAccessKey: "12345678",
|
|
Bucket: "bucket",
|
|
Location: "us-east-1",
|
|
},
|
|
}
|
|
message := "ERROR"
|
|
defer test.MockVariableValue(&getBucketVersioning, func(ctx context.Context, minioClient *minio.Client, bucket string) error {
|
|
return minio.ErrorResponse{
|
|
StatusCode: http.StatusBadRequest,
|
|
Code: "FixtureError",
|
|
Message: message,
|
|
}
|
|
})()
|
|
_, err := NewStorage(setting.MinioStorageType, cfg)
|
|
assert.ErrorContains(t, err, message)
|
|
|
|
_, stopped := lc.Check(100 * time.Millisecond)
|
|
assert.False(t, stopped)
|
|
}
|