TUN-6868: Return left padded tracing ID when tracing identity is converted to string

This commit is contained in:
cthuang
2022-10-18 13:09:38 +01:00
committed by Chung-Ting
parent 60a12fcb27
commit 11f4d10174
2 changed files with 41 additions and 18 deletions

View File

@@ -10,40 +10,55 @@ func TestNewIdentity(t *testing.T) {
testCases := []struct {
testCase string
trace string
valid bool
expected string
}{
{
testCase: "full length trace",
trace: "ec31ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
valid: true,
expected: "ec31ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
},
{
testCase: "short trace ID",
trace: "ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
valid: true,
expected: "0000ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
},
{
testCase: "short trace ID with 0s in the middle",
trace: "ad8a01fde11f000002efdce36873:52726f6cabc144f5:0:1",
expected: "0000ad8a01fde11f000002efdce36873:52726f6cabc144f5:0:1",
},
{
testCase: "short trace ID with 0s in the beginning and middle",
trace: "001ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
expected: "0001ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
},
{
testCase: "no trace",
trace: "",
valid: false,
},
{
testCase: "missing flags",
trace: "ec31ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0",
valid: false,
},
{
testCase: "missing separator",
trace: "ec31ad8a01fde11fdcabe2efdce3687352726f6cabc144f501",
valid: false,
},
}
for _, testCase := range testCases {
identity, err := NewIdentity(testCase.trace)
if testCase.valid {
require.NoError(t, err, testCase.testCase)
require.Equal(t, testCase.trace, identity.String())
if testCase.expected != "" {
require.NoError(t, err)
require.Equal(t, testCase.expected, identity.String())
serializedIdentity, err := identity.MarshalBinary()
require.NoError(t, err)
deserializedIdentity := new(Identity)
err = deserializedIdentity.UnmarshalBinary(serializedIdentity)
require.NoError(t, err)
require.Equal(t, identity, deserializedIdentity)
} else {
require.Error(t, err)
require.Nil(t, identity)