google-nomulus/javatests/google/registry/tools/ImportDatastoreCommandTest.java
weiminyu 4e71421c81 Support datastore restore in Nomulus tool
Two commands are being added:
- ImportDatastoreCommand starts an async import operation.
  User may choose to wait until import completes or quit
  immediately.
- GetOperationStatusCommand checks the status of an operation.
  It may be used to check the status of an operation started by
  ImportDatastoreCommand.

Both commands communicate with Datastore admin api directly, without
going through the Registry server.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=228400059
2019-01-10 16:23:35 -05:00

146 lines
5.1 KiB
Java

// Copyright 2019 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.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import google.registry.export.datastore.DatastoreAdmin;
import google.registry.export.datastore.DatastoreAdmin.Get;
import google.registry.export.datastore.DatastoreAdmin.Import;
import google.registry.export.datastore.Operation;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
/** Unit tests for {@link ImportDatastoreCommand}. */
@RunWith(JUnit4.class)
public class ImportDatastoreCommandTest extends CommandTestCase<ImportDatastoreCommand> {
@Captor ArgumentCaptor<String> backupUrl;
@Captor ArgumentCaptor<Collection<String>> kinds;
@Captor ArgumentCaptor<String> operationName;
@Mock private DatastoreAdmin datastoreAdmin;
@Mock private Import importRequest;
@Mock private Get getRequest;
@Mock private Operation importOperation;
@Mock private Operation getOperation;
@Before
public void setup() throws Exception {
command.datastoreAdmin = datastoreAdmin;
when(datastoreAdmin.importBackup(backupUrl.capture(), kinds.capture()))
.thenReturn(importRequest);
when(importRequest.execute()).thenReturn(importOperation);
when(importOperation.getName()).thenReturn("opName");
when(datastoreAdmin.get(operationName.capture())).thenReturn(getRequest);
when(getRequest.execute()).thenReturn(getOperation);
}
@Test
public void test_importAllKinds_immediateSuccess() throws Exception {
runCommandForced(
"--poll_interval", "PT0.001S",
"--backup_url", "gs://bucket/export-id/export-id.overall_export_metadata");
assertThat(backupUrl.getValue())
.isEqualTo("gs://bucket/export-id/export-id.overall_export_metadata");
assertThat(kinds.getValue()).isEmpty();
verify(datastoreAdmin, never()).get(anyString());
}
@Test
public void test_importSomeKinds_immediateSuccess() throws Exception {
runCommandForced(
"--poll_interval",
"PT0.001S",
"--backup_url",
"gs://bucket/export-id/export-id.overall_export_metadata",
"--kinds",
"Registrar",
"--kinds",
"Registry");
assertThat(backupUrl.getValue())
.isEqualTo("gs://bucket/export-id/export-id.overall_export_metadata");
assertThat(kinds.getValue()).containsExactly("Registrar", "Registry");
verify(datastoreAdmin, never()).get(anyString());
}
@Test
public void test_delayedSuccess_sync() throws Exception {
when(importOperation.isProcessing()).thenReturn(true);
when(getOperation.isProcessing()).thenReturn(true).thenReturn(false);
when(getOperation.isSuccessful()).thenReturn(true);
runCommandForced(
"--poll_interval", "PT0.001S",
"--backup_url", "gs://bucket/export-id/export-id.overall_export_metadata");
verify(datastoreAdmin, times(1)).get("opName");
}
@Test
public void test_delayedSuccess_async() throws Exception {
when(importOperation.isProcessing()).thenReturn(false);
when(getOperation.isProcessing()).thenReturn(true).thenReturn(false);
when(getOperation.isSuccessful()).thenReturn(true);
runCommandForced(
"--poll_interval",
"PT0.001S",
"--backup_url",
"gs://bucket/export-id/export-id.overall_export_metadata",
"--async");
verify(datastoreAdmin, never()).get("opName");
}
@Test
public void test_failure_notAllowedInProduction() {
assertThrows(
IllegalArgumentException.class,
() ->
runCommandInEnvironment(
RegistryToolEnvironment.PRODUCTION,
"--force",
"--poll_interval",
"PT0.001S",
"--backup_url",
"gs://bucket/export-id/export-id.overall_export_metadata"));
}
@Test
public void test_success_runInProduction() throws Exception {
runCommandInEnvironment(
RegistryToolEnvironment.PRODUCTION,
"--force",
"--confirm_production_import",
"PRODUCTION",
"--poll_interval",
"PT0.001S",
"--backup_url",
"gs://bucket/export-id/export-id.overall_export_metadata");
}
}