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

Add DB trimming #174

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.19.3'
go-version: '1.23.0'
- name: Create dist
run: mkdir ./dist/
- name: Run Test & Build
Expand All @@ -39,7 +39,7 @@ jobs:
- uses: actions/checkout@master
- uses: actions/setup-go@v2
with:
go-version: '1.19.3'
go-version: '1.23.0'
- name: Create dist
run: mkdir ./dist/
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: '1.19.3'
go-version: '1.23.0'
- name: Create dist
run: mkdir ./dist/

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ jobs:

steps:
- uses: actions/checkout@master
- uses: docker/setup-buildx-action@v1
- run: make itest
- uses: docker/setup-buildx-action@v2
- name: Run Integration Test
run: make itest
2 changes: 1 addition & 1 deletion .github/workflows/release-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.19.3'
go-version: '1.23.0'
- name: Create dist
run: mkdir ./dist/
- name: Get the version
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19.3-alpine3.17 AS build-env
FROM golang:1.23.0-alpine3.20 AS build-env

RUN apk update && apk add bash make git

Expand All @@ -23,11 +23,12 @@ ADD controllers ./controllers
ADD migration ./migration
ADD storage ./storage
ADD dispatcher ./dispatcher
ADD utils ./utils

RUN make build
RUN make test

FROM alpine:3.17
FROM alpine:3.20
RUN apk update && apk add curl
WORKDIR /
COPY --from=build-env /go/src/github.com/newscred/webhook-broker/webhook-broker /webhook-broker
Expand Down
23 changes: 23 additions & 0 deletions config/cliconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path/filepath"
Expand All @@ -21,18 +22,40 @@ var (
errTruncatedConfigFile = errors.New("truncated config file")
)

// CommandOption represents the command to be run by the CLI
type CommandOption string

// CLIConfig represents the Command Line Args config
type CLIConfig struct {
ConfigPath string
MigrationSource string
StopOnConfigChange bool
DoNotWatchConfigChange bool
Command CommandOption
callbacks []func()
watcherStarted bool
watcherStarterMutex sync.Mutex
watcher *fsnotify.Watcher
}

const (
// BrokerCMD represents the broker command
BrokerCMD CommandOption = "broker"
// PruneCMD represents the prune command
PruneCMD CommandOption = "prune"
)

// SetCommandIfValid sets the command if it's valid
func (conf *CLIConfig) SetCommandIfValid(command string) error {
switch CommandOption(command) {
case BrokerCMD, PruneCMD:
conf.Command = CommandOption(command)
return nil
default:
return fmt.Errorf("unknown command: %s", command)
}
}

// IsMigrationEnabled returns whether migration is enabled
func (conf *CLIConfig) IsMigrationEnabled() bool {
return len(conf.MigrationSource) > 0
Expand Down
26 changes: 26 additions & 0 deletions config/cliconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -42,7 +43,17 @@ func writeToFile(filePath, content string) (err error) {
}

func TestCLIConfigPathChangeNotification(t *testing.T) {
osRelease, err := os.ReadFile("/proc/sys/kernel/osrelease")
if err != nil {
log.Fatal().Err(err).Msg("Error reading /proc/sys/kernel/osrelease")
}
osReleaseStr := string(osRelease)
isWSL2 := strings.Contains(osReleaseStr, "microsoft") && strings.Contains(osReleaseStr, "-microsoft")

t.Run("NotifiedOnFileChange", func(t *testing.T) {
if isWSL2 {
t.Skip("Skipping test on WSL2")
}
err := writeToFile(notificationFilePath, notificationInitialContent)
if err != nil {
log.Fatal().Err(err).Msg("could not write to file")
Expand All @@ -63,6 +74,9 @@ func TestCLIConfigPathChangeNotification(t *testing.T) {
wg.Wait()
})
t.Run("NoNotifyOnFileContentUnchanged", func(t *testing.T) {
if isWSL2 {
t.Skip("Skipping test on WSL2")
}
err := writeToFile(noChangeFilePath, notificationInitialContent)
if err != nil {
log.Fatal().Err(err).Msg("could not write to file")
Expand All @@ -83,6 +97,9 @@ func TestCLIConfigPathChangeNotification(t *testing.T) {
wg.Wait()
})
t.Run("NoNotifyOnFileTruncation", func(t *testing.T) {
if isWSL2 {
t.Skip("Skipping test on WSL2")
}
var buf bytes.Buffer
oldLogger := log.Logger
log.Logger = log.Output(&buf)
Expand Down Expand Up @@ -111,6 +128,9 @@ func TestCLIConfigPathChangeNotification(t *testing.T) {
assert.Contains(t, buf.String(), errTruncatedConfigFile.Error())
})
t.Run("NothingHappensOnFileRemoval", func(t *testing.T) {
if isWSL2 {
t.Skip("Skipping test on WSL2")
}
/*
Nothing will happen when the file is removed and the worker will stop,
this is primarily for code coverage nothing meaningful to assert other
Expand All @@ -137,6 +157,9 @@ func TestCLIConfigPathChangeNotification(t *testing.T) {
wg.Wait()
})
t.Run("NoFilePathTest", func(t *testing.T) {
if isWSL2 {
t.Skip("Skipping test on WSL2")
}
var buf bytes.Buffer
oldLogger := log.Logger
log.Logger = log.Output(&buf)
Expand All @@ -157,6 +180,9 @@ func TestCLIConfigPathChangeNotification(t *testing.T) {
assert.Contains(t, buf.String(), "could not find any file to watch")
})
t.Run("HashErrors", func(t *testing.T) {
if isWSL2 {
t.Skip("Skipping test on WSL2")
}
var buf bytes.Buffer
oldLogger := log.Logger
log.Logger = log.Output(&buf)
Expand Down
3 changes: 2 additions & 1 deletion controllers/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/newscred/webhook-broker/storage/data"
storagemocks "github.com/newscred/webhook-broker/storage/mocks"
"github.com/newscred/webhook-broker/utils"
"github.com/stretchr/testify/mock"
)

Expand Down Expand Up @@ -85,7 +86,7 @@ func TestNotifyOnInterrupt(t *testing.T) {
<-stop
wg.Done()
}()
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
utils.NewProcessKiller().Kill(syscall.Getpid(), syscall.SIGTERM)
if waitTimeout(&wg, 100*time.Millisecond) {
t.Fail()
}
Expand Down
2 changes: 1 addition & 1 deletion docs/developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The project build is straight forward using the `Makefile`; just doing `make` sh
1. GoLang 1.16.x
1. GCC with deps (required for SQLite driver compilation)

That should be it. Tested on _Ubuntu 20.04_ with _GoLang 1.19.3_. One of the development assumption (especially in tests) is no configuration file exists in the box in which test is being executed; which means only default configuration shipped with code is applicable.
That should be it. Tested on _Ubuntu 20.04_ with _GoLang 1.23.0_. One of the development assumption (especially in tests) is no configuration file exists in the box in which test is being executed; which means only default configuration shipped with code is applicable.

Generate Migration script using command as follows from project root -

Expand Down
43 changes: 22 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
module github.com/newscred/webhook-broker

go 1.19
go 1.23

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/fsnotify/fsnotify v1.6.0
github.com/fsnotify/fsnotify v1.7.0
github.com/go-ini/ini v1.67.0
github.com/go-sql-driver/mysql v1.7.0
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/google/wire v0.5.0
github.com/go-sql-driver/mysql v1.8.1
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/google/wire v0.6.0
github.com/julienschmidt/httprouter v1.3.0
github.com/mattn/go-sqlite3 v1.14.16
github.com/rs/xid v1.4.0
github.com/rs/zerolog v1.29.0
github.com/stretchr/testify v1.8.1
github.com/mattn/go-sqlite3 v1.14.22
github.com/rs/xid v1.6.0
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.8.3
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/chigopher/pathlib v0.12.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand All @@ -26,8 +27,8 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
Expand All @@ -43,16 +44,16 @@ require (
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/vektra/mockery/v2 v2.20.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/term v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/tools v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20221024183307-1bc688fe9f3e // indirect
google.golang.org/grpc v1.50.1 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/grpc v1.59.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading
Loading