Search in sources :

Example 1 with TestComparisonMode

use of org.batfish.client.Command.TestComparisonMode in project batfish by batfish.

the class Client method test.

private boolean test(List<String> options, List<String> parameters) throws IOException {
    boolean failingTest = false;
    boolean missingReferenceFile = false;
    int testCommandIndex = 1;
    if (!isValidArgument(options, parameters, 1, 2, Integer.MAX_VALUE, Command.TEST)) {
        return false;
    }
    TestComparisonMode comparisonMode = TestComparisonMode.COMPAREANSWER;
    if (!options.isEmpty()) {
        comparisonMode = // remove '-'
        TestComparisonMode.valueOf(options.get(0).substring(1).toUpperCase());
    }
    if (parameters.get(testCommandIndex).equals(FLAG_FAILING_TEST)) {
        testCommandIndex++;
        failingTest = true;
    }
    String referenceFileName = parameters.get(0);
    String[] testCommand = parameters.subList(testCommandIndex, parameters.size()).toArray(new String[0]);
    _logger.debugf("Ref file is %s. \n", referenceFileName, parameters.size());
    _logger.debugf("Test command is %s\n", Arrays.toString(testCommand));
    File referenceFile = new File(referenceFileName);
    if (!referenceFile.exists()) {
        _logger.errorf("Reference file does not exist: %s\n", referenceFileName);
        missingReferenceFile = true;
    }
    // Delete any existing testout filename before running this test.
    Path failedTestoutPath = Paths.get(referenceFile + ".testout");
    CommonUtil.deleteIfExists(failedTestoutPath);
    File testoutFile = Files.createTempFile("test", "out").toFile();
    testoutFile.deleteOnExit();
    FileWriter testoutWriter = new FileWriter(testoutFile);
    boolean testCommandSucceeded = processCommand(testCommand, testoutWriter);
    testoutWriter.close();
    String testOutput = CommonUtil.readFile(Paths.get(testoutFile.getAbsolutePath()));
    boolean testPassed = false;
    if (failingTest) {
        if (!testCommandSucceeded) {
            // Command failed in the client.
            testPassed = true;
        } else {
            try {
                Answer testAnswer = BatfishObjectMapper.mapper().readValue(testOutput, Answer.class);
                testPassed = (testAnswer.getStatus() == AnswerStatus.FAILURE);
            } catch (JsonProcessingException e) {
            // pass here and let the test fail.
            }
        }
    } else if (testCommandSucceeded) {
        try {
            if (TestComparisonMode.RAW != comparisonMode) {
                Answer testAnswer = BatfishObjectMapper.mapper().readValue(testOutput, Answer.class);
                testOutput = getTestComparisonString(testAnswer, comparisonMode);
            }
            if (!missingReferenceFile) {
                String referenceOutput = CommonUtil.readFile(Paths.get(referenceFileName));
                if (referenceOutput.equals(testOutput)) {
                    testPassed = true;
                }
            }
        } catch (JsonProcessingException e) {
            _logger.errorf("Error deserializing answer %s: %s\n", testOutput, ExceptionUtils.getStackTrace(e));
        } catch (Exception e) {
            _logger.errorf("Exception in comparing test results: %s\n", ExceptionUtils.getStackTrace(e));
        }
    }
    StringBuilder sb = new StringBuilder();
    sb.append("'" + testCommand[0]);
    for (int i = 1; i < testCommand.length; i++) {
        sb.append(" " + testCommand[i]);
    }
    sb.append("'");
    String testCommandText = sb.toString();
    _logger.outputf("Test [%s]: %s %s: %s\n", comparisonMode, testCommandText, failingTest ? "results in error as expected" : "matches " + referenceFileName, testPassed ? "Pass" : "Fail");
    if (!testPassed) {
        CommonUtil.writeFile(failedTestoutPath, testOutput);
        _logger.outputf("Copied output to %s\n", failedTestoutPath);
    }
    return true;
}
Also used : Path(java.nio.file.Path) Answer(org.batfish.datamodel.answers.Answer) FileWriter(java.io.FileWriter) TestComparisonMode(org.batfish.client.Command.TestComparisonMode) File(java.io.File) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) EndOfFileException(org.jline.reader.EndOfFileException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) UserInterruptException(org.jline.reader.UserInterruptException) PatternSyntaxException(java.util.regex.PatternSyntaxException) FileNotFoundException(java.io.FileNotFoundException) BatfishException(org.batfish.common.BatfishException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 TestComparisonMode (org.batfish.client.Command.TestComparisonMode)1 BatfishException (org.batfish.common.BatfishException)1 Answer (org.batfish.datamodel.answers.Answer)1 JSONException (org.codehaus.jettison.json.JSONException)1 EndOfFileException (org.jline.reader.EndOfFileException)1 UserInterruptException (org.jline.reader.UserInterruptException)1