1
0
mirror of https://github.com/gluster/glusterd2.git synced 2026-02-05 12:45:38 +01:00

pkg:utils unit test

added unit test for pkgs/utils

Signed-off-by: Madhu Rajanna <mrajanna@redhat.com>
This commit is contained in:
Madhu Rajanna
2018-04-25 12:24:16 +05:30
parent b758adb3a3
commit b45382e877
8 changed files with 149 additions and 0 deletions

BIN
pkg/utils/debug.test Executable file

Binary file not shown.

35
pkg/utils/maps_test.go Normal file
View File

@@ -0,0 +1,35 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMergeStringMaps(t *testing.T) {
map1 := map[string]string{
"a": "a",
"b": "b",
"c": "c",
"d": "d",
}
map2 := map[string]string{
"a": "a",
"b": "b",
"c": "c",
"d": "d",
}
mergedmap := MergeStringMaps(map1, map2)
assert.Equal(t, mergedmap, map1)
map2 = map[string]string{
"e": "e",
"f": "f",
"g": "g",
"h": "h",
}
mergedmap = MergeStringMaps(map1, map2)
assert.Equal(t, len(mergedmap), 8)
}

16
pkg/utils/mutex_test.go Normal file
View File

@@ -0,0 +1,16 @@
package utils
import "testing"
import "github.com/stretchr/testify/assert"
func TestTryLock(t *testing.T) {
lock := MutexWithTry{}
defer lock.Unlock()
l := lock.TryLock()
assert.True(t, l)
l = lock.TryLock()
assert.False(t, l)
}

View File

@@ -0,0 +1,29 @@
package utils
import (
"testing"
config "github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func TestIsPeerAddressSame(t *testing.T) {
resp := IsPeerAddressSame("192.167.1.1:8080", "192.167.1.1:8080")
assert.True(t, resp)
resp = IsPeerAddressSame("192.167.1.1:8080", "192.167.1.1:8081")
assert.False(t, resp)
}
func TestFormRemotePeerAddress(t *testing.T) {
peer, err := FormRemotePeerAddress("192.168.1.1:8080")
assert.Nil(t, err)
config.SetDefault("defaultpeerport", "80")
peer, err = FormRemotePeerAddress("192.168.1.1")
assert.Equal(t, peer, "192.168.1.1:80")
peer, err = FormRemotePeerAddress(":8080")
assert.Contains(t, err.Error(), "Invalid peer address")
}

View File

@@ -0,0 +1,24 @@
package utils
import (
"fmt"
"os"
"strconv"
"testing"
"time"
config "github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func TestWriteStatedump(t *testing.T) {
filename := fmt.Sprintf("glusterd2.%s.dump.%s",
strconv.Itoa(os.Getpid()), strconv.Itoa(int(time.Now().Unix())))
WriteStatedump()
assert.FileExists(t, filename)
os.Remove(filename)
config.Set("rundir", "/abc")
WriteStatedump()
}

14
pkg/utils/types_test.go Normal file
View File

@@ -0,0 +1,14 @@
package utils
import (
"testing"
"github.com/gluster/glusterd2/pkg/api"
"github.com/stretchr/testify/assert"
)
func TestGetTypeString(t *testing.T) {
resp := GetTypeString((*api.PeerGetResp)(nil))
assert.Equal(t, resp, "api.PeerGetResp")
}

View File

@@ -88,6 +88,13 @@ func TestIsAddressSame(t *testing.T) {
resp = IsAddressSame("192.169.1.2", "192.168.1.1")
assert.False(t, resp)
resp = IsAddressSame("192.169.1.2", "192.168.1")
assert.False(t, resp)
resp = IsAddressSame("192.169.1", "192.168.1.1")
assert.False(t, resp)
}
func TestGetLocalIP(t *testing.T) {

View File

@@ -0,0 +1,24 @@
package utils
import (
"testing"
//"github.com/stretchr/testify/assert"
config "github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func TestGetVolumeDir(t *testing.T) {
config.Set("localstatedir", "/var/lib/glusterd2")
resp := GetVolumeDir("testvolume")
assert.Equal(t, resp, "/var/lib/glusterd2/vols/testvolume")
}
func TestGetSnapshotDir(t *testing.T) {
config.Set("localstatedir", "/var/lib/glusterd2")
resp := GetSnapshotDir("testvolume")
assert.Equal(t, resp, "/var/lib/glusterd2/snaps/testvolume")
}