mirror of
https://github.com/intel/intel-device-plugins-for-kubernetes.git
synced 2025-06-03 03:59:37 +00:00

If k8s label starts or ends with a non alphanumeric char, it is ignored. This new split method cuts labels from the last alphanum characters and adds a control character to the beginning of the next chunk. The entity that uses these labels needs to then remove the control character before concatenating the chunks. Co-authored-by: Ukri Niemimuukko <ukri.niemimuukko@intel.com> Signed-off-by: Tuomas Katila <tuomas.katila@intel.com>
123 lines
2.8 KiB
Go
123 lines
2.8 KiB
Go
// Copyright 2020-2021 Intel Corporation. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package pluginutils
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestSplitAlphaNumeric(t *testing.T) {
|
|
type testData struct {
|
|
label string
|
|
prefix string
|
|
expStrings []string
|
|
maxLength int
|
|
}
|
|
|
|
tds := []testData{
|
|
{
|
|
"0.0-1.0_0.1-1.1_0.1-1.1_0.0-1.0_0.1-1.1_0.0-1.0_0.1-1.1_0.0-1.0_0.1-1.1_0.0-1.0_0.1-1.1_0.0-1.0_0.1-1.1_0.1-1.1_1.0-0.0_1.1-0.1_1.0-0.0_1.1-0.1",
|
|
"Z",
|
|
[]string{
|
|
"0.0-1.0_0.1-1.1_0.1-1.1_0.0-1.0_0.1-1.1_0.0-1.0_0.1-1.1_0.0-1.0",
|
|
"Z_0.1-1.1_0.0-1.0_0.1-1.1_0.0-1.0_0.1-1.1_0.1-1.1_1.0-0.0_1.1-0",
|
|
"Z.1_1.0-0.0_1.1-0.1",
|
|
},
|
|
63,
|
|
},
|
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YYYZZZZZ",
|
|
[]string{},
|
|
4,
|
|
},
|
|
}
|
|
|
|
for _, td := range tds {
|
|
res := SplitAtLastAlphaNum(td.label, uint(td.maxLength), td.prefix)
|
|
|
|
if len(res) != len(td.expStrings) {
|
|
t.Errorf("Got invalid amount of string chunks: %d", len(res))
|
|
}
|
|
|
|
for i, s := range td.expStrings {
|
|
if res[i] != s {
|
|
t.Errorf("Invalid chunk from split %s (vs. %s)", res[i], s)
|
|
}
|
|
|
|
if len(res[i]) > td.maxLength {
|
|
t.Errorf("Chunk is too long %d (vs. %d)", len(res[i]), td.maxLength)
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, td := range tds[:4] {
|
|
res := ConcatAlphaNumSplitChunks(td.expStrings, td.prefix)
|
|
|
|
if res != td.label {
|
|
t.Errorf("Invalid concatenated string: %s vs. %s", res, td.label)
|
|
}
|
|
}
|
|
}
|