Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate tests to JUnit #142

Merged
merged 50 commits into from
Apr 24, 2024
Merged

Conversation

ChrisJohnNOAA
Copy link
Contributor

Description

This migrates tests to JUnit along with updates to file paths to make them runnable with minimal setup for a new developer.

This also loads test resources through maven by downloading from a github release rather than including the files in the repo. There were some files included previously those have been removed. The reasoning is a lot of the test data files are >10 mb and in total the test data is several gb of data. While downloading the files aren't great, I think its a better solution than including them in the main repo.

About 400 tests will now run with mvn test. On my computer(s) the first run takes a while, but once things are cached appropriately, future runs are around 30 minutes.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • [X ] New feature (non-breaking change which adds functionality)
  • Tests are runnable without needing to do extensive setup

Checklist before requesting a review

  • [X ] I have performed a self-review of my code
  • [ X] My code follows the style guidelines of this project
  • [X ] I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • [ X] My changes generate no new warnings
  • [X ] I have added tests that prove my fix is effective or that my feature works
  • [X ] New and existing unit tests pass locally with my changes

Note many of the images showed small differences from old tests due to fonts. I added a new tag that's just informative that these tests are font dependent (the images I'm adding pass on my machine-  there might be issues on different machines).
New tag RequiresContent for tests that are relying on the content/setup.xml, I still need to find a way to make those tests not require manual setup.
Skipping the setup.xml validation allows additional tests that don't rely on those values
Last modified is difficult to have consistent across machines (and even on the same machine).
…hat.

Also migrate EDDGridFromDap to JUnit.

Sadly most tests are reliant on datasets from THREDDS, so they still aren't running. The plan to fix that is one of: mock THREDDS responses, local files for those datasets, or different datasets.
Planning to transition to downloading the test resource files.
…e large files

Also moves some files to a new scripts folder that is for code designed to be run manually that is not part of the main server.
Also update image all comparisons to use resource paths.
…files

This is to make the folders small enough they can be zipped under 2gb and served from a git release
…ugin now)

Also adding flaky annotations for tests that failed (several of them I believe failed due to things like file last modified timestamp being different).
Add the flaky annotation to several more tests.
Also fix a couple hardcoded dataset ids to use suggested ids.
…rif for tests.

Tests are now using SansSerif because it is always available in java distributions and so it eliminates one more step needed for developer setup.
@srstsavage
Copy link
Contributor

Is any setup besides getting netcdfAll-5.5.3.jar needed before running mvn test? I get the following:

[ERROR] Tests run: 384, Failures: 0, Errors: 211, Skipped: 0 

Haven't dug in at all yet but many of the errors seem to be around tests taking a bit longer than the specific threshold, or the erddapContentDirectory not being properly set up, or failure to initialize various classes in this context (gov.noaa.pfel.erddap.util.EDStatic, gov.noaa.pfel.erddap.dataset.EDDTableFromFiles, etc). Or maybe it's expected that many of the tests will fail at this point?

@ChrisJohnNOAA
Copy link
Contributor Author

Is any setup besides getting netcdfAll-5.5.3.jar needed before running mvn test? I get the following:

[ERROR] Tests run: 384, Failures: 0, Errors: 211, Skipped: 0 

Haven't dug in at all yet but many of the errors seem to be around tests taking a bit longer than the specific threshold, or the erddapContentDirectory not being properly set up, or failure to initialize various classes in this context (gov.noaa.pfel.erddap.util.EDStatic, gov.noaa.pfel.erddap.dataset.EDDTableFromFiles, etc). Or maybe it's expected that many of the tests will fail at this point?

All of the tests that are currently enabled are passing on my machine. There are several hundred more tests that are disabled with various Tags.

I tried to have maven handle getting everything (including netcdf) so there should be minimal setup.

My guess with that number of errors is the src/test/resources directory didn't get setup properly. Maven should be downloading several large zip files from here: https://github.com/ERDDAP/erddapTest/releases/tag/rc-test
and then unzipping them to src/test/resources. The download and unzipping are specified in the pom.
The contents of the src/test/resources should be copied by maven (default behavior so not in the pom) to target/test-classes. So for example there should be target/test-classes/data, target/test-classes/largeFiles, target/test-classes/largePoints, target/test-classes/largeSatellite along with all of the directories from compiling the code/tests.

The other possibility (because you mentioned errors about erddapContentDirectory ) is the download of the content.zip (and unzipping of it) might have failed.

If it's neither of those, the earliest error about failing to initialize a class is generally the most helpful. So could you share that?

What OS are you on? Did any maven steps fail before running the tests?

@ChrisJohnNOAA
Copy link
Contributor Author

For the hash mismatches in HashDigestTests.basicTest, the culprit is actually an absolute path, not line endings.

https://github.com/ERDDAP/erddap/blob/main/src/test/java/com/cohort/util/HashDigestTests.java#L10

String tName = HashDigestTests.class.getResource("LICENSE.txt").getPath();

tName is set to an absolute path (/home/shane/src/erddap/target/classes/com/cohort/util/LICENSE.txt on my machine) which of course isn't portable. Seems like we should either make that path relative to the project root or not use a path at all. Thoughts?

I think that's actually hashing the file contents of the file at that path. The path comes from HashDigestTests.class.getResource("LICENSE.txt").getPath(); which turns a path relative to the resource directory into an absolute path.

It does also look like the resource path didn't get updated when I moved it to resources/data/LICENSE.txt I am shocked that didn't cause an error for me.

@srstsavage
Copy link
Contributor

Ah! Ok, that makes sense. The previous HashDigestTests.class.getResource("LICENSE.txt").getPath(); was finding target/classes/com/cohort/util/LICENSE.txt (aka WEB-INF/classes/com/cohort/util/LICENSE.txt) for me, VS HashDigestTests.class.getResource("/data/LICENSE.txt").getPath(); which resolves to target/test-classes/data/LICENSE.txt (aka src/test/resources/data/LICENSE.txt). The only difference between those two files is...line endings.

$ diff --strip-trailing-cr WEB-INF/classes/com/cohort/util/LICENSE.txt src/test/resources/data/LICENSE.txt
# empty output/no difference
$ diff WEB-INF/classes/com/cohort/util/LICENSE.txt src/test/resources/data/LICENSE.txt
# every line differs due to line endings

Ok great, I'll work on a PR fixing the case sensitivity issues and we'll see where we are...

@srstsavage
Copy link
Contributor

srstsavage commented Apr 13, 2024

Well, it looks like the case of the test file OQNux10S1day_20050712_x-135_X-105_y22_y50.grd is meaningfully wrong as the second _Y is supposed to indicate the max value of the y range, so it seems like the test file should be changed in the test data download.

https://github.com/ChrisJohnNOAA/erddap/blob/main/WEB-INF/classes/gov/noaa/pfel/coastwatch/griddata/FileNameUtility.java#L732-L739

    /**
     * This returns minY (e.g., 22).
     *
     * @param fileName a base (assumes full region) or custom file name.
     * @return minY.
     */
    public double getMinY(String fileName) {
        int poy = fileName.indexOf("_y");
        if (poy < 0) 
            return regionMinY;

        int poY = fileName.indexOf("_Y", poy + 2);
        return String2.parseDouble(fileName.substring(poy + 2, poY));
    }

Simply updating the testName path in GridTests results in a string index out of bounds error because the _Y can't be found in the filename by FileNameUtility.

@srstsavage
Copy link
Contributor

srstsavage commented Apr 15, 2024

Spent quite a bit of time over the weekend reviewing test failures on Linux. A PR with fixes for many of them are here:

ChrisJohnNOAA#7

Most of the remaining failing tests (~45 in total) fall into two categories:

Performance tests running slower than expected

This has been discussed, but the performance specifics in the test are non-portable and should be able to be disabled and/or adjusted during testing. The tests should be runnable on a minimal CI virtual machine, etc.

com.cohort.util.TestUtil.testString2canonicalStringHolder
com.cohort.util.TestUtil.testString2
gov.noaa.pfel.coastwatch.pointdata.TableTests.testBigAscii
gov.noaa.pfel.erddap.dataset.EDDTableFromFileNamesTests.testgoes17all

Differences in generated images due to font rendering

Haven't dug much into this one yet. It's possible I don't have my Linux environment set up completely correctly with regard to the desired fonts, but I believe I do. Also definitely possible that text rending in images is just a little different between Windows and Linux.

gov.noaa.pfel.coastwatch.sgt.SgtGraphTests.testSurface_ff
gov.noaa.pfel.coastwatch.sgt.SgtGraphTests.testSurface_ft
gov.noaa.pfel.coastwatch.sgt.SgtGraphTests.testSurface_tt
gov.noaa.pfel.coastwatch.sgt.SgtGraphTests.testDiverseGraphs_tff
gov.noaa.pfel.coastwatch.sgt.SgtGraphTests.testDiverseGraphs_tft
gov.noaa.pfel.coastwatch.sgt.SgtGraphTests.testDiverseGraphs_ttt
gov.noaa.pfel.coastwatch.sgt.SgtMapTests.testOceanPalette
gov.noaa.pfel.coastwatch.sgt.SgtMapTests.basicTest
gov.noaa.pfel.coastwatch.sgt.SgtMapTests.testTopography
gov.noaa.pfel.coastwatch.sgt.SgtMapTests.testBathymetry
gov.noaa.pfel.coastwatch.sgt.SgtMapTests.testX180To180Regions
gov.noaa.pfel.coastwatch.sgt.SgtMapTests.testX0To360Regions
gov.noaa.pfel.erddap.dataset.EDDGridFromDapTests.testMapAntialiasing
gov.noaa.pfel.erddap.dataset.EDDGridFromEtopoTests.testBasic
gov.noaa.pfel.erddap.dataset.EDDGridFromNcFilesTests.testUInt16File
gov.noaa.pfel.erddap.dataset.EDDGridFromNcFilesTests.testSimpleTestNc
gov.noaa.pfel.erddap.dataset.EDDGridFromNcFilesUnpackedTests.testUInt16File
gov.noaa.pfel.erddap.dataset.EDDGridFromNcFilesUnpackedTests.testMissingValue
gov.noaa.pfel.erddap.dataset.EDDGridLon0360Tests.testPM180
gov.noaa.pfel.erddap.dataset.EDDGridLon0360Tests.testInsert
gov.noaa.pfel.erddap.dataset.EDDGridLonPM180Tests.test120to320
gov.noaa.pfel.erddap.dataset.EDDTableFromAsciiFilesTests.testFixedValueAndScripts
gov.noaa.pfel.erddap.dataset.EDDTableFromNcFilesTests.testTimeAxis
gov.noaa.pfel.erddap.dataset.EDDTableFromNcFilesTests.testManyYears

The good news is that all of the image checking is done in a single utility method com.cohort.util.Image2Tests.testImagesIdentical, so it would be very easy to disable or adjust parameters in one place.

If we add an option to disable, I think it should only disable the image comparison part and still run the image generation code, as that's still very valuable to ensure working.

One example set of expected/observed/difference images:

Expected:
SgtGraphTestSurfaceY_exp

Observed:
SgtGraphTestSurfaceY_obs

Difference:
SgtGraphTestSurfaceY_diff

Other than that, there are ~10 other test failures which seem due to various differences, I'll try to chase those down later, but I probably won't be able to give them much attention next week.

Not sure what the goal is for Linux compatibility on these tests at this point. I don't think it needs to block merging, we can definitely fix tests in separate PRs later.

Oh and one other note that's already been discussed but I want to emphasize: the default values in content/erddap/setup.xml requiring /home/yourName and /data/httpGet to exist and be writing are pretty painful. The sooner those can be configurable and/or local to the project directory or /tmp the better.

@ChrisJohnNOAA
Copy link
Contributor Author

I believe there's around 40 image comparison tests currently so it's quite possible that's the vast majority of the differences left.

For anybody making changes that might impact the generated images, I'd like them to be able to run the tests with a before/after comparison. I actually set up the code to automatically create the expected files from the observed (if the expected files are missing).

https://github.com/ChrisJohnNOAA/erddap/blob/test_migrations/src/test/java/com/cohort/util/Image2Tests.java#L246-L254

We could have the expected images be a separate optional pack and just have the expected images be locally generated with instructions to generate the images before making changes to confirm what impact changes have. If you want to test this locally, delete src/test/resources/data/images and target/test-classes/data/images.

I do want the tests to be runnable on Linux, both to lower barriers to others contributing, but also because many people run ERDDAP on Linux servers, so it is good to be confident in that environment as well. I don't have a linux machine available to me currently though so it's something I'll need assistance with.

As for the setup.xml issues. Micah's pull #147 should help with that. It provides a setup.xml intended to work for local development/tests. I'll make sure it works well with all the tests once it is merged.

srstsavage and others added 5 commits April 17, 2024 10:40
* Fix some jUnit tests on Linux

Fix many path related and other issues with jUnit tests on Linux.

Note that these changes don't yet make every test pass, there
are still issues with font differences in image generation,
non-portable performance checks, and a handful of other differences.

* Fix watch directory tests on windows

While linux may not send the modify events on deletion or the directory event, windows does

* Disable the display in browser, it causes tests to pause

* Have the path regex work for both windows and linux

* Fix the forwardSlashDir utility function

This is only used in one spot, so should be safe to modify.
Also it was previously checking if the string started with a / and if not, adding a slash to the end of it.

* Fix some of the EDDGridFromNcFilesTests on Linux

The wording of FileNotFoundException differs, so just make sure its that kind of exception. Mark testNcml flaky (for now), and fix the capitilzation of testGridNThreads for systems that are sensitive to that.

* Disable the performance part of persistentTableTests for now

* Change to using a temp dir for eddtablefromhttpgettests

---------

Co-authored-by: Chris John <[email protected]>
Add some explanation for image comparison tests to the readme.
…move the performance checks in a couple spots.
@ChrisJohnNOAA
Copy link
Contributor Author

@srstsavage I've uploaded a new version of data.zip. The normal process would be to have a new version of the data which the download/cache system should update automatically. For right now though you'll need to delete your download_cache/data.zip, src/test/resources/data, and target/test-classes/data files.

ERDDAP assumes linux machines have csh installed. If you do not you likely have a few errors due to that missing.

And I disabled a couple tests for now due to different behavior on Linux/Windows which I'll need to investigate more.

This gets all of the tests passing in my virtual linux machine (I talked to IT and got VirtualBox installed).

@ChrisJohnNOAA ChrisJohnNOAA marked this pull request as ready for review April 22, 2024 21:15
@ChrisJohnNOAA
Copy link
Contributor Author

@srstsavage I marked this as ready for review. While debugging some of the differences I had between my Windows and Linux (VirtualBox) machines, I realized that because of how many of the tests are constructed there are quite possibly errors for others. It comes down to a lot of the tests being based on comparing the dataset's attributes to a hardcoded set of values. However the attributes for a dataset are in many cases based on which file has the first or last (depending on settings) file modification timestamp. On a windows machine a copied (or file extracted from a zip) will retain it's original modification timestamp (but have a creation timestamp when it was copied/extracted). On Linux (possibly exact distribution dependent) when Java asks for the last modified timestamp of a file, the (OS) provided value will not be earlier than the file creation. What this means is which file is used for the metadata (and therefore attributes the test is comparing against) can be effectively random because it's now based on the order the files were extracted from the zip.

It would likely be good to move away from tests that can break if files are extracted from the zip in a different order, but that is outside of the scope of this pull. I'm also open to changes to how dataset metadata is extracted from files if that's appropriate (but also outside the scope of this change).

So that's a long way of saying, I think this pull is good enough. If there are tests that are failing on a clean build on other machines, I'll fix them, but I don't currently know of any.

@srstsavage
Copy link
Contributor

Ah thanks, that makes sense about the zip extraction. I'll give this another full test run and review tonight.

@srstsavage
Copy link
Contributor

Added another small PR here to re-add maven-antrun-plugin execution to the pom that creates the bigParentDirectory data referenced in development/jetty/config/setup.xml and comment out a few more non-portable performance tests.

I still have ~10 failing tests but I believe they're mostly due to the indeterminate zip extraction issue you mentioned. I agree that the few remaining failures don't need to get sorted out here and can be addressed in more targeted PRs.

…-portable performance tests (#8)

* re-add maven-antrun-plugin creation of jetty test data directory

* Comment out additional non-portable performance tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants