Link domain to subordinate hosts on RDE import

Based on the original pull request below with some modifications for code drift
over time (including adding handling/testing for the case where superordinate
domains are in pending delete, and thus can't have hosts linked to them).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=169698157
This commit is contained in:
mcilwain 2017-09-22 09:26:56 -07:00 committed by Ben McIlwain
parent c64e9fe788
commit 8bd9b35dfd
2 changed files with 173 additions and 35 deletions

View file

@ -15,17 +15,21 @@
package google.registry.rde.imports;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.eppcommon.StatusValue.PENDING_DELETE;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.DatastoreHelper.newHostResource;
import static google.registry.testing.DatastoreHelper.persistActiveDomain;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.util.DateTimeUtils.END_OF_TIME;
import com.google.appengine.tools.cloudstorage.GcsFilename;
import com.google.appengine.tools.cloudstorage.GcsService;
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
import com.google.appengine.tools.cloudstorage.RetryParams;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.googlecode.objectify.Key;
@ -34,14 +38,20 @@ import google.registry.gcs.GcsUtils;
import google.registry.mapreduce.MapreduceRunner;
import google.registry.model.domain.DomainResource;
import google.registry.model.host.HostResource;
import google.registry.model.index.ForeignKeyIndex.ForeignKeyDomainIndex;
import google.registry.model.ofy.Ofy;
import google.registry.request.Response;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeResponse;
import google.registry.testing.InjectRule;
import google.registry.testing.mapreduce.MapreduceTestCase;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -57,14 +67,21 @@ public class RdeHostLinkActionTest extends MapreduceTestCase<RdeHostLinkAction>
private static final GcsService GCS_SERVICE =
GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
@Rule
public final InjectRule inject = new InjectRule();
private MapreduceRunner mrRunner;
private Response response;
private final Optional<Integer> mapShards = Optional.absent();
private final FakeClock clock = new FakeClock();
@Before
public void before() throws Exception {
clock.setTo(DateTime.parse("2016-05-07T14:55:38Z"));
inject.setStaticField(Ofy.class, "clock", clock);
createTld("test");
response = new FakeResponse();
mrRunner = new MapreduceRunner(Optional.<Integer>absent(), Optional.<Integer>absent());
@ -83,10 +100,63 @@ public class RdeHostLinkActionTest extends MapreduceTestCase<RdeHostLinkAction>
DomainResource superordinateDomain = persistActiveDomain("example1.test");
Key<DomainResource> superOrdinateDomainKey = Key.create(superordinateDomain);
pushToGcs(DEPOSIT_1_HOST);
// set transaction time to slightly after resource save
clock.advanceOneMilli();
runMapreduce();
// verify that host is linked to domain
List<HostResource> hosts = ofy().load().type(HostResource.class).list();
assertThat(hosts).hasSize(1);
assertThat(hosts.get(0).getSuperordinateDomain()).isEqualTo(superOrdinateDomainKey);
assertThat(hosts.get(0).getLastSuperordinateChange())
.isEqualTo(DateTime.parse("2016-05-07T14:55:38.001Z"));
// verify that domain is linked to host
List<DomainResource> domains = ofy().load().type(DomainResource.class).list();
assertThat(domains).hasSize(1);
assertThat(domains.get(0).getSubordinateHosts()).containsExactly("ns1.example1.test");
}
@Test
public void test_mapreduceIgnoresHostWithWrongRepoid() throws Exception {
// in-zone host with different repoid is ignored, since it would already be linked
// from a separate epp create or import.
// Create host and domain first
HostResource newHost = persistResource(
newHostResource("ns1.example1.test")
.asBuilder()
.setRepoId("wrong-repoid")
.build());
DomainResource superordinateDomain = persistActiveDomain("example1.test");
ForeignKeyDomainIndex.create(superordinateDomain, END_OF_TIME);
pushToGcs(DEPOSIT_1_HOST);
// set transaction time to slightly after resource save
clock.advanceOneMilli();
runMapreduce();
// verify that host has not been updated
List<HostResource> hosts = ofy().load().type(HostResource.class).list();
assertThat(hosts).hasSize(1);
assertThat(hosts.get(0).getUpdateAutoTimestamp()).isEqualTo(newHost.getUpdateAutoTimestamp());
}
@Test
public void test_mapreduceIgnoresHostWithSuperordinateDomainPendingDeletion() throws Exception {
HostResource newHost = persistResource(
newHostResource("ns1.example1.test")
.asBuilder()
.setRepoId("Hns1_example1_test-TEST")
.build());
persistResource(
newDomainResource("example1.test")
.asBuilder()
.setStatusValues(ImmutableSet.of(PENDING_DELETE))
.build());
pushToGcs(DEPOSIT_1_HOST);
// set transaction time to slightly after resource save
clock.advanceOneMilli();
runMapreduce();
// verify that host has not been updated
List<HostResource> hosts = ofy().load().type(HostResource.class).list();
assertThat(hosts).hasSize(1);
assertThat(hosts.get(0).getUpdateAutoTimestamp()).isEqualTo(newHost.getUpdateAutoTimestamp());
}
private void runMapreduce() throws Exception {