apiary/honeypot/ssh/store/postgres_test.go

82 lines
1.5 KiB
Go
Raw Normal View History

// nolint: errcheck
2021-09-17 13:00:58 +00:00
package store_test
import (
"database/sql"
"os"
"testing"
2022-01-13 08:10:36 +00:00
"git.t-juice.club/torjus/apiary/honeypot/ssh/store"
2021-09-17 13:00:58 +00:00
)
func TestPostgresStore(t *testing.T) {
var dsn string
var found bool
dsn, found = os.LookupEnv("APIARY_TEST_POSTGRES_DSN")
if !found {
t.Skipf("APIARY_TEST_POSTGRES_DSN not set. Skipping.")
}
dropPGDatabase(dsn)
s, err := store.NewPostgresStore(dsn)
if err != nil {
t.Fatalf("Error getting store: %s", err)
}
s.InitDB()
testLoginAttemptStore(s, t)
}
2021-09-17 13:00:58 +00:00
func TestPostgresStoreWithCache(t *testing.T) {
var dsn string
var found bool
dsn, found = os.LookupEnv("APIARY_TEST_POSTGRES_DSN")
if !found {
t.Skipf("APIARY_TEST_POSTGRES_DSN not set. Skipping.")
}
dropPGDatabase(dsn)
pgs, err := store.NewPostgresStore(dsn)
if err != nil {
t.Fatalf("Error getting store: %s", err)
}
pgs.InitDB()
s := store.NewCachingStore(pgs)
testLoginAttemptStore(s, t)
}
2021-11-03 20:24:23 +00:00
func BenchmarkPostgresStore(b *testing.B) {
var dsn string
var found bool
dsn, found = os.LookupEnv("APIARY_TEST_POSTGRES_DSN")
if !found {
b.Skipf("APIARY_TEST_POSTGRES_DSN not set. Skipping.")
}
dropPGDatabase(dsn)
setupFunc := func() store.LoginAttemptStore {
dropPGDatabase(dsn)
pgs, err := store.NewPostgresStore(dsn)
if err != nil {
b.Fatalf("Error getting store: %s", err)
}
pgs.InitDB()
return pgs
}
benchmarkLoginAttemptStore(setupFunc, b)
dropPGDatabase(dsn)
}
2021-09-17 13:00:58 +00:00
func dropPGDatabase(dsn string) {
db, err := sql.Open("pgx", dsn)
if err != nil {
panic(err)
}
2021-11-03 20:24:23 +00:00
_, _ = db.Exec("DROP TABLE login_attempts")
2021-09-17 13:00:58 +00:00
}