Skip to content

Commit

Permalink
Merge pull request #15 from Swiggy/env
Browse files Browse the repository at this point in the history
set aws env variables
  • Loading branch information
amit-shekhar committed Jul 3, 2023
2 parents e0cd2fa + ed2757f commit da0f123
Show file tree
Hide file tree
Showing 15 changed files with 631 additions and 76 deletions.
1 change: 1 addition & 0 deletions canned/clustered_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func NewClusteredRedis() (*ClusteredRedis, error) {
"SLAVES_PER_MASTER": "0",
},
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

Expand Down
3 changes: 2 additions & 1 deletion canned/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func NewConsul(ctx context.Context) (*Consul, error) {
os.Setenv("TC_HOST", "localhost")
req := testcontainers.ContainerRequest{
Image: getEnvString("CONSUL_CONTAINER_IMAGE", "consul:1.7.3"),
SkipReaper: skipReaper(),
ExposedPorts: []string{"8500/tcp"},
WaitingFor: wait.ForListeningPort("8500"),
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand Down
5 changes: 3 additions & 2 deletions canned/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ func NewDynamoDB(ctx context.Context) (*DynamoDB, error) {
os.Setenv("TC_HOST", "localhost")
req := testcontainers.ContainerRequest{
Image: getEnvString("DYNAMODB_CONTAINER_IMAGE", "amazon/dynamodb-local:1.13.5"),
SkipReaper: skipReaper(),
ExposedPorts: []string{"8000/tcp"},
WaitingFor: wait.ForListeningPort("8000"),
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand All @@ -50,7 +51,7 @@ func NewDynamoDB(ctx context.Context) (*DynamoDB, error) {

host, _ := container.Host(ctx)
port, _ := container.MappedPort(ctx, "8000")
accessKey, secretKey, region := "awsaccesskey", "awssecretkey", "ap-southeast-1"
accessKey, secretKey, region := getAWSConfig()
endpoint := fmt.Sprintf("http://%s:%s", host, port.Port())

dynamoClient, err := newDynamoClient(endpoint, accessKey, secretKey, region)
Expand Down
11 changes: 6 additions & 5 deletions canned/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func NewKafka(ctx context.Context) (*Kafka, error) {

req := testcontainers.ContainerRequest{
Image: getEnvString("KAFKA_CONTAINER_IMAGE", "confluentinc/cp-kafka:5.2.1"),
SkipReaper: skipReaper(),
ExposedPorts: []string{brokerPort.Port(), kafkaPort.Port(), zookeeperPort},
Cmd: []string{"sleep", "infinity"},
Env: env,
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand All @@ -75,12 +76,12 @@ func NewKafka(ctx context.Context) (*Kafka, error) {
return nil, fmt.Errorf("failed to start kafka, error: %v", err)
}

ac, err := confluent.NewAdminClient(&confluent.ConfigMap{"bootstrap.servers": bootstrapServer, "security.protocol": "PLAINTEXT"})
ac, err := confluent.NewAdminClient(&confluent.ConfigMap{"bootstrap.servers": bootstrapServer, "api.version.request": false})
if err != nil {
return nil, fmt.Errorf("error creating admin client, error: %v", err)
}

producer, err := confluent.NewProducer(&confluent.ConfigMap{"bootstrap.servers": bootstrapServer, "security.protocol": "PLAINTEXT"})
producer, err := confluent.NewProducer(&confluent.ConfigMap{"bootstrap.servers": bootstrapServer, "api.version.request": false})
if err != nil {
return nil, fmt.Errorf("error creating producer, error: %v", err)
}
Expand All @@ -104,7 +105,7 @@ func NewKafka(ctx context.Context) (*Kafka, error) {

func startZookeeper(ctx context.Context, container testcontainers.Container) error {
cmd := []string{"sh", "-c", "printf 'clientPort=2181\ndataDir=/var/lib/zookeeper/data\ndataLogDir=/var/lib/zookeeper/log'> /zookeeper.properties; zookeeper-server-start /zookeeper.properties >/dev/null 2>&1 &"}
_, err := container.Exec(ctx, cmd)
_, _, err := container.Exec(ctx, cmd)
if err != nil {
return err
}
Expand All @@ -114,7 +115,7 @@ func startZookeeper(ctx context.Context, container testcontainers.Container) err
func startKafka(ctx context.Context, container testcontainers.Container, bootstrapServers string) error {
zookeeperConnect := fmt.Sprintf("localhost:%s", zookeeperPort)
cmd := []string{"sh", "-c", fmt.Sprintf("export KAFKA_ZOOKEEPER_CONNECT=%s; export KAFKA_ADVERTISED_LISTENERS=%s,BROKER://:%s; /etc/confluent/docker/run >/tmp/kafka-start.log 2>/tmp/kafka-start &", zookeeperConnect, bootstrapServers, brokerPort.Port())}
_, err := container.Exec(ctx, cmd)
_, _, err := container.Exec(ctx, cmd)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion canned/localstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewLocalstack(ctx context.Context) (*Localstack, error) {
ExposedPorts: []string{"4566/tcp"},
WaitingFor: wait.ForListeningPort("4566"),
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

Expand All @@ -48,7 +49,7 @@ func NewLocalstack(ctx context.Context) (*Localstack, error) {

host, _ := container.Host(ctx)
port, _ := container.MappedPort(ctx, "4566")
accessKey, secretKey, region := "awsaccesskey", "awssecretkey", "ap-southeast-1"
accessKey, secretKey, region := getAWSConfig()
endpoint := fmt.Sprintf("http://%s:%s", host, port.Port())

awsSession, err := newAWSSession(accessKey, secretKey, endpoint, region)
Expand Down
7 changes: 4 additions & 3 deletions canned/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ type Minio struct {

func NewMinio(ctx context.Context) (*Minio, error) {
os.Setenv("TC_HOST", "localhost")
accessKey, secretKey, region := "awsaccesskey", "awssecretkey", "ap-southeast-1"
accessKey, secretKey, region := getAWSConfig()

req := testcontainers.ContainerRequest{
Image: getEnvString("MINIO_CONTAINER_IMAGE", "minio/minio:RELEASE.2023-05-18T00-05-36Z"),
SkipReaper: skipReaper(),
ExposedPorts: []string{"9000/tcp"},
WaitingFor: wait.ForListeningPort("9000/tcp"),
Cmd: []string{"server", "/data"},
Env: map[string]string{
"MINIO_ROOT_USER": accessKey,
"MINIO_ROOT_PASSWORD": secretKey,
},
AutoRemove: true,
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand Down
5 changes: 3 additions & 2 deletions canned/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ func NewMysql(ctx context.Context) (*Mysql, error) {

req := testcontainers.ContainerRequest{
Image: getEnvString("MYSQL_CONTAINER_IMAGE", "mysql:5.7.34"),
SkipReaper: skipReaper(),
ExposedPorts: []string{"3306/tcp"},
WaitingFor: wait.ForListeningPort("3306"),
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": "password",
"MYSQL_DATABASE": "test",
},
AutoRemove: true,
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand Down
3 changes: 2 additions & 1 deletion canned/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ func NewRedis(ctx context.Context) (*Redis, error) {

req := testcontainers.ContainerRequest{
Image: getEnvString("REDIS_CONTAINER_IMAGE", "redis:7.0.11"),
SkipReaper: skipReaper(),
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForListeningPort("6379"),
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand Down
5 changes: 3 additions & 2 deletions canned/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ func NewSQS(ctx context.Context) (*SQS, error) {
os.Setenv("TC_HOST", "localhost")
req := testcontainers.ContainerRequest{
Image: getEnvString("SQS_CONTAINER_IMAGE", "softwaremill/elasticmq-native:1.3.14"),
SkipReaper: skipReaper(),
ExposedPorts: []string{"9324/tcp"},
WaitingFor: wait.ForListeningPort("9324"),
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand All @@ -50,7 +51,7 @@ func NewSQS(ctx context.Context) (*SQS, error) {

host, _ := container.Host(ctx)
port, _ := container.MappedPort(ctx, "9324")
accessKey, secretKey, region := "awsaccesskey", "awssecretkey", "ap-southeast-1"
accessKey, secretKey, region := getAWSConfig()
endpoint := fmt.Sprintf("http://%s:%s", host, port.Port())

sqsClient, err := newSQSClient(endpoint, accessKey, secretKey, region)
Expand Down
3 changes: 2 additions & 1 deletion canned/tile38.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ func NewTile38(ctx context.Context) (*Tile38, error) {

req := testcontainers.ContainerRequest{
Image: getEnvString("TILE38_CONTAINER_IMAGE", "tile38/tile38:1.31.0"),
SkipReaper: skipReaper(),
ExposedPorts: []string{"9851/tcp"},
WaitingFor: wait.ForHTTP("/server").WithPort("9851"),
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand Down
8 changes: 8 additions & 0 deletions canned/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func getEnvString(variable string, defaultValue string) string {
return val
}

func getAWSConfig() (string, string, string) {
accessKey := getEnvString("AWS_ACCESS_KEY_ID", "awsaccesskey")
secretKey := getEnvString("AWS_SECRET_ACCESS_KEY", "awssecretkey")
region := getEnvString("AWS_REGION", "ap-southeast-1")

return accessKey, secretKey, region
}

func getBasicAuth() string {
username := os.Getenv("CONTAINER_REGISTRY_USERNAME")
password := os.Getenv("CONTAINER_REGISTRY_PASSWORD")
Expand Down
3 changes: 2 additions & 1 deletion canned/wiremock.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func NewWiremock(ctx context.Context) (*WireMock, error) {

req := testcontainers.ContainerRequest{
Image: getEnvString("WIREMOCK_CONTAINER_IMAGE", "wiremock/wiremock:2.32.0"),
SkipReaper: skipReaper(),
ExposedPorts: []string{"8080/tcp", "8443/tcp"},
WaitingFor: wait.ForListeningPort("8080"),
AutoRemove: true,
SkipReaper: skipReaper(),
RegistryCred: getBasicAuth(),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ go 1.13
require (
github.com/aws/aws-sdk-go v1.32.6
github.com/confluentinc/confluent-kafka-go v1.9.2
github.com/docker/docker v20.10.7+incompatible
github.com/docker/docker v20.10.17+incompatible
github.com/docker/go-connections v0.4.0
github.com/go-redis/redis/v8 v8.11.5
github.com/go-sql-driver/mysql v1.6.0
github.com/golang/mock v1.4.3
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/gomodule/redigo v2.0.0+incompatible
github.com/hashicorp/consul/api v1.5.0
github.com/testcontainers/testcontainers-go v0.11.1
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781
google.golang.org/grpc v1.46.0
github.com/testcontainers/testcontainers-go v0.14.0
golang.org/x/net v0.7.0
google.golang.org/grpc v1.47.0

)
Loading

0 comments on commit da0f123

Please sign in to comment.