Add Docker FV tests for metric gathering (#90)

* initial metric tests

* Add FV tests for metrics

* correct comment in test
This commit is contained in:
Rob Parker
2018-05-25 13:09:48 +01:00
committed by Stephen Marshall
parent 91e20f0f98
commit 531f2710c7
6 changed files with 550 additions and 59 deletions

View File

@@ -35,6 +35,7 @@ import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
"github.com/ibm-messaging/mq-golang/ibmmq"
@@ -263,7 +264,7 @@ func discoverElements(ty *MonType) error {
}
}
elem.MetricName = formatDescriptionElem(elem)
elem.MetricName = formatDescription(elem)
ty.Elements[elementIndex] = elem
}
}
@@ -631,59 +632,49 @@ bytes etc), and organisation of the elements of the name (units last)
While we can't change the MQ-generated descriptions for its statistics,
we can reformat most of them heuristically here.
*/
func formatDescriptionElem(elem *MonElement) string {
s := formatDescription(elem.Description)
unit := ""
switch elem.Datatype {
case ibmmq.MQIAMO_MONITOR_MICROSEC:
// Although the qmgr captures in us, we convert when
// pushing out to the backend, so this label needs to match
unit = "_seconds"
}
s += unit
return s
}
func formatDescription(baseName string) string {
s := baseName
func formatDescription(elem *MonElement) string {
s := elem.Description
s = strings.Replace(s, " ", "_", -1)
s = strings.Replace(s, "/", "_", -1)
s = strings.Replace(s, "-", "_", -1)
/* common pattern is "xxx - yyy" leading to 3 ugly adjacent underscores */
s = strings.Replace(s, "___", "_", -1)
s = strings.Replace(s, "__", "_", -1)
/* Make sure we don't have multiple underscores */
multiunder := regexp.MustCompile("__*")
s = multiunder.ReplaceAllLiteralString(s, "_")
/* make it all lowercase. Not essential, but looks better */
s = strings.ToLower(s)
// Do not use _count
/* Remove all cases of bytes, seconds, count or percentage (we add them back in later) */
s = strings.Replace(s, "_count", "", -1)
s = strings.Replace(s, "_bytes", "", -1)
s = strings.Replace(s, "_byte", "", -1)
s = strings.Replace(s, "_seconds", "", -1)
s = strings.Replace(s, "_second", "", -1)
s = strings.Replace(s, "_percentage", "", -1)
// Switch round a couple of specific names
s = strings.Replace(s, "bytes_written", "written_bytes", -1)
s = strings.Replace(s, "bytes_max", "max_bytes", -1)
s = strings.Replace(s, "bytes_in_use", "in_use_bytes", -1)
s = strings.Replace(s, "messages_expired", "expired_messages", -1)
if strings.HasSuffix(s, "free_space") {
// Add the unit at end
switch elem.Datatype {
case ibmmq.MQIAMO_MONITOR_PERCENT, ibmmq.MQIAMO_MONITOR_HUNDREDTHS:
s = s + "_percentage"
s = strings.Replace(s, "__", "_", -1)
}
// Make "byte", "file" and "message" units plural
if strings.HasSuffix(s, "byte") ||
strings.HasSuffix(s, "message") ||
strings.HasSuffix(s, "file") {
s = s + "s"
}
// Move % to the end
if strings.Contains(s, "_percentage_") {
s = strings.Replace(s, "_percentage_", "_", -1)
s += "_percentage"
case ibmmq.MQIAMO_MONITOR_MB, ibmmq.MQIAMO_MONITOR_GB:
s = s + "_bytes"
case ibmmq.MQIAMO_MONITOR_MICROSEC:
s = s + "_seconds"
default:
if strings.Contains(s, "_total") {
/* If we specify it is a total in description put that at the end */
s = strings.Replace(s, "_total", "", -1)
s = s + "_total"
} else if strings.Contains(s, "log_") {
/* Weird case where the log datatype is not MB or GB but should be bytes */
s = s + "_bytes"
} else {
s = s + "_count"
}
}
return s

View File

@@ -90,36 +90,61 @@ func TestReadPatterns(t *testing.T) {
}
}
func TestFormatDescription(t *testing.T) {
give := [...]string{"hello", "no space", "no/slash", "no-dash", "single___underscore", "single__underscore", "ALLLOWER", "no_count", "this_bytes_written_switch", "this_bytes_max_switch", "this_bytes_in_use_switch", "this messages_expired_switch", "add_free_space", "suffix_byte", "suffix_message", "suffix_file", "this_percentage_move"}
expected := [...]string{"hello", "no_space", "no_slash", "no_dash", "single_underscore", "single_underscore", "alllower", "no", "this_written_bytes_switch", "this_max_bytes_switch", "this_in_use_bytes_switch", "this_expired_messages_switch", "add_free_space_percentage", "suffix_bytes", "suffix_messages", "suffix_files", "this_move_percentage"}
give := [...]string{"hello", "no space", "no/slash", "no-dash", "single___underscore", "single__underscore__multiplace", "ALLLOWER", "this_bytes_written_switch", "this_byte_max_switch", "this_seconds_in_use_switch", "this messages_expired_switch", "this_seconds_max_switch", "this_count_max_switch", "this_percentage_max_switch"}
expected := [...]string{"hello_count", "no_space_count", "no_slash_count", "no_dash_count", "single_underscore_count", "single_underscore_multiplace_count", "alllower_count", "this_written_switch_count", "this_max_switch_count", "this_in_use_switch_count", "this_expired_messages_switch_count", "this_max_switch_count", "this_max_switch_count", "this_max_switch_count"}
for i, e := range give {
back := formatDescription(e)
elem := MonElement{
Description: e,
}
back := formatDescription(&elem)
if back != expected[i] {
t.Logf("Gave %s. Expected: %s, Got: %s", e, expected[i], back)
t.Fail()
}
}
}
func TestFormatDescriptionElem(t *testing.T) {
test := MonElement{}
test.Description = "THIS-should__be/formatted_count"
expected := "this_should_be_formatted"
back := formatDescriptionElem(&test)
if back != expected {
t.Logf("Gave %s. Expected: %s, Got: %s", test.Description, expected, back)
func TestSuffixes(t *testing.T) {
baseDescription := "test_suffix"
types := [...]int32{ibmmq.MQIAMO_MONITOR_MB, ibmmq.MQIAMO_MONITOR_GB, ibmmq.MQIAMO_MONITOR_MICROSEC, ibmmq.MQIAMO_MONITOR_PERCENT, ibmmq.MQIAMO_MONITOR_HUNDREDTHS, 0}
expected := [...]string{baseDescription + "_bytes", baseDescription + "_bytes", baseDescription + "_seconds", baseDescription + "_percentage", baseDescription + "_percentage", baseDescription + "_count"}
for i, ty := range types {
elem := MonElement{
Description: baseDescription,
Datatype: ty,
}
back := formatDescription(&elem)
if back != expected[i] {
t.Logf("Gave %s/%d Expected: %s, Got: %s", baseDescription, ty, expected[i], back)
t.Fail()
}
}
// special case log_bytes
elem := MonElement{
Description: "log_test_suffix",
Datatype: 0,
}
back := formatDescription(&elem)
if back != "log_test_suffix_bytes" {
t.Logf("Gave log_test_suffix/0 Expected: %s, Got: %s", "log_test_suffix_bytes", back)
t.Fail()
}
test.Datatype = ibmmq.MQIAMO_MONITOR_MICROSEC
expected = "this_should_be_formatted_seconds"
back = formatDescriptionElem(&test)
if back != expected {
t.Logf("Gave %s. Expected: %s, Got: %s", test.Description, expected, back)
// special case log_total
elem = MonElement{
Description: "log_total_suffix",
Datatype: 0,
}
back = formatDescription(&elem)
if back != "log_suffix_total" {
t.Logf("Gave log_total_suffix/0 Expected: %s, Got: %s", "log_suffix_total", back)
t.Fail()
}
}
func TestParsePCFResponse(t *testing.T) {
cfh := ibmmq.NewMQCFH()
cfh.Type = ibmmq.MQCFT_RESPONSE