Skip to content

Commit

Permalink
use env vars for hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaqiuxy committed Jun 8, 2021
1 parent 0c3a3cd commit f9f90f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ services:
depends_on:
- redis
- dynamodb-local
environment:
REDIS_HOST: "redis:6379"
DYNAMODB_HOST: "http://dynamodb-local:8000"
16 changes: 13 additions & 3 deletions dynamodb/dynamodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"sync"
"testing"
Expand All @@ -22,11 +23,16 @@ var dynamodbInstance *dynamodb.DynamoDB

func DynamodbClient() *dynamodb.DynamoDB {
var newLocalDynamodb = func() *dynamodb.DynamoDB {
dynamodbHost, ok := os.LookupEnv("DYNAMODB_HOST")
if !ok {
dynamodbHost = "http://localhost:8000"
}

s, err := session.NewSessionWithOptions(session.Options{
Profile: "dynamodb-local",
Config: aws.Config{
Region: aws.String("dev-region"),
Endpoint: aws.String("http://localhost:8000"),
Endpoint: aws.String(dynamodbHost),
Credentials: credentials.NewStaticCredentials("dev-key-id", "dev-secret-key", ""),
},
})
Expand Down Expand Up @@ -60,15 +66,19 @@ func TestDynamoDb(t *testing.T) {

items, err := c.BatchGet([]string{key})

if err != nil {
t.Errorf("Expected 1 item, got none %s", err)
}

if len(items) != 1 {
t.Errorf("Received %v items, expected 1", len(items))
t.Errorf("Expected 1 items, got %v", len(items))
}

var str string
json.Unmarshal(items[0].Value, &str)

if str != val {
t.Errorf("Received %v (type %v), expected %v (type %v)", str, reflect.TypeOf(str), val, reflect.TypeOf(val))
t.Errorf("Expected %v (type %v), got %v (type %v), ", str, reflect.TypeOf(str), val, reflect.TypeOf(val))
}

fmt.Println(items, str, err)
Expand Down
12 changes: 11 additions & 1 deletion redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"sync"
"testing"
Expand All @@ -19,8 +20,13 @@ var redisInstance *redis.Client

func RedisClient() *redis.Client {
var redisDb = func() (*redis.Client, error) {
redisHost, ok := os.LookupEnv("REDIS_HOST")
if !ok {
redisHost = "localhost:6379"
}

db := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Addr: redisHost,
Password: "",
})

Expand Down Expand Up @@ -58,6 +64,10 @@ func TestRedis(t *testing.T) {

items, err := c.BatchGet([]string{key})

if err != nil {
t.Errorf("Expected 1 item, got none %s", err)
}

if len(items) != 1 {
t.Errorf("Received %v items, expected 1", len(items))
}
Expand Down

0 comments on commit f9f90f7

Please sign in to comment.