github.com/aavshr/aws-sdk-go@v1.41.3/aws/csm/metric_test.go (about) 1 //go:build go1.7 2 // +build go1.7 3 4 package csm 5 6 import ( 7 "reflect" 8 "testing" 9 10 "github.com/aavshr/aws-sdk-go/aws" 11 ) 12 13 func TestTruncateString(t *testing.T) { 14 cases := map[string]struct { 15 Val string 16 Len int 17 Expect string 18 }{ 19 "no change": { 20 Val: "123456789", Len: 10, 21 Expect: "123456789", 22 }, 23 "max len": { 24 Val: "1234567890", Len: 10, 25 Expect: "1234567890", 26 }, 27 "too long": { 28 Val: "12345678901", Len: 10, 29 Expect: "1234567890", 30 }, 31 } 32 33 for name, c := range cases { 34 t.Run(name, func(t *testing.T) { 35 v := c.Val 36 actual := truncateString(&v, c.Len) 37 if e, a := c.Val, v; e != a { 38 t.Errorf("expect input value not to change, %v, %v", e, a) 39 } 40 if e, a := c.Expect, *actual; e != a { 41 t.Errorf("expect %v, got %v", e, a) 42 } 43 }) 44 } 45 46 } 47 48 func TestMetric_SetException(t *testing.T) { 49 cases := map[string]struct { 50 Exc metricException 51 Expect metric 52 Final bool 53 }{ 54 "aws exc": { 55 Exc: awsException{ 56 requestException{exception: "abc", message: "123"}, 57 }, 58 Expect: metric{ 59 AWSException: aws.String("abc"), 60 AWSExceptionMessage: aws.String("123"), 61 }, 62 }, 63 "sdk exc": { 64 Exc: sdkException{ 65 requestException{exception: "abc", message: "123"}, 66 }, 67 Expect: metric{ 68 SDKException: aws.String("abc"), 69 SDKExceptionMessage: aws.String("123"), 70 }, 71 }, 72 "final aws exc": { 73 Exc: awsException{ 74 requestException{exception: "abc", message: "123"}, 75 }, 76 Expect: metric{ 77 FinalAWSException: aws.String("abc"), 78 FinalAWSExceptionMessage: aws.String("123"), 79 }, 80 Final: true, 81 }, 82 "final sdk exc": { 83 Exc: sdkException{ 84 requestException{exception: "abc", message: "123"}, 85 }, 86 Expect: metric{ 87 FinalSDKException: aws.String("abc"), 88 FinalSDKExceptionMessage: aws.String("123"), 89 }, 90 Final: true, 91 }, 92 } 93 94 for name, c := range cases { 95 t.Run(name, func(t *testing.T) { 96 var m metric 97 if c.Final { 98 m.SetFinalException(c.Exc) 99 } else { 100 m.SetException(c.Exc) 101 } 102 if e, a := c.Expect, m; !reflect.DeepEqual(e, a) { 103 t.Errorf("expect:\n%#v\nactual:\n%#v\n", e, a) 104 } 105 }) 106 } 107 }