|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Additional copyrights: |
| 16 | +// Copyright The Jaeger Authors |
| 17 | + |
| 18 | +package naming |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | +) |
| 25 | + |
| 26 | +func TestTruncate(t *testing.T) { |
| 27 | + for _, tt := range []struct { |
| 28 | + format string |
| 29 | + max int |
| 30 | + values []interface{} |
| 31 | + expected string |
| 32 | + cap string |
| 33 | + }{ |
| 34 | + { |
| 35 | + format: "%s-collector", |
| 36 | + max: 63, |
| 37 | + values: []interface{}{"simplest"}, |
| 38 | + expected: "simplest-collector", |
| 39 | + cap: "the standard case", |
| 40 | + }, |
| 41 | + { |
| 42 | + format: "d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85b7644b6b5", |
| 43 | + max: 63, |
| 44 | + values: []interface{}{}, |
| 45 | + expected: "d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85", |
| 46 | + cap: "first N case", |
| 47 | + }, |
| 48 | + { |
| 49 | + format: "%s-collector", |
| 50 | + max: 63, |
| 51 | + values: []interface{}{"d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85b7644b6b5"}, |
| 52 | + expected: "d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11e-collector", |
| 53 | + cap: "instance + fixed within bounds", |
| 54 | + }, |
| 55 | + { |
| 56 | + format: "%s-%s-collector", |
| 57 | + max: 63, |
| 58 | + values: []interface{}{"d0c1e62", "4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85b7644b6b5"}, |
| 59 | + expected: "4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174--collector", |
| 60 | + cap: "first value gets dropped, second truncated", |
| 61 | + }, |
| 62 | + { |
| 63 | + format: "%d-%s-collector", |
| 64 | + max: 63, |
| 65 | + values: []interface{}{42, "d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85b7644b6b5"}, |
| 66 | + expected: "42-d0c1e62-4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96--collector", |
| 67 | + cap: "first value gets passed, second truncated", |
| 68 | + }, |
| 69 | + } { |
| 70 | + assert.Equal(t, tt.expected, Truncate(tt.format, tt.max, tt.values...)) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestTrimNonAlphaNumeric(t *testing.T) { |
| 75 | + tests := []struct { |
| 76 | + input string |
| 77 | + expected string |
| 78 | + }{ |
| 79 | + { |
| 80 | + input: "-%$#ThisIsALabel", |
| 81 | + expected: "ThisIsALabel", |
| 82 | + }, |
| 83 | + |
| 84 | + { |
| 85 | + input: "label-invalid--_truncated-.", |
| 86 | + expected: "label-invalid--_truncated", |
| 87 | + }, |
| 88 | + |
| 89 | + { |
| 90 | + input: "--(label-invalid--_truncated-#.1.", |
| 91 | + expected: "label-invalid--_truncated-#.1", |
| 92 | + }, |
| 93 | + |
| 94 | + { |
| 95 | + input: "12ValidLabel3", |
| 96 | + expected: "12ValidLabel3", |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for _, test := range tests { |
| 101 | + output := trimNonAlphaNumeric(test.input) |
| 102 | + assert.Equal(t, test.expected, output) |
| 103 | + } |
| 104 | +} |
0 commit comments