Add database invariant checker []

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=119162082
This commit is contained in:
mcilwain 2016-04-06 08:21:59 -07:00 committed by Justine Tunney
parent d213d9a114
commit 4b15d49f52
10 changed files with 690 additions and 0 deletions

View file

@ -15,11 +15,14 @@ java_library(
"//java/com/google/common/base",
"//java/com/google/common/collect",
"//java/com/google/common/net",
"//java/com/google/common/testing",
"//java/com/google/domain/registry/bigquery",
"//java/com/google/domain/registry/config",
"//java/com/google/domain/registry/mapreduce",
"//java/com/google/domain/registry/model",
"//java/com/google/domain/registry/monitoring/whitebox",
"//javatests/com/google/domain/registry/testing",
"//javatests/com/google/domain/registry/testing/mapreduce",
"//third_party/java/appengine:appengine-api-testonly",
"//third_party/java/appengine:appengine-stubs",
"//third_party/java/appengine:appengine-testing",

View file

@ -0,0 +1,87 @@
// Copyright 2016 The Domain Registry 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 com.google.domain.registry.monitoring.whitebox;
import static com.google.domain.registry.testing.DatastoreHelper.createTld;
import static com.google.domain.registry.testing.DatastoreHelper.deleteResource;
import static com.google.domain.registry.testing.DatastoreHelper.persistActiveDomain;
import static com.google.domain.registry.testing.LogsSubject.assertAboutLogs;
import static java.util.logging.Level.SEVERE;
import com.google.common.base.Optional;
import com.google.common.testing.TestLogHandler;
import com.google.domain.registry.mapreduce.MapreduceRunner;
import com.google.domain.registry.model.domain.DomainResource;
import com.google.domain.registry.model.index.ForeignKeyIndex;
import com.google.domain.registry.testing.FakeResponse;
import com.google.domain.registry.testing.mapreduce.MapreduceTestCase;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link VerifyEntityIntegrityAction}. */
@RunWith(JUnit4.class)
public class VerifyEntityIntegrityActionTest
extends MapreduceTestCase<VerifyEntityIntegrityAction> {
private TestLogHandler handler;
@Before
public void before() {
createTld("tld");
action = new VerifyEntityIntegrityAction();
handler = new TestLogHandler();
VerifyEntityIntegrityAction.logger.addHandler(handler);
action.mrRunner = new MapreduceRunner(Optional.of(2), Optional.of(2));
action.response = new FakeResponse();
}
@After
public void after() {
VerifyEntityIntegrityAction.logger.removeHandler(handler);
}
private void runMapreduce() throws Exception {
action.run();
executeTasksUntilEmpty("mapreduce");
}
@Test
public void test_singleDomain_noBadInvariants() throws Exception {
persistActiveDomain("ninetails.tld");
runMapreduce();
assertAboutLogs().that(handler).hasNoLogsAtLevel(SEVERE);
}
@Test
public void test_singleDomain_missingFki() throws Exception {
persistActiveDomain("ninetails.tld");
ForeignKeyIndex<DomainResource> fki =
ForeignKeyIndex.load(DomainResource.class, "ninetails.tld", DateTime.now(DateTimeZone.UTC));
deleteResource(fki);
runMapreduce();
// TODO(mcilwain): Check for exception message here.
assertAboutLogs()
.that(handler)
.hasLogAtLevelWithMessage(
SEVERE, "Integrity error found while checking foreign key contraints");
}
}

View file

@ -24,6 +24,7 @@ java_library(
"//java/com/google/common/collect",
"//java/com/google/common/io",
"//java/com/google/common/net",
"//java/com/google/common/testing",
"//java/com/google/common/util/concurrent",
"//java/com/google/domain/registry/config",
"//java/com/google/domain/registry/dns:constants",

View file

@ -0,0 +1,67 @@
// Copyright 2016 The Domain Registry 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 com.google.domain.registry.testing;
import static com.google.common.truth.Truth.assertAbout;
import com.google.common.testing.TestLogHandler;
import com.google.common.truth.AbstractVerb.DelegatedVerb;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.Subject;
import com.google.domain.registry.testing.TruthChainer.And;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
/** Utility methods for asserting things about logging {@link Handler} instances. */
public class LogsSubject extends Subject<LogsSubject, TestLogHandler> {
/** A factory for instances of this subject. */
private static class SubjectFactory
extends ReflectiveSubjectFactory<TestLogHandler, LogsSubject> {}
public LogsSubject(FailureStrategy strategy, TestLogHandler subject) {
super(strategy, subject);
}
public And<LogsSubject> hasNoLogsAtLevel(Level level) {
for (LogRecord log : getSubject().getStoredLogRecords()) {
if (log.getLevel() == level) {
failWithRawMessage(
"Not true that there are no logs at level %s. Found <%s>.", level, log.getMessage());
}
}
return new And<>(this);
}
public And<LogsSubject> hasLogAtLevelWithMessage(Level level, String message) {
boolean found = false;
for (LogRecord log : getSubject().getStoredLogRecords()) {
if (log.getLevel() == level && log.getMessage().contains(message)) {
found = true;
break;
}
}
if (!found) {
failWithRawMessage("Found no logs at level %s with message %s.", level, message);
}
return new And<>(this);
}
public static DelegatedVerb<LogsSubject, TestLogHandler> assertAboutLogs() {
return assertAbout(new SubjectFactory());
}
}