|
9 | 9 | "github.com/sirupsen/logrus" |
10 | 10 |
|
11 | 11 | dockerContainerType "github.com/docker/docker/api/types/container" |
| 12 | + dockerImageType "github.com/docker/docker/api/types/image" |
12 | 13 | dockerNetworkType "github.com/docker/docker/api/types/network" |
13 | 14 | dockerNat "github.com/docker/go-connections/nat" |
14 | 15 |
|
@@ -79,6 +80,7 @@ var _ = ginkgo.Describe("the container", func() { |
79 | 80 | }, |
80 | 81 | ) |
81 | 82 | }) |
| 83 | + |
82 | 84 | ginkgo.Describe("GetCreateConfig", func() { |
83 | 85 | ginkgo.When("container healthcheck config is equal to image config", func() { |
84 | 86 | ginkgo.It("should return empty healthcheck values", func() { |
@@ -174,6 +176,7 @@ var _ = ginkgo.Describe("the container", func() { |
174 | 176 | }) |
175 | 177 | }) |
176 | 178 | }) |
| 179 | + |
177 | 180 | ginkgo.When("asked for metadata", func() { |
178 | 181 | var container *Container |
179 | 182 | ginkgo.BeforeEach(func() { |
@@ -635,4 +638,85 @@ var _ = ginkgo.Describe("the container", func() { |
635 | 638 | }) |
636 | 639 | }) |
637 | 640 | }) |
| 641 | + |
| 642 | + ginkgo.Describe("DisableMemorySwappiness Configuration", func() { |
| 643 | + var mockContainer *Container |
| 644 | + var defaultMemorySwappiness int64 = 60 |
| 645 | + containerName := "test-container" |
| 646 | + containerID := "test-container-id" |
| 647 | + |
| 648 | + WithMemorySwappiness := func(swappiness int64) MockContainerUpdate { |
| 649 | + return func(c *dockerContainerType.InspectResponse, _ *dockerImageType.InspectResponse) { |
| 650 | + if c.HostConfig == nil { |
| 651 | + c.HostConfig = &dockerContainerType.HostConfig{} |
| 652 | + } |
| 653 | + c.HostConfig.MemorySwappiness = &swappiness |
| 654 | + } |
| 655 | + } |
| 656 | + |
| 657 | + ginkgo.BeforeEach(func() { |
| 658 | + mockContainer = MockContainer(WithMemorySwappiness(defaultMemorySwappiness)) |
| 659 | + inspectResponse := dockerContainerType.InspectResponse{ |
| 660 | + ContainerJSONBase: &dockerContainerType.ContainerJSONBase{ |
| 661 | + ID: containerID, |
| 662 | + Name: "/" + containerName, |
| 663 | + HostConfig: mockContainer.GetCreateHostConfig(), |
| 664 | + State: &dockerContainerType.State{Running: true}, |
| 665 | + }, |
| 666 | + Config: &dockerContainerType.Config{}, |
| 667 | + } |
| 668 | + mockContainer.containerInfo = &inspectResponse |
| 669 | + }) |
| 670 | + |
| 671 | + ginkgo.When("DisableMemorySwappiness is true", func() { |
| 672 | + ginkgo.It("sets MemorySwappiness to nil and logs debug message", func() { |
| 673 | + logOutput := &bytes.Buffer{} |
| 674 | + logrus.SetOutput(logOutput) |
| 675 | + logrus.SetLevel(logrus.DebugLevel) |
| 676 | + |
| 677 | + clog := logrus.WithFields(logrus.Fields{ |
| 678 | + "container": mockContainer.Name(), |
| 679 | + "id": mockContainer.ID().ShortID(), |
| 680 | + }) |
| 681 | + hostConfig := mockContainer.GetCreateHostConfig() |
| 682 | + disableMemorySwappiness := true |
| 683 | + |
| 684 | + if disableMemorySwappiness { |
| 685 | + hostConfig.MemorySwappiness = nil |
| 686 | + clog.Debug("Disabled memory swappiness for Podman compatibility") |
| 687 | + } |
| 688 | + |
| 689 | + gomega.Expect(hostConfig.MemorySwappiness).To(gomega.BeNil(), |
| 690 | + "MemorySwappiness should be nil when DisableMemorySwappiness is true") |
| 691 | + gomega.Expect(logOutput.String()).To(gomega.ContainSubstring( |
| 692 | + "Disabled memory swappiness for Podman compatibility")) |
| 693 | + }) |
| 694 | + }) |
| 695 | + |
| 696 | + ginkgo.When("DisableMemorySwappiness is false", func() { |
| 697 | + ginkgo.It("preserves MemorySwappiness and does not log debug message", func() { |
| 698 | + logOutput := &bytes.Buffer{} |
| 699 | + logrus.SetOutput(logOutput) |
| 700 | + logrus.SetLevel(logrus.DebugLevel) |
| 701 | + |
| 702 | + clog := logrus.WithFields(logrus.Fields{ |
| 703 | + "container": mockContainer.Name(), |
| 704 | + "id": mockContainer.ID().ShortID(), |
| 705 | + }) |
| 706 | + hostConfig := mockContainer.GetCreateHostConfig() |
| 707 | + disableMemorySwappiness := false |
| 708 | + |
| 709 | + if disableMemorySwappiness { |
| 710 | + hostConfig.MemorySwappiness = nil |
| 711 | + clog.Debug("Disabled memory swappiness for Podman compatibility") |
| 712 | + } |
| 713 | + |
| 714 | + gomega.Expect(hostConfig.MemorySwappiness). |
| 715 | + To(gomega.Equal(&defaultMemorySwappiness), |
| 716 | + "MemorySwappiness should remain unchanged when DisableMemorySwappiness is false") |
| 717 | + gomega.Expect(logOutput.String()).NotTo(gomega.ContainSubstring( |
| 718 | + "Disabled memory swappiness for Podman compatibility")) |
| 719 | + }) |
| 720 | + }) |
| 721 | + }) |
638 | 722 | }) |
0 commit comments