feat: initial commit
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
This commit is contained in:
9
internal/datastore/error.go
Normal file
9
internal/datastore/error.go
Normal file
@ -0,0 +1,9 @@
|
||||
package datastore
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrAlreadyExist = errors.New("already exist")
|
||||
ErrUnexpectedRevision = errors.New("unexpected revision")
|
||||
)
|
33
internal/datastore/proxy_repository.go
Normal file
33
internal/datastore/proxy_repository.go
Normal file
@ -0,0 +1,33 @@
|
||||
package datastore
|
||||
|
||||
type ProxyRepository interface{}
|
||||
|
||||
type InboundID string
|
||||
|
||||
type Inbound struct {
|
||||
ID InboundID
|
||||
Name string
|
||||
Matchers []InboundMatcherID
|
||||
Outbound OutboundID
|
||||
}
|
||||
|
||||
type InboundMatcherID string
|
||||
|
||||
type InboundMatcherType string
|
||||
|
||||
type InboundRuleMatcher struct {
|
||||
ID InboundMatcherID
|
||||
Type InboundMatcherType
|
||||
Options map[string]any
|
||||
}
|
||||
|
||||
type OutboundID string
|
||||
|
||||
type Outbound struct {
|
||||
ID OutboundID
|
||||
Middlewares []MiddlewareID
|
||||
}
|
||||
|
||||
type MiddlewareID string
|
||||
|
||||
type Middleware struct{}
|
19
internal/datastore/sqlite/agent_repository.go
Normal file
19
internal/datastore/sqlite/agent_repository.go
Normal file
@ -0,0 +1,19 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/datastore"
|
||||
)
|
||||
|
||||
type ProxyRepository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewProxyRepository(db *sql.DB) *ProxyRepository {
|
||||
return &ProxyRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
var _ datastore.ProxyRepository = &ProxyRepository{}
|
42
internal/datastore/sqlite/json.go
Normal file
42
internal/datastore/sqlite/json.go
Normal file
@ -0,0 +1,42 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type JSONMap map[string]any
|
||||
|
||||
func (j *JSONMap) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var data []byte
|
||||
|
||||
switch typ := value.(type) {
|
||||
case []byte:
|
||||
data = typ
|
||||
case string:
|
||||
data = []byte(typ)
|
||||
default:
|
||||
return errors.Errorf("unexpected type '%T'", value)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &j); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (j JSONMap) Value() (driver.Value, error) {
|
||||
data, err := json.Marshal(j)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
46
internal/datastore/sqlite/proxy_repository_test.go
Normal file
46
internal/datastore/sqlite/proxy_repository_test.go
Normal file
@ -0,0 +1,46 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/datastore/testsuite"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/migrate"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func TestSQLiteAgentRepository(t *testing.T) {
|
||||
logger.SetLevel(logger.LevelDebug)
|
||||
|
||||
file := "testdata/agent_repository_test.sqlite"
|
||||
|
||||
if err := os.Remove(file); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("%+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf("%s?_pragma=foreign_keys(1)&_pragma=busy_timeout=%d", file, (60 * time.Second).Milliseconds())
|
||||
|
||||
migr, err := migrate.New("../../../migrations", "sqlite", "sqlite://"+dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("%+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
if err := migr.Up(); err != nil {
|
||||
t.Fatalf("%+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("%+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
repo := NewProxyRepository(db)
|
||||
|
||||
testsuite.TestProxyRepository(t, repo)
|
||||
}
|
1
internal/datastore/sqlite/testdata/.gitignore
vendored
Normal file
1
internal/datastore/sqlite/testdata/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.sqlite*
|
14
internal/datastore/testsuite/proxy_repository.go
Normal file
14
internal/datastore/testsuite/proxy_repository.go
Normal file
@ -0,0 +1,14 @@
|
||||
package testsuite
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/datastore"
|
||||
)
|
||||
|
||||
func TestProxyRepository(t *testing.T, repo datastore.ProxyRepository) {
|
||||
t.Run("Cases", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
runProxyRepositoryTests(t, repo)
|
||||
})
|
||||
}
|
46
internal/datastore/testsuite/proxy_repository_cases.go
Normal file
46
internal/datastore/testsuite/proxy_repository_cases.go
Normal file
@ -0,0 +1,46 @@
|
||||
package testsuite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/datastore"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type proxyRepositoryTestCase struct {
|
||||
Name string
|
||||
Skip bool
|
||||
Run func(ctx context.Context, repo datastore.ProxyRepository) error
|
||||
}
|
||||
|
||||
var proxyRepositoryTestCases = []proxyRepositoryTestCase{
|
||||
{
|
||||
Name: "Create a new agent",
|
||||
Run: func(ctx context.Context, repo datastore.ProxyRepository) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func runProxyRepositoryTests(t *testing.T, repo datastore.ProxyRepository) {
|
||||
for _, tc := range proxyRepositoryTestCases {
|
||||
func(tc proxyRepositoryTestCase) {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if tc.Skip {
|
||||
t.SkipNow()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
if err := tc.Run(ctx, repo); err != nil {
|
||||
t.Errorf("%+v", errors.WithStack(err))
|
||||
}
|
||||
})
|
||||
}(tc)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user