Change GenerateEscrowDepositCommand to trigger back end deposit generation

Previously, GenerateEscrowDepositCommand generated the deposit itself. Channeling it through the existing deposit generation code make things more maintainable.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=152847950
This commit is contained in:
mountford 2017-04-11 12:53:07 -07:00 committed by Ben McIlwain
parent 8653d2f204
commit dea386d08a
7 changed files with 224 additions and 367 deletions

View file

@ -40,11 +40,11 @@ import org.joda.time.DateTime;
@Module @Module
public final class RdeModule { public final class RdeModule {
static final String PARAM_WATERMARK = "watermark"; public static final String PARAM_WATERMARK = "watermark";
static final String PARAM_MANUAL = "manual"; public static final String PARAM_MANUAL = "manual";
static final String PARAM_DIRECTORY = "directory"; public static final String PARAM_DIRECTORY = "directory";
static final String PARAM_MODE = "mode"; public static final String PARAM_MODE = "mode";
static final String PARAM_REVISION = "revision"; public static final String PARAM_REVISION = "revision";
@Provides @Provides
@Parameter(PARAM_WATERMARK) @Parameter(PARAM_WATERMARK)

View file

@ -181,9 +181,11 @@ import org.joda.time.Duration;
* @see <a href="https://tools.ietf.org/html/draft-arias-noguchi-registry-data-escrow-06">Registry Data Escrow Specification</a> * @see <a href="https://tools.ietf.org/html/draft-arias-noguchi-registry-data-escrow-06">Registry Data Escrow Specification</a>
* @see <a href="https://tools.ietf.org/html/draft-arias-noguchi-dnrd-objects-mapping-05">Domain Name Registration Data Objects Mapping</a> * @see <a href="https://tools.ietf.org/html/draft-arias-noguchi-dnrd-objects-mapping-05">Domain Name Registration Data Objects Mapping</a>
*/ */
@Action(path = "/_dr/task/rdeStaging") @Action(path = RdeStagingAction.PATH)
public final class RdeStagingAction implements Runnable { public final class RdeStagingAction implements Runnable {
public static final String PATH = "/_dr/task/rdeStaging";
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass(); private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
@Inject Clock clock; @Inject Clock clock;
@ -302,6 +304,10 @@ public final class RdeStagingAction implements Runnable {
} }
} }
if (revision.isPresent() && (revision.get() < 0)) {
throw new BadRequestException("Revision must be greater than or equal to zero");
}
ImmutableSetMultimap.Builder<String, PendingDeposit> pendingsBuilder = ImmutableSetMultimap.Builder<String, PendingDeposit> pendingsBuilder =
new ImmutableSetMultimap.Builder<>(); new ImmutableSetMultimap.Builder<>();

View file

@ -49,6 +49,7 @@ java_library(
"//java/google/registry/model", "//java/google/registry/model",
"//java/google/registry/pricing", "//java/google/registry/pricing",
"//java/google/registry/rde", "//java/google/registry/rde",
"//java/google/registry/request",
"//java/google/registry/request:modules", "//java/google/registry/request:modules",
"//java/google/registry/security", "//java/google/registry/security",
"//java/google/registry/tldconfig/idn", "//java/google/registry/tldconfig/idn",

View file

@ -14,234 +14,102 @@
package google.registry.tools; package google.registry.tools;
import static google.registry.model.EppResourceUtils.loadAtPointInTime; import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.registry.Registries.assertTldsExist;
import static google.registry.model.registry.Registries.assertTldExists;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.joda.time.DateTimeZone.UTC;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import com.google.common.base.Function; import com.google.appengine.api.modules.ModulesService;
import com.google.common.base.Predicates; import com.google.appengine.api.taskqueue.Queue;
import com.google.common.collect.FluentIterable; import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.common.collect.ImmutableList; import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Result;
import google.registry.config.RegistryConfig.Config;
import google.registry.model.EppResource;
import google.registry.model.EppResourceUtils;
import google.registry.model.ImmutableObject;
import google.registry.model.contact.ContactResource;
import google.registry.model.domain.DomainResource;
import google.registry.model.host.HostResource;
import google.registry.model.index.EppResourceIndex;
import google.registry.model.index.EppResourceIndexBucket;
import google.registry.model.rde.RdeMode; import google.registry.model.rde.RdeMode;
import google.registry.model.rde.RdeNamingUtils; import google.registry.rde.RdeModule;
import google.registry.model.registrar.Registrar; import google.registry.rde.RdeStagingAction;
import google.registry.rde.DepositFragment; import google.registry.request.RequestParameters;
import google.registry.rde.RdeCounter;
import google.registry.rde.RdeMarshaller;
import google.registry.rde.RdeResourceType;
import google.registry.rde.RdeUtil;
import google.registry.tldconfig.idn.IdnTableEnum;
import google.registry.tools.Command.RemoteApiCommand; import google.registry.tools.Command.RemoteApiCommand;
import google.registry.tools.params.DateTimeParameter; import google.registry.tools.params.DateTimeParameter;
import google.registry.tools.params.PathParameter; import java.util.List;
import google.registry.xjc.rdeheader.XjcRdeHeader;
import google.registry.xjc.rdeheader.XjcRdeHeaderElement;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import org.joda.time.DateTime; import org.joda.time.DateTime;
/** Command to generate an XML RDE escrow deposit (with relevant files) in current directory. */ /**
* Command to kick off the server-side generation of an XML RDE or BRDA escrow deposit, which will
* be stored in the specified manual subdirectory of the GCS RDE bucket.
*/
@Parameters(separators = " =", commandDescription = "Generate an XML escrow deposit.") @Parameters(separators = " =", commandDescription = "Generate an XML escrow deposit.")
final class GenerateEscrowDepositCommand implements RemoteApiCommand { final class GenerateEscrowDepositCommand implements RemoteApiCommand {
@Parameter( @Parameter(
names = {"-t", "--tld"}, names = {"-t", "--tld"},
description = "Top level domain for which deposit should be generated.", description = "Top level domain(s) for which deposit should be generated.",
required = true) required = true)
private String tld; private List<String> tlds;
@Parameter( @Parameter(
names = {"-w", "--watermark"}, names = {"-w", "--watermark"},
description = "Point-in-time timestamp for snapshotting Datastore.", description = "Point-in-time timestamp(s) for snapshotting Datastore.",
validateWith = DateTimeParameter.class) required = true,
private DateTime watermark = DateTime.now(UTC); converter = DateTimeParameter.class)
private List<DateTime> watermarks;
@Parameter( @Parameter(
names = {"-m", "--mode"}, names = {"-m", "--mode"},
description = "FULL/THIN mode of operation.") description = "Mode of operation: FULL for RDE deposits, THIN for BRDA deposits.")
private RdeMode mode = RdeMode.FULL; private RdeMode mode = RdeMode.FULL;
@Parameter( @Parameter(
names = {"-r", "--revision"}, names = {"-r", "--revision"},
description = "Revision number. Use >0 for resends.") description = "Revision number. Use >0 for resends.")
private int revision = 0; private Integer revision;
@Parameter( @Parameter(
names = {"-o", "--outdir"}, names = {"-o", "--outdir"},
description = "Specify output directory. Default is current directory.", description = "Specify output subdirectory (under GCS RDE bucket, directory manual).",
validateWith = PathParameter.OutputDirectory.class) required = true)
private Path outdir = Paths.get("."); private String outdir;
@Inject @Inject ModulesService modulesService;
EscrowDepositEncryptor encryptor; @Inject @Named("rde-report") Queue queue;
@Inject
RdeCounter counter;
@Inject
@Config("eppResourceIndexBucketCount")
int eppResourceIndexBucketCount;
@Override @Override
public void run() throws Exception { public void run() throws Exception {
RdeMarshaller marshaller = new RdeMarshaller();
assertTldExists(tld); if (tlds.isEmpty()) {
String suffix = String.format("-%s-%s.tmp.xml", tld, watermark); throw new ParameterException("At least one TLD must be specified");
Path xmlPath = outdir.resolve("deposit" + suffix);
Path reportPath = outdir.resolve("report" + suffix);
try {
String id = RdeUtil.timestampToId(watermark);
XjcRdeHeader header;
try (OutputStream xmlOutputBytes = Files.newOutputStream(xmlPath);
Writer xmlOutput = new OutputStreamWriter(xmlOutputBytes, UTF_8)) {
xmlOutput.write(
marshaller.makeHeader(id, watermark, RdeResourceType.getUris(mode), revision));
for (ImmutableObject resource
: Iterables.concat(Registrar.loadAll(), load(scan()))) {
DepositFragment frag;
if (resource instanceof Registrar) {
frag = marshaller.marshalRegistrar((Registrar) resource);
} else if (resource instanceof ContactResource) {
frag = marshaller.marshalContact((ContactResource) resource);
} else if (resource instanceof DomainResource) {
DomainResource domain = (DomainResource) resource;
if (!domain.getTld().equals(tld)) {
continue;
} }
frag = marshaller.marshalDomain(domain, mode); assertTldsExist(tlds);
} else if (resource instanceof HostResource) {
HostResource host = (HostResource) resource; for (DateTime watermark : watermarks) {
frag = host.isSubordinate() if (!watermark.withTimeAtStartOfDay().equals(watermark)) {
? marshaller.marshalSubordinateHost( throw new ParameterException("Each watermark date must be the start of a day");
host,
// Note that loadAtPointInTime() does cloneProjectedAtTime(watermark) for us.
loadAtPointInTime(
ofy().load().key(host.getSuperordinateDomain()).now(), watermark).now())
: marshaller.marshalExternalHost(host);
} else {
continue; // Surprise polymorphic entities, e.g. DomainApplication.
}
if (!frag.xml().isEmpty()) {
xmlOutput.write(frag.xml());
counter.increment(frag.type());
}
if (!frag.error().isEmpty()) {
System.err.print(frag.error());
}
}
for (IdnTableEnum idn : IdnTableEnum.values()) {
xmlOutput.write(marshaller.marshalIdn(idn.getTable()));
counter.increment(RdeResourceType.IDN);
}
header = counter.makeHeader(tld, mode);
xmlOutput.write(marshaller.marshalStrictlyOrDie(new XjcRdeHeaderElement(header)));
xmlOutput.write(marshaller.makeFooter());
}
try (OutputStream reportOutputBytes = Files.newOutputStream(reportPath)) {
counter.makeReport(id, watermark, header, revision).marshal(reportOutputBytes, UTF_8);
}
String name = RdeNamingUtils.makeRydeFilename(tld, watermark, mode, 1, revision);
encryptor.encrypt(tld, xmlPath, outdir);
Files.move(xmlPath, outdir.resolve(name + ".xml"), REPLACE_EXISTING);
Files.move(reportPath, outdir.resolve(name + "-report.xml"), REPLACE_EXISTING);
} finally {
Files.deleteIfExists(xmlPath);
Files.deleteIfExists(reportPath);
} }
} }
private Iterable<EppResource> scan() { if ((revision != null) && (revision < 0)) {
return Iterables.concat( throw new ParameterException("Revision must be greater than or equal to zero");
Iterables.transform(
getEppResourceIndexBuckets(),
new Function<Key<EppResourceIndexBucket>, Iterable<EppResource>>() {
@Override
public Iterable<EppResource> apply(Key<EppResourceIndexBucket> bucket) {
System.err.printf("Scanning EppResourceIndexBucket %d of %d...\n",
bucket.getId(), eppResourceIndexBucketCount);
return scanBucket(bucket);
}}));
} }
private Iterable<EppResource> scanBucket(final Key<EppResourceIndexBucket> bucket) { if (outdir.isEmpty()) {
return ofy().load() throw new ParameterException("Output subdirectory must not be empty");
.keys(FluentIterable
.from(mode == RdeMode.FULL
? Arrays.asList(
Key.getKind(ContactResource.class),
Key.getKind(DomainResource.class),
Key.getKind(HostResource.class))
: Arrays.asList(
Key.getKind(DomainResource.class)))
.transformAndConcat(new Function<String, Iterable<EppResourceIndex>>() {
@Override
public Iterable<EppResourceIndex> apply(String kind) {
return ofy().load()
.type(EppResourceIndex.class)
.ancestor(bucket)
.filter("kind", kind)
.iterable();
}})
.transform(new Function<EppResourceIndex, Key<EppResource>>() {
@Override
@SuppressWarnings("unchecked")
public Key<EppResource> apply(EppResourceIndex index) {
return (Key<EppResource>) index.getKey();
}}))
.values();
} }
private <T extends EppResource> Iterable<T> load(final Iterable<T> resources) { // Unlike many tool commands, this command is actually invoking an action on the backend module
return FluentIterable // (because it's a mapreduce). So we invoke it in a different way.
.from(Iterables.partition( String hostname = modulesService.getVersionHostname("backend", null);
Iterables.transform(resources, TaskOptions opts =
new Function<T, Result<T>>() { withUrl(RdeStagingAction.PATH)
@Override .header("Host", hostname)
public Result<T> apply(T resource) { .param(RdeModule.PARAM_MANUAL, String.valueOf(true))
return EppResourceUtils.loadAtPointInTime(resource, watermark); .param(RequestParameters.PARAM_TLD, Joiner.on(',').join(tlds))
}}), .param(RdeModule.PARAM_WATERMARK, Joiner.on(',').join(watermarks))
1000)) .param(RdeModule.PARAM_MODE, mode.toString())
.transformAndConcat(new Function<Iterable<Result<T>>, Iterable<T>>() { .param(RdeModule.PARAM_DIRECTORY, outdir);
@Override if (revision != null) {
public Iterable<T> apply(Iterable<Result<T>> results) { opts = opts.param(RdeModule.PARAM_REVISION, String.valueOf(revision));
return Iterables.transform(results,
new Function<Result<T>, T>() {
@Override
public T apply(Result<T> result) {
return result.now();
}});
}})
.filter(Predicates.notNull());
} }
queue.add(opts);
private ImmutableList<Key<EppResourceIndexBucket>> getEppResourceIndexBuckets() {
ImmutableList.Builder<Key<EppResourceIndexBucket>> builder = new ImmutableList.Builder<>();
for (int i = 1; i <= eppResourceIndexBucketCount; i++) {
builder.add(Key.create(EppResourceIndexBucket.class, i));
}
return builder.build();
} }
} }

View file

@ -22,9 +22,11 @@ import google.registry.dns.writer.dnsupdate.DnsUpdateWriterModule;
import google.registry.keyring.api.DummyKeyringModule; import google.registry.keyring.api.DummyKeyringModule;
import google.registry.keyring.api.KeyModule; import google.registry.keyring.api.KeyModule;
import google.registry.keyring.kms.KmsModule; import google.registry.keyring.kms.KmsModule;
import google.registry.rde.RdeModule;
import google.registry.request.Modules.AppIdentityCredentialModule; import google.registry.request.Modules.AppIdentityCredentialModule;
import google.registry.request.Modules.DatastoreServiceModule; import google.registry.request.Modules.DatastoreServiceModule;
import google.registry.request.Modules.Jackson2Module; import google.registry.request.Modules.Jackson2Module;
import google.registry.request.Modules.ModulesServiceModule;
import google.registry.request.Modules.URLFetchServiceModule; import google.registry.request.Modules.URLFetchServiceModule;
import google.registry.request.Modules.UrlFetchTransportModule; import google.registry.request.Modules.UrlFetchTransportModule;
import google.registry.request.Modules.UseAppIdentityCredentialForGoogleApisModule; import google.registry.request.Modules.UseAppIdentityCredentialForGoogleApisModule;
@ -57,6 +59,8 @@ import javax.inject.Singleton;
Jackson2Module.class, Jackson2Module.class,
KeyModule.class, KeyModule.class,
KmsModule.class, KmsModule.class,
ModulesServiceModule.class,
RdeModule.class,
RegistryToolModule.class, RegistryToolModule.class,
SystemClockModule.class, SystemClockModule.class,
SystemSleeperModule.class, SystemSleeperModule.class,

View file

@ -302,6 +302,20 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.run(); action.run();
} }
@Test
public void testManualRun_invalidRevision_throwsException() throws Exception {
createTldWithEscrowEnabled("lol");
clock.setTo(DateTime.parse("2000-01-01TZ"));
action.manual = true;
action.directory = Optional.of("test/");
action.modeStrings = ImmutableSet.of("full");
action.tlds = ImmutableSet.of("lol");
action.watermarks = ImmutableSet.of(DateTime.parse("2001-01-01T00:00:00Z"));
action.revision = Optional.of(-1);
thrown.expect(BadRequestException.class);
action.run();
}
@Test @Test
public void testManualRun_validParameters_runsMapReduce() throws Exception { public void testManualRun_validParameters_runsMapReduce() throws Exception {
createTldWithEscrowEnabled("lol"); createTldWithEscrowEnabled("lol");

View file

@ -14,50 +14,20 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.truth.Truth.assertThat; import static com.google.appengine.api.taskqueue.QueueFactory.getQueue;
import static google.registry.testing.DatastoreHelper.createTld; import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.createTlds; import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
import static google.registry.testing.DatastoreHelper.generateNewContactHostRoid; import static org.mockito.Mockito.when;
import static google.registry.testing.DatastoreHelper.persistResourceWithCommitLog;
import static google.registry.util.ResourceUtils.readResourceUtf8;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParameterException;
import com.google.common.collect.ImmutableMap; import com.google.appengine.api.modules.ModulesService;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.InetAddresses;
import google.registry.config.RegistryConfig;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.host.HostResource;
import google.registry.model.ofy.Ofy;
import google.registry.rde.RdeCounter;
import google.registry.rde.RdeResourceType;
import google.registry.rde.RdeUtil;
import google.registry.testing.BouncyCastleProviderRule;
import google.registry.testing.FakeClock;
import google.registry.testing.InjectRule; import google.registry.testing.InjectRule;
import google.registry.util.Idn; import google.registry.testing.TaskQueueHelper.TaskMatcher;
import google.registry.xjc.XjcXmlTransformer;
import google.registry.xjc.rde.XjcRdeContentType;
import google.registry.xjc.rde.XjcRdeDeposit;
import google.registry.xjc.rde.XjcRdeDepositTypeType;
import google.registry.xjc.rdeheader.XjcRdeHeader;
import google.registry.xjc.rdeheader.XjcRdeHeaderCount;
import google.registry.xjc.rdehost.XjcRdeHost;
import google.registry.xjc.rderegistrar.XjcRdeRegistrar;
import google.registry.xml.XmlException;
import google.registry.xml.XmlTestUtils;
import java.io.ByteArrayInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import org.joda.time.DateTime;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mock;
/** Unit tests for {@link GenerateEscrowDepositCommand}. */ /** Unit tests for {@link GenerateEscrowDepositCommand}. */
public class GenerateEscrowDepositCommandTest public class GenerateEscrowDepositCommandTest
@ -66,169 +36,163 @@ public class GenerateEscrowDepositCommandTest
@Rule @Rule
public final InjectRule inject = new InjectRule(); public final InjectRule inject = new InjectRule();
@Rule @Mock ModulesService modulesService;
public final BouncyCastleProviderRule bouncy = new BouncyCastleProviderRule();
private final FakeClock clock = new FakeClock(DateTime.parse("2010-10-17T04:20:00Z"));
private final List<? super XjcRdeContentType> alreadyExtracted = new ArrayList<>();
@Before @Before
public void before() throws Exception { public void before() throws Exception {
inject.setStaticField(Ofy.class, "clock", clock); createTld("tld");
command.encryptor = EncryptEscrowDepositCommandTest.createEncryptor(); createTld("anothertld");
command.counter = new RdeCounter(); command = new GenerateEscrowDepositCommand();
command.eppResourceIndexBucketCount = RegistryConfig.getEppResourceIndexBucketCount(); command.modulesService = modulesService;
command.queue = getQueue("rde-report");
when(modulesService.getVersionHostname(Matchers.anyString(), Matchers.anyString()))
.thenReturn("1.backend.test.localhost");
} }
@Test @Test
public void testRun_randomDomain_generatesXmlAndEncryptsItToo() throws Exception { public void testCommand_missingTld() throws Exception {
createTld("xn--q9jyb4c"); thrown.expect(ParameterException.class, "The following option is required: -t, --tld");
runCommand( runCommand("--watermark=2017-01-01T00:00:00Z", "--mode=thin", "-r 42", "-o test");
"--outdir=" + tmpDir.getRoot(),
"--tld=xn--q9jyb4c",
"--watermark=" + clock.nowUtc().withTimeAtStartOfDay());
assertThat(tmpDir.getRoot().list()).asList().containsExactly(
"xn--q9jyb4c_2010-10-17_full_S1_R0.xml",
"xn--q9jyb4c_2010-10-17_full_S1_R0-report.xml",
"xn--q9jyb4c_2010-10-17_full_S1_R0.ryde",
"xn--q9jyb4c_2010-10-17_full_S1_R0.sig",
"xn--q9jyb4c.pub");
} }
@Test @Test
public void testRun_missingTldName_fails() throws Exception { public void testCommand_emptyTld() throws Exception {
thrown.expect(ParameterException.class); thrown.expect(IllegalArgumentException.class, "Null or empty TLD specified");
runCommand( runCommand("--tld=", "--watermark=2017-01-01T00:00:00Z", "--mode=thin", "-r 42", "-o test");
"--outdir=" + tmpDir.getRoot(),
"--watermark=" + clock.nowUtc());
} }
@Test @Test
public void testRun_nonexistentTld_fails() throws Exception { public void testCommand_invalidTld() throws Exception {
thrown.expect(IllegalArgumentException.class); thrown.expect(IllegalArgumentException.class, "TLDs do not exist: invalid");
runCommand( runCommand(
"--outdir=" + tmpDir.getRoot(), "--tld=invalid", "--watermark=2017-01-01T00:00:00Z", "--mode=thin", "-r 42", "-o test");
"--tld=xn--q9jyb4c",
"--watermark=" + clock.nowUtc());
} }
@Test @Test
public void testRun_oneHostNotDeletedOrFuture_producesValidDepositXml() throws Exception { public void testCommand_missingWatermark() throws Exception {
createTlds("lol", "ussr", "xn--q9jyb4c"); thrown.expect(ParameterException.class, "The following option is required: -w, --watermark");
clock.setTo(DateTime.parse("1980-01-01TZ")); runCommand("--tld=tld", "--mode=full", "-r 42", "-o test");
persistResourceWithCommitLog(
newHostResource("communism.ussr", "dead:beef::cafe").asBuilder()
.setDeletionTime(DateTime.parse("1991-12-25TZ")) // nope you're deleted
.build());
clock.setTo(DateTime.parse("1999-12-31TZ"));
saveHostResource("ns1.cat.lol", "feed::a:bee"); // different tld doesn't matter
clock.setTo(DateTime.parse("2020-12-31TZ"));
saveHostResource("ns2.cat.lol", "a:fed::acab"); // nope you're from the future
clock.setTo(DateTime.parse("2010-10-17TZ"));
runCommand(
"--outdir=" + tmpDir.getRoot(),
"--tld=xn--q9jyb4c",
"--watermark=" + clock.nowUtc());
XmlTestUtils.assertXmlEquals(
readResourceUtf8(getClass(), "testdata/xn--q9jyb4c_2010-10-17_full_S1_R0.xml"),
new String(
Files.readAllBytes(
Paths.get(tmpDir.getRoot().toString(), "xn--q9jyb4c_2010-10-17_full_S1_R0.xml")),
UTF_8),
"deposit.contents.registrar.crDate",
"deposit.contents.registrar.upDate");
} }
@Test @Test
public void testRun_generatedXml_isSchemaValidAndHasStuffInIt() throws Exception { public void testCommand_emptyWatermark() throws Exception {
clock.setTo(DateTime.parse("1984-12-17TZ")); thrown.expect(IllegalArgumentException.class, "Invalid format: \"\"");
createTld("xn--q9jyb4c"); runCommand("--tld=tld", "--watermark=", "--mode=full", "-r 42", "-o test");
saveHostResource("ns1.cat.lol", "feed::a:bee");
clock.setTo(DateTime.parse("1984-12-18TZ"));
runCommand(
"--outdir=" + tmpDir.getRoot(),
"--tld=xn--q9jyb4c",
"--watermark=" + clock.nowUtc());
XjcRdeDeposit deposit = (XjcRdeDeposit)
unmarshal(Files.readAllBytes(
Paths.get(tmpDir.getRoot().toString(), "xn--q9jyb4c_1984-12-18_full_S1_R0.xml")));
assertThat(deposit.getType()).isEqualTo(XjcRdeDepositTypeType.FULL);
assertThat(deposit.getId()).isEqualTo(RdeUtil.timestampToId(DateTime.parse("1984-12-18TZ")));
assertThat(deposit.getWatermark()).isEqualTo(DateTime.parse("1984-12-18TZ"));
XjcRdeRegistrar registrar1 = extractAndRemoveContentWithType(XjcRdeRegistrar.class, deposit);
XjcRdeRegistrar registrar2 = extractAndRemoveContentWithType(XjcRdeRegistrar.class, deposit);
XjcRdeHost host = extractAndRemoveContentWithType(XjcRdeHost.class, deposit);
XjcRdeHeader header = extractAndRemoveContentWithType(XjcRdeHeader.class, deposit);
assertThat(host.getName()).isEqualTo("ns1.cat.lol");
assertThat(asList(registrar1.getName(), registrar2.getName()))
.containsExactly("New Registrar", "The Registrar");
assertThat(mapifyCounts(header)).containsEntry(RdeResourceType.HOST.getUri(), 1L);
assertThat(mapifyCounts(header)).containsEntry(RdeResourceType.REGISTRAR.getUri(), 2L);
} }
@Test @Test
public void testRun_thinBrdaDeposit_hostsGetExcluded() throws Exception { public void testCommand_missingOutdir() throws Exception {
clock.setTo(DateTime.parse("1984-12-17TZ")); thrown.expect(ParameterException.class, "The following option is required: -o, --outdir");
createTld("xn--q9jyb4c"); runCommand("--tld=tld", "--watermark=2017-01-01T00:00:00Z", "--mode=thin", "-r 42");
saveHostResource("ns1.cat.lol", "feed::a:bee"); }
clock.setTo(DateTime.parse("1984-12-18TZ"));
@Test
public void testCommand_emptyOutdir() throws Exception {
thrown.expect(ParameterException.class, "Output subdirectory must not be empty");
runCommand( runCommand(
"--outdir=" + tmpDir.getRoot(), "--tld=tld", "--watermark=2017-01-01T00:00:00Z", "--mode=thin", "--outdir=", "-r 42");
"--tld=xn--q9jyb4c",
"--watermark=" + clock.nowUtc(),
"--mode=THIN");
XjcRdeDeposit deposit = (XjcRdeDeposit)
unmarshal(Files.readAllBytes(
Paths.get(tmpDir.getRoot().toString(), "xn--q9jyb4c_1984-12-18_thin_S1_R0.xml")));
assertThat(deposit.getType()).isEqualTo(XjcRdeDepositTypeType.FULL);
assertThat(deposit.getId()).isEqualTo(RdeUtil.timestampToId(DateTime.parse("1984-12-18TZ")));
assertThat(deposit.getWatermark()).isEqualTo(DateTime.parse("1984-12-18TZ"));
XjcRdeHeader header = extractAndRemoveContentWithType(XjcRdeHeader.class, deposit);
assertThat(mapifyCounts(header)).doesNotContainKey(RdeResourceType.HOST.getUri());
assertThat(mapifyCounts(header)).containsEntry(RdeResourceType.REGISTRAR.getUri(), 2L);
} }
private HostResource saveHostResource(String fqdn, String ip) { @Test
clock.advanceOneMilli(); public void testCommand_invalidWatermark() throws Exception {
return persistResourceWithCommitLog(newHostResource(fqdn, ip)); thrown.expect(ParameterException.class, "Each watermark date must be the start of a day");
runCommand(
"--tld=tld",
"--watermark=2017-01-01T10:00:00Z,2017-01-02T00:00:00Z",
"--mode=thin",
"-r 42",
"-o test");
} }
private HostResource newHostResource(String fqdn, String ip) { @Test
return new HostResource.Builder() public void testCommand_invalidMode() throws Exception {
.setRepoId(generateNewContactHostRoid()) thrown.expect(
.setCreationClientId("LawyerCat") ParameterException.class, "Invalid value for -m parameter. Allowed values:[FULL, THIN]");
.setCreationTimeForTest(clock.nowUtc()) runCommand(
.setPersistedCurrentSponsorClientId("BusinessCat") "--tld=tld",
.setFullyQualifiedHostName(Idn.toASCII(fqdn)) "--watermark=2017-01-01T00:00:00Z",
.setInetAddresses(ImmutableSet.of(InetAddresses.forString(ip))) "--mode=thing",
.setLastTransferTime(DateTime.parse("1910-01-01T00:00:00Z")) "-r 42",
.setLastEppUpdateClientId("CeilingCat") "-o test");
.setLastEppUpdateTime(clock.nowUtc())
.setStatusValues(ImmutableSet.of(StatusValue.OK))
.build();
} }
public static Object unmarshal(byte[] xml) throws XmlException { @Test
return XjcXmlTransformer.unmarshal(Object.class, new ByteArrayInputStream(xml)); public void testCommand_invalidRevision() throws Exception {
thrown.expect(
ParameterException.class, "Revision must be greater than or equal to zero");
runCommand(
"--tld=tld",
"--watermark=2017-01-01T00:00:00Z",
"--mode=thin",
"-r -1",
"-o test");
} }
private static ImmutableMap<String, Long> mapifyCounts(XjcRdeHeader header) { @Test
ImmutableMap.Builder<String, Long> builder = new ImmutableMap.Builder<>(); public void testCommand_success() throws Exception {
for (XjcRdeHeaderCount count : header.getCounts()) { runCommand("--tld=tld", "--watermark=2017-01-01T00:00:00Z", "--mode=thin", "-r 42", "-o test");
builder.put(count.getUri(), count.getValue());
} assertTasksEnqueued("rde-report",
return builder.build(); new TaskMatcher()
.url("/_dr/task/rdeStaging")
.header("Host", "1.backend.test.localhost")
.param("mode", "THIN")
.param("watermark", "2017-01-01T00:00:00.000Z")
.param("tld", "tld")
.param("directory", "test")
.param("manual", "true")
.param("revision", "42"));
} }
private <T extends XjcRdeContentType> @Test
T extractAndRemoveContentWithType(Class<T> type, XjcRdeDeposit deposit) { public void testCommand_successWithDefaultRevision() throws Exception {
for (JAXBElement<? extends XjcRdeContentType> content : deposit.getContents().getContents()) { runCommand("--tld=tld", "--watermark=2017-01-01T00:00:00Z", "--mode=thin", "-o test");
XjcRdeContentType piece = content.getValue();
if (type.isInstance(piece) && !alreadyExtracted.contains(piece)) { assertTasksEnqueued("rde-report",
alreadyExtracted.add(piece); new TaskMatcher()
return type.cast(piece); .url("/_dr/task/rdeStaging")
} .header("Host", "1.backend.test.localhost")
} .param("mode", "THIN")
throw new AssertionError("Expected deposit to contain another " + type.getSimpleName()); .param("watermark", "2017-01-01T00:00:00.000Z")
.param("tld", "tld")
.param("directory", "test")
.param("manual", "true"));
}
@Test
public void testCommand_successWithDefaultMode() throws Exception {
runCommand("--tld=tld", "--watermark=2017-01-01T00:00:00Z", "-r=42", "-o test");
assertTasksEnqueued("rde-report",
new TaskMatcher()
.url("/_dr/task/rdeStaging")
.header("Host", "1.backend.test.localhost")
.param("mode", "FULL")
.param("watermark", "2017-01-01T00:00:00.000Z")
.param("tld", "tld")
.param("directory", "test")
.param("manual", "true")
.param("revision", "42"));
}
@Test
public void testCommand_successWithMultipleArgumentValues() throws Exception {
runCommand(
"--tld=tld,anothertld",
"--watermark=2017-01-01T00:00:00Z,2017-01-02T00:00:00Z",
"--mode=thin",
"-r 42",
"-o test");
assertTasksEnqueued("rde-report",
new TaskMatcher()
.url("/_dr/task/rdeStaging")
.header("Host", "1.backend.test.localhost")
.param("mode", "THIN")
.param("watermark", "2017-01-01T00:00:00.000Z,2017-01-02T00:00:00.000Z")
.param("tld", "tld,anothertld")
.param("directory", "test")
.param("manual", "true")
.param("revision", "42"));
} }
} }