|
| 1 | +package s3provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/docker/docker/api/types/container" |
| 7 | + "github.com/docker/go-connections/nat" |
| 8 | + "github.com/minio/minio-go/v7" |
| 9 | + "github.com/minio/minio-go/v7/pkg/credentials" |
| 10 | + "github.com/smartcontractkit/chainlink-testing-framework/framework" |
| 11 | + tc "github.com/testcontainers/testcontainers-go" |
| 12 | + tcwait "github.com/testcontainers/testcontainers-go/wait" |
| 13 | + "math/rand" |
| 14 | + "strconv" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + DefaultImage = "minio/minio" |
| 19 | + DefaultName = "minio" |
| 20 | + DefaultBucket = "test-bucket" |
| 21 | + DefaultRegion = "us-east-1" |
| 22 | + DefaultPort = 9000 |
| 23 | + DefaultConsolePort = 9001 |
| 24 | +) |
| 25 | + |
| 26 | +type Minio struct { |
| 27 | + host string |
| 28 | + port int |
| 29 | + consolePort int |
| 30 | + accessKey string |
| 31 | + secretKey string |
| 32 | + bucket string |
| 33 | + region string |
| 34 | +} |
| 35 | + |
| 36 | +func (m Minio) GetSecretKey() string { |
| 37 | + return m.secretKey |
| 38 | +} |
| 39 | + |
| 40 | +func (m Minio) GetAccessKey() string { |
| 41 | + return m.accessKey |
| 42 | +} |
| 43 | + |
| 44 | +func (m Minio) GetBucket() string { |
| 45 | + return m.bucket |
| 46 | +} |
| 47 | + |
| 48 | +func (m Minio) GetURL() string { |
| 49 | + return fmt.Sprintf("http://%s:%d", m.host, m.port) |
| 50 | +} |
| 51 | + |
| 52 | +func (m Minio) GetConsoleURL() string { |
| 53 | + return fmt.Sprintf("http://%s:%d", m.host, m.consolePort) |
| 54 | +} |
| 55 | + |
| 56 | +func (m Minio) GetEndpoint() string { |
| 57 | + return fmt.Sprintf("%s:%d", m.host, m.consolePort) |
| 58 | +} |
| 59 | + |
| 60 | +func (m Minio) GetRegion() string { |
| 61 | + return m.region |
| 62 | +} |
| 63 | + |
| 64 | +type Option func(*Minio) |
| 65 | + |
| 66 | +type MinioFactory struct{} |
| 67 | + |
| 68 | +func NewMinioFactory() ProviderFactory { |
| 69 | + return MinioFactory{} |
| 70 | +} |
| 71 | + |
| 72 | +func (mf MinioFactory) NewProvider(options ...Option) (Provider, error) { |
| 73 | + m := &Minio{ |
| 74 | + port: DefaultPort, |
| 75 | + consolePort: DefaultConsolePort, |
| 76 | + accessKey: randomStr(20), |
| 77 | + secretKey: randomStr(40), |
| 78 | + bucket: DefaultBucket, |
| 79 | + region: DefaultRegion, |
| 80 | + } |
| 81 | + |
| 82 | + for _, opt := range options { |
| 83 | + opt(m) |
| 84 | + } |
| 85 | + |
| 86 | + ctx := context.Background() |
| 87 | + containerName := framework.DefaultTCName(DefaultName) |
| 88 | + bindPort := fmt.Sprintf("%d/tcp", m.port) |
| 89 | + bindConsolePort := fmt.Sprintf("%d/tcp", m.consolePort) |
| 90 | + |
| 91 | + req := tc.ContainerRequest{ |
| 92 | + Name: containerName, |
| 93 | + Image: DefaultImage, |
| 94 | + Labels: framework.DefaultTCLabels(), |
| 95 | + Networks: []string{framework.DefaultNetworkName}, |
| 96 | + NetworkAliases: map[string][]string{ |
| 97 | + framework.DefaultNetworkName: {containerName}, |
| 98 | + }, |
| 99 | + ExposedPorts: []string{bindPort, bindConsolePort}, |
| 100 | + Env: map[string]string{ |
| 101 | + "MINIO_ROOT_USER": m.accessKey, |
| 102 | + "MINIO_ROOT_PASSWORD": m.secretKey, |
| 103 | + "MINIO_BUCKET": DefaultBucket, |
| 104 | + }, |
| 105 | + Entrypoint: []string{ |
| 106 | + "minio", |
| 107 | + "server", |
| 108 | + "data", |
| 109 | + "--address", |
| 110 | + fmt.Sprintf(":%d", m.port), |
| 111 | + "--console-address", |
| 112 | + fmt.Sprintf(":%d", m.consolePort), |
| 113 | + }, |
| 114 | + HostConfigModifier: func(h *container.HostConfig) { |
| 115 | + framework.NoDNS(true, h) |
| 116 | + h.PortBindings = framework.MapTheSamePort(bindPort) |
| 117 | + }, |
| 118 | + WaitingFor: tcwait.ForAll( |
| 119 | + tcwait.ForListeningPort(nat.Port(fmt.Sprintf("%d/tcp", m.port))), |
| 120 | + ), |
| 121 | + } |
| 122 | + req.HostConfigModifier = func(h *container.HostConfig) { |
| 123 | + h.PortBindings = nat.PortMap{ |
| 124 | + nat.Port(bindPort): []nat.PortBinding{ |
| 125 | + { |
| 126 | + HostIP: "0.0.0.0", |
| 127 | + HostPort: strconv.Itoa(m.port), |
| 128 | + }, |
| 129 | + }, |
| 130 | + nat.Port(bindPort): []nat.PortBinding{ |
| 131 | + { |
| 132 | + HostIP: "0.0.0.0", |
| 133 | + HostPort: strconv.Itoa(m.consolePort), |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + c, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ |
| 140 | + ContainerRequest: req, |
| 141 | + Started: true, |
| 142 | + }) |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + host, err := framework.GetHost(c) |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + m.host = host |
| 152 | + |
| 153 | + // Initialize minio client object. |
| 154 | + minioClient, err := minio.New(m.GetEndpoint(), &minio.Options{ |
| 155 | + Creds: credentials.NewStaticV4(m.GetAccessKey(), m.GetSecretKey(), ""), |
| 156 | + Secure: false, |
| 157 | + }) |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + |
| 162 | + // Initialize default bucket |
| 163 | + err = minioClient.MakeBucket(ctx, m.GetBucket(), minio.MakeBucketOptions{Region: m.GetRegion()}) |
| 164 | + if err != nil { |
| 165 | + return nil, err |
| 166 | + } |
| 167 | + |
| 168 | + return m, nil |
| 169 | +} |
| 170 | + |
| 171 | +func WithPort(port int) Option { |
| 172 | + return func(m *Minio) { |
| 173 | + m.port = port |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func WithConsolePort(consolePort int) Option { |
| 178 | + return func(m *Minio) { |
| 179 | + m.consolePort = consolePort |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 184 | + |
| 185 | +func randomStr(n int) string { |
| 186 | + b := make([]byte, n) |
| 187 | + for i := range b { |
| 188 | + b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))] |
| 189 | + } |
| 190 | + return string(b) |
| 191 | +} |
0 commit comments