Skip to content

Commit d7e850a

Browse files
authored
Separate dsn into parts (frain-dev#1579)
* feat: spilt dsn values into parts * feat: enable native realm and jwt by default * feat: make redis a top level dependency * chore: update tests * chore: remove default values * chore: update limiter config requirement * chore: update test files * chore: update CI env vars * chore: remove dup CI env var * chore: pass db option to server test suite * chore: run migration before testing locally * chore: add default connection timeout * chore: use real env vars * chore: fix os version * chore: fix bug that might occur when loading config from OS env fot tests * chore: fix the way tests read values from env vars * chore: use default creds * chore: use redis port env var * chore: downgrade version to v0.9.2
1 parent 2b1b1a2 commit d7e850a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+718
-1077
lines changed

.github/workflows/go.yml

+30-13
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ jobs:
1212
matrix:
1313
go-version: [1.18.x]
1414
os: [ubuntu-latest, macos-latest]
15-
postgres-version: ["5.0"]
15+
postgres-version: ["15"]
1616
redis-version: ["6.2.6"]
17+
typesense-version: ["0.24.0"]
1718

1819
runs-on: ubuntu-latest
1920
services:
2021
postgres:
21-
image: postgres:15
22+
image: postgres:${{ matrix.postgres-version }}
2223
ports:
2324
- 5432:5432
2425
env:
25-
POSTGRES_USER: admin
26+
POSTGRES_USER: postgres
2627
POSTGRES_DB: convoy
27-
POSTGRES_PASSWORD: password
28+
POSTGRES_PASSWORD: postgres
2829
POSTGRES_MAX_CONNECTIONS: 2000
2930
# set up health check for postgres
3031
options: --health-cmd pg_isready --health-interval 10ms --health-timeout 500ms --health-retries 15
@@ -35,10 +36,10 @@ jobs:
3536
redis-version: ${{ matrix.redis-version }}
3637
redis-port: 6379
3738

38-
- name: Start Typesense
39+
- name: Start Typesense v${{ matrix.typesense-version }}
3940
uses: jirevwe/typesense-github-action@v1.0.1
4041
with:
41-
typesense-version: 0.23.1
42+
typesense-version: ${{ matrix.typesense-version }}
4243
typesense-api-key: some-api-key
4344

4445
- name: Get the version
@@ -72,14 +73,30 @@ jobs:
7273
- name: Migrate Postgres
7374
run: go run ./cmd migrate up
7475
env:
75-
CONVOY_DB_DSN: "postgres://admin:password@localhost:5432/convoy?sslmode=disable"
76-
CONVOY_REDIS_DSN: "redis://localhost:6379"
76+
CONVOY_DB_SCHEME: postgres
77+
CONVOY_DB_HOST: localhost
78+
CONVOY_DB_USERNAME: postgres
79+
CONVOY_DB_PASSWORD: postgres
80+
CONVOY_DB_DATABASE: convoy
81+
CONVOY_DB_OPTIONS: sslmode=disable&connect_timeout=30
82+
CONVOY_DB_PORT: 5432
83+
CONVOY_REDIS_SCHEME: redis
84+
CONVOY_REDIS_HOST: localhost
85+
CONVOY_REDIS_PORT: 6379
7786

7887
- name: Run integration tests
7988
run: make integration_tests
8089
env:
81-
TEST_POSTGRES_DSN: "postgres://admin:password@localhost:5432/convoy?sslmode=disable"
82-
TEST_REDIS_DSN: "redis://localhost:6379"
83-
TEST_CONVOY_TYPESENSE_HOST: "http://localhost:8108"
84-
TEST_CONVOY_TYPESENSE_API_KEY: "some-api-key"
85-
TEST_CONVOY_SEARCH_TYPE: "typesense"
90+
TEST_DB_SCHEME: postgres
91+
TEST_DB_HOST: localhost
92+
TEST_DB_USERNAME: postgres
93+
TEST_DB_PASSWORD: postgres
94+
TEST_DB_DATABASE: convoy
95+
TEST_DB_OPTIONS: sslmode=disable&connect_timeout=30
96+
TEST_DB_PORT: 5432
97+
TEST_REDIS_SCHEME: redis
98+
TEST_REDIS_HOST: localhost
99+
TEST_REDIS_PORT: 6379
100+
TEST_TYPESENSE_HOST: http://localhost:8108
101+
TEST_TYPESENSE_API_KEY: some-api-key
102+
TEST_SEARCH_TYPE: typesense

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ build:
1616
scripts/build.sh -b $(type)
1717

1818
integration_tests:
19+
go run ./cmd migrate up
1920
go test -tags integration -p 1 ./...
2021

2122
generate_migration_time:

api/portal_api_integration_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ func (s *PortalEventIntegrationTestSuite) Test_GetEventsPaged() {
537537
require.NoError(s.T(), err)
538538

539539
portalLink, err := testdb.SeedPortalLink(s.ConvoyApp.A.DB, s.DefaultProject, []string{endpoint2.UID})
540-
fmt.Println("endpoint2UID is >>>", endpoint2.UID)
541540
require.NoError(s.T(), err)
542541

543542
url := fmt.Sprintf("/portal-api/events?endpointId=%s&sourceId=%s&token=%s", endpoint1.UID, sourceID, portalLink.Token)

api/server_suite_test.go

+31-32
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@ import (
3737
)
3838

3939
// TEST HELPERS.
40-
func getPostgresDSN() string {
41-
return os.Getenv("TEST_POSTGRES_DSN")
42-
}
43-
44-
func getRedisDSN() string {
45-
return os.Getenv("TEST_REDIS_DSN")
46-
}
47-
4840
func getConfig() config.Configuration {
49-
return config.Configuration{
50-
Queue: config.QueueConfiguration{
51-
Type: config.RedisQueueProvider,
52-
Redis: config.RedisQueueConfiguration{
53-
Dsn: getRedisDSN(),
54-
},
55-
},
56-
Database: config.DatabaseConfiguration{
57-
Type: config.PostgresDatabaseProvider,
58-
Dsn: getPostgresDSN(),
59-
},
41+
_ = os.Setenv("CONVOY_DB_HOST", os.Getenv("TEST_REDIS_HOST"))
42+
_ = os.Setenv("CONVOY_REDIS_SCHEME", os.Getenv("TEST_REDIS_SCHEME"))
43+
_ = os.Setenv("CONVOY_REDIS_PORT", os.Getenv("TEST_REDIS_PORT"))
44+
45+
_ = os.Setenv("CONVOY_DB_HOST", os.Getenv("TEST_DB_HOST"))
46+
_ = os.Setenv("CONVOY_DB_SCHEME", os.Getenv("TEST_DB_SCHEME"))
47+
_ = os.Setenv("CONVOY_DB_USERNAME", os.Getenv("TEST_DB_USERNAME"))
48+
_ = os.Setenv("CONVOY_DB_PASSWORD", os.Getenv("TEST_DB_PASSWORD"))
49+
_ = os.Setenv("CONVOY_DB_DATABASE", os.Getenv("TEST_DB_DATABASE"))
50+
_ = os.Setenv("CONVOY_DB_OPTIONS", os.Getenv("TEST_DB_OPTIONS"))
51+
_ = os.Setenv("CONVOY_DB_PORT", os.Getenv("TEST_DB_PORT"))
52+
53+
err := config.LoadConfig("")
54+
if err != nil {
55+
log.Fatal(err)
6056
}
57+
58+
cfg, _ := config.Get()
59+
60+
return cfg
6161
}
6262

6363
func getDB() database.Database {
@@ -73,10 +73,10 @@ func getDB() database.Database {
7373
return db
7474
}
7575

76-
func getQueueOptions(name string) (queue.QueueOptions, error) {
76+
func getQueueOptions() (queue.QueueOptions, error) {
7777
var opts queue.QueueOptions
7878
cfg := getConfig()
79-
rdb, err := rdb.NewClient(cfg.Queue.Redis.Dsn)
79+
redis, err := rdb.NewClient(cfg.Redis.BuildDsn())
8080
if err != nil {
8181
return opts, err
8282
}
@@ -87,39 +87,38 @@ func getQueueOptions(name string) (queue.QueueOptions, error) {
8787
}
8888
opts = queue.QueueOptions{
8989
Names: queueNames,
90-
RedisClient: rdb,
91-
RedisAddress: cfg.Queue.Redis.Dsn,
90+
RedisClient: redis,
91+
RedisAddress: cfg.Redis.BuildDsn(),
9292
Type: string(config.RedisQueueProvider),
9393
}
9494

9595
return opts, nil
9696
}
9797

9898
func buildServer() *ApplicationHandler {
99-
var tracer tracer.Tracer
99+
var t tracer.Tracer = nil
100100
var logger *log.Logger
101101
var qOpts queue.QueueOptions
102102

103103
db := getDB()
104-
qOpts, _ = getQueueOptions("EventQueue")
104+
qOpts, _ = getQueueOptions()
105105

106-
queue := redisqueue.NewQueue(qOpts)
106+
newQueue := redisqueue.NewQueue(qOpts)
107107
logger = log.NewLogger(os.Stderr)
108108
logger.SetLevel(log.FatalLevel)
109109

110-
cache := ncache.NewNoopCache()
111-
tracer = nil
110+
noopCache := ncache.NewNoopCache()
112111

113112
ah, _ := NewApplicationHandler(
114113
&types.APIOptions{
115114
DB: db,
116-
Queue: queue,
115+
Queue: newQueue,
117116
Logger: logger,
118-
Tracer: tracer,
119-
Cache: cache,
117+
Tracer: t,
118+
Cache: noopCache,
120119
})
121120

122-
ah.RegisterPolicy()
121+
_ = ah.RegisterPolicy()
123122

124123
return ah
125124
}

auth/realm/jwt/jwt_realm_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ func TestJwtRealm_Authenticate(t *testing.T) {
2020
defer ctrl.Finish()
2121

2222
userRepo := mocks.NewMockUserRepository(ctrl)
23-
cache, err := cache.NewCache(config.CacheConfiguration{})
23+
newCache, err := cache.NewCache(config.DefaultConfiguration.Redis)
2424

2525
require.Nil(t, err)
2626

27-
jr := NewJwtRealm(userRepo, &config.JwtRealmOptions{}, cache)
27+
jr := NewJwtRealm(userRepo, &config.JwtRealmOptions{}, newCache)
2828

2929
user := &datastore.User{UID: "123456"}
3030
token, err := jr.jwt.GenerateToken(user)
@@ -114,7 +114,9 @@ func TestJwtRealm_Authenticate(t *testing.T) {
114114
Token: token.AccessToken,
115115
},
116116
},
117-
dbFn: nil,
117+
dbFn: func(userRepo *mocks.MockUserRepository) {
118+
userRepo.EXPECT().FindUserByID(gomock.Any(), gomock.Any()).Return(nil, ErrInvalidToken)
119+
},
118120
want: nil,
119121
blacklist: true,
120122
wantErr: true,

auth/realm/jwt/jwt_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
)
1111

1212
func provideJwt(t *testing.T) *Jwt {
13-
cache, err := cache.NewCache(config.CacheConfiguration{})
13+
newCache, err := cache.NewCache(config.DefaultConfiguration.Redis)
1414

1515
require.Nil(t, err)
1616

17-
jwt := NewJwt(&config.JwtRealmOptions{}, cache)
17+
jwt := NewJwt(&config.JwtRealmOptions{}, newCache)
1818
return jwt
1919
}
2020

cache/cache.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"time"
66

7-
mcache "github.com/frain-dev/convoy/cache/memory"
87
rcache "github.com/frain-dev/convoy/cache/redis"
98
"github.com/frain-dev/convoy/config"
109
)
@@ -15,15 +14,11 @@ type Cache interface {
1514
Delete(ctx context.Context, key string) error
1615
}
1716

18-
func NewCache(cfg config.CacheConfiguration) (Cache, error) {
19-
if cfg.Type == config.RedisCacheProvider {
20-
ca, err := rcache.NewRedisCache(cfg.Redis.Dsn)
21-
if err != nil {
22-
return nil, err
23-
}
24-
25-
return ca, nil
17+
func NewCache(cfg config.RedisConfiguration) (Cache, error) {
18+
ca, err := rcache.NewRedisCache(cfg.BuildDsn())
19+
if err != nil {
20+
return nil, err
2621
}
2722

28-
return mcache.NewMemoryCache(), nil
23+
return ca, nil
2924
}

cache/redis/client_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package rcache
55

66
import (
77
"context"
8+
"github.com/frain-dev/convoy/config"
89
"os"
10+
"strconv"
911
"testing"
1012
"time"
1113

@@ -17,7 +19,13 @@ type data struct {
1719
}
1820

1921
func getDSN() string {
20-
return os.Getenv("TEST_REDIS_DSN")
22+
port, _ := strconv.Atoi(os.Getenv("TEST_REDIS_PORT"))
23+
c := config.RedisConfiguration{
24+
Scheme: "redis",
25+
Host: os.Getenv("TEST_REDIS_HOST"),
26+
Port: port,
27+
}
28+
return c.BuildDsn()
2129
}
2230

2331
const key = "test_key"

0 commit comments

Comments
 (0)