mirror of
https://github.com/fleetdm/fleet
synced 2026-05-20 15:38:39 +00:00
Resolves #36909. # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually
113 lines
3.1 KiB
Go
113 lines
3.1 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"
|
|
testEndpoint = "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")
|
|
}
|
|
}
|