Commit 45d1cd57 authored by Akshay Mahendrakar's avatar Akshay Mahendrakar
Browse files

CoreImmuDB package - Implementation

parent cffa6f98
Branches
Tags
2 merge requests!270Core ImmuDB package and TOTP plugin - Implementation,!269Core ImmuDB package and TOTP plugin - Implementation
Showing with 1839 additions and 35 deletions
package coreimmudb
import (
"context"
"sync"
"time"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"github.com/codenotary/immudb/pkg/api/schema"
immudb "github.com/codenotary/immudb/pkg/client"
)
var (
configs map[string]*ImmuHost // Immudb config details with host
instances map[string]immudb.ImmuClient // Immudb client connection with host
defaultHostName string
mutex sync.Mutex
)
type ImmuHost struct {
HostName string `json:"hostName"`
Address string `json:"address"` // Database server address
Port int `json:"port"` // Database port
Database string `json:"database"` // Database name
Username string `json:"username"` // Database username
Password string `json:"password"` // Database password
IsDefault bool `json:"isDefault"` // Default ImmuDB connection
IsDisabled bool `json:"isDisabled"` // If true connection then connection to database server is skipped
}
type ImmuDAO struct {
HostName string
}
// For setting multiple keys - to know more see SetAllKeysData
type KeyValue struct {
Key, Value []byte
}
func init() {
configs = make(map[string]*ImmuHost)
instances = make(map[string]immudb.ImmuClient)
}
// InitNewImmuDbSession - Initializes connection to ImmuDB server using []ImmuHost data
func InitNewImmuDbSession(hosts []ImmuHost) error {
configs = make(map[string]*ImmuHost)
instances = make(map[string]immudb.ImmuClient)
mutex.Lock()
defer mutex.Unlock()
for _, v := range hosts {
if v.IsDisabled { // Not connecting to database if isDisabled == true
continue
}
var client immudb.ImmuClient
dbConfig := immudb.DefaultOptions()
dbConfig.Address = v.Address
dbConfig.Port = v.Port
client = immudb.NewClient().WithOptions(dbConfig)
err := client.OpenSession(context.TODO(), []byte(v.Username), []byte(v.Password), v.Database)
if err != nil {
configs = make(map[string]*ImmuHost)
instances = make(map[string]immudb.ImmuClient)
return err
}
configs[v.HostName] = &v
instances[v.HostName] = client
if v.IsDefault {
defaultHostName = v.HostName
}
}
return nil
}
// AddNewImmuDbConnection - Creating and connecting to ImmuDB server using ImmuHost details
func AddNewImmuDbConnection(host ImmuHost) error {
mutex.Lock()
defer mutex.Unlock()
if host.IsDisabled {
return nil
}
var client immudb.ImmuClient
dbconfig := immudb.DefaultOptions()
dbconfig.Address = host.Address
dbconfig.Port = host.Port
client = immudb.NewClient().WithOptions(dbconfig)
err := client.OpenSession(context.TODO(), []byte(host.Username), []byte(host.Password), host.Database)
if err != nil {
return err
}
configs[host.HostName] = &host
instances[host.HostName] = client
if host.IsDefault {
defaultHostName = host.HostName
}
return nil
}
// DeleteImmuDbSession - Disconnecting Immu Database connection
func DeleteImmuDbSession(hostName string) error {
mutex.Lock()
defer mutex.Unlock()
inst, ok := instances[hostName]
if !ok {
return errormdl.Wrap("CONNECTION NOT FOUND FOR " + hostName)
}
delete(instances, hostName)
return inst.CloseSession(context.TODO()) // Closing immudb connection if there are any errors then returning.
}
// Get ImmuDB connection - immudb.ImmuClient
func getImmuDbConnection(hostName string) (immudb.ImmuClient, error) {
mutex.Lock()
defer mutex.Unlock()
// Returning if there are no immudb connections present
if instances == nil {
return nil, errormdl.Wrap("IMMUDB CONNECTION NOT FOUND FOR " + hostName)
}
// If hostname is empty
if hostName == "" {
hostName = defaultHostName
}
inst, ok := instances[hostName]
if !ok {
return nil, errormdl.Wrap("CONNECTION NOT FOUND FOR " + hostName)
}
// Health check
_, err := inst.Health(context.TODO())
if err != nil {
return nil, err
}
return inst, nil
}
// GetImmuDAOWithHost - return Immu DAO instance for provided host
func GetImmuDAOWithHost(hostname string) *ImmuDAO {
return &ImmuDAO{
HostName: hostname,
}
}
/* READ OPERATIONS */
// GetKeyData - Get single value for provided key
func (i *ImmuDAO) GetKeyData(key []byte) (*schema.Entry, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
return client.Get(context.TODO(), key)
}
// GetVerifiedKeyData - Get single value for provided key with additional server-provided proof validation.
func (i *ImmuDAO) GetVerifiedKeyData(key []byte) (*schema.Entry, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
return client.VerifiedGet(context.TODO(), key)
}
// GetAllKeysData - Getting all keys data
func (i *ImmuDAO) GetAllKeysData(keys [][]byte) (*schema.Entries, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
return client.GetAll(context.TODO(), keys)
}
// GetKeyHistory - Get history of single key
/*
Offset - Number of entries to skip
Limit - Maximum numbers of entries to return
Desc - Sorting in descending order
*/
func (i *ImmuDAO) GetKeyHistory(key []byte, offset uint64, limit int32, desc bool) (*schema.Entries, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
req := &schema.HistoryRequest{
Key: key,
Offset: offset,
Limit: limit,
Desc: desc,
}
return client.History(context.TODO(), req)
}
/* END OF READ OPERATIONS */
/* WRITE OPERATIONS */
// SetKeyData - Commits provided value for provided key
func (i *ImmuDAO) SetKeyData(key, value []byte) (*schema.TxHeader, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
return client.Set(context.TODO(), key, value)
}
// SetVerifiedKeyData - Commits provided value for provided key and also requests a server-generated proof
func (i *ImmuDAO) SetVerifiedKeyData(key, value []byte) (*schema.TxHeader, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
return client.VerifiedSet(context.TODO(), key, value)
}
// SetAllKeysData - Set multiple keys with their values in a single transaction.
func (i *ImmuDAO) SetAllKeysData(kv []KeyValue) (*schema.TxHeader, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
var kvs []*schema.KeyValue
for _, v := range kv {
kvs = append(kvs, &schema.KeyValue{Key: v.Key, Value: v.Value})
}
return client.SetAll(context.TODO(), &schema.SetRequest{KVs: kvs})
}
// DeleteKey - Key is deleted logically from Database, physically it is present in database
func (i *ImmuDAO) DeleteKey(keys [][]byte) (*schema.TxHeader, error) {
client, err := getImmuDbConnection(i.HostName) // ImmuDB connection for the provided host
if err != nil {
return nil, err
}
req := &schema.DeleteKeysRequest{
Keys: keys,
}
return client.Delete(context.TODO(), req)
}
// ExpirableKeySet - Sets value for the key with expiration time
func (i *ImmuDAO) ExpirableKeySet(key, value []byte, expiresAt time.Time) (*schema.TxHeader, error) {
client, err := getImmuDbConnection(i.HostName)
if err != nil {
return nil, err
}
return client.ExpirableSet(context.TODO(), key, value, expiresAt)
}
/* END OF WRITE OPERATIONS */
package coreimmudb
import (
"reflect"
"testing"
"time"
"github.com/codenotary/immudb/pkg/api/schema"
immudb "github.com/codenotary/immudb/pkg/client"
)
func init() {
// Creating and connecting two Immu database connection: ImmuDbHost1,ImmuDbHost4
// err := InitNewImmuDbSession([]ImmuHost{{"ImmuDbHost1", "localhost", 3322, "defaultdb", "immudb", "immudb", true, false},
// {"ImmuDbHost4", "localhost", 3322, "defaultdb", "immudb", "immudb", false, false}})
// if err != nil {
// panic(err)
// }
}
// Test - InitNewImmuDbSession
func TestInitNewImmuDbSession(t *testing.T) {
type args struct {
hosts []ImmuHost
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "Test 1", args: args{[]ImmuHost{{"LocalHostDB1", "localhost", 3322, "defaultdb", "immudb", "immudb", true, false}}}, wantErr: false}, // PASS
{name: "Test 2", args: args{[]ImmuHost{{"LocalHostDB2", "localhost", 3325, "defaultdb", "immudb", "immudb", true, false}}}, wantErr: true}, // FAIL - Wrong connection details
{name: "Test 3", args: args{[]ImmuHost{{"LocalHostDB3", "localhost", 3322, "wrongdb", "immudb", "immudb", true, false}}}, wantErr: true}, // FAIL - Wrong connection details
{name: "Test 4", args: args{[]ImmuHost{{"LocalHostDB4", "localhost", 3322, "defaultdb", "wrongUsername", "immudb", true, false}}}, wantErr: true}, // FAIL - Wrong connection details
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := InitNewImmuDbSession(tt.args.hosts); (err != nil) != tt.wantErr {
t.Errorf("InitNewImmuDbSession() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestAddNewImmuDbConnection(t *testing.T) {
type args struct {
host ImmuHost
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "Test 1", args: args{host: ImmuHost{"LocalHostDB1", "localhost", 3322, "defaultdb", "immudb", "immudb", true, false}}, wantErr: false}, // PASS
{name: "Test 2", args: args{host: ImmuHost{"LocalHostDB2", "localhost", 3422, "defaultdbname", "wrongimmudb", "immudb", true, false}}, wantErr: true}, // FAIL - Wrong connection details
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := AddNewImmuDbConnection(tt.args.host); (err != nil) != tt.wantErr {
t.Errorf("AddNewImmuDbConnection() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
// Test to disconnect ImmuDb connection / session
func TestDeleteImmuDbSession(t *testing.T) {
type args struct {
hostName string
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
args args
wantErr bool
}{
{name: "Test 1", args: args{hostName: "ImmuDbHost1"}, wantErr: false}, // PASS
{name: "Test 2", args: args{hostName: "ImmuDbHost2"}, wantErr: true}, // FAIL - Connection not found
{name: "Test 3", args: args{hostName: "ImmuDbHost3"}, wantErr: true}, // FAIL - Connection not found
{name: "Test 4", args: args{hostName: "ImmuDbHost4"}, wantErr: false}, // PASS
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteImmuDbSession(tt.args.hostName); (err != nil) != tt.wantErr {
t.Errorf("DeleteImmuDbSession() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_getImmuDbConnection(t *testing.T) {
type args struct {
hostName string
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
args args
want immudb.ImmuClient
wantErr bool
}{
{name: "Test 1", args: args{hostName: "ImmuDbHost1"}, wantErr: false}, // PASS
{name: "Test 2", args: args{hostName: "ImmuDbHost2"}, wantErr: true}, // FAIL - Connection not found
{name: "Test 3", args: args{hostName: "ImmuDbHost3"}, wantErr: true}, // FAIL - Connection not found
{name: "Test 4", args: args{hostName: "ImmuDbHost4"}, wantErr: false}, // PASS
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getImmuDbConnection(tt.args.hostName)
if (err != nil) != tt.wantErr {
t.Errorf("getImmuDbConnection() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual(got, tt.want) && err == nil {
t.Errorf("getImmuDbConnection() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetImmuDAOWithHost(t *testing.T) {
type args struct {
hostname string
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
args args
want *ImmuDAO
}{
{name: "Test 1", args: args{hostname: "ImmuDbHost1"}},
{name: "Test 2", args: args{hostname: "ImmuDbHost2"}},
{name: "Test 3", args: args{hostname: "ImmuDbHost3"}},
{name: "Test 4", args: args{hostname: "ImmuDbHost4"}}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GetImmuDAOWithHost(tt.args.hostname)
if reflect.DeepEqual(got, nil) {
t.Errorf("GetImmuDAOWithHost() = %v, want %v", got, tt.want)
}
})
}
}
func TestImmuDAO_GetKeyData(t *testing.T) {
type fields struct {
HostName string
}
type args struct {
key []byte
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
fields fields
args args
want *schema.Entry
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("Akshay")}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost2"}, args: args{key: []byte("Demo")}, wantErr: true}, // FAIL - Connection not found
{name: "Test 3", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("Demo")}, wantErr: true}, // FAIL - Key not found
{name: "Test 4", fields: fields{HostName: "ImmuDbHost4"}, args: args{key: []byte("key")}, wantErr: false}, // PASS
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
}
got, err := i.GetKeyData(tt.args.key)
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.GetKeyData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual(got, tt.want) && err == nil {
t.Errorf("ImmuDAO.GetKeyData() = %v, want %v", got, tt.want)
}
})
}
}
func TestImmuDAO_GetVerifiedKeyData(t *testing.T) {
type fields struct {
HostName string
}
type args struct {
key []byte
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
fields fields
args args
want *schema.Entry
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("Akshay")}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("Demo")}, wantErr: true}, // FAIL - Wrong key
{name: "Test 3", fields: fields{HostName: "ImmuDbHost2"}, args: args{key: []byte("Demo")}, wantErr: true}, // FAIL - Connection not found
{name: "Test 4", fields: fields{HostName: "ImmuDbHost4"}, args: args{key: []byte("key")}, wantErr: false}, // PASS
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
}
got, err := i.GetVerifiedKeyData(tt.args.key)
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.GetVerifiedKeyData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual(got, tt.want) && err == nil {
t.Errorf("ImmuDAO.GetVerifiedKeyData() = %v, want %v", got, tt.want)
}
})
}
}
func TestImmuDAO_GetAllKeysData(t *testing.T) {
type fields struct {
HostName string
}
type args struct {
keys [][]byte
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
fields fields
args args
want *schema.Entries
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, args: args{keys: [][]byte{[]byte("Akshay"), []byte("key")}}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost1"}, args: args{keys: [][]byte{[]byte("nokey1"), []byte("nokey2")}}, wantErr: false}, // PASS - but empty result due no keys found
{name: "Test 3", fields: fields{HostName: "ImmuDbHost2"}, args: args{keys: [][]byte{[]byte("nokey1"), []byte("nokey2")}}, wantErr: true}, // FAIL - Connection not found
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
}
got, err := i.GetAllKeysData(tt.args.keys)
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.GetAllKeysData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual(got, tt.want) && err == nil {
t.Errorf("ImmuDAO.GetAllKeysData() = %v, want %v", got, tt.want)
}
})
}
}
func TestImmuDAO_GetKeyHistory(t *testing.T) {
type fields struct {
HostName string
}
type args struct {
key []byte
offset uint64
limit int32
desc bool
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
fields fields
args args
want *schema.Entries
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("Akshay"), offset: 0, limit: 0, desc: false}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost2"}, args: args{key: []byte("nokey"), offset: 0, limit: 0, desc: false}, wantErr: true}, // FAIL - Connection not found
{name: "Test 3", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("nokey"), offset: 0, limit: 0, desc: false}, wantErr: true}, // FAIL - Key not found
{name: "Test 3", fields: fields{HostName: "ImmuDbHost4"}, args: args{key: []byte("key"), offset: 0, limit: 0, desc: false}, wantErr: false}, // PASS
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
}
got, err := i.GetKeyHistory(tt.args.key, tt.args.offset, tt.args.limit, tt.args.desc)
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.GetKeyHistory() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual(got, tt.want) && err == nil {
t.Errorf("ImmuDAO.GetKeyHistory() = %v, want %v", got, tt.want)
}
})
}
}
func TestImmuDAO_SetKeyData(t *testing.T) {
type fields struct {
HostName string
}
type args struct {
key []byte
value []byte
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
fields fields
args args
want *schema.TxHeader
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("Akshay"), value: []byte("Mahendrakar")}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost2"}, args: args{key: []byte("immu"), value: []byte("db")}, wantErr: true}, // FAIL - Connection not found
{name: "Test 3", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("valueEmpty"), value: []byte("")}, wantErr: false}, // FAIL
{name: "Test 4", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte(""), value: []byte("keyEmpty")}, wantErr: true}, // FAIL - Key is empty
{name: "Test 5", fields: fields{HostName: "ImmuDbHost4"}, args: args{key: []byte("jsonStringObj"), value: []byte("{'first_name':'Akshay', 'last_name':'Mahendrakar', 'company':'MKCL Pune' }")}}, // PASS
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
}
got, err := i.SetKeyData(tt.args.key, tt.args.value)
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.SetKeyData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual(got, tt.want) && err == nil {
t.Errorf("ImmuDAO.SetKeyData() = %v, want %v", got, tt.want)
}
})
}
}
func TestImmuDAO_SetVerifiedKeyData(t *testing.T) {
type fields struct {
HostName string
}
type args struct {
key []byte
value []byte
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
fields fields
args args
want *schema.TxHeader
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("Akshay"), value: []byte("Mahendrakar")}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost2"}, args: args{key: []byte("immu"), value: []byte("db")}, wantErr: true}, // FAIL - Connection not found
{name: "Test 3", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("valueEmpty"), value: []byte("")}, wantErr: false}, // FAIL
{name: "Test 4", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte(""), value: []byte("keyEmpty")}, wantErr: true}, // FAIL - Key is empty
{name: "Test 5", fields: fields{HostName: "ImmuDbHost4"}, args: args{key: []byte("jsonStringObj"), value: []byte("{'first_name':'Akshay', 'last_name':'Mahendrakar', 'company':'MKCL Pune' }")}}, // PASS
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
}
got, err := i.SetVerifiedKeyData(tt.args.key, tt.args.value)
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.SetVerifiedKeyData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual(got, tt.want) && err == nil {
t.Errorf("ImmuDAO.SetVerifiedKeyData() = %v, want %v", got, tt.want)
}
})
}
}
func TestImmuDAO_SetAllKeysData(t *testing.T) {
type fields struct {
HostName string
}
type args struct {
kv []KeyValue
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
fields fields
args args
want *schema.TxHeader
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, args: args{kv: []KeyValue{{Key: []byte("test1"), Value: []byte("test1 value")}, {Key: []byte("test2"), Value: []byte("test2 value")}}}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost2"}, args: args{kv: []KeyValue{{Key: []byte("test3"), Value: []byte("test3 value")}, {Key: []byte("test4"), Value: []byte("test4 value")}}}, wantErr: true}, // FAIL - Connection not found
{name: "Test 3", fields: fields{HostName: "ImmuDbHost4"}, args: args{kv: []KeyValue{{Key: []byte(""), Value: []byte("test3 value")}, {Key: []byte("test4"), Value: []byte("test4 value")}}}, wantErr: true}, // FAIL - Key not set
{name: "Test 4", fields: fields{HostName: "ImmuDbHost4"}, args: args{kv: []KeyValue{{Key: []byte("test3"), Value: []byte("test3 value")}, {Key: []byte("test4"), Value: []byte("test4 value")}}}, wantErr: false}, // PASS
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
}
got, err := i.SetAllKeysData(tt.args.kv)
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.SetAllKeysData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual(got, tt.want) && err == nil {
t.Errorf("ImmuDAO.SetAllKeysData() = %v, want %v", got, tt.want)
}
})
}
}
func TestImmuDAO_DeleteKey(t *testing.T) {
type fields struct {
HostName string
}
type args struct {
keys [][]byte
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
fields fields
args args
want *schema.TxHeader
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, args: args{keys: [][]byte{[]byte("test 3"), []byte("test4")}}, wantErr: true}, // FAIL - Key not found "test 3"
{name: "Test 1", fields: fields{HostName: "ImmuDbHost2"}, args: args{keys: [][]byte{[]byte("test 3"), []byte("test4")}}, wantErr: true}, // FAIL - Connection not found
{name: "Test 2", fields: fields{HostName: "ImmuDbHost1"}, args: args{keys: [][]byte{[]byte("test3"), []byte("test4")}}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost4"}, args: args{keys: [][]byte{[]byte("test3"), []byte("test4")}}, wantErr: true}, // FAIL - Key not found
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
}
got, err := i.DeleteKey(tt.args.keys)
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.DeleteKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual("got", tt.want) && err == nil {
t.Errorf("ImmuDAO.DeleteKey() = %v, want %v", got, tt.want)
}
})
}
}
func TestImmuDAO_ExpirableKeySet(t *testing.T) {
type fields struct {
HostName string
}
type args struct {
key []byte
value []byte
expiresAt time.Time
}
// NOTE: ImmuDbHost1 and ImmuDbHost4 database connection are created at init method
tests := []struct {
name string
fields fields
args args
want *schema.TxHeader
wantErr bool
}{
{name: "Test 1", fields: fields{HostName: "ImmuDbHost1"}, args: args{key: []byte("test5"), value: []byte("test5 value"), expiresAt: time.Now().Add(2 * time.Minute)}, wantErr: false}, // PASS
{name: "Test 2", fields: fields{HostName: "ImmuDbHost2"}, args: args{key: []byte("test6"), value: []byte("test6 value"), expiresAt: time.Now().Add(2 * time.Minute)}, wantErr: true}, // FAIL - Connection not found
{name: "Test 3", fields: fields{HostName: "ImmuDbHost"}, args: args{key: []byte(""), value: []byte("test6 value"), expiresAt: time.Now().Add(2 * time.Minute)}, wantErr: true}, // FAIL - Key not found
{name: "Test 4", fields: fields{HostName: "ImmuDbHost4"}, args: args{key: []byte("test6"), value: []byte("test6 value"), expiresAt: time.Now().Add(2 * time.Minute)}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := &ImmuDAO{
HostName: tt.fields.HostName,
}
got, err := i.ExpirableKeySet(tt.args.key, tt.args.value, tt.args.expiresAt)
if (err != nil) != tt.wantErr {
t.Errorf("ImmuDAO.ExpirableKeySet() error = %v, wantErr %v", err, tt.wantErr)
return
}
if reflect.DeepEqual(got, tt.want) && err == nil {
t.Errorf("ImmuDAO.ExpirableKeySet() = %v, want %v", got, tt.want)
}
})
}
}
......@@ -9,6 +9,7 @@ require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/aymerick/raymond v2.0.2+incompatible
github.com/boltdb/bolt v1.3.1
github.com/codenotary/immudb v1.4.1
github.com/denisenkom/go-mssqldb v0.12.2
github.com/dgraph-io/dgo/v2 v2.2.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
......@@ -17,7 +18,7 @@ require (
github.com/go-redis/redis/v7 v7.4.1
github.com/go-sql-driver/mysql v1.6.0
github.com/gocraft/dbr/v2 v2.7.3
github.com/golang/protobuf v1.5.2
github.com/golang/protobuf v1.5.3
github.com/hashicorp/go-version v1.5.0
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b
github.com/klauspost/compress v1.15.6
......@@ -31,7 +32,7 @@ require (
github.com/qiangxue/fasthttp-routing v0.0.0-20160225050629-6ccdc2a18d87
github.com/segmentio/ksuid v1.0.4
github.com/shurcooL/go-goon v1.0.0
github.com/stretchr/testify v1.7.2
github.com/stretchr/testify v1.8.1
github.com/tidwall/buntdb v1.2.9
github.com/tidwall/gjson v1.14.1
github.com/tidwall/sjson v1.2.4
......@@ -39,8 +40,8 @@ require (
github.com/zhouzhuojie/conditions v0.2.3
go.mongodb.org/mongo-driver v1.9.1
go.uber.org/zap v1.21.0
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
google.golang.org/grpc v1.47.0
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
google.golang.org/grpc v1.54.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
gopkg.in/natefinch/lumberjack.v2 v2.0.0
......@@ -49,8 +50,14 @@ require (
require (
github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29 // indirect
github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
github.com/go-ozzo/ozzo-routing v2.1.4+incompatible // indirect
......@@ -62,16 +69,37 @@ require (
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/gddo v0.0.0-20210115222349-20d68f94ee1f // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/o1egl/paseto v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/rs/xid v1.4.0 // indirect
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.15.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/tidwall/btree v1.1.0 // indirect
github.com/tidwall/grect v0.1.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
......@@ -84,15 +112,17 @@ require (
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/protobuf v1.28.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20230403163135-c38d8f061ccd // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
This diff is collapsed.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment