Exported EchoVault interface now onlu contains the keyspace methods. All other methods are private. Private methods are accessed using the reflect package in the test folder

This commit is contained in:
Kelvin Clement Mwinuka
2024-04-26 02:33:35 +08:00
parent 44e4f06670
commit 6ad3b7baab
35 changed files with 709 additions and 634 deletions

View File

@@ -23,10 +23,12 @@ import (
"github.com/echovault/echovault/pkg/echovault"
"github.com/tidwall/resp"
"net"
"reflect"
"slices"
"strings"
"sync"
"testing"
"unsafe"
)
var bindAddr string
@@ -64,12 +66,22 @@ func setUpServer(bindAddr string, port uint16, requirePass bool, aclConfig strin
)
// Add the initial test users to the ACL module
a := mockServer.GetACL().(*acl.ACL)
a := getACL(mockServer)
a.AddUsers(generateInitialTestUsers())
return mockServer
}
func getUnexportedField(field reflect.Value) interface{} {
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface()
}
func getACL(mockServer *echovault.EchoVault) *acl.ACL {
method := getUnexportedField(reflect.ValueOf(mockServer).Elem().FieldByName("getACL"))
f := method.(func() interface{})
return f().(*acl.ACL)
}
func generateInitialTestUsers() []*acl.User {
// User with both hash password and plaintext password
withPasswordUser := acl.CreateUser("with_password_user")
@@ -459,10 +471,7 @@ func Test_HandleSetUser(t *testing.T) {
}()
wg.Wait()
a, ok := mockServer.GetACL().(*acl.ACL)
if !ok {
t.Error("error loading ACL module")
}
a := getACL(mockServer)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", bindAddr, port))
if err != nil {
@@ -1055,7 +1064,7 @@ func Test_HandleGetUser(t *testing.T) {
}()
wg.Wait()
a, _ := mockServer.GetACL().(*acl.ACL)
a := getACL(mockServer)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", bindAddr, port))
if err != nil {
@@ -1208,7 +1217,7 @@ func Test_HandleDelUser(t *testing.T) {
}()
wg.Wait()
a, _ := mockServer.GetACL().(*acl.ACL)
a := getACL(mockServer)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", bindAddr, port))
if err != nil {
@@ -1358,7 +1367,7 @@ func Test_HandleList(t *testing.T) {
}()
wg.Wait()
a, _ := mockServer.GetACL().(*acl.ACL)
a := getACL(mockServer)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", bindAddr, port))
if err != nil {