Skip to content
Snippets Groups Projects
Commit c7486daa authored by jakubs's avatar jakubs
Browse files

SP-45, BIS-21 create a robust tool to check for the executed jython hooks

SVN: 25331
parent 539dbfbe
No related branches found
No related tags found
No related merge requests found
......@@ -215,6 +215,7 @@ public class JythonDropboxRecoveryTest extends AbstractJythonDataSetHandlerTest
RecoveryInfoDateConstraint.ORIGINAL, testCase.recoveryLastTry);
assertOriginalMarkerFileExists();
assertDirNotEmpty(precommitDirectory, "Precommit directory should not be empty");
assertJythonHooks("pre_metadata_registration");
// nothing happened
} else
{
......@@ -234,6 +235,8 @@ public class JythonDropboxRecoveryTest extends AbstractJythonDataSetHandlerTest
assertNoRecoveryMarkerFile();
assertDirEmpty(stagingDirectory);
assertJythonHooks("pre_metadata_registration", "rollback_pre_registration");
// FIXME: this check is commented out because of a bug!
// assertDirEmpty(precommitDirectory);
......@@ -241,10 +244,25 @@ public class JythonDropboxRecoveryTest extends AbstractJythonDataSetHandlerTest
// rolllback requirementes
}
private void assertJythonHooks(String... messages)
{
JythonHookTestTool jythonHookTestTool =
JythonHookTestTool.createFromWorkingDirectory(workingDirectory);
for (String msg : messages)
{
jythonHookTestTool.assertLogged(msg);
}
jythonHookTestTool.assertNoMoreMessages();
}
private void assertPostRecoveryConstraints(
final RecoveryTestCase testCase,
final RecordingMatcher<ch.systemsx.cisd.openbis.generic.shared.dto.AtomicEntityOperationDetails> atomicatOperationDetails)
{
JythonHookTestTool jythonHookTestTool =
JythonHookTestTool.createFromWorkingDirectory(workingDirectory);
// the check from the original registration
jythonHookTestTool.assertLogged("pre_metadata_registration");
switch (testCase.registrationCheckResult)
{
......@@ -258,6 +276,10 @@ public class JythonDropboxRecoveryTest extends AbstractJythonDataSetHandlerTest
assertNoOriginalMarkerFileExists();
assertNoRecoveryMarkerFile();
// the hooks after successful registration
jythonHookTestTool.assertLogged("post_metadata_registration");
jythonHookTestTool.assertLogged("post_storage");
break;
case REGISTRATION_FAILED:
assertDataSetNotStoredProcess(DATA_SET_CODE);
......@@ -268,6 +290,8 @@ public class JythonDropboxRecoveryTest extends AbstractJythonDataSetHandlerTest
assertNoOriginalMarkerFileExists();
assertNoRecoveryMarkerFile();
jythonHookTestTool.assertLogged("rollback_pre_registration");
break;
case CHECK_FAILED:
assertDataSetNotStoredProcess(DATA_SET_CODE);
......@@ -288,7 +312,7 @@ public class JythonDropboxRecoveryTest extends AbstractJythonDataSetHandlerTest
break;
}
jythonHookTestTool.assertNoMoreMessages();
}
/**
......
/*
* Copyright 2012 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.etlserver.registrator;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* The helper class to assert jython hooks. The jython hooks can log into the working directory, and
* the test classes can compare what the script has logged with expectations
*
* @author jakubs
*/
public class JythonHookTestTool
{
private final File logFile;
private String[] originalFileContents;
private int lineNumber;
private JythonHookTestTool(File workingDirectory)
{
this.logFile = new File(workingDirectory, "jython_hook_test");
if (logFile.exists())
{
readContents();
}
lineNumber = 0;
}
private void readContents()
{
try
{
final Scanner s = new Scanner(logFile);
List<String> list = new LinkedList<String>();
while (s.hasNextLine())
{
final String line = s.nextLine();
list.add(line);
}
originalFileContents = list.toArray(new String[0]);
} catch (FileNotFoundException fnfe)
{
throw new RuntimeException(
"Impossible as we check if the file exists before creating scanner", fnfe);
}
}
/**
* The factory method to create util
*
* @param incoming - pre-staging copy of the file - the logical incoming available from the
* dropbox
*/
public static JythonHookTestTool createFromIncoming(File incoming)
{
File workingDirectory = incoming.getParentFile().getParentFile().getParentFile();
return new JythonHookTestTool(workingDirectory);
}
/**
* @param workingDirectory - the working directory of the test class
*/
public static JythonHookTestTool createFromWorkingDirectory(File workingDirectory)
{
return new JythonHookTestTool(workingDirectory);
}
/**
* Use this method in the dropbox to log some information during registration
*
* @param message - message that will be logged with a \n at the end
*/
public void log(String message) throws IOException
{
BufferedWriter output = null;
try
{
output = new BufferedWriter(new FileWriter(logFile, true));
output.write(message);
output.newLine();
} finally
{
if (output != null)
{
output.close();
}
}
}
/**
* Method to use in the test to assert that the given messages were written to the file in the
* specified order
*/
public void assertLogged(String message)
{
if (lineNumber >= originalFileContents.length)
{
throw new AssertionError("Missing message in jython hook test " + message);
}
String line = originalFileContents[lineNumber];
if (false == line.equals(message))
{
throw new AssertionError("Mismatch in jython hook test. Expected " + message
+ " but got " + line);
}
lineNumber++;
}
public void assertNoMoreMessages()
{
if (lineNumber < originalFileContents.length)
{
throw new AssertionError("Unexpected message in jython hook test."
+ originalFileContents[lineNumber]);
}
}
}
from ch.systemsx.cisd.etlserver.registrator import JythonHookTestTool
jythonHookTestTool = JythonHookTestTool.createFromIncoming(incoming)
def rollback_transaction(service, transaction, algorithmRunner, throwable):
global didTransactionRollbackHappen
didTransactionRollbackHappen = True
jythonHookTestTool.log("rollback_transaction")
def rollback_pre_registration(context, throwable):
global didPreRegistrationRollbackHappen
didPreRegistrationRollbackHappen = True
jythonHookTestTool.log("rollback_pre_registration")
def commit_transaction(service, transaction):
global didTransactionCommitHappen
didTransactionCommitHappen = True
jythonHookTestTool.log("commit_transaction")
def post_storage(context):
global didPostStorageFunctionRunHappen
didPostStorageFunctionRunHappen = True
jythonHookTestTool.log("post_storage")
def pre_metadata_registration(context):
global didPreRegistrationFunctionRunHappen
didPreRegistrationFunctionRunHappen = True
jythonHookTestTool.log("pre_metadata_registration")
def post_metadata_registration(context):
global didPostRegistrationFunctionRunHappen
didPostRegistrationFunctionRunHappen = True
jythonHookTestTool.log("post_metadata_registration")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment