Use v2.0.0 of mq-golang now it has been created (#159)

* Use v2.0.0 of mq-golang now it has been created

* do a strip vendor
This commit is contained in:
Rob Parker
2018-07-13 10:51:54 +01:00
committed by Stephen Marshall
parent 59eaa9c7d2
commit e4f02d55cf
447 changed files with 75582 additions and 25951 deletions

View File

@@ -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()
}
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()
}
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()
}
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(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()
}
})
}
}
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)
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"},
}
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) {
//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) {
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"}
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"},
}
for i, e := range give {
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()
}
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 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"}
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"},
}
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()
}
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()
}
})
}
// 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()
}
t.Run("log_bytes", func(t *testing.T) {
elem := MonElement{
Description: "log_test_suffix",
Datatype: 0,
}
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()
}
})
// 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()
}
t.Run("log_bytes", func(t *testing.T) {
elem := MonElement{
Description: "log_total_suffix",
Datatype: 0,
}
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()
}
})
}
func TestParsePCFResponse(t *testing.T) {
cfh := ibmmq.NewMQCFH()
cfh.Type = ibmmq.MQCFT_RESPONSE
headerbytes := cfh.Bytes()
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,
},
},
},
}
params, last := parsePCFResponse(headerbytes)
if len(params) != 0 && !last {
t.Logf("Gave just a header. Expected: 0, false , Got: %d, %t", len(params), last)
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 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()
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 elem.Parameter != parm.Parameter {
t.Logf("Returned parameter 'Parameter' did not match. Expected: %d, Got: %d", parm.Parameter, elem.Parameter)
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.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(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)
}
}
}