test: TestServiceDeployResources_MergeLastWin

This commit is contained in:
Markus Pesch 2023-07-24 11:16:01 +02:00
parent a8984ead0e
commit 78f6a201e5
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 114 additions and 12 deletions

View File

@ -566,7 +566,14 @@ func (sdr *ServiceDeployResources) MergeLastWin(serviceDeployResources *ServiceD
}
func (sdr *ServiceDeployResources) mergeLastWinLimits(limits *ServiceDeployResourcesLimits) {
if !sdr.Limits.Equal(limits) {
switch {
case sdr.Limits == nil && limits != nil:
sdr.Limits = limits
case sdr.Limits != nil && limits == nil:
fallthrough
case sdr.Limits == nil && limits == nil:
return
default:
sdr.Limits.MergeLastWin(limits)
}
}

View File

@ -562,6 +562,81 @@ func TestSecretDeployResources_Equal(t *testing.T) {
}
}
func TestServiceDeployResources_MergeLastWin(t *testing.T) {
require := require.New(t)
testCases := []struct {
serviceDeploymentResourcesA *dockerCompose.ServiceDeployResources
serviceDeploymentResourcesB *dockerCompose.ServiceDeployResources
expectedServiceDeploymentResources *dockerCompose.ServiceDeployResources
}{
{
serviceDeploymentResourcesA: nil,
serviceDeploymentResourcesB: nil,
expectedServiceDeploymentResources: nil,
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Limits: nil,
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Limits: nil,
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
Memory: "1000",
},
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
Memory: "1000",
},
},
},
}
for i, testCase := range testCases {
testCase.serviceDeploymentResourcesA.MergeLastWin(testCase.serviceDeploymentResourcesB)
require.True(testCase.expectedServiceDeploymentResources.Equal(testCase.serviceDeploymentResourcesA), "Failed test case %v", i)
}
}
func TestServiceDeployResourcesLimits_Equal(t *testing.T) {
require := require.New(t)
@ -626,14 +701,14 @@ func TestServiceDeployResourcesLimits_MergeLastWin(t *testing.T) {
require := require.New(t)
testCases := []struct {
serviceDeploymentResourcesLimitsA *dockerCompose.ServiceDeployResourcesLimits
serviceDeploymentResourcesLimitsB *dockerCompose.ServiceDeployResourcesLimits
expectedResult *dockerCompose.ServiceDeployResourcesLimits
serviceDeploymentResourcesLimitsA *dockerCompose.ServiceDeployResourcesLimits
serviceDeploymentResourcesLimitsB *dockerCompose.ServiceDeployResourcesLimits
expectedServiceDeploymentResourcesLimits *dockerCompose.ServiceDeployResourcesLimits
}{
{
serviceDeploymentResourcesLimitsA: nil,
serviceDeploymentResourcesLimitsB: nil,
expectedResult: nil,
serviceDeploymentResourcesLimitsA: nil,
serviceDeploymentResourcesLimitsB: nil,
expectedServiceDeploymentResourcesLimits: nil,
},
{
serviceDeploymentResourcesLimitsA: &dockerCompose.ServiceDeployResourcesLimits{
@ -641,7 +716,7 @@ func TestServiceDeployResourcesLimits_MergeLastWin(t *testing.T) {
Memory: "500",
},
serviceDeploymentResourcesLimitsB: nil,
expectedResult: &dockerCompose.ServiceDeployResourcesLimits{
expectedServiceDeploymentResourcesLimits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
@ -655,7 +730,7 @@ func TestServiceDeployResourcesLimits_MergeLastWin(t *testing.T) {
CPUs: "1",
Memory: "500",
},
expectedResult: &dockerCompose.ServiceDeployResourcesLimits{
expectedServiceDeploymentResourcesLimits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
@ -667,7 +742,7 @@ func TestServiceDeployResourcesLimits_MergeLastWin(t *testing.T) {
serviceDeploymentResourcesLimitsB: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
},
expectedResult: &dockerCompose.ServiceDeployResourcesLimits{
expectedServiceDeploymentResourcesLimits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
},
},
@ -678,7 +753,7 @@ func TestServiceDeployResourcesLimits_MergeLastWin(t *testing.T) {
serviceDeploymentResourcesLimitsB: &dockerCompose.ServiceDeployResourcesLimits{
Memory: "1000",
},
expectedResult: &dockerCompose.ServiceDeployResourcesLimits{
expectedServiceDeploymentResourcesLimits: &dockerCompose.ServiceDeployResourcesLimits{
Memory: "1000",
},
},
@ -686,7 +761,7 @@ func TestServiceDeployResourcesLimits_MergeLastWin(t *testing.T) {
for i, testCase := range testCases {
testCase.serviceDeploymentResourcesLimitsA.MergeLastWin(testCase.serviceDeploymentResourcesLimitsB)
require.True(testCase.expectedResult.Equal(testCase.serviceDeploymentResourcesLimitsA), "Failed test case %v", i)
require.True(testCase.expectedServiceDeploymentResourcesLimits.Equal(testCase.serviceDeploymentResourcesLimitsA), "Failed test case %v", i)
}
}
@ -763,6 +838,11 @@ func TestServiceNetwork_MergeLastWin(t *testing.T) {
ServiceNetworkB *dockerCompose.ServiceNetwork
expectedServiceNetwork *dockerCompose.ServiceNetwork
}{
{
ServiceNetworkA: nil,
ServiceNetworkB: nil,
expectedServiceNetwork: nil,
},
{
ServiceNetworkA: &dockerCompose.ServiceNetwork{},
ServiceNetworkB: nil,
@ -873,6 +953,11 @@ func TestServiceULimits_MergeLastWin(t *testing.T) {
ServiceULimitsB *dockerCompose.ServiceULimits
expectedServiceULimits *dockerCompose.ServiceULimits
}{
{
ServiceULimitsA: nil,
ServiceULimitsB: nil,
expectedServiceULimits: nil,
},
{
ServiceULimitsA: &dockerCompose.ServiceULimits{},
ServiceULimitsB: nil,
@ -966,6 +1051,11 @@ func TestServiceULimitsNoFile_MergeLastWin(t *testing.T) {
ServiceULimitsNoFileB *dockerCompose.ServiceULimitsNoFile
expectedServiceULimitsNoFile *dockerCompose.ServiceULimitsNoFile
}{
{
ServiceULimitsNoFileA: nil,
ServiceULimitsNoFileB: nil,
expectedServiceULimitsNoFile: nil,
},
{
ServiceULimitsNoFileA: &dockerCompose.ServiceULimitsNoFile{},
ServiceULimitsNoFileB: nil,
@ -1091,6 +1181,11 @@ func TestVolume_MergeLastWin(t *testing.T) {
volumeB *dockerCompose.Volume
expectedVolume *dockerCompose.Volume
}{
{
volumeA: nil,
volumeB: nil,
expectedVolume: nil,
},
{
volumeA: &dockerCompose.Volume{},
volumeB: nil,