You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.0 KiB
Go
39 lines
1.0 KiB
Go
package hypervisor
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestEstimateMemoryUsedKiBRequiresAvailableAndUnused(t *testing.T) {
|
|
available := uint64(2 * 1024 * 1024)
|
|
unused := uint64(512 * 1024)
|
|
used := estimateMemoryUsedKiB(memoryUsageStats{
|
|
AvailableKiB: &available,
|
|
UnusedKiB: &unused,
|
|
})
|
|
if used == nil || *used != 1536*1024 {
|
|
t.Fatalf("used memory = %v", used)
|
|
}
|
|
if got := estimateMemoryUsedKiB(memoryUsageStats{AvailableKiB: &available}); got != nil {
|
|
t.Fatalf("missing unused field produced %d", *got)
|
|
}
|
|
invalidUnused := available + 1
|
|
if got := estimateMemoryUsedKiB(memoryUsageStats{
|
|
AvailableKiB: &available, UnusedKiB: &invalidUnused,
|
|
}); got != nil {
|
|
t.Fatalf("invalid memory statistics produced %d", *got)
|
|
}
|
|
}
|
|
|
|
func TestMetricsMarshalUnavailableMemoryAsNull(t *testing.T) {
|
|
encoded, err := json.Marshal(Metrics{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(encoded), `"memory_used_kib":null`) {
|
|
t.Fatalf("unavailable memory did not marshal as null: %s", encoded)
|
|
}
|
|
}
|