Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(database): add integration testing #896

Merged
merged 98 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
74626c9
Merge branch 'main' of github.com:go-vela/server
jbrockopp Feb 14, 2023
974c8e6
Merge branch 'main' of github.com:go-vela/server
jbrockopp Mar 3, 2023
2f2c425
Merge branch 'main' of github.com:go-vela/server
jbrockopp Mar 20, 2023
528291a
Merge branch 'main' of github.com:go-vela/server
jbrockopp Apr 8, 2023
b53c687
Merge branch 'main' of github.com:go-vela/server
jbrockopp Apr 8, 2023
4734dcb
Merge branch 'main' of github.com:go-vela/server
jbrockopp Apr 16, 2023
1fb52df
Merge branch 'main' of github.com:go-vela/server
jbrockopp Apr 21, 2023
e996aa6
Merge branch 'main' of github.com:go-vela/server
jbrockopp Apr 27, 2023
c299ee4
Merge branch 'main' of github.com:go-vela/server
jbrockopp May 11, 2023
c8da9e3
Merge branch 'main' of github.com:go-vela/server
jbrockopp May 16, 2023
1ee254f
Merge branch 'main' of github.com:go-vela/server
jbrockopp May 22, 2023
8dd6033
Merge branch 'main' of github.com:go-vela/server
jbrockopp May 22, 2023
0eb92b1
Merge branch 'main' of github.com:go-vela/server
jbrockopp May 26, 2023
d5dcb6d
Merge branch 'main' of github.com:go-vela/server
jbrockopp Jun 1, 2023
be8dd9a
Merge branch 'main' of github.com:go-vela/server
jbrockopp Jun 5, 2023
73893b5
Merge branch 'main' of github.com:go-vela/server
jbrockopp Jun 7, 2023
355017b
Merge branch 'main' of github.com:go-vela/server
jbrockopp Jun 8, 2023
3e17278
Merge branch 'main' of github.com:go-vela/server
jbrockopp Jun 8, 2023
d3c1e06
Merge branch 'main' of github.com:go-vela/server
jbrockopp Jun 12, 2023
d63abfe
Merge branch 'main' of github.com:go-vela/server
jbrockopp Jun 17, 2023
a353552
Merge branch 'main' of github.com:go-vela/server
jbrockopp Jun 21, 2023
f1a8ab6
Merge branch 'main' of github.com:go-vela/server
jbrockopp Jun 26, 2023
c23c599
Merge branch 'main' of github.com:go-vela/server
jbrockopp Jun 28, 2023
683248d
feat(database): add integration testing
jbrockopp Jun 28, 2023
e2f9307
feat(actions): add integration-test workflow
jbrockopp Jun 28, 2023
b910bfc
feat(database): add integration tests
jbrockopp Jun 28, 2023
09fd217
fix: use env var to skip integration tests
jbrockopp Jun 28, 2023
643f27a
fix: password for integration tests
jbrockopp Jun 28, 2023
ffca1a2
test: comment out paths for prs
jbrockopp Jun 28, 2023
4f57c94
revert: comment out paths for prs
jbrockopp Jun 28, 2023
7bfb032
enhance(database): add worker integration tests
jbrockopp Jun 28, 2023
aa52426
fix(database): timestamps for worker tests
jbrockopp Jun 28, 2023
23a9c59
chore: save work
jbrockopp Jun 28, 2023
1dcb17b
enhance(database): add method counter
jbrockopp Jun 28, 2023
77eb03e
fix(database): subtract 2 from methods
jbrockopp Jun 28, 2023
ad25372
enhance(database): add user integration tests
jbrockopp Jun 29, 2023
b218eea
chore: address review feedback
jbrockopp Jun 29, 2023
d99f960
fix(database): names for users
jbrockopp Jun 29, 2023
4a0601a
enhance(database): add step integration tests
jbrockopp Jun 29, 2023
541d2e3
fix: typo
jbrockopp Jun 29, 2023
43cc062
test: output diff
jbrockopp Jun 29, 2023
cd8f56e
chore: minor fixes
jbrockopp Jun 29, 2023
111916c
fix(database): users
jbrockopp Jun 29, 2023
a6d0dcc
fix(database): steps
jbrockopp Jun 29, 2023
c1f92cc
fix: typo
jbrockopp Jun 29, 2023
3a4ef4d
fix(database): steps
jbrockopp Jun 29, 2023
6384b5e
feat(database): add services
jbrockopp Jun 29, 2023
c34dcb3
feat(database): add builds
jbrockopp Jun 29, 2023
d6912ca
fix(database): users
jbrockopp Jun 29, 2023
18a68fe
fix(database): integration tests
jbrockopp Jun 29, 2023
63d6b19
feat(database): add hooks
jbrockopp Jun 29, 2023
eddc018
feat(database): add logs
jbrockopp Jun 29, 2023
39ed3ff
feat(database): add pipelines
jbrockopp Jun 29, 2023
d40a96d
feat(database): add repos
jbrockopp Jun 29, 2023
29881ac
feat(database): add schedules
jbrockopp Jun 29, 2023
263b1df
feat(database): add secrets
jbrockopp Jun 29, 2023
ada878b
Merge branch 'main' into hackathon/integration
jbrockopp Jun 29, 2023
69cf142
Merge branch 'hackathon/integration' of github.com:go-vela/server int…
jbrockopp Jun 29, 2023
d1a6232
fix(database): integration tests
jbrockopp Jun 29, 2023
f50a30d
fix(database): integration tests
jbrockopp Jun 29, 2023
e9766d4
fix(database: fix logs
jbrockopp Jun 29, 2023
0603090
fix(database: fix pipelines
jbrockopp Jun 29, 2023
32b076d
fix(database): repos
jbrockopp Jun 29, 2023
8735c04
fix(database): integration tests
jbrockopp Jun 29, 2023
94f66eb
fix(database): integration tests
jbrockopp Jun 29, 2023
dd27785
fix(database): minus one
jbrockopp Jun 29, 2023
e0e82b1
chore: cleanup
jbrockopp Jun 30, 2023
b4f16a2
chore: misc fixes
jbrockopp Jun 30, 2023
3f841bc
fix(database): user integration test
jbrockopp Jun 30, 2023
a02f2fd
fix(database): build integration tests
jbrockopp Jun 30, 2023
b432a66
fix(database): build integration tests
jbrockopp Jun 30, 2023
db6e2f0
fix(database): integration tests
jbrockopp Jun 30, 2023
ac0d699
fix(database): secrets integration tests
jbrockopp Jun 30, 2023
ad9c574
fix(database): integration tests
jbrockopp Jun 30, 2023
5a56952
fix(database): integration tests
jbrockopp Jun 30, 2023
f42085f
fix(database): secrets integration tests
jbrockopp Jul 3, 2023
ea00645
test: troubleshoot build failures
jbrockopp Jul 3, 2023
aaea5aa
fix(database): integration tests
jbrockopp Jul 3, 2023
88e0d5c
fix(database): integration tests
jbrockopp Jul 3, 2023
e168b62
fix(database): integration tests
jbrockopp Jul 3, 2023
c226c03
feat(database): add sqlite integration tests
jbrockopp Jul 3, 2023
78c8ac4
fix(database): sqlite integration tests
jbrockopp Jul 3, 2023
2f3f7fe
Merge branch 'main' into hackathon/integration
jbrockopp Jul 10, 2023
836279c
Merge branch 'main' into hackathon/integration
jbrockopp Jul 11, 2023
9a644d8
Merge branch 'hackathon/integration' of github.com:go-vela/server int…
jbrockopp Jul 11, 2023
95ed34b
fix(database/hook): unique constraints for CreateSqliteTable
jbrockopp Jul 11, 2023
387eed0
fix(database/logs): consistency between drivers
jbrockopp Jul 11, 2023
33b20e7
Merge branch 'main' into hackathon/integration
jbrockopp Jul 24, 2023
f6a16de
Merge branch 'main' into hackathon/integration
jbrockopp Jul 25, 2023
8581d6d
Merge branch 'hackathon/integration' of github.com:go-vela/server int…
jbrockopp Jul 25, 2023
ea17b66
fix: issues from merge
jbrockopp Jul 25, 2023
3ed7f45
chore: add missing comment
jbrockopp Jul 25, 2023
95b534d
Merge branch 'main' into hackathon/integration
jbrockopp Jul 25, 2023
8e3a77d
Merge branch 'main' into hackathon/integration
jbrockopp Jul 27, 2023
09fa268
Merge branch 'hackathon/integration' of github.com:go-vela/server int…
jbrockopp Jul 28, 2023
221cb08
chore: address review feedback
jbrockopp Jul 28, 2023
6b67568
chore: remove unneeded file
jbrockopp Jul 28, 2023
061ff80
chore: undo comments
jbrockopp Jul 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# name of the action
name: integration-test

# trigger on pull_request events that modify this file or any database files
on:
pull_request:
paths:
- '.github/workflows/integration-test.yml'
- 'database/**'

# pipeline to execute
jobs:
database:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:15-alpine
env:
POSTGRES_DB: vela
POSTGRES_PASSWORD: notARealPassword12345
POSTGRES_USER: vela
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- name: clone
uses: actions/checkout@v3

- name: install go
uses: actions/setup-go@v4
with:
# use version from go.mod file
go-version-file: 'go.mod'
cache: true
check-latest: true

- name: test
run: |
make integration-test
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:

- name: test
run: |
go test -race -covermode=atomic -coverprofile=coverage.out ./...
make test

- name: coverage
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: coverage.out
file: coverage.out
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ fix:
@echo "### Fixing Go Code"
@go fix ./...

# The `integration-test` target is intended to run all integration tests for the Go source code.
.PHONY: integration-test
integration-test:
@echo
@echo "### Integration Testing"
INTEGRATION=1 go test -run TestDatabase_Integration ./...

# The `test` target is intended to run
# the tests for the Go source code.
#
Expand All @@ -99,18 +106,15 @@ fix:
test:
@echo
@echo "### Testing Go Code"
@go test -race ./...
@go test -race -covermode=atomic -coverprofile=coverage.out ./...

# The `test-cover` target is intended to run
# the tests for the Go source code and then
# open the test coverage report.
#
# Usage: `make test-cover`
.PHONY: test-cover
test-cover:
@echo
@echo "### Creating test coverage report"
@go test -race -covermode=atomic -coverprofile=coverage.out ./...
test-cover: test
@echo
@echo "### Opening test coverage report"
@go tool cover -html=coverage.out
Expand Down
198 changes: 198 additions & 0 deletions database/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package database

import (
"os"
"reflect"
"strings"
"testing"
"time"

"github.com/go-vela/server/database/worker"

"github.com/go-vela/types/library"
)

func TestDatabase_Integration(t *testing.T) {
// check if we should skip the integration test
if os.Getenv("INTEGRATION") == "" {
jbrockopp marked this conversation as resolved.
Show resolved Hide resolved
t.Skipf("skipping %s integration test due to environment variable constraint", t.Name())
}

// setup tests
tests := []struct {
failure bool
name string
config *config
}{
{
name: "success with postgres",
failure: false,
config: &config{
Driver: "postgres",
Address: "postgres://vela:notARealPassword12345@localhost:5432/vela",
jbrockopp marked this conversation as resolved.
Show resolved Hide resolved
CompressionLevel: 3,
ConnectionLife: 10 * time.Second,
ConnectionIdle: 5,
ConnectionOpen: 20,
EncryptionKey: "A1B2C3D4E5G6H7I8J9K0LMNOPQRSTUVW",
SkipCreation: false,
},
},
}

// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
db, err := New(
WithAddress(test.config.Address),
WithCompressionLevel(test.config.CompressionLevel),
WithConnectionLife(test.config.ConnectionLife),
WithConnectionIdle(test.config.ConnectionIdle),
WithConnectionOpen(test.config.ConnectionOpen),
WithDriver(test.config.Driver),
WithEncryptionKey(test.config.EncryptionKey),
WithSkipCreation(test.config.SkipCreation),
)
if err != nil {
t.Errorf("unable to create new database engine for %s: %v", test.name, err)
}

driver := db.Driver()
if !strings.EqualFold(driver, test.config.Driver) {
t.Errorf("Driver() is %v, want %v", driver, test.config.Driver)
}

err = db.Ping()
if err != nil {
t.Errorf("unable to ping database engine for %s: %v", test.name, err)
}

t.Run("test_workers", func(t *testing.T) {
testWorkers(t, db)
})
jbrockopp marked this conversation as resolved.
Show resolved Hide resolved

err = db.Close()
if err != nil {
t.Errorf("unable to close database engine for %s: %v", test.name, err)
}
})
}
}

func testWorkers(t *testing.T, db Interface) {
// used to track the number of methods we call for workers
counter := 0
jbrockopp marked this conversation as resolved.
Show resolved Hide resolved

one := new(library.Worker)
one.SetID(1)
one.SetHostname("worker-1.example.com")
one.SetAddress("https://worker-1.example.com")
one.SetRoutes([]string{"vela"})
one.SetActive(true)
one.SetStatus("available")
one.SetLastStatusUpdateAt(time.Now().UTC().Unix())
one.SetRunningBuildIDs([]string{"12345"})
one.SetLastBuildStartedAt(time.Now().UTC().Unix())
one.SetLastBuildFinishedAt(time.Now().UTC().Unix())
one.SetLastCheckedIn(time.Now().UTC().Unix())
one.SetBuildLimit(1)

two := new(library.Worker)
two.SetID(2)
two.SetHostname("worker-2.example.com")
two.SetAddress("https://worker-2.example.com")
two.SetRoutes([]string{"vela"})
two.SetActive(true)
two.SetStatus("available")
two.SetLastStatusUpdateAt(time.Now().UTC().Unix())
two.SetRunningBuildIDs([]string{"12345"})
two.SetLastBuildStartedAt(time.Now().UTC().Unix())
two.SetLastBuildFinishedAt(time.Now().UTC().Unix())
two.SetLastCheckedIn(time.Now().UTC().Unix())
two.SetBuildLimit(1)

workers := []*library.Worker{one, two}

// create the workers
for _, worker := range workers {
err := db.CreateWorker(worker)
if err != nil {
t.Errorf("unable to create worker %s: %v", worker.GetHostname(), err)
}
}
counter++

// count the workers
count, err := db.CountWorkers()
if err != nil {
t.Errorf("unable to count workers: %v", err)
}
if int(count) != len(workers) {
t.Errorf("CountWorkers() is %v, want 2", count)
}
counter++

// list the workers
list, err := db.ListWorkers()
if err != nil {
t.Errorf("unable to list workers: %v", err)
}
if !reflect.DeepEqual(list, workers) {
t.Errorf("ListWorkers() is %v, want %v", list, workers)
}
counter++

// lookup the workers by hostname
for _, worker := range workers {
got, err := db.GetWorkerForHostname(worker.GetHostname())
if err != nil {
t.Errorf("unable to get worker %s by hostname: %v", worker.GetHostname(), err)
}
if !reflect.DeepEqual(got, worker) {
t.Errorf("GetWorkerForHostname() is %v, want %v", got, worker)
}
}
counter++

// update the workers
for _, worker := range workers {
worker.SetActive(false)
err = db.UpdateWorker(worker)
if err != nil {
t.Errorf("unable to update worker %s: %v", worker.GetHostname(), err)
}

// lookup the worker by ID
got, err := db.GetWorker(worker.GetID())
if err != nil {
t.Errorf("unable to get worker %s by ID: %v", worker.GetHostname(), err)
}
if !reflect.DeepEqual(got, worker) {
t.Errorf("GetWorker() is %v, want %v", got, worker)
}
}
counter++
counter++

// delete the workers
for _, worker := range workers {
err = db.DeleteWorker(worker)
if err != nil {
t.Errorf("unable to delete worker %s: %v", worker.GetHostname(), err)
}
}
counter++

// ensure we called all the functions we should have
//
// we subtract 2 for creating the table and indexes for workers
// since those are already called when the database engine starts
methods := reflect.TypeOf(new(worker.WorkerInterface)).Elem().NumMethod() - 2
if counter != methods {
t.Errorf("total number of methods called is %v, want %v", counter, methods)
}
}
Loading