Use injected times in URSC and CommandTestCase (#1805)

We started getting failures because some of the tests used October. In
general we should freeze the clock for testing as much as possible.

Same thing with the Get*Commands
This commit is contained in:
gbrodman 2022-10-04 15:36:41 -04:00 committed by GitHub
parent fd635628ee
commit 72119d92a1
8 changed files with 36 additions and 23 deletions

View file

@ -15,30 +15,31 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static org.joda.time.DateTimeZone.UTC;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import google.registry.model.EppResource; import google.registry.model.EppResource;
import google.registry.util.Clock;
import java.util.Optional; import java.util.Optional;
import javax.inject.Inject;
import org.joda.time.DateTime; import org.joda.time.DateTime;
/** Abstract command to print one or more resources to stdout. */ /** Abstract command to print one or more resources to stdout. */
@Parameters(separators = " =") @Parameters(separators = " =")
abstract class GetEppResourceCommand implements CommandWithRemoteApi { abstract class GetEppResourceCommand implements CommandWithRemoteApi {
private final DateTime now = DateTime.now(UTC);
@Parameter( @Parameter(
names = "--read_timestamp", names = "--read_timestamp",
description = "Timestamp to use when reading. May not be in the past.") description = "Timestamp to use when reading. May not be in the past.")
protected DateTime readTimestamp = now; protected DateTime readTimestamp;
@Parameter( @Parameter(
names = "--expand", names = "--expand",
description = "Fully expand the requested resource. NOTE: Output may be lengthy.") description = "Fully expand the requested resource. NOTE: Output may be lengthy.")
boolean expand; boolean expand;
@Inject Clock clock;
/** Runs the command's own logic that calls {@link #printResource}. */ /** Runs the command's own logic that calls {@link #printResource}. */
abstract void runAndPrint(); abstract void runAndPrint();
@ -59,7 +60,11 @@ abstract class GetEppResourceCommand implements CommandWithRemoteApi {
@Override @Override
public void run() { public void run() {
checkArgument(!readTimestamp.isBefore(now), "--read_timestamp may not be in the past"); if (readTimestamp == null) {
readTimestamp = clock.nowUtc();
}
checkArgument(
!readTimestamp.isBefore(clock.nowUtc()), "--read_timestamp may not be in the past");
runAndPrint(); runAndPrint();
} }
} }

View file

@ -21,7 +21,6 @@ import static google.registry.model.EppResourceUtils.checkResourcesExist;
import static google.registry.model.EppResourceUtils.loadByForeignKey; import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.util.PreconditionsUtils.checkArgumentPresent; import static google.registry.util.PreconditionsUtils.checkArgumentPresent;
import static org.joda.time.DateTimeZone.UTC;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
@ -38,11 +37,13 @@ import google.registry.model.eppcommon.StatusValue;
import google.registry.model.host.Host; import google.registry.model.host.Host;
import google.registry.tools.soy.DomainRenewSoyInfo; import google.registry.tools.soy.DomainRenewSoyInfo;
import google.registry.tools.soy.UniformRapidSuspensionSoyInfo; import google.registry.tools.soy.UniformRapidSuspensionSoyInfo;
import google.registry.util.Clock;
import google.registry.util.DomainNameUtils; import google.registry.util.DomainNameUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import javax.inject.Inject;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter; import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
@ -119,10 +120,12 @@ final class UniformRapidSuspensionCommand extends MutatingEppToolCommand {
/** Set of status values to remove. */ /** Set of status values to remove. */
ImmutableSet<String> removeStatuses; ImmutableSet<String> removeStatuses;
@Inject Clock clock;
@Override @Override
protected void initMutatingEppToolCommand() { protected void initMutatingEppToolCommand() {
superuser = true; superuser = true;
DateTime now = DateTime.now(UTC); DateTime now = clock.nowUtc();
ImmutableList<String> newCanonicalHosts = ImmutableList<String> newCanonicalHosts =
newHosts.stream().map(DomainNameUtils::canonicalizeHostname).collect(toImmutableList()); newHosts.stream().map(DomainNameUtils::canonicalizeHostname).collect(toImmutableList());
ImmutableSet<String> newHostsSet = ImmutableSet.copyOf(newCanonicalHosts); ImmutableSet<String> newHostsSet = ImmutableSet.copyOf(newCanonicalHosts);

View file

@ -19,7 +19,6 @@ import static com.google.common.collect.Iterables.toArray;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.joda.time.DateTimeZone.UTC;
import com.beust.jcommander.JCommander; import com.beust.jcommander.JCommander;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
@ -63,7 +62,7 @@ public abstract class CommandTestCase<C extends Command> {
protected C command; protected C command;
protected final FakeClock fakeClock = new FakeClock(DateTime.now(UTC)); protected final FakeClock fakeClock = new FakeClock(DateTime.parse("2022-09-01T00:00:00.000Z"));
@RegisterExtension @RegisterExtension
public final AppEngineExtension appEngine = public final AppEngineExtension appEngine =

View file

@ -19,22 +19,19 @@ import static google.registry.testing.DatabaseHelper.newContact;
import static google.registry.testing.DatabaseHelper.persistActiveContact; import static google.registry.testing.DatabaseHelper.persistActiveContact;
import static google.registry.testing.DatabaseHelper.persistDeletedContact; import static google.registry.testing.DatabaseHelper.persistDeletedContact;
import static google.registry.testing.DatabaseHelper.persistResource; import static google.registry.testing.DatabaseHelper.persistResource;
import static org.joda.time.DateTimeZone.UTC;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParameterException;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** Unit tests for {@link GetContactCommand}. */ /** Unit tests for {@link GetContactCommand}. */
class GetContactCommandTest extends CommandTestCase<GetContactCommand> { class GetContactCommandTest extends CommandTestCase<GetContactCommand> {
private DateTime now = DateTime.now(UTC);
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() {
createTld("tld"); createTld("tld");
command.clock = fakeClock;
} }
@Test @Test
@ -67,7 +64,7 @@ class GetContactCommandTest extends CommandTestCase<GetContactCommand> {
@Test @Test
void testSuccess_deletedContact() throws Exception { void testSuccess_deletedContact() throws Exception {
persistDeletedContact("sh8013", now.minusDays(1)); persistDeletedContact("sh8013", fakeClock.nowUtc().minusDays(1));
runCommand("sh8013"); runCommand("sh8013");
assertInStdout("Contact 'sh8013' does not exist or is deleted"); assertInStdout("Contact 'sh8013' does not exist or is deleted");
} }
@ -85,8 +82,9 @@ class GetContactCommandTest extends CommandTestCase<GetContactCommand> {
@Test @Test
void testSuccess_contactDeletedInFuture() throws Exception { void testSuccess_contactDeletedInFuture() throws Exception {
persistResource(newContact("sh8013").asBuilder().setDeletionTime(now.plusDays(1)).build()); persistResource(
runCommand("sh8013", "--read_timestamp=" + now.plusMonths(1)); newContact("sh8013").asBuilder().setDeletionTime(fakeClock.nowUtc().plusDays(1)).build());
runCommand("sh8013", "--read_timestamp=" + fakeClock.nowUtc().plusMonths(1));
assertInStdout("Contact 'sh8013' does not exist or is deleted"); assertInStdout("Contact 'sh8013' does not exist or is deleted");
} }
} }

View file

@ -31,6 +31,7 @@ class GetDomainCommandTest extends CommandTestCase<GetDomainCommand> {
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() {
createTld("tld"); createTld("tld");
command.clock = fakeClock;
} }
@Test @Test

View file

@ -19,22 +19,19 @@ import static google.registry.testing.DatabaseHelper.newHost;
import static google.registry.testing.DatabaseHelper.persistActiveHost; import static google.registry.testing.DatabaseHelper.persistActiveHost;
import static google.registry.testing.DatabaseHelper.persistDeletedHost; import static google.registry.testing.DatabaseHelper.persistDeletedHost;
import static google.registry.testing.DatabaseHelper.persistResource; import static google.registry.testing.DatabaseHelper.persistResource;
import static org.joda.time.DateTimeZone.UTC;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParameterException;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** Unit tests for {@link GetHostCommand}. */ /** Unit tests for {@link GetHostCommand}. */
class GetHostCommandTest extends CommandTestCase<GetHostCommand> { class GetHostCommandTest extends CommandTestCase<GetHostCommand> {
private DateTime now = DateTime.now(UTC);
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() {
createTld("tld"); createTld("tld");
command.clock = fakeClock;
} }
@Test @Test
@ -77,7 +74,7 @@ class GetHostCommandTest extends CommandTestCase<GetHostCommand> {
@Test @Test
void testSuccess_deletedHost() throws Exception { void testSuccess_deletedHost() throws Exception {
persistDeletedHost("ns1.example.tld", now.minusDays(1)); persistDeletedHost("ns1.example.tld", fakeClock.nowUtc().minusDays(1));
runCommand("ns1.example.tld"); runCommand("ns1.example.tld");
assertInStdout("Host 'ns1.example.tld' does not exist or is deleted"); assertInStdout("Host 'ns1.example.tld' does not exist or is deleted");
} }
@ -91,8 +88,11 @@ class GetHostCommandTest extends CommandTestCase<GetHostCommand> {
@Test @Test
void testSuccess_hostDeletedInFuture() throws Exception { void testSuccess_hostDeletedInFuture() throws Exception {
persistResource( persistResource(
newHost("ns1.example.tld").asBuilder().setDeletionTime(now.plusDays(1)).build()); newHost("ns1.example.tld")
runCommand("ns1.example.tld", "--read_timestamp=" + now.plusMonths(1)); .asBuilder()
.setDeletionTime(fakeClock.nowUtc().plusDays(1))
.build());
runCommand("ns1.example.tld", "--read_timestamp=" + fakeClock.nowUtc().plusMonths(1));
assertInStdout("Host 'ns1.example.tld' does not exist or is deleted"); assertInStdout("Host 'ns1.example.tld' does not exist or is deleted");
} }

View file

@ -28,11 +28,17 @@ import google.registry.model.domain.token.PackagePromotion;
import org.joda.money.CurrencyUnit; import org.joda.money.CurrencyUnit;
import org.joda.money.Money; import org.joda.money.Money;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** Unit tests for {@link GetPackagePromotionCommand}. */ /** Unit tests for {@link GetPackagePromotionCommand}. */
public class GetPackagePromotionCommandTest extends CommandTestCase<GetPackagePromotionCommand> { public class GetPackagePromotionCommandTest extends CommandTestCase<GetPackagePromotionCommand> {
@BeforeEach
void beforeEach() {
command.clock = fakeClock;
}
@Test @Test
void testSuccess() throws Exception { void testSuccess() throws Exception {
AllocationToken token = AllocationToken token =

View file

@ -48,6 +48,7 @@ class UniformRapidSuspensionCommandTest
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() {
command.clock = fakeClock;
// Since the command's history client ID must be CharlestonRoad, resave TheRegistrar that way. // Since the command's history client ID must be CharlestonRoad, resave TheRegistrar that way.
persistResource( persistResource(
loadRegistrar("TheRegistrar").asBuilder().setRegistrarId("CharlestonRoad").build()); loadRegistrar("TheRegistrar").asBuilder().setRegistrarId("CharlestonRoad").build());