From 0c405d22715dc019c1e70b79efd24b10ecdf98f8 Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Thu, 21 Jun 2018 10:14:36 +0100 Subject: [PATCH] Refactor and extend existing tests --- mqmetric/mqmetric_test.go | 354 +++++++++++++++++++++++--------------- 1 file changed, 220 insertions(+), 134 deletions(-) diff --git a/mqmetric/mqmetric_test.go b/mqmetric/mqmetric_test.go index 1b97fb7..c3cde12 100644 --- a/mqmetric/mqmetric_test.go +++ b/mqmetric/mqmetric_test.go @@ -16,6 +16,7 @@ limitations under the License. package mqmetric import ( + "fmt" "io/ioutil" "os" "testing" @@ -24,177 +25,262 @@ import ( ) func TestNormalise(t *testing.T) { - var expected float64 - test := MonElement{} - value := int64(1000000) - - test.Datatype = ibmmq.MQIAMO_MONITOR_PERCENT - expected = 10000 - returned := Normalise(&test, "", value) - if returned != expected { - t.Logf("Gave %s, %d. Expected: %f, Got: %f", "ibmmq.MQIAMO_MONITOR_PERCENT", value, expected, returned) - t.Fail() + testCases := []struct { + dataType int32 + dataTypeName string + value int64 + expected float64 + }{ + {ibmmq.MQIAMO_MONITOR_PERCENT, "MQIAMO_MONITOR_PERCENT", 1, 0.01}, + {ibmmq.MQIAMO_MONITOR_PERCENT, "MQIAMO_MONITOR_PERCENT", 1000000, 10000}, + {ibmmq.MQIAMO_MONITOR_HUNDREDTHS, "MQIAMO_MONITOR_HUNDREDTHS", 1, 0.01}, + {ibmmq.MQIAMO_MONITOR_HUNDREDTHS, "MQIAMO_MONITOR_HUNDREDTHS", 1000000, 10000}, + {ibmmq.MQIAMO_MONITOR_MB, "MQIAMO_MONITOR_MB", 1000000, 1048576000000}, + {ibmmq.MQIAMO_MONITOR_GB, "MQIAMO_MONITOR_GB", 1000000, 1073741824000000}, + {ibmmq.MQIAMO_MONITOR_MICROSEC, "MQIAMO_MONITOR_MICROSEC", 1000000, 1}, + {ibmmq.MQIAMO_MONITOR_MICROSEC, "MQIAMO_MONITOR_MICROSEC", 1, 0.000001}, } - test.Datatype = ibmmq.MQIAMO_MONITOR_HUNDREDTHS - expected = 10000 - returned = Normalise(&test, "", value) - if returned != expected { - t.Logf("Gave %s, %d. Expected: %f, Got: %f", "ibmmq.MQIAMO_MONITOR_HUNDREDTHS", value, expected, returned) - t.Fail() + for _, tc := range testCases { + t.Run(fmt.Sprintf("%s/%d", tc.dataTypeName, tc.value), func(t *testing.T) { + test := MonElement{Datatype: tc.dataType} + returned := Normalise(&test, "", tc.value) + if returned != tc.expected { + t.Logf("Gave %s, %d. Expected: %f, Got: %f", tc.dataTypeName, tc.value, tc.expected, returned) + t.Fail() + } + }) } +} - test.Datatype = ibmmq.MQIAMO_MONITOR_MB - expected = 1048576000000 - returned = Normalise(&test, "", value) - if returned != expected { - t.Logf("Gave %s, %d. Expected: %f, Got: %f", "ibmmq.MQIAMO_MONITOR_MB", value, expected, returned) - t.Fail() +func TestReadPatterns(t *testing.T) { + const filename = "testFile" + testCases := []struct { + name string + value string + expected string + }{ + {"golden", "test1=yes\ntest2=no\n", "test1=yes,test2=no"}, + {"nolf", "test1=yes\ntest2=no", "test1=yes,test2=no"}, + {"crlf", "test1=yes\r\ntest2=no\r\n", "test1=yes,test2=no"}, + {"oneliner", "test1=yes,test2=no\ntest3=maybe", "test1=yes,test2=no,test3=maybe"}, } - test.Datatype = ibmmq.MQIAMO_MONITOR_GB - expected = 1073741824000000 - returned = Normalise(&test, "", value) - if returned != expected { - t.Logf("Gave %s, %d. Expected: %f, Got: %f", "ibmmq.MQIAMO_MONITOR_GB", value, expected, returned) - t.Fail() + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + //Create dummy test file + err := ioutil.WriteFile(filename, []byte(tc.value), 0644) + if err != nil { + t.Fatalf("Could not create test file - %v", err) + } + defer os.Remove(filename) + + returned, err := ReadPatterns(filename) + if err != nil { + t.Logf("Got error while running ReadPatterns - %v", err) + t.Fail() + } else if returned != tc.expected { + t.Logf("File was not parsed correctly. Expected: %s. Got: %s", tc.expected, returned) + t.Fail() + } + }) + } +} +func TestFormatDescription(t *testing.T) { + testCases := []struct { + value string + expected string + }{ + {"hello", "hello_count"}, + {"no space", "no_space_count"}, + {"no/slash", "no_slash_count"}, + {"no-dash", "no_dash_count"}, + {"single___underscore", "single_underscore_count"}, + {"single__underscore__multiplace", "single_underscore_multiplace_count"}, + {"ALLLOWER", "alllower_count"}, + {"this_bytes_written_switch", "this_written_switch_count"}, + {"this_byte_max_switch", "this_max_switch_count"}, + {"this_seconds_in_use_switch", "this_in_use_switch_count"}, + {"this messages_expired_switch", "this_expired_messages_switch_count"}, + {"this_seconds_max_switch", "this_max_switch_count"}, + {"this_count_max_switch", "this_max_switch_count"}, + {"this_percentage_max_switch", "this_max_switch_count"}, } - test.Datatype = ibmmq.MQIAMO_MONITOR_MICROSEC - expected = 1 - returned = Normalise(&test, "", value) - if returned != expected { - t.Logf("Gave %s, %d. Expected: %f, Got: %f", "ibmmq.MQIAMO_MONITOR_GB", value, expected, returned) - t.Fail() + for _, tc := range testCases { + t.Run(tc.value, func(t *testing.T) { + elem := MonElement{ + Description: tc.value, + } + returned := formatDescription(&elem) + if returned != tc.expected { + t.Logf("Gave %s. Expected: %s, Got: %s", tc.value, tc.expected, returned) + t.Fail() + } + }) } } -func TestReadPatterns(t *testing.T) { - const filename = "testFile" - //Create dummy test file - testData := []byte("test1=yes\ntest2=no\n") - err := ioutil.WriteFile(filename, testData, 0644) - if err != nil { - t.Fatalf("Could not create test file - %v", err) +func TestSuffixes(t *testing.T) { + baseDescription := "test_suffix" + testCases := []struct { + name string + value int32 + expected string + }{ + {"MQIAMO_MONITOR_MB", ibmmq.MQIAMO_MONITOR_MB, baseDescription + "_bytes"}, + {"MQIAMO_MONITOR_GB", ibmmq.MQIAMO_MONITOR_GB, baseDescription + "_bytes"}, + {"MQIAMO_MONITOR_MICROSEC", ibmmq.MQIAMO_MONITOR_MICROSEC, baseDescription + "_seconds"}, + {"MQIAMO_MONITOR_PERCENT", ibmmq.MQIAMO_MONITOR_PERCENT, baseDescription + "_percentage"}, + {"MQIAMO_MONITOR_HUNDREDTHS", ibmmq.MQIAMO_MONITOR_HUNDREDTHS, baseDescription + "_percentage"}, + {"0", 0, baseDescription + "_count"}, } - defer os.Remove(filename) - expected := "test1=yes,test2=no" - back, err := ReadPatterns(filename) - if err != nil { - t.Logf("Got error while running ReadPatterns - %v", err) - t.Fail() - } else if back != expected { - t.Logf("File was not parsed correctly. Expected: %s. Got: %s", expected, back) - t.Fail() + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + elem := MonElement{ + Description: baseDescription, + Datatype: tc.value, + } + returned := formatDescription(&elem) + if returned != tc.expected { + t.Logf("Gave %s/%d Expected: %s, Got: %s", baseDescription, tc.value, tc.expected, returned) + t.Fail() + } + }) } -} -func TestFormatDescription(t *testing.T) { - 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 { + // special case log_bytes + t.Run("log_bytes", func(t *testing.T) { elem := MonElement{ - Description: e, + Description: "log_test_suffix", + Datatype: 0, } - back := formatDescription(&elem) - if back != expected[i] { - t.Logf("Gave %s. Expected: %s, Got: %s", e, expected[i], back) + returned := formatDescription(&elem) + if returned != "log_test_suffix_bytes" { + t.Logf("Gave log_test_suffix/0 Expected: %s, Got: %s", "log_test_suffix_bytes", returned) t.Fail() } - } -} - -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 { + // special case log_total + t.Run("log_bytes", func(t *testing.T) { elem := MonElement{ - Description: baseDescription, - Datatype: ty, + Description: "log_total_suffix", + Datatype: 0, } - back := formatDescription(&elem) - if back != expected[i] { - t.Logf("Gave %s/%d Expected: %s, Got: %s", baseDescription, ty, expected[i], back) + returned := formatDescription(&elem) + if returned != "log_suffix_total" { + t.Logf("Gave log_total_suffix/0 Expected: %s, Got: %s", "log_suffix_total", returned) 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() +func TestParsePCFResponse(t *testing.T) { + testCases := []struct { + name string + params []ibmmq.PCFParameter + }{ + { + "noParams", + make([]ibmmq.PCFParameter, 0), + }, + { + "oneParam", + []ibmmq.PCFParameter{ + ibmmq.PCFParameter{ + Type: ibmmq.MQCFT_STRING, // String + Parameter: ibmmq.MQCACF_APPL_NAME, + String: []string{"HELLOTEST"}, + ParameterCount: 1, + }, + }, + }, + { + "twoParams", + []ibmmq.PCFParameter{ + ibmmq.PCFParameter{ + Type: ibmmq.MQCFT_STRING, // String + Parameter: ibmmq.MQCACF_APPL_NAME, + String: []string{"HELLOTEST"}, + ParameterCount: 1, + }, + ibmmq.PCFParameter{ + Type: ibmmq.MQCFT_STRING, // String + Parameter: ibmmq.MQCACF_APPL_NAME, + String: []string{"FIRST"}, + ParameterCount: 1, + }, + }, + }, } - // 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() + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + cfh := ibmmq.NewMQCFH() + cfh.Type = ibmmq.MQCFT_RESPONSE + cfh.ParameterCount = int32(len(tc.params)) + headerbytes := cfh.Bytes() + + parmbytes := []byte{} + for _, parm := range tc.params { + parmbytes = append(parmbytes, parm.Bytes()...) + } + messagebytes := append(headerbytes, parmbytes...) + + returned, last := parsePCFResponse(messagebytes) + + if len(returned) != len(tc.params) && !last { + t.Logf("Gave header and parameter. Expected: 1, false , Got: %d, %t", len(returned), last) + t.Fail() + } else { + for i := range returned { + t.Logf("Checking param %d", i) + checkParamsMatch(returned[i], &tc.params[i], t) + } + } + }) } } -func TestParsePCFResponse(t *testing.T) { - cfh := ibmmq.NewMQCFH() - cfh.Type = ibmmq.MQCFT_RESPONSE - headerbytes := cfh.Bytes() - - params, last := parsePCFResponse(headerbytes) - if len(params) != 0 && !last { - t.Logf("Gave just a header. Expected: 0, false , Got: %d, %t", len(params), last) +func checkParamsMatch(returned *ibmmq.PCFParameter, expected *ibmmq.PCFParameter, t *testing.T) { + if returned.Type != expected.Type { + t.Logf("Returned parameter 'Type' did not match. Expected: %d, Got: %d", expected.Type, returned.Type) t.Fail() } - - cfh.ParameterCount = 1 - parm := ibmmq.PCFParameter{ - Type: ibmmq.MQCFT_STRING, // String - Parameter: ibmmq.MQCACF_APPL_NAME, - String: []string{"HELLOTEST"}, - ParameterCount: 1, + if returned.Parameter != expected.Parameter { + t.Logf("Returned parameter 'Parameter' did not match. Expected: %d, Got: %d", expected.Parameter, returned.Parameter) + t.Fail() } - headerbytes = cfh.Bytes() - parmbytes := parm.Bytes() - messagebytes := append(headerbytes, parmbytes...) - - params, last = parsePCFResponse(messagebytes) - if len(params) != 1 && !last { - t.Logf("Gave header and parameter. Expected: 1, false , Got: %d, %t", len(params), last) + if len(returned.String) != len(expected.String) { + t.Logf("Length of Returned parameter 'String' did not match. Expected: %d, Got: %d", len(expected.String), len(returned.String)) t.Fail() } else { - elem := params[0] - if elem.Type != parm.Type { - t.Logf("Returned parameter 'Type' did not match. Expected: %d, Got: %d", parm.Type, elem.Type) - t.Fail() - } - if elem.Parameter != parm.Parameter { - t.Logf("Returned parameter 'Parameter' did not match. Expected: %d, Got: %d", parm.Parameter, elem.Parameter) - t.Fail() + for i := range returned.String { + if returned.String[i] != expected.String[i] { + t.Logf("Returned parameter 'String[%d]' did not match. Expected: %s, Got: %s", i, expected.String[i], returned.String[i]) + t.Fail() + } } - if len(elem.String) != len(parm.String) { - t.Logf("Length of Returned parameter 'String' did not match. Expected: %d, Got: %d", len(parm.String), len(elem.String)) - t.Fail() - } else if elem.String[0] != parm.String[0] { - t.Logf("Returned parameter 'String' did not match. Expected: %s, Got: %s", parm.String[0], elem.String[0]) - t.Fail() - } - if len(elem.Int64Value) != 0 { - t.Logf("Returned parameter 'Int64Value' was not empty, length=%d", len(elem.Int64Value)) - t.Fail() + } + if len(returned.Int64Value) != len(expected.Int64Value) { + t.Logf("Length of Returned parameter 'Int64Value' did not match. Expected: %d, Got: %d", len(expected.Int64Value), len(returned.Int64Value)) + t.Fail() + } else { + for i := range returned.Int64Value { + if returned.Int64Value[i] != expected.Int64Value[i] { + t.Logf("Returned parameter 'Int64Value[%d]' did not match. Expected: %d, Got: %d", i, expected.Int64Value[i], returned.Int64Value[i]) + t.Fail() + } } - if len(elem.GroupList) != 0 { - t.Logf("Returned parameter 'GroupList' was not empty, length=%d", len(elem.GroupList)) - t.Fail() + } + if len(returned.GroupList) != len(expected.GroupList) { + t.Logf("Length of Returned parameter 'GroupList' did not match. Expected: %d, Got: %d", len(expected.GroupList), len(returned.GroupList)) + t.Fail() + } else { + for i := range returned.GroupList { + checkParamsMatch(returned.GroupList[i], expected.GroupList[i], t) } } }