From 761378708e2e04b7f9077d167bafe5ccfa77ac22 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 21 May 2024 14:26:43 +0200 Subject: [PATCH] move test that belongs to DistributeFairly() --- .../apply_computed_project_quota_test.go | 21 -------- internal/util/algorithms_test.go | 50 +++++++++++++++++++ 2 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 internal/util/algorithms_test.go diff --git a/internal/datamodel/apply_computed_project_quota_test.go b/internal/datamodel/apply_computed_project_quota_test.go index c110eb25..6129c23c 100644 --- a/internal/datamodel/apply_computed_project_quota_test.go +++ b/internal/datamodel/apply_computed_project_quota_test.go @@ -24,7 +24,6 @@ import ( "testing" "github.com/sapcc/go-api-declarations/limes" - "github.com/sapcc/go-bits/assert" "github.com/sapcc/limes/internal/core" "github.com/sapcc/limes/internal/db" @@ -403,23 +402,3 @@ func expectACPQResult(t *testing.T, input map[limes.AvailabilityZone]clusterAZAl func p2u64(x uint64) *uint64 { return &x } - -func TestACPQDistributeFairlyWithLargeNumbers(t *testing.T) { - // This tests how acpqDistributeFairly() deals with very large numbers - // (as can occur e.g. for Swift capacity measured in bytes). - // We used to have a crash here because of an overflowing uint64 multiplication. - total := uint64(200000000000000) - requested := map[db.ProjectServiceID]uint64{ - 401: total / 2, - 402: total / 2, - 403: total / 2, - 404: total / 2, - } - result := acpqDistributeFairly(total, requested) - assert.DeepEqual(t, "output of acpqDistributeFairly", result, map[db.ProjectServiceID]uint64{ - 401: total / 4, - 402: total / 4, - 403: total / 4, - 404: total / 4, - }) -} diff --git a/internal/util/algorithms_test.go b/internal/util/algorithms_test.go new file mode 100644 index 000000000..bd45cc3f --- /dev/null +++ b/internal/util/algorithms_test.go @@ -0,0 +1,50 @@ +/******************************************************************************* +* +* Copyright 2024 SAP SE +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You should have received a copy of the License along with this +* program. If not, you may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*******************************************************************************/ + +package util + +import ( + "testing" + + "github.com/sapcc/go-bits/assert" + + "github.com/sapcc/limes/internal/db" +) + +// NOTE: Most test coverage for DistributeFairly() is implicit as part of datamodel.ApplyComputedProjectQuota(). + +func TestDistributeFairlyWithLargeNumbers(t *testing.T) { + // This tests how DistributeFairly() deals with very large numbers + // (as can occur e.g. for Swift capacity measured in bytes). + // We used to have a crash here because of an overflowing uint64 multiplication. + total := uint64(200000000000000) + requested := map[db.ProjectServiceID]uint64{ + 401: total / 2, + 402: total / 2, + 403: total / 2, + 404: total / 2, + } + result := DistributeFairly(total, requested) + assert.DeepEqual(t, "output of DistributeFairly", result, map[db.ProjectServiceID]uint64{ + 401: total / 4, + 402: total / 4, + 403: total / 4, + 404: total / 4, + }) +}