ソースを参照

Merge pull request #46591 from vvoland/c8d-adjust-TestPsListContainersSize

integration-cli: Make TestPsListContainersSize work with c8d
Bjorn Neergaard 1 年間 前
コミット
8047b69ba3
1 ファイル変更18 行追加6 行削除
  1. 18 6
      integration-cli/docker_cli_ps_test.go

+ 18 - 6
integration-cli/docker_cli_ps_test.go

@@ -4,7 +4,6 @@ import (
 	"context"
 	"context"
 	"fmt"
 	"fmt"
 	"sort"
 	"sort"
-	"strconv"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 	"time"
 	"time"
@@ -13,6 +12,7 @@ import (
 	"github.com/docker/docker/integration-cli/cli"
 	"github.com/docker/docker/integration-cli/cli"
 	"github.com/docker/docker/integration-cli/cli/build"
 	"github.com/docker/docker/integration-cli/cli/build"
 	"github.com/docker/docker/pkg/stringid"
 	"github.com/docker/docker/pkg/stringid"
+	"github.com/docker/go-units"
 	"gotest.tools/v3/assert"
 	"gotest.tools/v3/assert"
 	is "gotest.tools/v3/assert/cmp"
 	is "gotest.tools/v3/assert/cmp"
 	"gotest.tools/v3/icmd"
 	"gotest.tools/v3/icmd"
@@ -159,8 +159,8 @@ func (s *DockerCLIPsSuite) TestPsListContainersSize(c *testing.T) {
 	baseOut, _ := dockerCmd(c, "ps", "-s", "-n=1")
 	baseOut, _ := dockerCmd(c, "ps", "-s", "-n=1")
 	baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
 	baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
 	baseSizeIndex := strings.Index(baseLines[0], "SIZE")
 	baseSizeIndex := strings.Index(baseLines[0], "SIZE")
-	baseFoundsize := baseLines[1][baseSizeIndex:]
-	baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, "B")[0])
+	baseFoundsize, _, _ := strings.Cut(baseLines[1][baseSizeIndex:], " ")
+	baseBytes, err := units.FromHumanSize(baseFoundsize)
 	assert.NilError(c, err)
 	assert.NilError(c, err)
 
 
 	name := "test_size"
 	name := "test_size"
@@ -186,9 +186,21 @@ func (s *DockerCLIPsSuite) TestPsListContainersSize(c *testing.T) {
 	idIndex := strings.Index(lines[0], "CONTAINER ID")
 	idIndex := strings.Index(lines[0], "CONTAINER ID")
 	foundID := lines[1][idIndex : idIndex+12]
 	foundID := lines[1][idIndex : idIndex+12]
 	assert.Equal(c, foundID, id[:12], fmt.Sprintf("Expected id %s, got %s", id[:12], foundID))
 	assert.Equal(c, foundID, id[:12], fmt.Sprintf("Expected id %s, got %s", id[:12], foundID))
-	expectedSize := fmt.Sprintf("%dB", 2+baseBytes)
-	foundSize := lines[1][sizeIndex:]
-	assert.Assert(c, strings.Contains(foundSize, expectedSize), "Expected size %q, got %q", expectedSize, foundSize)
+	foundSize, _, _ := strings.Cut(strings.TrimSpace(lines[1][sizeIndex:]), " ")
+
+	// With snapshotters the reported usage is the real space occupied on the
+	// filesystem (also includes metadata), so this new file can actually
+	// result in a bigger increase depending on the underlying filesystem (on
+	// ext4 this would be 4096 which is a minimum allocation unit).
+	if testEnv.UsingSnapshotter() {
+		newBytes, err := units.FromHumanSize(foundSize)
+		assert.NilError(c, err)
+		// Check if size increased by at least 2 bytes.
+		assert.Check(c, newBytes >= baseBytes+2)
+	} else {
+		expectedSize := units.HumanSize(float64(baseBytes + 2))
+		assert.Assert(c, strings.Contains(foundSize, expectedSize), "Expected size %q, got %q", expectedSize, foundSize)
+	}
 }
 }
 
 
 func (s *DockerCLIPsSuite) TestPsListContainersFilterStatus(c *testing.T) {
 func (s *DockerCLIPsSuite) TestPsListContainersFilterStatus(c *testing.T) {