From 7a65eb809956af38d606b3f1d8488c9f2cb8a672 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Mon, 24 Jul 2023 11:17:34 +0200 Subject: [PATCH] test: TestServiceDeployResources_MergeLastWin --- pkg/domain/dockerCompose/config.go | 9 ++++- pkg/domain/dockerCompose/config_test.go | 54 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/pkg/domain/dockerCompose/config.go b/pkg/domain/dockerCompose/config.go index 20cb3a9..7d4783a 100644 --- a/pkg/domain/dockerCompose/config.go +++ b/pkg/domain/dockerCompose/config.go @@ -579,7 +579,14 @@ func (sdr *ServiceDeployResources) mergeLastWinLimits(limits *ServiceDeployResou } func (sdr *ServiceDeployResources) mergeLastWinReservations(reservations *ServiceDeployResourcesLimits) { - if !sdr.Reservations.Equal(reservations) { + switch { + case sdr.Reservations == nil && reservations != nil: + sdr.Reservations = reservations + case sdr.Reservations != nil && reservations == nil: + fallthrough + case sdr.Reservations == nil && reservations == nil: + return + default: sdr.Reservations.MergeLastWin(reservations) } } diff --git a/pkg/domain/dockerCompose/config_test.go b/pkg/domain/dockerCompose/config_test.go index 5b63d10..1c7cc2d 100644 --- a/pkg/domain/dockerCompose/config_test.go +++ b/pkg/domain/dockerCompose/config_test.go @@ -629,6 +629,60 @@ func TestServiceDeployResources_MergeLastWin(t *testing.T) { }, }, }, + { + serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{ + Reservations: nil, + }, + serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{ + Reservations: &dockerCompose.ServiceDeployResourcesLimits{ + CPUs: "1", + Memory: "500", + }, + }, + expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{ + Reservations: &dockerCompose.ServiceDeployResourcesLimits{ + CPUs: "1", + Memory: "500", + }, + }, + }, + { + serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{ + Reservations: &dockerCompose.ServiceDeployResourcesLimits{ + CPUs: "1", + Memory: "500", + }, + }, + serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{ + Reservations: nil, + }, + expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{ + Reservations: &dockerCompose.ServiceDeployResourcesLimits{ + CPUs: "1", + Memory: "500", + }, + }, + }, + { + serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{ + Reservations: &dockerCompose.ServiceDeployResourcesLimits{ + CPUs: "1", + Memory: "500", + }, + }, + serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{ + Reservations: &dockerCompose.ServiceDeployResourcesLimits{ + CPUs: "2", + Memory: "1000", + }, + }, + expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{ + Reservations: &dockerCompose.ServiceDeployResourcesLimits{ + CPUs: "2", + Memory: "1000", + }, + }, + }, } for i, testCase := range testCases {