Fix dry-run to return OK when quota is available
The dry-run path in MaxRepositorySizeQuota erroneously reported ERROR
even when the requested size was within the remaining repository-size
quota. Change Ibb4d282ebb introduced the method with a misplaced return,
so successful checks fell through to the final error response.
This change moves the `ok()` return to the correct scope, ensuring the
method:
- returns OK when the requested size is ≤ the available quota,
- returns ERROR when the request exceeds the limit, and
- returns NO_OP when repository-size quota is not enforced.
Bug: Issue 445458888
Change-Id: I949fe894fa719b30d0632f542829916f4dc8c726
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java b/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java
index 26c2a08..bd2ddd6 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java
@@ -255,8 +255,8 @@
log.warn(msg, e);
return error(msg);
}
- return ok();
}
+ return ok();
}
return error(
diff --git a/src/test/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuotaTest.java b/src/test/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuotaTest.java
new file mode 100644
index 0000000..2c1c916
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuotaTest.java
@@ -0,0 +1,119 @@
+// Copyright (C) 2025 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// 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 com.googlesource.gerrit.plugins.quota;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.gerrit.server.quota.QuotaGroupDefinitions.REPOSITORY_SIZE_GROUP;
+import static com.google.gerrit.server.quota.QuotaResponse.Status.ERROR;
+import static com.google.gerrit.server.quota.QuotaResponse.Status.NO_OP;
+import static com.google.gerrit.server.quota.QuotaResponse.Status.OK;
+import static com.googlesource.gerrit.plugins.quota.QuotaSection.KEY_MAX_REPO_SIZE;
+import static com.googlesource.gerrit.plugins.quota.QuotaSection.QUOTA;
+import static org.mockito.Mockito.when;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.LoadingCache;
+import com.google.gerrit.entities.Project;
+import com.google.gerrit.server.project.ProjectCache;
+import com.google.gerrit.server.quota.QuotaRequestContext;
+import com.google.gerrit.server.quota.QuotaResponse;
+import java.io.IOException;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicLong;
+import javax.servlet.ServletException;
+import org.eclipse.jgit.lib.Config;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MaxRepositorySizeQuotaTest {
+
+ @Mock QuotaFinder quotaFinder;
+ @Mock ProjectCache projectCache;
+ @Mock QuotaRequestContext quotaRequestContext;
+ @Mock MaxRepositorySizeQuota.Loader repoSizeLoader;
+ MaxRepositorySizeQuota maxRepositorySizeQuota;
+
+ private static final Project.NameKey PROJECT_NAME = Project.nameKey("foo");
+
+ @Before
+ public void setUp() throws IOException, ServletException {
+ when(quotaRequestContext.project()).thenReturn(Optional.of(PROJECT_NAME));
+ LoadingCache<Project.NameKey, AtomicLong> repoSizeCache =
+ CacheBuilder.newBuilder().build(repoSizeLoader);
+ maxRepositorySizeQuota = new MaxRepositorySizeQuota(quotaFinder, repoSizeCache, projectCache);
+ }
+
+ @Test
+ public void dryRunOKWhenRequestingLessThanAvailableQuota() throws IOException {
+ long currentRepoSize = 1;
+ long maxRepoSize = 3;
+ long requestQuotaSize = 1;
+ setupQuotas(currentRepoSize, maxRepoSize);
+
+ QuotaResponse quotaResponse =
+ maxRepositorySizeQuota.dryRun(REPOSITORY_SIZE_GROUP, quotaRequestContext, requestQuotaSize);
+
+ assertThat(quotaResponse.status()).isEqualTo(OK);
+ }
+
+ @Test
+ public void dryRunOKWhenRequestingExactlyAvailableQuota() throws IOException {
+ long currentRepoSize = 1;
+ long maxRepoSize = 3;
+ long requestQuotaSize = 2;
+ setupQuotas(currentRepoSize, maxRepoSize);
+
+ QuotaResponse quotaResponse =
+ maxRepositorySizeQuota.dryRun(REPOSITORY_SIZE_GROUP, quotaRequestContext, requestQuotaSize);
+
+ assertThat(quotaResponse.status()).isEqualTo(OK);
+ }
+
+ @Test
+ public void dryRunErrorWhenRequestingTooMuchQuota() throws IOException {
+ long currentRepoSize = 1;
+ long maxRepoSize = 3;
+ long requestQuotaSize = 4;
+ setupQuotas(currentRepoSize, maxRepoSize);
+
+ QuotaResponse quotaResponse =
+ maxRepositorySizeQuota.dryRun(REPOSITORY_SIZE_GROUP, quotaRequestContext, requestQuotaSize);
+
+ assertThat(quotaResponse.status()).isEqualTo(ERROR);
+ }
+
+ @Test
+ public void dryRunNoopWhenQuotaIsNotEnforcedForProject() {
+ long requestQuotaSize = 1;
+
+ QuotaResponse quotaResponse =
+ maxRepositorySizeQuota.dryRun(REPOSITORY_SIZE_GROUP, quotaRequestContext, requestQuotaSize);
+
+ assertThat(quotaResponse.status()).isEqualTo(NO_OP);
+ }
+
+ private void setupQuotas(long currentRepoSize, long maxRepoSize) throws IOException {
+ Config config = new Config();
+ config.setLong(QUOTA, PROJECT_NAME.get(), KEY_MAX_REPO_SIZE, maxRepoSize);
+ when(repoSizeLoader.load(PROJECT_NAME)).thenReturn(new AtomicLong(currentRepoSize));
+
+ when(quotaFinder.firstMatching(PROJECT_NAME))
+ .thenReturn(new QuotaSection(config, PROJECT_NAME.get()));
+ }
+}