From cf2118eab3b736feb8018bcc9f4cdb548b7ab70a Mon Sep 17 00:00:00 2001
From: Giuseppe Scrivano <gscrivan@redhat.com>
Date: Wed, 14 Sep 2022 23:18:54 +0200
Subject: [PATCH] stats: map MaxUsage to the correct value

and make sure it is not set for cgroup v2

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
---
 pkg/api/handlers/compat/containers_stats.go | 2 +-
 test/apiv2/20-containers.at                 | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/pkg/api/handlers/compat/containers_stats.go b/pkg/api/handlers/compat/containers_stats.go
index c115b41817..16311ef1e6 100644
--- a/pkg/api/handlers/compat/containers_stats.go
+++ b/pkg/api/handlers/compat/containers_stats.go
@@ -177,7 +177,7 @@ streamLabel: // A label to flatten the scope
 				PreCPUStats: preCPUStats,
 				MemoryStats: docker.MemoryStats{
 					Usage:             cgroupStat.MemoryStats.Usage.Usage,
-					MaxUsage:          cgroupStat.MemoryStats.Usage.Limit,
+					MaxUsage:          cgroupStat.MemoryStats.Usage.MaxUsage,
 					Stats:             nil,
 					Failcnt:           0,
 					Limit:             memoryLimit,
diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at
index 9ace46b8b6..e581b10d10 100644
--- a/test/apiv2/20-containers.at
+++ b/test/apiv2/20-containers.at
@@ -98,6 +98,12 @@ else
     fi
 fi
 
+# max_usage is not set for cgroupv2
+if have_cgroupsv2; then
+    t GET libpod/containers/stats?containers='[$cid]' 200 \
+    .memory_stats.max_usage=null
+fi
+
 t DELETE libpod/containers/$cid 200 .[0].Id=$cid
 
 # Issue #14676: make sure the stats show the memory limit specified for the container