Skip to content

Commit 39af492

Browse files
committed
UPSTREAM: 131217: Shorten long directory names with e2e pod logs
1 parent b9412cf commit 39af492

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

test/e2e/storage/utils/file.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import (
20+
"fmt"
21+
"hash/crc32"
22+
)
23+
24+
// The max length for ntfs, ext4, xfs and btrfs.
25+
const maxFileNameLength = 255
26+
27+
// Shorten a file name to size allowed by the most common filesystems.
28+
// If the filename is too long, cut it + add a short hash (crc32) that makes it unique.
29+
// Note that the input should be a single file / directory name, not a path
30+
// composed of several directories.
31+
func ShortenFileName(filename string) string {
32+
if len(filename) <= maxFileNameLength {
33+
return filename
34+
}
35+
36+
hash := crc32.ChecksumIEEE([]byte(filename))
37+
hashString := fmt.Sprintf("%x", hash)
38+
hashLen := len(hashString)
39+
40+
return fmt.Sprintf("%s-%s", filename[:maxFileNameLength-1-hashLen], hashString)
41+
}

test/e2e/storage/utils/file_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestShortenFileName(t *testing.T) {
26+
const hashLength = 10
27+
tests := []struct {
28+
name string
29+
filename string
30+
expected string
31+
}{
32+
{
33+
name: "Shorter than max length",
34+
filename: "short file name",
35+
expected: "short file name",
36+
},
37+
{
38+
name: "Longer than max length, truncated",
39+
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..",
40+
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",
41+
},
42+
{
43+
name: "Exactly max length, not truncated",
44+
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.",
45+
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.",
46+
},
47+
}
48+
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
result := ShortenFileName(tt.filename)
52+
assert.Equal(t, tt.expected, result)
53+
assert.LessOrEqual(t, len(result), maxFileNameLength)
54+
})
55+
}
56+
}

test/e2e/storage/utils/pod.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"io"
2323
"os"
2424
"path"
25+
"path/filepath"
2526
"regexp"
26-
"strings"
2727

2828
"github.com/onsi/ginkgo/v2"
2929
"github.com/onsi/gomega"
@@ -68,6 +68,12 @@ func StartPodLogs(ctx context.Context, f *framework.Framework, driverNamespace *
6868
testName = append(testName, reg.ReplaceAllString(test.LeafNodeText, "_"))
6969
}
7070
}
71+
72+
// Make sure each directory name is short enough for Linux + Windows
73+
for i, testNameComponent := range testName {
74+
testName[i] = ShortenFileName(testNameComponent)
75+
}
76+
7177
// We end the prefix with a slash to ensure that all logs
7278
// end up in a directory named after the current test.
7379
//
@@ -76,7 +82,7 @@ func StartPodLogs(ctx context.Context, f *framework.Framework, driverNamespace *
7682
// keeps each directory name smaller (the full test
7783
// name at one point exceeded 256 characters, which was
7884
// too much for some filesystems).
79-
logDir := framework.TestContext.ReportDir + "/" + strings.Join(testName, "/")
85+
logDir := filepath.Join(framework.TestContext.ReportDir, filepath.Join(testName...))
8086
to.LogPathPrefix = logDir + "/"
8187

8288
err := os.MkdirAll(logDir, 0755)

0 commit comments

Comments
 (0)