-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 664a7cf
Showing
38 changed files
with
2,154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module github.com/braumsmilk/go-auth | ||
|
||
go 1.22.0 | ||
|
||
require ( | ||
github.com/braumsmilk/go-log v0.1.0 | ||
github.com/braumsmilk/go-registry v0.1.0 | ||
github.com/go-redis/cache/v8 v8.4.4 | ||
github.com/go-redis/redis/v8 v8.11.5 | ||
github.com/google/uuid v1.6.0 | ||
github.com/jmoiron/sqlx v1.4.0 | ||
github.com/lib/pq v1.10.9 | ||
github.com/stretchr/testify v1.9.0 | ||
github.com/testcontainers/testcontainers-go v0.31.0 | ||
go.uber.org/zap v1.27.0 | ||
) | ||
|
||
require ( | ||
dario.cat/mergo v1.0.0 // indirect | ||
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect | ||
github.com/Microsoft/go-winio v0.6.1 // indirect | ||
github.com/Microsoft/hcsshim v0.11.4 // indirect | ||
github.com/cenkalti/backoff/v4 v4.2.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.2.0 // indirect | ||
github.com/containerd/containerd v1.7.15 // indirect | ||
github.com/containerd/log v0.1.0 // indirect | ||
github.com/cpuguy83/dockercfg v0.3.1 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect | ||
github.com/distribution/reference v0.5.0 // indirect | ||
github.com/docker/docker v25.0.5+incompatible // indirect | ||
github.com/docker/go-connections v0.5.0 // indirect | ||
github.com/docker/go-units v0.5.0 // indirect | ||
github.com/felixge/httpsnoop v1.0.4 // indirect | ||
github.com/go-logr/logr v1.4.1 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/go-ole/go-ole v1.2.6 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/protobuf v1.5.4 // indirect | ||
github.com/klauspost/compress v1.16.0 // indirect | ||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect | ||
github.com/magiconair/properties v1.8.7 // indirect | ||
github.com/moby/patternmatcher v0.6.0 // indirect | ||
github.com/moby/sys/sequential v0.5.0 // indirect | ||
github.com/moby/sys/user v0.1.0 // indirect | ||
github.com/moby/term v0.5.0 // indirect | ||
github.com/morikuni/aec v1.0.0 // indirect | ||
github.com/opencontainers/go-digest v1.0.0 // indirect | ||
github.com/opencontainers/image-spec v1.1.0 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect | ||
github.com/shirou/gopsutil/v3 v3.23.12 // indirect | ||
github.com/shoenig/go-m1cpu v0.1.6 // indirect | ||
github.com/sirupsen/logrus v1.9.3 // indirect | ||
github.com/tklauser/go-sysconf v0.3.12 // indirect | ||
github.com/tklauser/numcpus v0.6.1 // indirect | ||
github.com/vmihailenco/go-tinylfu v0.2.2 // indirect | ||
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect | ||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect | ||
github.com/yusufpapurcu/wmi v1.2.3 // indirect | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect | ||
go.opentelemetry.io/otel v1.24.0 // indirect | ||
go.opentelemetry.io/otel/metric v1.24.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.24.0 // indirect | ||
go.uber.org/multierr v1.10.0 // indirect | ||
golang.org/x/crypto v0.22.0 // indirect | ||
golang.org/x/mod v0.16.0 // indirect | ||
golang.org/x/sync v0.3.0 // indirect | ||
golang.org/x/sys v0.19.0 // indirect | ||
golang.org/x/tools v0.13.0 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731190214-cbb8c96f2d6d // indirect | ||
google.golang.org/grpc v1.58.3 // indirect | ||
google.golang.org/protobuf v1.33.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
BenchmarkContainerRepository_AddUserToContainer/creating_1000_users-12 3 345690687 ns/op 871000 B/op 21930 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/creating_1000_containers-12 3 334973202 ns/op 492616 B/op 13250 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/getting_all_users-12 1000000000 0.002086 ns/op 0 B/op 0 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/adding_all_users_to_a_container-12 1 1043769151 ns/op 1543528 B/op 24275 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/getting_all_users_in_container-12 1000000000 0.003753 ns/op 0 B/op 0 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/deleting_all_users-12 1000000000 0.1076 ns/op 0 B/op 0 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/getting_all_containers-12 1000000000 0.001761 ns/op 0 B/op 0 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/deleting_all_containers-12 1 2411660041 ns/op 1126104 B/op 35500 allocs/op | ||
PASS | ||
|
||
|
||
enchmarkContainerRepository_AddUserToContainer/creating_1000_users-12 10 102499424 ns/op 260663 B/op 6575 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/creating_1000_containers-12 3 395841802 ns/op 11642538 B/op 45960 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/getting_all_users-12 1000000000 0.004330 ns/op 0 B/op 0 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/adding_all_users_to_a_container-12 1000000000 0.2470 ns/op 0 B/op 0 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/getting_all_users_in_container-12 1000000000 0.0004446 ns/op 0 B/op 0 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/deleting_all_users-12 1000000000 0.1114 ns/op 0 B/op 0 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/getting_all_containers-12 1000000000 0.002422 ns/op 0 B/op 0 allocs/op | ||
BenchmarkContainerRepository_AddUserToContainer/deleting_all_containers-12 1 1951405461 ns/op 1126104 B/op 35500 allocs/op |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"log" | ||
"os" | ||
|
||
"github.com/braumsmilk/go-registry" | ||
"github.com/braumsmilk/go-auth/pg" | ||
"github.com/braumsmilk/go-auth/init/seed" | ||
) | ||
|
||
var host string | ||
var port int | ||
var user string | ||
var pw string | ||
var database string | ||
var configPath string | ||
|
||
func parseFlags() { | ||
flag.StringVar(&host, "host", "192.168.49.2", "host of postgres") | ||
flag.IntVar(&port, "port", 30040, "port of postgres") | ||
flag.StringVar(&user, "user", "user", "user of the postgres") | ||
flag.StringVar(&pw, "password", "user100", "password of user") | ||
flag.StringVar(&database, "db", "postgres", "database for postgres") | ||
flag.StringVar(&configPath, "config", "./registry.yaml", "path to a registry.yaml") | ||
flag.Parse() | ||
} | ||
|
||
func main() { | ||
parseFlags() | ||
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) { | ||
registry.Set(registry.Registry{ | ||
Postgres: ®istry.Server{ | ||
Host: host, | ||
Port: port, | ||
Auth: registry.Auth{ | ||
Username: user, | ||
Password: pw, | ||
}, | ||
}, | ||
}) | ||
} else { | ||
err = registry.Init(configPath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
postg := registry.Get().Postgres | ||
opts := pg.PostgresOptions{ | ||
Host: postg.Host, | ||
Port: postg.Port, | ||
User: postg.Auth.Username, | ||
DB: database, | ||
Pw: postg.Auth.Password, | ||
} | ||
log.Printf("user=%s, password=%s, port=%d, host=%s, database=%s", | ||
opts.User, opts.Pw, opts.Port, opts.Host, opts.DB) | ||
_, err := pg.Init(opts) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
seed.SeedPostgresDatabase() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package seed | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/braumsmilk/go-auth/pg" | ||
"github.com/braumsmilk/go-auth/pg/tables" | ||
) | ||
|
||
func SeedPostgresDatabase() { | ||
pg.Get().Exec(tables.GetDropTableStmt()) | ||
|
||
for _, stmt := range tables.GetCreateTableStmts() { | ||
log.Printf("%s", stmt) | ||
_, err := pg.Get().Exec(stmt) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
for _, stmt := range tables.GetCreateIndexStmts() { | ||
log.Printf("%s", stmt) | ||
_, err := pg.Get().Exec(stmt) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package containerv1 | ||
|
||
type Id int64 | ||
|
||
type ContainerType int32 | ||
|
||
var ( | ||
Group ContainerType = 0 | ||
Direct ContainerType = 1 | ||
Channel ContainerType = 2 | ||
) | ||
|
||
type Container struct { | ||
Id Id `db:"id"` | ||
Name string `db:"name"` | ||
ContainerType ContainerType `db:"container_type"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package integrationtest | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"testing" | ||
"time" | ||
|
||
"github.com/braumsmilk/go-auth/pg/containerv1" | ||
"github.com/braumsmilk/go-auth/pg/userv1" | ||
"github.com/braumsmilk/go-auth/testutils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var cr containerv1.ContainerRepository | ||
|
||
func TestMain(m *testing.M) { | ||
c := testutils.InitPostgres() | ||
defer func() { | ||
log.Printf("stopping container") | ||
timeout := time.Second * 30 | ||
err := c.Stop(context.Background(), &timeout) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}() | ||
cr = &containerv1.PostgresRepository{} | ||
|
||
m.Run() | ||
} | ||
|
||
func TestContainerRepository(t *testing.T) { | ||
ctx := context.Background() | ||
userr := userv1.PostgresRepository{} | ||
|
||
uid, err := userr.CreateUser(ctx, "user", "name", "pw") | ||
assert.Nilf(t, err, "should not error when creating user") | ||
|
||
cid, err := cr.CreateContainer(ctx, "test", containerv1.Channel) | ||
assert.Nilf(t, err, "should not error when creating container") | ||
|
||
err = cr.AddUserToContainer(ctx, uid, cid) | ||
assert.Nilf(t, err, "should not error when adding user to container") | ||
|
||
users, err := cr.GetUsersInContainer(ctx, cid, 0, 10) | ||
assert.Nilf(t, err, "should not error when getting users in container") | ||
assert.Lenf(t, users, 1, "should only have one user in container") | ||
|
||
containers, err := cr.GetJoinedContainers(ctx, uid) | ||
assert.Nilf(t, err, "should not fail to get user joined containers") | ||
assert.Lenf(t, containers, 1, "user has only joined one container") | ||
|
||
err = cr.RemoveUserFromContainer(ctx, uid, cid) | ||
assert.Nilf(t, err, "should not fail to remove user from container") | ||
|
||
users, err = cr.GetUsersInContainer(ctx, cid, 0, 10) | ||
assert.Nilf(t, err, "should not fail to get users in container") | ||
assert.Lenf(t, users, 0, "should not have any users in container") | ||
|
||
err = userr.DeleteUser(ctx, uid) | ||
assert.Nilf(t, err, "should not error when deleting user") | ||
|
||
users, err = cr.GetUsersInContainer(ctx, cid, 0, 10) | ||
assert.Nilf(t, err, "should not error when getting users in container") | ||
assert.Lenf(t, users, 0, "should not have any users in container") | ||
|
||
ctrs, err := cr.GetAllContainers(ctx) | ||
assert.Nilf(t, err, "should not error when getting all containers") | ||
assert.Lenf(t, ctrs, 1, "should only have one containers") | ||
|
||
err = cr.DeleteContainer(ctx, cid) | ||
assert.Nilf(t, err, "should not error when deleting container") | ||
|
||
ctrs, err = cr.GetAllContainers(ctx) | ||
assert.Nilf(t, err, "should not error when getting all containers") | ||
assert.Lenf(t, ctrs, 0, "should not have any containers") | ||
|
||
c, err := cr.GetContainer(ctx, cid) | ||
assert.Nilf(t, c, "should not have gotten a container") | ||
assert.Nilf(t, err, "should not get error when no containers are found") | ||
|
||
_, err = cr.CreateContainer(ctx, "new", containerv1.Channel) | ||
assert.Nilf(t, err, "should not fail to create 'new' container") | ||
|
||
qc, err := cr.QueryContainers(ctx, "ne") | ||
assert.Nilf(t, err, "should not fail to query containers") | ||
assert.Lenf(t, qc, 1, "should get one container when querying containers") | ||
} | ||
|
||
func BenchmarkContainerRepository_AddUserToContainer(t *testing.B) { | ||
usersN := 1000 | ||
containerN := 1000 | ||
ctx := context.Background() | ||
userr := userv1.PostgresRepository{} | ||
var err error | ||
t.Run(fmt.Sprintf("creating %d users", usersN), func(b *testing.B) { | ||
for i := 0; i < usersN; i++ { | ||
_, err = userr.CreateUser(ctx, fmt.Sprintf("user-%d", i), "name", "aaa") | ||
assert.Nilf(t, err, "should not error when creating user") | ||
} | ||
}) | ||
|
||
var cid containerv1.Id | ||
t.Run(fmt.Sprintf("creating %d containers", containerN), func(b *testing.B) { | ||
for i := 0; i < containerN; i++ { | ||
cid, err = cr.CreateContainer(ctx, fmt.Sprintf("tester-container-%d", i), containerv1.Channel) | ||
assert.Nilf(t, err, "should not error when creating container") | ||
} | ||
}) | ||
|
||
var users []userv1.User | ||
t.Run("getting all users", func(b *testing.B) { | ||
users, err = userr.GetAllUsers(ctx, 0, usersN) | ||
assert.Nilf(t, err, "should not error when getting all users") | ||
assert.Lenf(t, users, usersN, "should have gotten all users") | ||
}) | ||
|
||
t.Run("adding all users to a container", func(b *testing.B) { | ||
for _, u := range users { | ||
t.Logf("adding user %d", u.UserId) | ||
err = cr.AddUserToContainer(ctx, u.UserId, cid) | ||
assert.Nilf(t, err, "should not error when adding user to container") | ||
} | ||
}) | ||
|
||
t.Run("getting all users in container", func(b *testing.B) { | ||
usersInContainer, err := cr.GetUsersInContainer(ctx, cid, 0, usersN) | ||
assert.Nilf(t, err, "should not error when getting users in container") | ||
assert.Lenf(t, usersInContainer, usersN, "should have all the users in this container") | ||
}) | ||
|
||
t.Run("deleting all users", func(b *testing.B) { | ||
for _, u := range users { | ||
err := userr.DeleteUser(ctx, u.UserId) | ||
assert.Nilf(t, err, "should not error when deleting user") | ||
} | ||
}) | ||
|
||
var containers []containerv1.Container | ||
t.Run("getting all containers", func(b *testing.B) { | ||
containers, err = cr.GetAllContainers(ctx) | ||
assert.Nilf(t, err, "should not error when getting all containers") | ||
}) | ||
|
||
t.Run("deleting all containers", func(b *testing.B) { | ||
for _, c := range containers { | ||
assert.Nilf(t, cr.DeleteContainer(ctx, c.Id), "should not error when deleting container") | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkContainerRepository_createContainer(t *testing.B) { | ||
ctx := context.Background() | ||
containerN := 1000 | ||
var ids []containerv1.Id = []containerv1.Id{} | ||
var err error | ||
t.Run(fmt.Sprintf("create %d container", containerN), func(b *testing.B) { | ||
for i := 0; i < containerN; i++ { | ||
id, err := cr.CreateContainer(ctx, "test", containerv1.Channel) | ||
assert.Nilf(b, err, "should not error when creating a container") | ||
ids = append(ids, id) | ||
} | ||
}) | ||
|
||
var c *containerv1.Container | ||
t.Run(fmt.Sprintf("get %d containers", containerN), func(b *testing.B) { | ||
for _, id := range ids { | ||
c, err = cr.GetContainer(ctx, id) | ||
assert.Nilf(t, err, "should not error when getting container") | ||
assert.NotNilf(t, c, "should not have gotten a nil container") | ||
assert.Equalf(t, "test", c.Name, "should have gotten the right container") | ||
} | ||
}) | ||
|
||
t.Run(fmt.Sprintf("delete %d containers", containerN), func(b *testing.B) { | ||
for _, id := range ids { | ||
err = cr.DeleteContainer(ctx, id) | ||
assert.Nilf(t, err, "should not error deleting a container") | ||
} | ||
}) | ||
|
||
} |
Oops, something went wrong.