mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-28 00:21:56 +00:00
Add: translate tests
This commit is contained in:

committed by
Sveta Smirnova

parent
78aebd272d
commit
ad91b7d407
@@ -66,11 +66,15 @@ func AddHashToIP(hash, ip string, ts time.Time) {
|
||||
db.HashToIP[hash] = translationUnit{Value: ip, Timestamp: ts}
|
||||
}
|
||||
|
||||
func sameAsLatestValue(m map[string][]translationUnit, key string, newvalue string) bool {
|
||||
return len(m[key]) > 0 && m[key][len(m[key])-1].Value == newvalue
|
||||
}
|
||||
|
||||
func AddHashToNodeName(hash, name string, ts time.Time) {
|
||||
db.rwlock.Lock()
|
||||
defer db.rwlock.Unlock()
|
||||
name = utils.ShortNodeName(name)
|
||||
if len(db.HashToNodeNames[hash]) > 0 && db.HashToNodeNames[hash][len(db.HashToNodeNames[hash])-1].Value == name {
|
||||
if sameAsLatestValue(db.HashToNodeNames, hash, name) {
|
||||
return
|
||||
}
|
||||
db.HashToNodeNames[hash] = append(db.HashToNodeNames[hash], translationUnit{Value: name, Timestamp: ts})
|
||||
@@ -80,7 +84,7 @@ func AddIPToNodeName(ip, name string, ts time.Time) {
|
||||
db.rwlock.Lock()
|
||||
defer db.rwlock.Unlock()
|
||||
name = utils.ShortNodeName(name)
|
||||
if len(db.IPToNodeNames[ip]) > 0 && db.IPToNodeNames[ip][len(db.IPToNodeNames[ip])-1].Value == name {
|
||||
if sameAsLatestValue(db.IPToNodeNames, ip, name) {
|
||||
return
|
||||
}
|
||||
db.IPToNodeNames[ip] = append(db.IPToNodeNames[ip], translationUnit{Value: name, Timestamp: ts})
|
||||
@@ -89,7 +93,7 @@ func AddIPToNodeName(ip, name string, ts time.Time) {
|
||||
func AddIPToMethod(ip, method string, ts time.Time) {
|
||||
db.rwlock.Lock()
|
||||
defer db.rwlock.Unlock()
|
||||
if len(db.IPToMethods[ip]) > 0 && db.IPToMethods[ip][len(db.IPToMethods[ip])-1].Value == method {
|
||||
if sameAsLatestValue(db.IPToMethods, ip, method) {
|
||||
return
|
||||
}
|
||||
db.IPToMethods[ip] = append(db.IPToMethods[ip], translationUnit{Value: method, Timestamp: ts})
|
||||
|
103
src/go/pt-galera-log-explainer/translate/translate_test.go
Normal file
103
src/go/pt-galera-log-explainer/translate/translate_test.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package translate
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAddHashToNodeName(t *testing.T) {
|
||||
hash1 := "somehash"
|
||||
name1 := "somename"
|
||||
ResetDB()
|
||||
ts1, _ := time.Parse(time.RFC3339, "2001-01-01T01:01:01")
|
||||
AddHashToNodeName(hash1, name1, ts1)
|
||||
if length := len(db.HashToNodeNames[hash1]); length != 1 {
|
||||
t.Errorf("incorrect hashtonodenames length: %d, expected 1", length)
|
||||
t.Fail()
|
||||
}
|
||||
name2 := "somename.with.a.fqdn.net"
|
||||
AddHashToNodeName(hash1, name2, ts1)
|
||||
if length := len(db.HashToNodeNames[hash1]); length != 1 {
|
||||
t.Errorf("incorrect hashtonodenames length: %d, expected 1", length)
|
||||
t.Fail()
|
||||
}
|
||||
name3 := "somename2.with.a.fqdn.net"
|
||||
AddHashToNodeName(hash1, name3, ts1)
|
||||
if length := len(db.HashToNodeNames[hash1]); length != 2 {
|
||||
t.Errorf("incorrect hashtonodenames length: %d, expected 2", length)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddIPToNodeName(t *testing.T) {
|
||||
ip := "127.0.0.1"
|
||||
name1 := "somename"
|
||||
ResetDB()
|
||||
ts1, _ := time.Parse(time.RFC3339, "2001-01-01T01:01:01")
|
||||
AddIPToNodeName(ip, name1, ts1)
|
||||
if length := len(db.IPToNodeNames[ip]); length != 1 {
|
||||
t.Errorf("incorrect iptonodenames length: %d, expected 1", length)
|
||||
t.Fail()
|
||||
}
|
||||
name2 := "somename.with.a.fqdn.net"
|
||||
AddIPToNodeName(ip, name2, ts1)
|
||||
if length := len(db.IPToNodeNames[ip]); length != 1 {
|
||||
t.Errorf("incorrect iptonodenames length: %d, expected 1", length)
|
||||
t.Fail()
|
||||
}
|
||||
name3 := "somename2.with.a.fqdn.net"
|
||||
AddIPToNodeName(ip, name3, ts1)
|
||||
if length := len(db.IPToNodeNames[ip]); length != 2 {
|
||||
t.Errorf("incorrect iptonodenames length: %d, expected 2", length)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func testMostAppropriateValueFromTS(t *testing.T) {
|
||||
tests := []struct {
|
||||
inputunits []translationUnit
|
||||
inputts time.Time
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
inputunits: []translationUnit{
|
||||
translationUnit{Value: "value1", Timestamp: time.Date(2001, time.January, 1, 1, 1, 1, 0, time.UTC)},
|
||||
translationUnit{Value: "value2", Timestamp: time.Date(2001, time.January, 1, 3, 1, 1, 0, time.UTC)},
|
||||
},
|
||||
inputts: time.Date(2001, time.January, 1, 2, 1, 1, 0, time.UTC),
|
||||
expected: "value1",
|
||||
},
|
||||
{
|
||||
inputunits: []translationUnit{
|
||||
translationUnit{Value: "value1", Timestamp: time.Date(2001, time.January, 1, 1, 1, 1, 0, time.UTC)},
|
||||
translationUnit{Value: "value2", Timestamp: time.Date(2001, time.January, 1, 3, 1, 1, 0, time.UTC)},
|
||||
},
|
||||
inputts: time.Date(2001, time.January, 1, 4, 1, 1, 0, time.UTC),
|
||||
expected: "value2",
|
||||
},
|
||||
{
|
||||
inputunits: []translationUnit{
|
||||
translationUnit{Value: "value1", Timestamp: time.Date(2001, time.January, 1, 1, 1, 1, 0, time.UTC)},
|
||||
translationUnit{Value: "value2", Timestamp: time.Date(2001, time.January, 1, 3, 1, 1, 0, time.UTC)},
|
||||
translationUnit{Value: "value3", Timestamp: time.Date(2001, time.January, 1, 5, 1, 1, 0, time.UTC)},
|
||||
},
|
||||
inputts: time.Date(2001, time.January, 1, 4, 1, 1, 0, time.UTC),
|
||||
expected: "value2",
|
||||
},
|
||||
{
|
||||
inputunits: []translationUnit{
|
||||
translationUnit{Value: "value1", Timestamp: time.Date(2001, time.January, 1, 1, 1, 1, 0, time.UTC)},
|
||||
translationUnit{Value: "value2", Timestamp: time.Date(2001, time.January, 1, 3, 1, 1, 0, time.UTC)},
|
||||
},
|
||||
inputts: time.Date(2001, time.January, 1, 0, 1, 1, 0, time.UTC),
|
||||
expected: "value1",
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
out := mostAppropriateValueFromTS(test.inputunits, test.inputts)
|
||||
if out != test.expected {
|
||||
t.Errorf("test %d, expected: %s, got: %s", i, test.expected, out)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user