Julien Riou
8709ee542b
- Add filters to include and exclude strings - Use filters to include and exclude sessions (user and databases supported) - Add tests to filters and terminator Signed-off-by: Julien Riou <julien@riou.xyz>
144 lines
3.4 KiB
Go
144 lines
3.4 KiB
Go
package terminator
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/jouir/pgterminate/base"
|
|
)
|
|
|
|
func TestFilterUsers(t *testing.T) {
|
|
|
|
sessions := []*base.Session{
|
|
{User: "test"},
|
|
{User: "test_1"},
|
|
{User: "test_2"},
|
|
{User: "postgres"},
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
config *base.Config
|
|
want []*base.Session
|
|
}{
|
|
{
|
|
"No filter",
|
|
&base.Config{},
|
|
sessions,
|
|
},
|
|
{
|
|
"Include a single user",
|
|
&base.Config{IncludeUsers: []string{"test"}},
|
|
[]*base.Session{{User: "test"}},
|
|
},
|
|
{
|
|
"Include multiple users",
|
|
&base.Config{IncludeUsers: []string{"test_1", "test_2"}},
|
|
[]*base.Session{{User: "test_1"}, {User: "test_2"}},
|
|
},
|
|
{
|
|
"Exclude a single user",
|
|
&base.Config{ExcludeUsers: []string{"test"}},
|
|
[]*base.Session{{User: "test_1"}, {User: "test_2"}, {User: "postgres"}},
|
|
},
|
|
{
|
|
"Exclude multiple users",
|
|
&base.Config{ExcludeUsers: []string{"test_1", "test_2"}},
|
|
[]*base.Session{{User: "test"}, {User: "postgres"}},
|
|
},
|
|
{
|
|
"Include multiple users and exclude one",
|
|
&base.Config{IncludeUsers: []string{"test", "test_1", "test_2"}, ExcludeUsers: []string{"test"}},
|
|
[]*base.Session{{User: "test_1"}, {User: "test_2"}},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
tc.config.CompileFilters()
|
|
terminator := &Terminator{config: tc.config}
|
|
got := terminator.filterUsers(sessions)
|
|
if !reflect.DeepEqual(got, tc.want) {
|
|
t.Errorf("got %+v; want %+v", ListUsers(got), ListUsers(tc.want))
|
|
} else {
|
|
t.Logf("Success")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// ListUsers extract usernames from a list of sessions
|
|
func ListUsers(sessions []*base.Session) (users []string) {
|
|
for _, session := range sessions {
|
|
users = append(users, session.User)
|
|
}
|
|
return users
|
|
}
|
|
|
|
func TestFilterDatabases(t *testing.T) {
|
|
|
|
sessions := []*base.Session{
|
|
{Db: "test"},
|
|
{Db: "test_1"},
|
|
{Db: "test_2"},
|
|
{Db: "postgres"},
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
config *base.Config
|
|
want []*base.Session
|
|
}{
|
|
{
|
|
"No filter",
|
|
&base.Config{},
|
|
sessions,
|
|
},
|
|
{
|
|
"Include a single database",
|
|
&base.Config{IncludeDatabases: []string{"test"}},
|
|
[]*base.Session{{Db: "test"}},
|
|
},
|
|
{
|
|
"Include multiple databases",
|
|
&base.Config{IncludeDatabases: []string{"test_1", "test_2"}},
|
|
[]*base.Session{{Db: "test_1"}, {Db: "test_2"}},
|
|
},
|
|
{
|
|
"Exclude a single database",
|
|
&base.Config{ExcludeDatabases: []string{"test"}},
|
|
[]*base.Session{{Db: "test_1"}, {Db: "test_2"}, {Db: "postgres"}},
|
|
},
|
|
{
|
|
"Exclude multiple databases",
|
|
&base.Config{ExcludeDatabases: []string{"test_1", "test_2"}},
|
|
[]*base.Session{{Db: "test"}, {Db: "postgres"}},
|
|
},
|
|
{
|
|
"Include multiple databases and exclude one",
|
|
&base.Config{IncludeDatabases: []string{"test", "test_1", "test_2"}, ExcludeDatabases: []string{"test"}},
|
|
[]*base.Session{{Db: "test_1"}, {Db: "test_2"}},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
tc.config.CompileFilters()
|
|
terminator := &Terminator{config: tc.config}
|
|
got := terminator.filterDatabases(sessions)
|
|
if !reflect.DeepEqual(got, tc.want) {
|
|
t.Errorf("got %+v; want %+v", ListDatabases(got), ListDatabases(tc.want))
|
|
} else {
|
|
t.Logf("Success")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// ListDatabases extract usernames from a list of sessions
|
|
func ListDatabases(sessions []*base.Session) (databases []string) {
|
|
for _, session := range sessions {
|
|
databases = append(databases, session.Db)
|
|
}
|
|
return databases
|
|
}
|