summaryrefslogtreecommitdiff
path: root/util_test.go
blob: ff700be41fc97b43f2c1b7b90ea22538bf7bcd44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package s3

import (
	"context"
	"fmt"
	"time"

	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"github.com/spf13/afero"
)

func newTestAfs() (*afero.Afero, func()) {
	accessKeyID := "testuser"
	secretAccessKey := "testsecret"
	endpoint := "127.0.0.1:9000"
	useSSL := false
	bucket := "test-bucket"

	client, err := minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
		Secure: useSSL,
	})
	if err != nil {
		panic("unable connect")
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	if exists, err := client.BucketExists(ctx, bucket); err != nil {
		panic("unable connect")
	} else if !exists {
		if err := client.MakeBucket(ctx, bucket, minio.MakeBucketOptions{}); err != nil {
			panic("failed to seup a bucket")
		}
	}

	url := fmt.Sprintf("http://%s:%s@%s/%s", accessKeyID, secretAccessKey, endpoint, bucket)
	fs, err := NewFsFromURL(url)
	if err != nil {
		panic("unexpected error")
	}
	afs := &afero.Afero{Fs: fs}
	return afs, func() {
		afs.RemoveAll("")
	}
}