Skip to content

Commit 16dabc4

Browse files
feat(dir): add integration tests for server
Signed-off-by: Bendegúz Csirmaz <csirmazbendeguz@gmail.com>
1 parent 2ac4944 commit 16dabc4

File tree

17 files changed

+1805
-19
lines changed

17 files changed

+1805
-19
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright AGNTCY Contributors (https://github.com/agntcy)
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
name: Test Integration
5+
6+
on:
7+
workflow_call:
8+
inputs:
9+
enable_coverage:
10+
required: false
11+
type: boolean
12+
default: false
13+
description: "Whether to collect and upload coverage as artifacts."
14+
15+
jobs:
16+
integration:
17+
name: Test ${{ matrix.label }}
18+
runs-on: ubuntu-latest
19+
strategy:
20+
matrix:
21+
include:
22+
- task: test:integration:server
23+
label: "Server"
24+
coverage_path: ".coverage/integration/server.out"
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
28+
with:
29+
fetch-depth: 0
30+
31+
- name: Login to ghcr.io
32+
uses: docker/login-action@b45d80f862d83dbcd57f89517bcf500b2ab88fb2 # v4.0.0
33+
with:
34+
registry: ghcr.io
35+
username: notused
36+
password: ${{ secrets.GITHUB_TOKEN }}
37+
38+
- name: Install Task
39+
uses: go-task/setup-task@3be4020d41929789a01026e0e427a4321ce0ad44 #v2.0.0
40+
41+
- name: Setup Cosign
42+
uses: sigstore/cosign-installer@ba7bc0a3fef59531c69a25acd34668d6d3fe6f22 # v4.1.0
43+
44+
- name: Setup Go
45+
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
46+
with:
47+
go-version: "1.26.1"
48+
cache-dependency-path: "**/*.sum"
49+
cache: true
50+
51+
- name: Run tests
52+
run: |
53+
COVERAGE_ENABLED="${{ inputs.enable_coverage }}" task ${{ matrix.task }}
54+
55+
- name: Upload coverage artifact
56+
if: ${{ inputs.enable_coverage }}
57+
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
58+
with:
59+
name: coverage-integration-${{ matrix.label }}
60+
path: ${{ matrix.coverage_path }}
61+
include-hidden-files: true
62+
retention-days: 1

.github/workflows/reusable-test.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ jobs:
5454
- name: Run unit tests
5555
run: |
5656
task test:unit
57+
58+
integration:
59+
name: Integration
60+
uses: ./.github/workflows/reusable-test-integration.yaml
61+
with:
62+
enable_coverage: true
63+
5764
sdk:
5865
name: SDK
5966
uses: ./.github/workflows/reusable-test-sdk.yaml

Taskfile.deps.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,12 @@ tasks:
325325
- python3 {{.PRE_COMMIT_PYZ}} install
326326
status:
327327
- test -d {{.PRE_COMMIT_PYZ}}
328+
329+
deps:ginkgo:
330+
desc: Install ginkgo
331+
run: once
332+
cmds:
333+
- GOBIN="{{ .BIN_DIR }}" go install github.com/onsi/ginkgo/v2/ginkgo@v{{ .GINKGO_VERSION }}
334+
- mv "{{ .BIN_DIR }}/ginkgo" "{{ .GINKGO_BIN }}"
335+
status:
336+
- test -f "{{ .GINKGO_BIN }}"

Taskfile.vars.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ vars:
8181
TRIVY_BIN: "{{ .BIN_DIR }}/trivy-{{.TRIVY_VERSION}}"
8282
PRE_COMMIT_VERSION: "4.5.1"
8383
PRE_COMMIT_PYZ: "{{ .BIN_DIR }}/pre-commit-{{.PRE_COMMIT_VERSION}}.pyz"
84+
GINKGO_VERSION: "2.28.1"
85+
GINKGO_BIN: "{{ .BIN_DIR }}/ginkgo-{{ .GINKGO_VERSION }}"
8486

8587
## Coverage related values
8688
COVERAGE_DIR: '{{ .COVERAGE_DIR | default (print .ROOT_DIR "/.coverage") }}'

server/database/database.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ func newSQLite(cfg config.SQLiteConfig) (*gormdb.DB, error) {
100100

101101
// newPostgres creates a new database connection using PostgreSQL driver.
102102
func newPostgres(cfg config.PostgresConfig) (*gormdb.DB, error) {
103+
db, err := NewPostgresGormDb(cfg)
104+
if err != nil {
105+
return nil, fmt.Errorf("failed to connect to PostgreSQL database: %w", err)
106+
}
107+
108+
gdb, err := gormdb.New(db)
109+
if err != nil {
110+
return nil, fmt.Errorf("failed to initialize PostgreSQL database: %w", err)
111+
}
112+
113+
return gdb, nil
114+
}
115+
116+
func NewPostgresGormDb(cfg config.PostgresConfig) (*gorm.DB, error) {
103117
host := cfg.Host
104118
if host == "" {
105119
host = config.DefaultPostgresHost
@@ -123,17 +137,7 @@ func newPostgres(cfg config.PostgresConfig) (*gormdb.DB, error) {
123137
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
124138
host, port, cfg.Username, cfg.Password, database, sslMode)
125139

126-
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
140+
return gorm.Open(postgres.Open(dsn), &gorm.Config{ //nolint:wrapcheck
127141
Logger: newCustomLogger(),
128142
})
129-
if err != nil {
130-
return nil, fmt.Errorf("failed to connect to PostgreSQL database: %w", err)
131-
}
132-
133-
gdb, err := gormdb.New(db)
134-
if err != nil {
135-
return nil, fmt.Errorf("failed to initialize PostgreSQL database: %w", err)
136-
}
137-
138-
return gdb, nil
139143
}

server/server.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func buildConnectionOptions(cfg config.ConnectionConfig) []grpc.ServerOption {
114114
func Run(ctx context.Context, cfg *config.Config) error {
115115
errCh := make(chan error)
116116

117-
server, err := New(ctx, cfg)
117+
server, err := New(ctx, cfg, nil)
118118
if err != nil {
119119
return fmt.Errorf("failed to create server: %w", err)
120120
}
@@ -153,7 +153,7 @@ func configureOASFValidation(cfg *config.Config) error {
153153
}
154154

155155
//nolint:cyclop // This function has been at the limit; refactoring is out of scope.
156-
func New(ctx context.Context, cfg *config.Config) (*Server, error) {
156+
func New(ctx context.Context, cfg *config.Config, databaseAPI types.DatabaseAPI) (*Server, error) {
157157
logger.Debug("Creating server with config", "config", cfg, "version", version.String())
158158

159159
if err := configureOASFValidation(cfg); err != nil {
@@ -227,9 +227,11 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) {
227227
return nil, fmt.Errorf("failed to create routing: %w", err)
228228
}
229229

230-
databaseAPI, err := database.New(cfg.Database)
231-
if err != nil {
232-
return nil, fmt.Errorf("failed to create database API: %w", err)
230+
if databaseAPI == nil {
231+
databaseAPI, err = database.New(cfg.Database)
232+
if err != nil {
233+
return nil, fmt.Errorf("failed to create database API: %w", err)
234+
}
233235
}
234236

235237
// Create JWT authentication service if enabled
@@ -317,6 +319,8 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) {
317319
}, nil
318320
}
319321

322+
func (s Server) GrpcServer() *grpc.Server { return s.grpcServer }
323+
320324
func (s Server) Options() types.APIOptions { return s.options }
321325

322326
func (s Server) Store() types.StoreAPI { return s.store }

tests/Taskfile.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,3 +589,26 @@ tasks:
589589
else
590590
echo "No pods directory found"
591591
fi
592+
593+
test:integration:server:
594+
desc: Run integration tests for the API server
595+
dir: "{{ .ROOT_DIR }}/tests/integration-server"
596+
deps:
597+
- task: deps:ginkgo
598+
vars:
599+
COVERAGE_ENABLED: '{{ .COVERAGE_ENABLED | default "false" }}'
600+
cmds:
601+
- defer: docker compose down -v
602+
- docker compose up -d --build --wait
603+
- |
604+
if [ "{{ .COVERAGE_ENABLED }}" = "true" ]; then
605+
mkdir -p "{{ .COVERAGE_DIR }}/integration"
606+
{{ .GINKGO_BIN }} --randomize-all \
607+
-p \
608+
-covermode="atomic" \
609+
-coverpkg="github.com/agntcy/dir/api/...,github.com/agntcy/dir/server/..." \
610+
--output-dir="{{ .COVERAGE_DIR }}/integration" \
611+
-coverprofile="server.out"
612+
else
613+
{{ .GINKGO_BIN }} --randomize-all -p
614+
fi

0 commit comments

Comments
 (0)