mirror of
https://github.com/google/nomulus.git
synced 2025-07-14 06:55:20 +02:00
Remove deprecated Datastore backup code
Removed three Action classes and the CheckSnapshot command. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=230545631
This commit is contained in:
parent
701ebc6a28
commit
acbd23fa64
20 changed files with 14 additions and 1527 deletions
|
@ -1,221 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 google.registry.export;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.export.CheckSnapshotAction.CHECK_SNAPSHOT_KINDS_TO_LOAD_PARAM;
|
||||
import static google.registry.export.CheckSnapshotAction.CHECK_SNAPSHOT_NAME_PARAM;
|
||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
import static google.registry.testing.TaskQueueHelper.assertNoTasksEnqueued;
|
||||
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import google.registry.request.Action.Method;
|
||||
import google.registry.request.HttpException.BadRequestException;
|
||||
import google.registry.request.HttpException.NoContentException;
|
||||
import google.registry.request.HttpException.NotModifiedException;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.FakeResponse;
|
||||
import google.registry.testing.InjectRule;
|
||||
import google.registry.testing.TaskQueueHelper.TaskMatcher;
|
||||
import java.util.Optional;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.Duration;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link CheckSnapshotAction}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class CheckSnapshotActionTest {
|
||||
|
||||
static final DateTime START_TIME = DateTime.parse("2014-08-01T01:02:03Z");
|
||||
static final DateTime COMPLETE_TIME = START_TIME.plus(Duration.standardMinutes(30));
|
||||
|
||||
@Rule public final InjectRule inject = new InjectRule();
|
||||
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withTaskQueue().build();
|
||||
private final DatastoreBackupService backupService = mock(DatastoreBackupService.class);
|
||||
|
||||
private DatastoreBackupInfo backupInfo;
|
||||
|
||||
private final FakeResponse response = new FakeResponse();
|
||||
private final FakeClock clock = new FakeClock(COMPLETE_TIME.plusMillis(1000));
|
||||
private final CheckSnapshotAction action = new CheckSnapshotAction();
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
inject.setStaticField(DatastoreBackupInfo.class, "clock", clock);
|
||||
action.requestMethod = Method.POST;
|
||||
action.snapshotName = "some_backup";
|
||||
action.kindsToLoadParam = "one,two";
|
||||
action.response = response;
|
||||
action.backupService = backupService;
|
||||
|
||||
backupInfo =
|
||||
new DatastoreBackupInfo(
|
||||
"some_backup",
|
||||
START_TIME,
|
||||
Optional.of(COMPLETE_TIME),
|
||||
ImmutableSet.of("one", "two", "three"),
|
||||
Optional.of("gs://somebucket/some_backup_20140801.backup_info"));
|
||||
|
||||
when(backupService.findByName("some_backup")).thenReturn(backupInfo);
|
||||
}
|
||||
|
||||
private void setPendingBackup() {
|
||||
backupInfo =
|
||||
new DatastoreBackupInfo(
|
||||
backupInfo.getName(),
|
||||
backupInfo.getStartTime(),
|
||||
Optional.empty(),
|
||||
backupInfo.getKinds(),
|
||||
backupInfo.getGcsFilename());
|
||||
|
||||
when(backupService.findByName("some_backup")).thenReturn(backupInfo);
|
||||
}
|
||||
|
||||
private static void assertLoadTaskEnqueued(String id, String file, String kinds) {
|
||||
assertTasksEnqueued(
|
||||
"export-snapshot",
|
||||
new TaskMatcher()
|
||||
.url("/_dr/task/loadSnapshot")
|
||||
.method("POST")
|
||||
.param("id", id)
|
||||
.param("file", file)
|
||||
.param("kinds", kinds));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_enqueuePollTask() {
|
||||
CheckSnapshotAction.enqueuePollTask(
|
||||
"some_snapshot_name", ImmutableSet.of("one", "two", "three"));
|
||||
assertTasksEnqueued(
|
||||
CheckSnapshotAction.QUEUE,
|
||||
new TaskMatcher()
|
||||
.url(CheckSnapshotAction.PATH)
|
||||
.param(CHECK_SNAPSHOT_NAME_PARAM, "some_snapshot_name")
|
||||
.param(CHECK_SNAPSHOT_KINDS_TO_LOAD_PARAM, "one,two,three")
|
||||
.method("POST"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forPendingBackup_returnsNotModified() {
|
||||
setPendingBackup();
|
||||
|
||||
NotModifiedException thrown = assertThrows(NotModifiedException.class, action::run);
|
||||
assertThat(thrown).hasMessageThat().contains("Datastore backup some_backup still pending");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forStalePendingBackupBackup_returnsNoContent() {
|
||||
setPendingBackup();
|
||||
|
||||
when(backupService.findByName("some_backup")).thenReturn(backupInfo);
|
||||
|
||||
clock.setTo(
|
||||
START_TIME
|
||||
.plus(Duration.standardHours(20))
|
||||
.plus(Duration.standardMinutes(3))
|
||||
.plus(Duration.millis(1234)));
|
||||
|
||||
NoContentException thrown = assertThrows(NoContentException.class, action::run);
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains(
|
||||
"Datastore backup some_backup abandoned - "
|
||||
+ "not complete after 20 hours, 3 minutes and 1 second");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forCompleteBackup_enqueuesLoadTask() {
|
||||
|
||||
action.run();
|
||||
assertLoadTaskEnqueued(
|
||||
"20140801_010203", "gs://somebucket/some_backup_20140801.backup_info", "one,two");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forCompleteAutoBackup_enqueuesLoadTask_usingBackupName() {
|
||||
action.snapshotName = "auto_snapshot_somestring";
|
||||
when(backupService.findByName("auto_snapshot_somestring")).thenReturn(backupInfo);
|
||||
|
||||
action.run();
|
||||
assertLoadTaskEnqueued(
|
||||
"somestring", "gs://somebucket/some_backup_20140801.backup_info", "one,two");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forCompleteBackup_withExtraKindsToLoad_enqueuesLoadTask() {
|
||||
action.kindsToLoadParam = "one,foo";
|
||||
|
||||
action.run();
|
||||
assertLoadTaskEnqueued(
|
||||
"20140801_010203", "gs://somebucket/some_backup_20140801.backup_info", "one");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forCompleteBackup_withEmptyKindsToLoad_skipsLoadTask() {
|
||||
action.kindsToLoadParam = "";
|
||||
|
||||
action.run();
|
||||
assertNoTasksEnqueued("export-snapshot");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forBadBackup_returnsBadRequest() {
|
||||
when(backupService.findByName("some_backup"))
|
||||
.thenThrow(new IllegalArgumentException("No backup found"));
|
||||
|
||||
BadRequestException thrown = assertThrows(BadRequestException.class, action::run);
|
||||
assertThat(thrown).hasMessageThat().contains("Bad backup name some_backup: No backup found");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet_returnsInformation() {
|
||||
action.requestMethod = Method.GET;
|
||||
|
||||
action.run();
|
||||
assertThat(response.getPayload())
|
||||
.isEqualTo(
|
||||
Joiner.on("\n")
|
||||
.join(
|
||||
ImmutableList.of(
|
||||
"Backup name: some_backup",
|
||||
"Status: COMPLETE",
|
||||
"Started: 2014-08-01T01:02:03.000Z",
|
||||
"Ended: 2014-08-01T01:32:03.000Z",
|
||||
"Duration: 30m",
|
||||
"GCS: gs://somebucket/some_backup_20140801.backup_info",
|
||||
"Kinds: [one, two, three]",
|
||||
"")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet_forBadBackup_returnsError() {
|
||||
action.requestMethod = Method.GET;
|
||||
when(backupService.findByName("some_backup"))
|
||||
.thenThrow(new IllegalArgumentException("No backup found"));
|
||||
|
||||
BadRequestException thrown = assertThrows(BadRequestException.class, action::run);
|
||||
assertThat(thrown).hasMessageThat().contains("Bad backup name some_backup: No backup found");
|
||||
}
|
||||
}
|
|
@ -1,130 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 google.registry.export;
|
||||
|
||||
import static com.google.appengine.api.datastore.DatastoreServiceFactory.getDatastoreService;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
|
||||
import com.google.appengine.api.datastore.Entity;
|
||||
import com.google.appengine.api.datastore.EntityNotFoundException;
|
||||
import com.google.appengine.api.datastore.Text;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.export.DatastoreBackupInfo.BackupStatus;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.InjectRule;
|
||||
import java.util.Date;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.Duration;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link DatastoreBackupInfo}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class DatastoreBackupInfoTest {
|
||||
@Rule
|
||||
public final InjectRule inject = new InjectRule();
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine = AppEngineRule.builder()
|
||||
.withDatastore()
|
||||
.build();
|
||||
|
||||
private FakeClock clock = new FakeClock();
|
||||
|
||||
private DateTime startTime = DateTime.parse("2014-08-01T01:02:03Z");
|
||||
|
||||
private Entity backupEntity; // Can't initialize until AppEngineRule has set up Datastore.
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
inject.setStaticField(DatastoreBackupInfo.class, "clock", clock);
|
||||
backupEntity = new Entity("_unused_");
|
||||
backupEntity.setProperty("name", "backup1");
|
||||
backupEntity.setProperty("kinds", ImmutableList.of("one", "two", "three"));
|
||||
backupEntity.setProperty("start_time", new Date(startTime.getMillis()));
|
||||
}
|
||||
|
||||
/** Force a roundtrip to Datastore to ensure that we're simulating a freshly fetched entity. */
|
||||
private static Entity persistEntity(Entity entity) throws EntityNotFoundException {
|
||||
return getDatastoreService().get(getDatastoreService().put(entity));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_pendingBackup() throws Exception {
|
||||
DatastoreBackupInfo backup = new DatastoreBackupInfo(persistEntity(backupEntity));
|
||||
|
||||
assertThat(backup.getName()).isEqualTo("backup1");
|
||||
assertThat(backup.getKinds()).containsExactly("one", "two", "three");
|
||||
assertThat(backup.getStartTime()).isEqualTo(startTime);
|
||||
assertThat(backup.getCompleteTime()).isEmpty();
|
||||
assertThat(backup.getGcsFilename()).isEmpty();
|
||||
assertThat(backup.getStatus()).isEqualTo(BackupStatus.PENDING);
|
||||
|
||||
clock.setTo(startTime.plusMinutes(1));
|
||||
assertThat(backup.getRunningTime()).isEqualTo(Duration.standardMinutes(1));
|
||||
clock.setTo(startTime.plusMinutes(1).plusSeconds(42));
|
||||
assertThat(backup.getRunningTime()).isEqualTo(Duration.standardSeconds(102));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_completeBackup() throws Exception {
|
||||
DateTime completeTime = startTime.plusMinutes(1).plusSeconds(42);
|
||||
backupEntity.setProperty("complete_time", new Date(completeTime.getMillis()));
|
||||
backupEntity.setProperty("gs_handle", new Text("/gs/somebucket/timestamp.backup_info"));
|
||||
DatastoreBackupInfo backup = new DatastoreBackupInfo(persistEntity(backupEntity));
|
||||
|
||||
assertThat(backup.getName()).isEqualTo("backup1");
|
||||
assertThat(backup.getKinds()).containsExactly("one", "two", "three");
|
||||
assertThat(backup.getStartTime()).isEqualTo(startTime);
|
||||
assertThat(backup.getCompleteTime().get()).isEqualTo(completeTime);
|
||||
assertThat(backup.getGcsFilename()).hasValue("gs://somebucket/timestamp.backup_info");
|
||||
assertThat(backup.getStatus()).isEqualTo(BackupStatus.COMPLETE);
|
||||
assertThat(backup.getRunningTime()).isEqualTo(Duration.standardSeconds(102));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_missingName() {
|
||||
backupEntity.removeProperty("name");
|
||||
assertThrows(
|
||||
NullPointerException.class, () -> new DatastoreBackupInfo(persistEntity(backupEntity)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_missingKinds() {
|
||||
backupEntity.removeProperty("kinds");
|
||||
assertThrows(
|
||||
NullPointerException.class, () -> new DatastoreBackupInfo(persistEntity(backupEntity)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_missingStartTime() {
|
||||
backupEntity.removeProperty("start_time");
|
||||
assertThrows(
|
||||
NullPointerException.class, () -> new DatastoreBackupInfo(persistEntity(backupEntity)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_badGcsFilenameFormat() {
|
||||
backupEntity.setProperty("gs_handle", new Text("foo"));
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> new DatastoreBackupInfo(persistEntity(backupEntity)));
|
||||
}
|
||||
}
|
|
@ -1,123 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 google.registry.export;
|
||||
|
||||
import static com.google.appengine.api.datastore.DatastoreServiceFactory.getDatastoreService;
|
||||
import static com.google.common.collect.Iterables.transform;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.appengine.api.datastore.Entity;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.MockitoJUnitRule;
|
||||
import google.registry.testing.TaskQueueHelper.TaskMatcher;
|
||||
import google.registry.util.AppEngineServiceUtils;
|
||||
import java.util.Date;
|
||||
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;
|
||||
import org.mockito.Mock;
|
||||
|
||||
/** Unit tests for {@link DatastoreBackupService}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class DatastoreBackupServiceTest {
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine =
|
||||
AppEngineRule.builder().withDatastore().withTaskQueue().build();
|
||||
|
||||
@Rule public final MockitoJUnitRule mocks = MockitoJUnitRule.create();
|
||||
|
||||
@Mock private AppEngineServiceUtils appEngineServiceUtils;
|
||||
|
||||
private static final DateTime START_TIME = DateTime.parse("2014-08-01T01:02:03Z");
|
||||
|
||||
private DatastoreBackupService backupService;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
backupService = new DatastoreBackupService(appEngineServiceUtils);
|
||||
when(appEngineServiceUtils.getVersionHostname("default", "ah-builtin-python-bundle"))
|
||||
.thenReturn("ah-builtin-python-bundle.default.localhost");
|
||||
|
||||
persistBackupEntityWithName("backupA1");
|
||||
persistBackupEntityWithName("backupA2");
|
||||
persistBackupEntityWithName("backupA3");
|
||||
persistBackupEntityWithName("backupB1");
|
||||
persistBackupEntityWithName("backupB42");
|
||||
}
|
||||
|
||||
private static void persistBackupEntityWithName(String name) {
|
||||
Entity entity = new Entity(DatastoreBackupService.BACKUP_INFO_KIND);
|
||||
entity.setProperty("name", name);
|
||||
entity.setProperty("kinds", ImmutableList.of("one", "two", "three"));
|
||||
entity.setProperty("start_time", new Date(START_TIME.getMillis()));
|
||||
getDatastoreService().put(entity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_launchBackup() {
|
||||
backupService.launchNewBackup(
|
||||
"export-snapshot", "backup1", "somebucket", ImmutableSet.of("foo", "bar"));
|
||||
assertTasksEnqueued("export-snapshot",
|
||||
new TaskMatcher()
|
||||
.url("/_ah/datastore_admin/backup.create")
|
||||
.header("Host", "ah-builtin-python-bundle.default.localhost")
|
||||
.method("GET")
|
||||
.param("name", "backup1_")
|
||||
.param("filesystem", "gs")
|
||||
.param("gs_bucket_name", "somebucket")
|
||||
.param("queue", "export-snapshot")
|
||||
.param("kind", "foo")
|
||||
.param("kind", "bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_findAllByNamePrefix() {
|
||||
assertThat(
|
||||
transform(backupService.findAllByNamePrefix("backupA"), DatastoreBackupInfo::getName))
|
||||
.containsExactly("backupA1", "backupA2", "backupA3");
|
||||
assertThat(
|
||||
transform(backupService.findAllByNamePrefix("backupB"), DatastoreBackupInfo::getName))
|
||||
.containsExactly("backupB1", "backupB42");
|
||||
assertThat(
|
||||
transform(backupService.findAllByNamePrefix("backupB4"), DatastoreBackupInfo::getName))
|
||||
.containsExactly("backupB42");
|
||||
assertThat(backupService.findAllByNamePrefix("backupX")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_findByName() {
|
||||
assertThat(backupService.findByName("backupA1").getName()).isEqualTo("backupA1");
|
||||
assertThat(backupService.findByName("backupB4").getName()).isEqualTo("backupB42");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_findByName_multipleMatchingBackups() {
|
||||
assertThrows(IllegalArgumentException.class, () -> backupService.findByName("backupA"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_findByName_noMatchingBackups() {
|
||||
assertThrows(IllegalArgumentException.class, () -> backupService.findByName("backupX"));
|
||||
}
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 google.registry.export;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.export.CheckSnapshotAction.CHECK_SNAPSHOT_KINDS_TO_LOAD_PARAM;
|
||||
import static google.registry.export.CheckSnapshotAction.CHECK_SNAPSHOT_NAME_PARAM;
|
||||
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.FakeResponse;
|
||||
import google.registry.testing.TaskQueueHelper.TaskMatcher;
|
||||
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;
|
||||
|
||||
/** Unit tests for {@link ExportSnapshotAction}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class ExportSnapshotActionTest {
|
||||
|
||||
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withTaskQueue().build();
|
||||
|
||||
private final DatastoreBackupService backupService = mock(DatastoreBackupService.class);
|
||||
|
||||
private final FakeResponse response = new FakeResponse();
|
||||
private final FakeClock clock = new FakeClock(DateTime.parse("2014-08-01T01:02:03Z"));
|
||||
private final ExportSnapshotAction action = new ExportSnapshotAction();
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
action.clock = clock;
|
||||
action.backupService = backupService;
|
||||
action.response = response;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_launchesBackup_andEnqueuesPollTask() {
|
||||
action.run();
|
||||
verify(backupService)
|
||||
.launchNewBackup(
|
||||
ExportSnapshotAction.QUEUE,
|
||||
"auto_snapshot_20140801_010203",
|
||||
"registry-project-id-snapshots",
|
||||
ExportConstants.getBackupKinds());
|
||||
assertTasksEnqueued(
|
||||
CheckSnapshotAction.QUEUE,
|
||||
new TaskMatcher()
|
||||
.url(CheckSnapshotAction.PATH)
|
||||
.param(CHECK_SNAPSHOT_NAME_PARAM, "auto_snapshot_20140801_010203")
|
||||
.param(
|
||||
CHECK_SNAPSHOT_KINDS_TO_LOAD_PARAM,
|
||||
Joiner.on(",").join(ExportConstants.getReportingKinds()))
|
||||
.method("POST"));
|
||||
assertThat(response.getPayload())
|
||||
.isEqualTo("Datastore backup started with name: auto_snapshot_20140801_010203");
|
||||
}
|
||||
}
|
|
@ -1,207 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 google.registry.export;
|
||||
|
||||
import static com.google.common.collect.Iterables.transform;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.export.LoadSnapshotAction.LATEST_SNAPSHOT_VIEW_NAME;
|
||||
import static google.registry.export.LoadSnapshotAction.LOAD_SNAPSHOT_FILE_PARAM;
|
||||
import static google.registry.export.LoadSnapshotAction.LOAD_SNAPSHOT_ID_PARAM;
|
||||
import static google.registry.export.LoadSnapshotAction.LOAD_SNAPSHOT_KINDS_PARAM;
|
||||
import static google.registry.export.LoadSnapshotAction.PATH;
|
||||
import static google.registry.export.LoadSnapshotAction.QUEUE;
|
||||
import static google.registry.export.LoadSnapshotAction.enqueueLoadSnapshotTask;
|
||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.api.services.bigquery.Bigquery;
|
||||
import com.google.api.services.bigquery.model.Dataset;
|
||||
import com.google.api.services.bigquery.model.Job;
|
||||
import com.google.api.services.bigquery.model.JobConfigurationLoad;
|
||||
import com.google.api.services.bigquery.model.JobReference;
|
||||
import com.google.appengine.api.taskqueue.QueueFactory;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import google.registry.bigquery.CheckedBigquery;
|
||||
import google.registry.export.BigqueryPollJobAction.BigqueryPollJobEnqueuer;
|
||||
import google.registry.request.HttpException.BadRequestException;
|
||||
import google.registry.request.HttpException.InternalServerErrorException;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.TaskQueueHelper.TaskMatcher;
|
||||
import java.io.IOException;
|
||||
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;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
/** Unit tests for {@link LoadSnapshotAction}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class LoadSnapshotActionTest {
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine = AppEngineRule.builder()
|
||||
.withTaskQueue()
|
||||
.build();
|
||||
private final CheckedBigquery checkedBigquery = mock(CheckedBigquery.class);
|
||||
private final Bigquery bigquery = mock(Bigquery.class);
|
||||
private final Bigquery.Jobs bigqueryJobs = mock(Bigquery.Jobs.class);
|
||||
private final Bigquery.Jobs.Insert bigqueryJobsInsert = mock(Bigquery.Jobs.Insert.class);
|
||||
private final Bigquery.Datasets bigqueryDatasets = mock(Bigquery.Datasets.class);
|
||||
private final Bigquery.Datasets.Insert bigqueryDatasetsInsert =
|
||||
mock(Bigquery.Datasets.Insert.class);
|
||||
private final BigqueryPollJobEnqueuer bigqueryPollEnqueuer = mock(BigqueryPollJobEnqueuer.class);
|
||||
|
||||
private FakeClock clock = new FakeClock(new DateTime(1391096117045L, UTC));
|
||||
private LoadSnapshotAction action;
|
||||
|
||||
@Before
|
||||
public void before() throws Exception {
|
||||
when(checkedBigquery.ensureDataSetExists("Project-Id", "snapshots")).thenReturn(bigquery);
|
||||
when(bigquery.jobs()).thenReturn(bigqueryJobs);
|
||||
when(bigqueryJobs.insert(eq("Project-Id"), any(Job.class))).thenReturn(bigqueryJobsInsert);
|
||||
when(bigquery.datasets()).thenReturn(bigqueryDatasets);
|
||||
when(bigqueryDatasets.insert(eq("Project-Id"), any(Dataset.class)))
|
||||
.thenReturn(bigqueryDatasetsInsert);
|
||||
action = new LoadSnapshotAction();
|
||||
action.checkedBigquery = checkedBigquery;
|
||||
action.bigqueryPollEnqueuer = bigqueryPollEnqueuer;
|
||||
action.clock = clock;
|
||||
action.projectId = "Project-Id";
|
||||
action.snapshotFile = "gs://bucket/snapshot.backup_info";
|
||||
action.snapshotId = "id12345";
|
||||
action.snapshotKinds = "one,two,three";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_enqueueLoadTask() {
|
||||
enqueueLoadSnapshotTask(
|
||||
"id12345", "gs://bucket/snapshot.backup_info", ImmutableSet.of("one", "two", "three"));
|
||||
assertTasksEnqueued(
|
||||
QUEUE,
|
||||
new TaskMatcher()
|
||||
.url(PATH)
|
||||
.method("POST")
|
||||
.param(LOAD_SNAPSHOT_ID_PARAM, "id12345")
|
||||
.param(LOAD_SNAPSHOT_FILE_PARAM, "gs://bucket/snapshot.backup_info")
|
||||
.param(LOAD_SNAPSHOT_KINDS_PARAM, "one,two,three"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_doPost() throws Exception {
|
||||
action.run();
|
||||
|
||||
// Verify that checkedBigquery was called in a way that would create the dataset if it didn't
|
||||
// already exist.
|
||||
verify(checkedBigquery).ensureDataSetExists("Project-Id", "snapshots");
|
||||
|
||||
// Capture the load jobs we inserted to do additional checking on them.
|
||||
ArgumentCaptor<Job> jobArgument = ArgumentCaptor.forClass(Job.class);
|
||||
verify(bigqueryJobs, times(3)).insert(eq("Project-Id"), jobArgument.capture());
|
||||
List<Job> jobs = jobArgument.getAllValues();
|
||||
assertThat(jobs).hasSize(3);
|
||||
|
||||
// Check properties that should be common to all load jobs.
|
||||
for (Job job : jobs) {
|
||||
assertThat(job.getJobReference().getProjectId()).isEqualTo("Project-Id");
|
||||
JobConfigurationLoad config = job.getConfiguration().getLoad();
|
||||
assertThat(config.getSourceFormat()).isEqualTo("DATASTORE_BACKUP");
|
||||
assertThat(config.getDestinationTable().getProjectId()).isEqualTo("Project-Id");
|
||||
assertThat(config.getDestinationTable().getDatasetId()).isEqualTo("snapshots");
|
||||
}
|
||||
|
||||
// Check the job IDs for each load job.
|
||||
assertThat(transform(jobs, job -> job.getJobReference().getJobId()))
|
||||
.containsExactly(
|
||||
"load-snapshot-id12345-one-1391096117045",
|
||||
"load-snapshot-id12345-two-1391096117045",
|
||||
"load-snapshot-id12345-three-1391096117045");
|
||||
|
||||
// Check the source URI for each load job.
|
||||
assertThat(
|
||||
transform(
|
||||
jobs,
|
||||
job -> Iterables.getOnlyElement(job.getConfiguration().getLoad().getSourceUris())))
|
||||
.containsExactly(
|
||||
"gs://bucket/snapshot.one.backup_info",
|
||||
"gs://bucket/snapshot.two.backup_info",
|
||||
"gs://bucket/snapshot.three.backup_info");
|
||||
|
||||
// Check the destination table ID for each load job.
|
||||
assertThat(
|
||||
transform(
|
||||
jobs, job -> job.getConfiguration().getLoad().getDestinationTable().getTableId()))
|
||||
.containsExactly("id12345_one", "id12345_two", "id12345_three");
|
||||
|
||||
// Check that we executed the inserted jobs.
|
||||
verify(bigqueryJobsInsert, times(3)).execute();
|
||||
|
||||
// Check that the poll tasks for each load job were enqueued.
|
||||
verify(bigqueryPollEnqueuer)
|
||||
.enqueuePollTask(
|
||||
new JobReference()
|
||||
.setProjectId("Project-Id")
|
||||
.setJobId("load-snapshot-id12345-one-1391096117045"),
|
||||
UpdateSnapshotViewAction.createViewUpdateTask(
|
||||
"snapshots", "id12345_one", "one", LATEST_SNAPSHOT_VIEW_NAME),
|
||||
QueueFactory.getQueue(UpdateSnapshotViewAction.QUEUE));
|
||||
verify(bigqueryPollEnqueuer)
|
||||
.enqueuePollTask(
|
||||
new JobReference()
|
||||
.setProjectId("Project-Id")
|
||||
.setJobId("load-snapshot-id12345-two-1391096117045"),
|
||||
UpdateSnapshotViewAction.createViewUpdateTask(
|
||||
"snapshots", "id12345_two", "two", LATEST_SNAPSHOT_VIEW_NAME),
|
||||
QueueFactory.getQueue(UpdateSnapshotViewAction.QUEUE));
|
||||
verify(bigqueryPollEnqueuer)
|
||||
.enqueuePollTask(
|
||||
new JobReference()
|
||||
.setProjectId("Project-Id")
|
||||
.setJobId("load-snapshot-id12345-three-1391096117045"),
|
||||
UpdateSnapshotViewAction.createViewUpdateTask(
|
||||
"snapshots", "id12345_three", "three", LATEST_SNAPSHOT_VIEW_NAME),
|
||||
QueueFactory.getQueue(UpdateSnapshotViewAction.QUEUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_doPost_badGcsFilename() {
|
||||
action.snapshotFile = "gs://bucket/snapshot";
|
||||
BadRequestException thrown = assertThrows(BadRequestException.class, action::run);
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains("Error calling load snapshot: backup info file extension missing");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_doPost_bigqueryThrowsException() throws Exception {
|
||||
when(bigqueryJobsInsert.execute()).thenThrow(new IOException("The Internet has gone missing"));
|
||||
InternalServerErrorException thrown =
|
||||
assertThrows(InternalServerErrorException.class, action::run);
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains("Error loading snapshot: The Internet has gone missing");
|
||||
}
|
||||
}
|
|
@ -7,7 +7,6 @@ PATH CLASS METHOD
|
|||
/_dr/task/backupDatastore BackupDatastoreAction POST y INTERNAL APP IGNORED
|
||||
/_dr/task/brdaCopy BrdaCopyAction POST y INTERNAL APP IGNORED
|
||||
/_dr/task/checkDatastoreBackup CheckBackupAction POST,GET y INTERNAL APP IGNORED
|
||||
/_dr/task/checkSnapshot CheckSnapshotAction POST,GET y INTERNAL APP IGNORED
|
||||
/_dr/task/copyDetailReports CopyDetailReportsAction POST n INTERNAL,API APP ADMIN
|
||||
/_dr/task/deleteContactsAndHosts DeleteContactsAndHostsAction GET n INTERNAL APP IGNORED
|
||||
/_dr/task/deleteLoadTestData DeleteLoadTestDataAction POST n INTERNAL APP IGNORED
|
||||
|
@ -18,7 +17,6 @@ PATH CLASS METHOD
|
|||
/_dr/task/exportDomainLists ExportDomainListsAction POST n INTERNAL APP IGNORED
|
||||
/_dr/task/exportPremiumTerms ExportPremiumTermsAction POST n INTERNAL APP IGNORED
|
||||
/_dr/task/exportReservedTerms ExportReservedTermsAction POST n INTERNAL APP IGNORED
|
||||
/_dr/task/exportSnapshot ExportSnapshotAction POST y INTERNAL APP IGNORED
|
||||
/_dr/task/generateInvoices GenerateInvoicesAction POST n INTERNAL APP IGNORED
|
||||
/_dr/task/generateSpec11 GenerateSpec11ReportAction POST n INTERNAL APP IGNORED
|
||||
/_dr/task/icannReportingStaging IcannReportingStagingAction POST n INTERNAL APP IGNORED
|
||||
|
@ -27,7 +25,6 @@ PATH CLASS METHOD
|
|||
/_dr/task/importRdeDomains RdeDomainImportAction GET n INTERNAL APP IGNORED
|
||||
/_dr/task/importRdeHosts RdeHostImportAction GET n INTERNAL APP IGNORED
|
||||
/_dr/task/linkRdeHosts RdeHostLinkAction GET n INTERNAL APP IGNORED
|
||||
/_dr/task/loadSnapshot LoadSnapshotAction POST n INTERNAL APP IGNORED
|
||||
/_dr/task/nordnUpload NordnUploadAction POST y INTERNAL APP IGNORED
|
||||
/_dr/task/nordnVerify NordnVerifyAction POST y INTERNAL APP IGNORED
|
||||
/_dr/task/pollBigqueryJob BigqueryPollJobAction GET,POST y INTERNAL APP IGNORED
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue