mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-22 17:51:28 +00:00
In a downstream test job, I've seen an e2e test failing to create a directory for Pod logs when running an e2e test. Shorten them to 255 characters. For example, consider this test: "[sig-storage] CSI Mock selinux on mount metrics and SELinuxWarningController SELinuxMount metrics [LinuxOnly] [Feature:SELinux] [Serial] error is not bumped on two Pods with a different policy RWX volume (MountOption + MountOption) [FeatureGate:SELinuxMountReadWriteOncePod] [Beta] [FeatureGate:SELinuxChangePolicy] [Beta] [FeatureGate:SELinuxMount] [Beta] [Feature:OffByDefault] [sig-storage, Feature:SELinux, Serial, FeatureGate:SELinuxMountReadWriteOncePod, Beta, FeatureGate:SELinuxChangePolicy, FeatureGate:SELinuxMount, Feature:OffByDefault, BetaOffByDefault]" During execution, the test will create a directory `error_is_not_bumped_on_two_Pods_with_Recursive_policy_and_a_different_context_on_RWX_volume_FeatureGate_SELinuxMountReadWriteOncePod_Beta_FeatureGate_SELinuxChangePolicy_Beta_FeatureGate_SELinuxMount_Beta_Feature_OffByDefault_`, which has 226 characters and it's close to the limit (256).
56 lines
2.3 KiB
Go
56 lines
2.3 KiB
Go
/*
|
|
Copyright 2025 The Kubernetes Authors.
|
|
|
|
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 utils
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestShortenFileName(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
filename string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "Shorter than max length",
|
|
filename: "short file name",
|
|
expected: "short file name",
|
|
},
|
|
{
|
|
name: "Longer than max length, truncated",
|
|
filename: "a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 characters..",
|
|
expected: "a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 characters a very long string that has exactly 256 ch-ad31f675",
|
|
},
|
|
{
|
|
name: "Exactly max length, not truncated",
|
|
filename: "a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters.",
|
|
expected: "a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters a very long string that has exactly 255 characters.",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := ShortenFileName(tt.filename)
|
|
assert.Equal(t, tt.expected, result)
|
|
assert.LessOrEqual(t, len(result), maxFileNameLength)
|
|
})
|
|
}
|
|
}
|