Send a plaintext link to the mapreduce console in fluent style

The link was previously being sent using a JS redirect, which doesn't work
because the endpoints that trigger mapreduces can only be hit from the command
line (because they require auth). This commit switches the link to be in
plaintext and renders the full URL instead of just the path, so that clicking it
directly from the terminal works.

This also improves how these links are sent from callsites by using a fluent
style.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=228764606
This commit is contained in:
mcilwain 2019-01-10 13:20:08 -08:00 committed by Ben McIlwain
parent 072576ec9d
commit 765e63e7e9
31 changed files with 202 additions and 239 deletions

View file

@ -55,6 +55,7 @@ public class ExportDomainListsActionTest extends MapreduceTestCase<ExportDomainL
private GcsService gcsService;
private DriveConnection driveConnection = mock(DriveConnection.class);
private ArgumentCaptor<byte[]> bytesExportedToDrive = ArgumentCaptor.forClass(byte[].class);
private final FakeResponse response = new FakeResponse();
@Before
public void init() {
@ -67,7 +68,7 @@ public class ExportDomainListsActionTest extends MapreduceTestCase<ExportDomainL
action = new ExportDomainListsAction();
action.mrRunner = makeDefaultRunner();
action.response = new FakeResponse();
action.response = response;
action.gcsBucket = "outputbucket";
action.gcsBufferSize = 500;
gcsService = createGcsService();
@ -88,6 +89,14 @@ public class ExportDomainListsActionTest extends MapreduceTestCase<ExportDomainL
assertThat(new String(bytesExportedToDrive.getValue(), "UTF-8")).isEqualTo(domains);
}
@Test
public void test_writesLinkToMapreduceConsoleToResponse() throws Exception {
runMapreduce();
assertThat(response.getPayload())
.startsWith(
"Mapreduce console: https://backend.hostname.tld/_ah/pipeline/status.html?root=");
}
@Test
public void test_outputsOnlyActiveDomains() throws Exception {
persistActiveDomain("onetwo.tld");

View file

@ -69,7 +69,7 @@ public class RdeHostImportActionTest extends MapreduceTestCase<RdeHostImportActi
@Before
public void before() {
response = new FakeResponse();
mrRunner = new MapreduceRunner(Optional.empty(), Optional.empty());
mrRunner = makeDefaultRunner();
action = new RdeHostImportAction(
mrRunner,
response,

View file

@ -85,7 +85,7 @@ public class RdeHostLinkActionTest extends MapreduceTestCase<RdeHostLinkAction>
inject.setStaticField(Ofy.class, "clock", clock);
createTld("test");
response = new FakeResponse();
mrRunner = new MapreduceRunner(Optional.empty(), Optional.empty());
mrRunner = makeDefaultRunner();
action =
new RdeHostLinkAction(mrRunner, response, IMPORT_BUCKET_NAME, IMPORT_FILE_NAME, mapShards);
}

View file

@ -55,13 +55,4 @@ public class ResponseImplTest {
new ResponseImpl(rsp).setPayload("hello world");
assertThat(httpOutput.toString()).isEqualTo("hello world");
}
@Test
public void testSendJavaScriptRedirect_producesHtmlScript() throws Exception {
StringWriter httpOutput = new StringWriter();
when(rsp.getWriter()).thenReturn(new PrintWriter(httpOutput));
new ResponseImpl(rsp).sendJavaScriptRedirect("/hello");
assertThat(httpOutput.toString()).isEqualTo(
"<script>window.location.replace(\"/hello\");</script><a href=\"/hello\">/hello</a>");
}
}

View file

@ -80,16 +80,11 @@ public final class FakeResponse implements Response {
headers.put(checkNotNull(header), checkNotNull(timestamp));
}
@Override
public void sendJavaScriptRedirect(String redirectUrl) {
checkResponsePerformedOnce();
this.status = 200;
this.payload = "Javascript redirect to " + redirectUrl;
}
private void checkResponsePerformedOnce() {
checkState(!wasMutuallyExclusiveResponseSet,
"Two responses were sent. Here's the previous call:\n%s", lastResponseStackTrace);
checkState(
!wasMutuallyExclusiveResponseSet,
"Two responses were sent. Here's the previous call:\n%s",
lastResponseStackTrace);
wasMutuallyExclusiveResponseSet = true;
lastResponseStackTrace = getStackTrace();
}

View file

@ -14,6 +14,7 @@ java_library(
"//java/google/registry/config",
"//java/google/registry/mapreduce",
"//java/google/registry/model",
"//java/google/registry/util",
"//javatests/google/registry/testing",
"@com_google_appengine_api_1_0_sdk",
"@com_google_appengine_api_stubs",

View file

@ -14,7 +14,6 @@
package google.registry.testing.mapreduce;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.config.RegistryConfig.getEppResourceIndexBucketCount;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static org.mockito.Mockito.mock;
@ -24,11 +23,9 @@ import com.google.appengine.api.blobstore.dev.LocalBlobstoreService;
import com.google.appengine.api.taskqueue.dev.LocalTaskQueue;
import com.google.appengine.api.taskqueue.dev.QueueStateInfo;
import com.google.appengine.api.taskqueue.dev.QueueStateInfo.HeaderWrapper;
import com.google.appengine.api.taskqueue.dev.QueueStateInfo.TaskStateInfo;
import com.google.appengine.tools.development.ApiProxyLocal;
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
import com.google.appengine.tools.mapreduce.MapReduceServlet;
import com.google.appengine.tools.mapreduce.impl.shardedjob.ShardedJobHandler;
import com.google.appengine.tools.pipeline.impl.servlets.PipelineServlet;
import com.google.appengine.tools.pipeline.impl.servlets.TaskHandler;
import com.google.apphosting.api.ApiProxy;
@ -37,7 +34,9 @@ import com.google.common.flogger.FluentLogger;
import google.registry.mapreduce.MapreduceRunner;
import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeClock;
import google.registry.testing.MockitoJUnitRule;
import google.registry.testing.ShardableTestCase;
import google.registry.util.AppEngineServiceUtils;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.io.UnsupportedEncodingException;
@ -52,13 +51,16 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Rule;
import org.mockito.Mock;
/**
* Base test class for mapreduces. Adapted from EndToEndTestCase with some modifications that
* allow it to work with the Nomulus project, most notably inside knowledge of our
* routing paths and our Datastore/Task Queue configurations.
* Base test class for mapreduces.
*
* <p>See https://github.com/GoogleCloudPlatform/appengine-mapreduce/blob/master/java/src/test/java/com/google/appengine/tools/mapreduce/EndToEndTestCase.java
* <p>Adapted from EndToEndTestCase with some modifications that allow it to work with Nomulus, most
* notably inside knowledge of our routing paths and our Datastore/Task Queue configurations.
*
* <p>See
* https://github.com/GoogleCloudPlatform/appengine-mapreduce/blob/master/java/src/test/java/com/google/appengine/tools/mapreduce/EndToEndTestCase.java
*
* @param <T> The type of the Action class that implements the mapreduce.
*/
@ -79,16 +81,23 @@ public abstract class MapreduceTestCase<T> extends ShardableTestCase {
.withTaskQueue()
.build();
@Rule public final MockitoJUnitRule mocks = MockitoJUnitRule.create();
@Mock
AppEngineServiceUtils appEngineServiceUtils;
@Before
public void setUp() {
taskQueue = LocalTaskQueueTestConfig.getLocalTaskQueue();
ApiProxyLocal proxy = (ApiProxyLocal) ApiProxy.getDelegate();
// Creating files is not allowed in some test execution environments, so don't.
proxy.setProperty(LocalBlobstoreService.NO_STORAGE_PROPERTY, "true");
when(appEngineServiceUtils.getServiceHostname("backend")).thenReturn("backend.hostname.tld");
}
protected MapreduceRunner makeDefaultRunner() {
return new MapreduceRunner(Optional.of(getEppResourceIndexBucketCount()), Optional.of(1));
return new MapreduceRunner(
Optional.of(getEppResourceIndexBucketCount()), Optional.of(1), appEngineServiceUtils);
}
protected List<QueueStateInfo.TaskStateInfo> getTasks(String queueName) {
@ -210,14 +219,6 @@ public abstract class MapreduceTestCase<T> extends ShardableTestCase {
}
}
protected TaskStateInfo grabNextTaskFromQueue(String queueName) {
List<TaskStateInfo> taskInfo = getTasks(queueName);
assertThat(taskInfo).isNotEmpty();
TaskStateInfo taskStateInfo = taskInfo.get(0);
taskQueue.deleteTask(queueName, taskStateInfo.getTaskName());
return taskStateInfo;
}
// Sadly there's no way to parse query string with JDK. This is a good enough approximation.
private static Map<String, String> decodeParameters(String requestBody)
throws UnsupportedEncodingException {
@ -236,8 +237,4 @@ public abstract class MapreduceTestCase<T> extends ShardableTestCase {
return result;
}
protected String getTaskId(TaskStateInfo taskStateInfo) throws UnsupportedEncodingException {
return decodeParameters(taskStateInfo.getBody()).get(ShardedJobHandler.TASK_ID_PARAM);
}
}

View file

@ -16,7 +16,7 @@ package google.registry.tools.server;
import static com.google.appengine.tools.cloudstorage.GcsServiceFactory.createGcsService;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.createTlds;
import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.DatastoreHelper.newHostResource;
import static google.registry.testing.DatastoreHelper.persistActiveContact;
@ -58,9 +58,7 @@ public class GenerateZoneFilesActionTest extends MapreduceTestCase<GenerateZoneF
@Test
public void testGenerate() throws Exception {
DateTime now = DateTime.now(DateTimeZone.UTC).withTimeAtStartOfDay();
createTld("tld");
createTld("com");
createTlds("tld", "com");
ImmutableSet<InetAddress> ips =
ImmutableSet.of(InetAddress.getByName("127.0.0.1"), InetAddress.getByName("::1"));
@ -126,12 +124,15 @@ public class GenerateZoneFilesActionTest extends MapreduceTestCase<GenerateZoneF
action.dnsDefaultDsTtl = Duration.standardSeconds(3333);
action.clock = new FakeClock(now.plusMinutes(2)); // Move past the actions' 2 minute check.
Map<String, Object> response = action.handleJsonRequest(ImmutableMap.<String, Object>of(
"tlds", ImmutableList.of("tld"),
"exportTime", now));
assertThat(response).containsEntry(
"filenames",
ImmutableList.of("gs://zonefiles-bucket/tld-" + now + ".zone"));
Map<String, Object> response =
action.handleJsonRequest(
ImmutableMap.<String, Object>of("tlds", ImmutableList.of("tld"), "exportTime", now));
assertThat(response)
.containsEntry("filenames", ImmutableList.of("gs://zonefiles-bucket/tld-" + now + ".zone"));
assertThat(response).containsKey("mapreduceConsoleLink");
assertThat(response.get("mapreduceConsoleLink").toString())
.startsWith(
"Mapreduce console: https://backend.hostname.tld/_ah/pipeline/status.html?root=");
executeTasksUntilEmpty("mapreduce");