mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 13:37:30 +00:00
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #41848 Docs updates: https://github.com/fleetdm/fleet/pull/41868/changes # Checklist for submitter - changes not needed since this is a dev environment and test issue ## Testing - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Enhanced test infrastructure to support environment-variable-based configuration for SAML, mail, database, and S3 services, enabling more flexible and dynamic test setups. * **Chores** * Updated Docker Compose configuration to use environment variables for service ports, allowing runtime customization while maintaining backward compatibility with default values. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
121 lines
3.3 KiB
Go
121 lines
3.3 KiB
Go
package s3
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
|
"github.com/fleetdm/fleet/v4/server/config"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
accessKeyID = "locals3"
|
|
secretAccessKey = "locals3"
|
|
)
|
|
|
|
var testEndpoint = getTestEndpoint()
|
|
|
|
func getTestEndpoint() string {
|
|
if port := os.Getenv("FLEET_S3_PORT"); port != "" {
|
|
return "http://localhost:" + port
|
|
}
|
|
return "http://localhost:9000"
|
|
}
|
|
|
|
func SetupTestSoftwareInstallerStore(tb testing.TB, bucket, prefix string) *SoftwareInstallerStore {
|
|
store := setupTestStore(tb, bucket, prefix, NewSoftwareInstallerStore)
|
|
tb.Cleanup(func() { cleanupStore(tb, store.s3store) })
|
|
return store
|
|
}
|
|
|
|
func SetupTestBootstrapPackageStore(tb testing.TB, bucket, prefix string) *BootstrapPackageStore {
|
|
store := setupTestStore(tb, bucket, prefix, NewBootstrapPackageStore)
|
|
tb.Cleanup(func() { cleanupStore(tb, store.s3store) })
|
|
return store
|
|
}
|
|
|
|
func SetupTestSoftwareTitleIconStore(tb testing.TB, bucket, prefix string) *SoftwareTitleIconStore {
|
|
store := setupTestStore(tb, bucket, prefix, NewSoftwareTitleIconStore)
|
|
tb.Cleanup(func() { cleanupStore(tb, store.s3store) })
|
|
return store
|
|
}
|
|
|
|
type testBucketCreator interface {
|
|
CreateTestBucket(ctx context.Context, name string) error
|
|
}
|
|
|
|
func setupTestStore[T testBucketCreator](tb testing.TB, bucket, prefix string, newFn func(config.S3Config) (T, error)) T {
|
|
checkEnv(tb)
|
|
|
|
store, err := newFn(config.S3Config{
|
|
SoftwareInstallersBucket: bucket,
|
|
SoftwareInstallersPrefix: prefix,
|
|
SoftwareInstallersRegion: "localhost",
|
|
SoftwareInstallersEndpointURL: testEndpoint,
|
|
SoftwareInstallersAccessKeyID: accessKeyID,
|
|
SoftwareInstallersSecretAccessKey: secretAccessKey,
|
|
SoftwareInstallersForceS3PathStyle: true,
|
|
SoftwareInstallersDisableSSL: true,
|
|
|
|
CarvesBucket: bucket,
|
|
CarvesPrefix: prefix,
|
|
CarvesRegion: "localhost",
|
|
CarvesEndpointURL: testEndpoint,
|
|
CarvesAccessKeyID: accessKeyID,
|
|
CarvesSecretAccessKey: secretAccessKey,
|
|
CarvesForceS3PathStyle: true,
|
|
CarvesDisableSSL: true,
|
|
})
|
|
require.Nil(tb, err)
|
|
|
|
err = store.CreateTestBucket(context.Background(), bucket)
|
|
require.NoError(tb, err)
|
|
|
|
return store
|
|
}
|
|
|
|
func cleanupStore(tb testing.TB, store *s3store) {
|
|
checkEnv(tb)
|
|
|
|
ctx := context.Background()
|
|
resp, err := store.s3Client.ListObjects(ctx, &s3.ListObjectsInput{
|
|
Bucket: &store.bucket,
|
|
})
|
|
var noSuchBucket *types.NoSuchBucket
|
|
if errors.As(err, &noSuchBucket) {
|
|
// OK, nothing to clean-up if the bucket no longer exists, no error
|
|
return
|
|
}
|
|
require.NoError(tb, err)
|
|
|
|
var objs []types.ObjectIdentifier
|
|
for _, o := range resp.Contents {
|
|
objs = append(objs, types.ObjectIdentifier{
|
|
Key: o.Key,
|
|
})
|
|
}
|
|
if len(objs) > 0 {
|
|
_, err = store.s3Client.DeleteObjects(ctx, &s3.DeleteObjectsInput{
|
|
Bucket: &store.bucket,
|
|
Delete: &types.Delete{
|
|
Objects: objs,
|
|
},
|
|
})
|
|
require.NoError(tb, err)
|
|
}
|
|
|
|
_, err = store.s3Client.DeleteBucket(ctx, &s3.DeleteBucketInput{
|
|
Bucket: &store.bucket,
|
|
})
|
|
require.NoError(tb, err)
|
|
}
|
|
|
|
func checkEnv(tb testing.TB) {
|
|
if _, ok := os.LookupEnv("S3_STORAGE_TEST"); !ok {
|
|
tb.Skip("set S3_STORAGE_TEST environment variable to run S3-based tests")
|
|
}
|
|
}
|