You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.4 KiB
38 lines
1.4 KiB
package systemmetrics
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"math"
|
|
"tera-clone/services/tera-api/internal/domain"
|
|
"testing"
|
|
)
|
|
|
|
func TestStorageMetricsDistinguishUsedAvailableAndReserved(t *testing.T) {
|
|
m, err := storageMetrics("/userdata", 1000, 750, 700, 4096)
|
|
if err != nil || m.TotalBytes != 4096000 || m.UsedBytes != 1024000 || m.AvailableBytes != 2867200 || m.ReservedBytes != 204800 || m.UsagePercent != 25 {
|
|
t.Fatalf("metrics=%+v,%v", m, err)
|
|
}
|
|
for _, v := range [][4]uint64{{0, 0, 0, 4096}, {10, 11, 0, 1}, {10, 4, 5, 1}, {1, 0, 0, 0}, {math.MaxUint64, 0, 0, 2}} {
|
|
if _, err := storageMetrics("/userdata", v[0], v[1], v[2], v[3]); err == nil {
|
|
t.Fatalf("accepted %v", v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStorageReadFailureIsUnknownNotZeroUsage(t *testing.T) {
|
|
root := t.TempDir()
|
|
r := NewLinuxReader(LinuxReaderOptions{ProcRoot: root, SysRoot: root, EtcRoot: root, StorageReader: func(path string) (*domain.StorageMetrics, error) {
|
|
if path != "/userdata" {
|
|
t.Fatalf("path=%s", path)
|
|
}
|
|
return nil, errors.New("offline")
|
|
}})
|
|
if got := r.Read(context.Background(), nil); got.Storage != nil {
|
|
t.Fatal("failed read was fabricated")
|
|
}
|
|
r.storageReader = func(path string) (*domain.StorageMetrics, error) { return storageMetrics(path, 100, 80, 75, 4096) }
|
|
if got := r.Read(context.Background(), nil); got.Storage == nil || got.Storage.UsagePercent != 20 {
|
|
t.Fatalf("storage=%+v", got.Storage)
|
|
}
|
|
}
|
|
|