2022-07-14 17:14:24 +00:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2022-07-14 19:11:52 +00:00
|
|
|
"context"
|
2022-07-14 17:14:24 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/config"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
accessKeyID = "minio"
|
|
|
|
secretAccessKey = "minio123!"
|
|
|
|
testEndpoint = "localhost:9000"
|
|
|
|
mockInstallerContents = "mock"
|
|
|
|
)
|
|
|
|
|
2022-07-18 16:44:30 +00:00
|
|
|
// SetupTestInstallerStore creates a new store with minio as a back-end
|
|
|
|
// for local testing
|
|
|
|
func SetupTestInstallerStore(tb testing.TB, bucket, prefix string) *InstallerStore {
|
2022-07-14 17:14:24 +00:00
|
|
|
checkEnv(tb)
|
|
|
|
|
|
|
|
store, err := NewInstallerStore(config.S3Config{
|
|
|
|
Bucket: bucket,
|
|
|
|
Prefix: prefix,
|
|
|
|
Region: "minio",
|
|
|
|
EndpointURL: testEndpoint,
|
|
|
|
AccessKeyID: accessKeyID,
|
|
|
|
SecretAccessKey: secretAccessKey,
|
|
|
|
ForceS3PathStyle: true,
|
|
|
|
DisableSSL: true,
|
|
|
|
})
|
|
|
|
require.Nil(tb, err)
|
|
|
|
|
2022-07-15 15:20:24 +00:00
|
|
|
err = store.CreateTestBucket(bucket)
|
|
|
|
require.NoError(tb, err)
|
2022-07-14 17:14:24 +00:00
|
|
|
|
|
|
|
tb.Cleanup(func() { cleanupStore(tb, store) })
|
|
|
|
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
2022-07-18 16:44:30 +00:00
|
|
|
// SeedTestInstallerStore adds mock installers to the given store
|
|
|
|
func SeedTestInstallerStore(tb testing.TB, store *InstallerStore, enrollSecret string) []fleet.Installer {
|
2022-07-14 17:14:24 +00:00
|
|
|
checkEnv(tb)
|
2022-07-14 19:11:52 +00:00
|
|
|
installers := []fleet.Installer{
|
|
|
|
mockInstaller(enrollSecret, "pkg", true),
|
|
|
|
mockInstaller(enrollSecret, "msi", true),
|
|
|
|
mockInstaller(enrollSecret, "deb", true),
|
|
|
|
mockInstaller(enrollSecret, "rpm", true),
|
|
|
|
mockInstaller(enrollSecret, "pkg", false),
|
|
|
|
mockInstaller(enrollSecret, "msi", false),
|
|
|
|
mockInstaller(enrollSecret, "deb", false),
|
|
|
|
mockInstaller(enrollSecret, "rpm", false),
|
2022-07-14 17:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range installers {
|
2022-07-14 19:11:52 +00:00
|
|
|
_, err := store.Put(context.Background(), i)
|
|
|
|
require.NoError(tb, err)
|
2022-07-14 17:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return installers
|
|
|
|
}
|
|
|
|
|
2022-07-14 19:11:52 +00:00
|
|
|
func mockInstaller(secret, kind string, desktop bool) fleet.Installer {
|
|
|
|
return fleet.Installer{
|
|
|
|
EnrollSecret: secret,
|
|
|
|
Kind: kind,
|
|
|
|
Desktop: desktop,
|
|
|
|
Content: aws.ReadSeekCloser(strings.NewReader(mockInstallerContents)),
|
|
|
|
}
|
2022-07-14 17:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupStore(tb testing.TB, store *InstallerStore) {
|
|
|
|
checkEnv(tb)
|
|
|
|
resp, err := store.s3client.ListObjects(&s3.ListObjectsInput{
|
|
|
|
Bucket: &store.bucket,
|
|
|
|
})
|
|
|
|
require.NoError(tb, err)
|
|
|
|
|
|
|
|
var objs []*s3.ObjectIdentifier
|
|
|
|
for _, o := range resp.Contents {
|
|
|
|
objs = append(objs, &s3.ObjectIdentifier{Key: o.Key})
|
|
|
|
}
|
|
|
|
_, err = store.s3client.DeleteObjects(&s3.DeleteObjectsInput{
|
|
|
|
Bucket: &store.bucket,
|
|
|
|
Delete: &s3.Delete{
|
|
|
|
Objects: objs,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(tb, err)
|
|
|
|
|
|
|
|
_, err = store.s3client.DeleteBucket(&s3.DeleteBucketInput{
|
|
|
|
Bucket: &store.bucket,
|
|
|
|
})
|
|
|
|
require.NoError(tb, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkEnv(tb testing.TB) {
|
|
|
|
if _, ok := os.LookupEnv("MINIO_STORAGE_TEST"); !ok {
|
|
|
|
tb.Skip("set MINIO_STORAGE_TEST environment variable to run S3-based tests")
|
|
|
|
}
|
|
|
|
}
|