Commit 09620ea7 authored by Akshay Mahendrakar's avatar Akshay Mahendrakar
Browse files

TestDbConnection, InitUsingJson functionality added.

Added unit test for the same
Setting default limit in GetKeyHistory if limit value is not passed
parent 00e638b1
Branches
Tags
2 merge requests!270Core ImmuDB package and TOTP plugin - Implementation,!269Core ImmuDB package and TOTP plugin - Implementation
Showing with 167 additions and 16 deletions
......@@ -2,6 +2,8 @@ package coreimmudb
import (
"context"
"encoding/json"
"os"
"strings"
"sync"
"time"
......@@ -16,6 +18,8 @@ const (
AdminPermission = auth.PermissionAdmin
ReadPermission = auth.PermissionR
ReadWritePermission = auth.PermissionRW
defaultLimit = 100
)
var (
......@@ -26,11 +30,12 @@ var (
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
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
Dir string `json:"dir"`
IsDefault bool `json:"isDefault"` // Default ImmuDB connection
IsDisabled bool `json:"isDisabled"` // If true connection then connection to database server is skipped
}
......@@ -49,6 +54,52 @@ func init() {
instances = make(map[string]immudb.ImmuClient)
}
// InitUsingJsonFile - Initializes connection to ImmuDB server using json config file
func InitUsingJson(filePath string) error {
// Reading ImmuDB config file
byteData, err := os.ReadFile(filePath)
if err != nil {
return err
}
// Binding byte data into []ImmuHost{}
var hosts []ImmuHost
err = json.Unmarshal(byteData, &hosts)
if err != nil {
return err
}
instances = make(map[string]immudb.ImmuClient)
mutex.Lock()
defer mutex.Unlock()
for _, h := range hosts {
if h.IsDisabled { // Not connecting to database if isDisabled == true
continue
}
var client immudb.ImmuClient
opts := immudb.DefaultOptions()
opts.Address = h.Address
opts.Port = h.Port
if h.Dir != "" {
opts.Dir = h.Dir
}
client = immudb.NewClient().WithOptions(opts)
err := client.OpenSession(context.TODO(), []byte(h.Username), []byte(h.Password), h.Database)
if err != nil {
instances = make(map[string]immudb.ImmuClient)
return err
}
instances[h.HostName] = client
if h.IsDefault {
defaultHostName = h.HostName
}
}
return nil
}
// InitNewImmuDbSession - Initializes connection to ImmuDB server using []ImmuHost data
func InitImmuDBConnections(hosts []ImmuHost) error {
instances = make(map[string]immudb.ImmuClient)
......@@ -60,10 +111,13 @@ func InitImmuDBConnections(hosts []ImmuHost) error {
}
var client immudb.ImmuClient
dbConfig := immudb.DefaultOptions()
dbConfig.Address = v.Address
dbConfig.Port = v.Port
client = immudb.NewClient().WithOptions(dbConfig)
opts := immudb.DefaultOptions()
opts.Address = v.Address
opts.Port = v.Port
if v.Dir != "" {
opts.Dir = v.Dir
}
client = immudb.NewClient().WithOptions(opts)
err := client.OpenSession(context.TODO(), []byte(v.Username), []byte(v.Password), v.Database)
if err != nil {
instances = make(map[string]immudb.ImmuClient)
......@@ -90,6 +144,28 @@ func DeleteImmuDbSession(hostName string) error {
return inst.CloseSession(context.TODO()) // Closing immudb connection if there are any errors then returning.
}
// TestImmuDbConnection - Connects to Immudb with provided Database host details, returns false - if not able to connect ImmuDB
func TestDbConnection(host ImmuHost) bool {
var client immudb.ImmuClient
opts := immudb.DefaultOptions()
opts.Address = host.Address
opts.Port = host.Port
if host.Dir != "" {
opts.Dir = host.Dir
}
client = immudb.NewClient().WithOptions(opts)
err := client.OpenSession(context.TODO(), []byte(host.Username), []byte(host.Password), host.Database)
if err != nil {
return false
}
if ok := client.IsConnected(); !ok {
return false
}
return true // Sending true if connection successfully established to ImmuDB
}
// Get ImmuDB connection - immudb.ImmuClient
func GetImmuDbConnection(hostName string) (immudb.ImmuClient, error) {
mutex.Lock()
......@@ -164,6 +240,11 @@ func (i *ImmuDAO) GetKeyHistory(key []byte, offset uint64, limit int32, desc boo
if err != nil {
return nil, err
}
if limit <= 0 {
limit = defaultLimit // Setting to defaultLimit
}
req := &schema.HistoryRequest{
Key: key,
Offset: offset,
......
......@@ -12,12 +12,38 @@ import (
func init() {
// Creating and connecting two Immu database connection: ImmuDbHost1,ImmuDbHost4
// err := InitImmuDBConnections([]ImmuHost{{"ImmuDbHost1", "localhost", 3322, "defaultdb", "immudb", "immudb", true, false},
// {"ImmuDbHost4", "localhost", 3322, "defaultdb", "immudb", "immudb", false, false}})
// err := InitImmuDBConnections([]ImmuHost{{"ImmuDbHost1", "localhost", 3322, "defaultdb", "immudb", "immudb","", true, false},
// {"ImmuDbHost4", "localhost", 3322, "defaultdb", "immudb", "immudb", "",false, false}})
// if err != nil {
// panic(err)
// }
// Establishing ImmuDB connection using dbconfig.json file
// err := InitUsingJsonFile("./dbconfig.json")
// if err != nil {
// panic(err)
// }
}
func TestInitUsingJson(t *testing.T) {
type args struct {
filePath string
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "Test 1", args: args{filePath: "./dbconfig.json"}, wantErr: false}, // PASS
{name: "Test 2", args: args{filePath: "./sample.json"}, wantErr: true}, // FAIL - Config file does not exists
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := InitUsingJson(tt.args.filePath); (err != nil) != tt.wantErr {
t.Errorf("InitUsingJsonFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
// Test - InitImmuDBConnections
......@@ -30,10 +56,10 @@ func TestInitImmuDBConnections(t *testing.T) {
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
{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) {
......@@ -69,7 +95,28 @@ func TestDeleteImmuDbSession(t *testing.T) {
}
}
func Test_getImmuDbConnection(t *testing.T) {
func TestImmuDbConnection(t *testing.T) {
type args struct {
host ImmuHost
}
tests := []struct {
name string
args args
want bool
}{
{name: "Test 1", args: args{host: ImmuHost{"ImmuDbHost1", "localhost", 3322, "defaultdb", "immudb", "immudb", "", true, false}}, want: true}, // PASS
{name: "Test 2", args: args{host: ImmuHost{"ImmuDbHost2", "localhost", 3324, "defaultdb", "immudb", "immudb", "", false, false}}, want: false}, // FAIL - Immudb not running at provided port
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TestDbConnection(tt.args.host); got != tt.want {
t.Errorf("TestImmuDbConnection() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetImmuDbConnection(t *testing.T) {
type args struct {
hostName string
}
......
[
{
"hostName": "ImmuDbHost1",
"address": "localhost",
"port": 3322,
"database": "defaultdb",
"username": "immudb",
"password": "immudb",
"dir": "./tmp",
"isDefault": true,
"isDisabled": false
},
{
"hostName": "ImmuDbHost4",
"address": "localhost",
"port": 3322,
"database": "defaultdb",
"username": "immudb",
"password": "immudb",
"isDefault": false,
"isDisabled": false
}
]
\ No newline at end of file
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