Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions collector/filesystem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package collector

import (
"bytes"
"errors"
"fmt"
"log/slog"
Expand Down Expand Up @@ -237,13 +236,13 @@ func isFilesystemReadOnly(labels filesystemLabels) bool {
}

func mountOptionsString(m map[string]string) string {
b := new(bytes.Buffer)
parts := make([]string, 0, len(m))
for key, value := range m {
if value == "" {
fmt.Fprintf(b, "%s", key)
parts = append(parts, key)
} else {
fmt.Fprintf(b, "%s=%s", key, value)
parts = append(parts, key+"="+value)
}
}
return b.String()
return strings.Join(parts, ",")
}
84 changes: 84 additions & 0 deletions collector/filesystem_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package collector
import (
"io"
"log/slog"
"sort"
"strings"
"testing"

"github.com/alecthomas/kingpin/v2"
Expand Down Expand Up @@ -168,6 +170,88 @@ func TestMountsFallback(t *testing.T) {
}
}

func TestMountOptionsString(t *testing.T) {
tests := []struct {
name string
input map[string]string
wantParts []string
}{
{
name: "single flag option",
input: map[string]string{"ro": ""},
wantParts: []string{"ro"},
},
{
name: "single key=value option",
input: map[string]string{"errors": "remount-ro"},
wantParts: []string{"errors=remount-ro"},
},
{
name: "multiple options including ro",
input: map[string]string{"ro": "", "relatime": "", "errors": "remount-ro"},
wantParts: []string{"errors=remount-ro", "relatime", "ro"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := mountOptionsString(tt.input)
parts := strings.Split(result, ",")
sort.Strings(parts)
sort.Strings(tt.wantParts)
if len(parts) != len(tt.wantParts) {
t.Fatalf("mountOptionsString(%v) = %q, got %d parts, want %d", tt.input, result, len(parts), len(tt.wantParts))
}
for i := range parts {
if parts[i] != tt.wantParts[i] {
t.Errorf("mountOptionsString(%v) = %q, sorted part[%d] = %q, want %q", tt.input, result, i, parts[i], tt.wantParts[i])
}
}
})
}
}

func TestMountOptionsStringReadOnlyDetection(t *testing.T) {
tests := []struct {
name string
mountOptions map[string]string
superOptions map[string]string
wantReadOnly bool
}{
{
name: "ro among multiple mount options (ZFS / remount-ro scenario)",
mountOptions: map[string]string{"ro": "", "relatime": "", "errors": "remount-ro"},
superOptions: map[string]string{"rw": ""},
wantReadOnly: true,
},
{
name: "ro in super options only",
mountOptions: map[string]string{"rw": "", "relatime": ""},
superOptions: map[string]string{"ro": "", "user_id": "1000"},
wantReadOnly: true,
},
{
name: "no ro anywhere",
mountOptions: map[string]string{"rw": "", "nosuid": "", "nodev": ""},
superOptions: map[string]string{"rw": ""},
wantReadOnly: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
labels := filesystemLabels{
mountOptions: mountOptionsString(tt.mountOptions),
superOptions: mountOptionsString(tt.superOptions),
}
if got := isFilesystemReadOnly(labels); got != tt.wantReadOnly {
t.Errorf("isFilesystemReadOnly(%+v) = %v, want %v (mountOptions=%q superOptions=%q)",
tt, got, tt.wantReadOnly, labels.mountOptions, labels.superOptions)
}
})
}
}

func TestPathRootfs(t *testing.T) {
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_bindmount/proc", "--path.rootfs", "/host"}); err != nil {
t.Fatal(err)
Expand Down