mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-18 04:54:54 +00:00
Add tmpfs detection to mounttest and unit tests
Refactor fsType() to use a platform-specific formatFsType() helper that translates filesystem magic numbers to human-readable names. On Linux, tmpfs filesystems now display as "tmpfs" instead of the raw magic number. Signed-off-by: Davanum Srinivas <davanum@gmail.com>
This commit is contained in:
@@ -39,7 +39,7 @@ func fsType(path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("mount type of %q: %v\n", path, buf.Type)
|
||||
fmt.Printf("mount type of %q: %s\n", path, formatFsType(int64(buf.Type)))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
34
test/images/agnhost/mounttest/mt_utils_linux.go
Normal file
34
test/images/agnhost/mounttest/mt_utils_linux.go
Normal file
@@ -0,0 +1,34 @@
|
||||
//go:build linux
|
||||
|
||||
/*
|
||||
Copyright 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 mounttest
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// formatFsType returns a string representation of the filesystem type.
|
||||
// It translates known filesystem magic numbers to human-readable names.
|
||||
func formatFsType(fsType int64) string {
|
||||
if fsType == unix.TMPFS_MAGIC {
|
||||
return "tmpfs"
|
||||
}
|
||||
return strconv.FormatInt(fsType, 10)
|
||||
}
|
||||
26
test/images/agnhost/mounttest/mt_utils_other.go
Normal file
26
test/images/agnhost/mounttest/mt_utils_other.go
Normal file
@@ -0,0 +1,26 @@
|
||||
//go:build !linux && !windows
|
||||
|
||||
/*
|
||||
Copyright 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 mounttest
|
||||
|
||||
import "strconv"
|
||||
|
||||
// formatFsType returns a string representation of the filesystem type.
|
||||
func formatFsType(fsType int64) string {
|
||||
return strconv.FormatInt(fsType, 10)
|
||||
}
|
||||
117
test/images/agnhost/mounttest/mt_utils_test.go
Normal file
117
test/images/agnhost/mounttest/mt_utils_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
//go:build linux
|
||||
|
||||
/*
|
||||
Copyright 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 mounttest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func captureStdout(f func()) string {
|
||||
old := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
f()
|
||||
|
||||
w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func TestFsType_EmptyPath(t *testing.T) {
|
||||
output := captureStdout(func() {
|
||||
err := fsType("")
|
||||
if err != nil {
|
||||
t.Errorf("fsType(\"\") returned error: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
if output != "" {
|
||||
t.Errorf("fsType(\"\") should not print anything, got: %q", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsType_ValidPath(t *testing.T) {
|
||||
// Use a path that exists on the system
|
||||
testPath := os.TempDir()
|
||||
|
||||
output := captureStdout(func() {
|
||||
err := fsType(testPath)
|
||||
if err != nil {
|
||||
t.Errorf("fsType(%q) returned error: %v", testPath, err)
|
||||
}
|
||||
})
|
||||
|
||||
// Verify the output format
|
||||
expectedPrefix := fmt.Sprintf("mount type of %q:", testPath)
|
||||
if !strings.HasPrefix(output, expectedPrefix) {
|
||||
t.Errorf("fsType(%q) output should start with %q, got: %q", testPath, expectedPrefix, output)
|
||||
}
|
||||
|
||||
// Verify the output is printed exactly once (not duplicated)
|
||||
lines := strings.Split(strings.TrimSpace(output), "\n")
|
||||
if len(lines) != 1 {
|
||||
t.Errorf("fsType(%q) should print exactly one line, got %d lines: %q", testPath, len(lines), output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsType_NonExistentPath(t *testing.T) {
|
||||
testPath := "/nonexistent/path/that/does/not/exist"
|
||||
|
||||
output := captureStdout(func() {
|
||||
err := fsType(testPath)
|
||||
if err == nil {
|
||||
t.Errorf("fsType(%q) should return error for non-existent path", testPath)
|
||||
}
|
||||
})
|
||||
|
||||
// Should print an error message
|
||||
if !strings.Contains(output, "error from statfs") {
|
||||
t.Errorf("fsType(%q) should print error message, got: %q", testPath, output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatFsType_Tmpfs(t *testing.T) {
|
||||
result := formatFsType(unix.TMPFS_MAGIC)
|
||||
|
||||
expected := "tmpfs"
|
||||
if result != expected {
|
||||
t.Errorf("formatFsType(TMPFS_MAGIC) = %q, want %q", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatFsType_OtherType(t *testing.T) {
|
||||
otherType := int64(0x12345678)
|
||||
result := formatFsType(otherType)
|
||||
|
||||
expected := "305419896"
|
||||
if result != expected {
|
||||
t.Errorf("formatFsType(%v) = %q, want %q", otherType, result, expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user