mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
Implement compare_db_backups "main"
Implement toplevel class that reads in two database backups and displays diffs. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=168592124
This commit is contained in:
parent
51298aeabb
commit
07e5741cbb
6 changed files with 345 additions and 61 deletions
98
javatests/google/registry/tools/LevelDbFileBuilderTest.java
Normal file
98
javatests/google/registry/tools/LevelDbFileBuilderTest.java
Normal file
|
@ -0,0 +1,98 @@
|
|||
// 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.tools;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import com.google.appengine.api.datastore.Entity;
|
||||
import com.google.appengine.api.datastore.EntityTranslator;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.storage.onestore.v3.OnestoreEntity.EntityProto;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.tools.LevelDbFileBuilder.Property;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class LevelDbFileBuilderTest {
|
||||
|
||||
public static final int BASE_ID = 1001;
|
||||
|
||||
@Rule public final TemporaryFolder tempFs = new TemporaryFolder();
|
||||
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
|
||||
|
||||
@Test
|
||||
public void testSingleRecordWrites() throws FileNotFoundException, IOException {
|
||||
File subdir = tempFs.newFolder("folder");
|
||||
File logFile = new File(subdir, "testfile");
|
||||
LevelDbFileBuilder builder = new LevelDbFileBuilder(logFile);
|
||||
ComparableEntity entity =
|
||||
builder.addEntityProto(
|
||||
BASE_ID, Property.create("first", 100L), Property.create("second", 200L));
|
||||
builder.build();
|
||||
|
||||
LevelDbLogReader reader = new LevelDbLogReader();
|
||||
reader.readFrom(new FileInputStream(logFile));
|
||||
|
||||
ImmutableList<byte[]> records = reader.getRecords();
|
||||
assertThat(records).hasSize(1);
|
||||
|
||||
// Reconstitute an entity, make sure that what we've got is the same as what we started with.
|
||||
EntityProto proto = new EntityProto();
|
||||
proto.parseFrom(records.get(0));
|
||||
Entity materializedEntity = EntityTranslator.createFromPb(proto);
|
||||
assertThat(new ComparableEntity(materializedEntity)).isEqualTo(entity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleRecordWrites() throws FileNotFoundException, IOException {
|
||||
File subdir = tempFs.newFolder("folder");
|
||||
File logFile = new File(subdir, "testfile");
|
||||
LevelDbFileBuilder builder = new LevelDbFileBuilder(logFile);
|
||||
|
||||
// Generate enough records to cross a block boundary. These records end up being around 80
|
||||
// bytes, so 1000 works.
|
||||
ImmutableList.Builder<ComparableEntity> originalEntitiesBuilder = new ImmutableList.Builder<>();
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
ComparableEntity entity =
|
||||
builder.addEntityProto(
|
||||
BASE_ID + i, Property.create("first", 100L), Property.create("second", 200L));
|
||||
originalEntitiesBuilder.add(entity);
|
||||
}
|
||||
builder.build();
|
||||
ImmutableList<ComparableEntity> originalEntities = originalEntitiesBuilder.build();
|
||||
|
||||
LevelDbLogReader reader = new LevelDbLogReader();
|
||||
reader.readFrom(new FileInputStream(logFile));
|
||||
|
||||
ImmutableList<byte[]> records = reader.getRecords();
|
||||
assertThat(records).hasSize(1000);
|
||||
int index = 0;
|
||||
for (byte[] record : records) {
|
||||
EntityProto proto = new EntityProto();
|
||||
proto.parseFrom(record);
|
||||
Entity materializedEntity = EntityTranslator.createFromPb(proto);
|
||||
assertThat(new ComparableEntity(materializedEntity)).isEqualTo(originalEntities.get(index));
|
||||
++index;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue