Skip to content

Fix test failures on MacOS: symlink expansion, check err, download problems - #140

Merged
rfay merged 6 commits into
ddev:masterfrom
rfay:20170411_fix_symlink_tmpdir_problem.2
Apr 13, 2017
Merged

rfay merged 6 commits into
ddev:masterfrom
rfay:20170411_fix_symlink_tmpdir_problem.2

Conversation

@rfay

@rfay rfay commented Apr 11, 2017 •

Copy link
Copy Markdown
Member

The Problem:

On MacOS tests are failing related to the excellent new testcommon library.

  1. symlinks don't seem to have expanded properly in docker or golang. See os.TempDir() features fail with docker, causing tests to fail on MacOS #134
  2. On MacOS, downloads are corrupted when using traditional golang techniques like our system.DownloadFile(). In my tests, even when the tarball was readable, it had a different checksum on every download. About half the time it was corrupted enough that it was unreadable. Edit: This appears to have been the result of using the same target location for download (filename based on project name "drupal8" and location in root of $TMPDIR) for multiple tests; they may have been able to collide with each other.
  3. There was a missing error check that led to a variety of following errors when the download file could not be untarred. That has been added back and instrumentation added.

The Fix:

  1. Expand symlinks explicitly when creating a tmpdir
  2. Change the download directory for tarball to be unique in each test.
  3. Fix the error check.
  4. Make test debugging a bit easier by
    • Splitting the two test stanzas into separate targets make testcmd and make testpkg so they can be run separately.
    • Adding $(TESTARGS) variable so you can make test TESTARGS="-run TestValidTestSite" for example

The Test:

  1. For testing the corrupted download/untar problem, you can run the test directly several times (macos) with:
    CGO_ENABLED=0 go test -v -installsuffix 'static' -ldflags "-extldflags -static" ./pkg/... -run TestValidTestSite

Without this PR you should see many ugly failures (maybe 50% of the time). With it you should get consistent results.

  1. Overall tests should be run several times on Macos with make test

Automation Overview:

No new tests were introduced, but some changes were made to existing test infrastructure, including expanding symlinks in temp dir creation.

Related Issue Link(s):

Release/Deployment notes:

Post-Mortem of this issue: Serious race condition in tests

I really had to completely understand how we got corrupted downloads in our tests so spent some time on it last night.

From Lesser-Known Features of Go Test:

Another source of parallelization, albeit not at the granular test function level, but at the package level, is when go test p1 p2 p3 is called (that is, when it is called with multiple packages to test). In this case, the test binaries of all packages are built then run at the same time. This can be great for the total running time, but it can also lead to randomly failing tests if some shared resources are used by many packages (e.g. if some tests access the database and delete some rows used by tests in other packages).

  1. Even though we didn't explicitly sign up for parallelism in our tests, we test several packages in the ./pkg/... test target that do tests in ddev Makefile. (now make pkg). Per the above, that means we can have more than one test going at a time.
  2. In my debugging I focused on one particular failing test, TestValidTestSite, so was using CGO_ENABLED=0 go test -v -installsuffix 'static' -ldflags "-extldflags -static" ./pkg/... -run TestValidTestSite, and I saw intermittent failures, even though I thought I was explicitly running one test. What I saw was a corrupted tarball after download nearly half the runs.
  3. I attempted to recreate the tarball corruption using the same library code we used, and was completely unable to make it happen.
  4. Careful placement of instrumentation revealed that all of the TestMain() functions in ./pkg/... were being run during the attempt above to run exactly one tests - the TestMain() is run when golang is searching for the pattern of tests to run.
  5. It turned out that local_test.go's TestMainhttps://github.com/drud/ddev/blob/fcb2d45900272904e64fabb3c113e9efe8b550fe/pkg/plugins/platform/local_test.go#L29-L42) (in the platform package was starting just 2 seconds after TestValidTestSite() started (in the testcommon package). The download of the drupal8 tarball is obviously dependent on bandwidth, but at the Starbucks today it takes 50 seconds. So both of these sections of code, using the exact same helper function (testcommon.Prepare()) were downloading to the exact same filepath.

Now for some of the simpler things:

  1. The code was choosing the location for the download as being in the root of the system tmpdir, and building a name for the file based on the project name, which was always the same. Using a varied name would have prevented this, as would using a unique directory under the $TMPDIR.
  2. It's hard to vary the name of the project, but it will help us a lot, especially when tests may be running concurrently and container names might collide. comment suggests that 'name' be optional in the config.yaml, so that it can be overridden by tests.
  3. The actual reason that this became so difficult was a lack of check on one error return, so the test continued and generally made a mess of everything and obscured the source of the problem. We have to check error returns even when it seems trivially obvious, IMO. build-tools 1.10.0 adds static analysis tools and the "errcheck" tool and target so we can actually add this checking to circle.yml.

@rfay
rfay force-pushed the 20170411_fix_symlink_tmpdir_problem.2 branch from 4a3a2e3 to e824cb3 Compare April 11, 2017 20:06
@rfay
rfay requested a review from beeradb April 11, 2017 21:17
@rfay
rfay force-pushed the 20170411_fix_symlink_tmpdir_problem.2 branch 6 times, most recently from 9a54eaf to e30d76c Compare April 12, 2017 04:11
@rfay rfay changed the title Expand/check any symlinks in tmpdir creation Fix test failures on MacOS: symlink expansion, check err, download problems Apr 12, 2017
@rfay
rfay requested a review from tannerjfco April 12, 2017 13:37
@rfay
rfay force-pushed the 20170411_fix_symlink_tmpdir_problem.2 branch from 37f30df to 555ff78 Compare April 12, 2017 17:46

@tannerjfco tannerjfco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've run this several times on my mac and ubuntu machines, both seem to be passing consistently with this changes. Thanks for spending the time to track these problems down.

// This wrapper function can prevent problems with docker-for-mac trying to use /var/..., which is not typically
// shared/mounted. It will be expanded via the /var symlink to /private/var/...
func OsTempDir() (string, error) {
dirName := os.TempDir()

@beeradb beeradb Apr 13, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we had issues w/ previous test runs, do we need to use RandString to add some entropy to these directory names by default?

I realize we added the prefix argument to CreateTmpDir, but since those are static values which are the same on every test run is that actually enough?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OsTempDir() is only used to get the actual normalized $TMPDIR; getting unique names inside it is handled tin testcommon.CreateTmpDir(). It's guaranteed to get a unique dir that nobody will fight over. We just need to use it everywhere. (or ioutil.TempDir() where we aren't worried about the docker mounting).

Comment thread pkg/testcommon/testcommon.go Outdated
testDir := CreateTmpDir(site.Name)
site.Dir = testDir
fmt.Printf("Prepping test for %s.", site.Name)
log.Printf("Prepping test for %s.\n", site.Name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make all of these a log.Debug or log.Info? I think I'm mostly in favor of keeping tests quiet unless we specifically need to enable output for some reason. Especially on stuff like this that should work 99.99% of the time.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to logrus, updated as necessary.

Comment thread pkg/testcommon/testcommon.go Outdated
})
if err != nil {
log.Printf("Tar extraction failed err=%v ", err)
_, _ = system.RunCommand("cp", []string{tarballPath, "/tmp/"})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do this copy in golang instead of shelling out? It's more work, but it seems like we've been moving away from shelling out.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might be able to delete this line now. Shelling out for a test-only situation that's only intended for preserving some artifact is probably not so bad. But I think it can just be removed.


// Create a testsite and ensure the prepare() method extracts files into a temporary directory.
ts.Prepare()
err = ts.Prepare()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably outside of the scope of fixing here, just want to comment to tag this: I'm betting this change needs to be made elsewhere as well. Test code has a nasty habit of getting copy+pasted around.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I grepped for instances of this particular problem and found none, but really want to get some tools in there that will catch this kind of thing.

@beeradb beeradb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some minor feedback but all of it is optional. Thanks for getting this fixed!

@rfay
rfay force-pushed the 20170411_fix_symlink_tmpdir_problem.2 branch from 555ff78 to b81b085 Compare April 13, 2017 21:14
@rfay
rfay merged commit 7dd52dc into ddev:master Apr 13, 2017
@rfay
rfay deleted the 20170411_fix_symlink_tmpdir_problem.2 branch April 13, 2017 21:31
@rfay

rfay commented Apr 14, 2017

Copy link
Copy Markdown
Member Author

Added this post-mortem to the OP:

Post-Mortem of this issue: Serious race condition in tests

I really had to completely understand how we got corrupted downloads in our tests so spent some time on it last night.

From Lesser-Known Features of Go Test:

Another source of parallelization, albeit not at the granular test function level, but at the package level, is when go test p1 p2 p3 is called (that is, when it is called with multiple packages to test). In this case, the test binaries of all packages are built then run at the same time. This can be great for the total running time, but it can also lead to randomly failing tests if some shared resources are used by many packages (e.g. if some tests access the database and delete some rows used by tests in other packages).

  1. Even though we didn't explicitly sign up for parallelism in our tests, we test several packages in the ./pkg/... test target that do tests in ddev Makefile. (now make pkg). Per the above, that means we can have more than one test going at a time.
  2. In my debugging I focused on one particular failing test, TestValidTestSite, so was using CGO_ENABLED=0 go test -v -installsuffix 'static' -ldflags "-extldflags -static" ./pkg/... -run TestValidTestSite, and I saw intermittent failures, even though I thought I was explicitly running one test. What I saw was a corrupted tarball after download nearly half the runs.
  3. I attempted to recreate the tarball corruption using the same library code we used, and was completely unable to make it happen.
  4. Careful placement of instrumentation revealed that all of the TestMain() functions in ./pkg/... were being run during the attempt above to run exactly one tests - the TestMain() is run when golang is searching for the pattern of tests to run.
  5. It turned out that local_test.go's TestMainhttps://github.com/drud/ddev/blob/fcb2d45900272904e64fabb3c113e9efe8b550fe/pkg/plugins/platform/local_test.go#L29-L42) (in the platform package was starting just 2 seconds after TestValidTestSite() started (in the testcommon package). The download of the drupal8 tarball is obviously dependent on bandwidth, but at the Starbucks today it takes 50 seconds. So both of these sections of code, using the exact same helper function (testcommon.Prepare()) were downloading to the exact same filepath.

Now for some of the simpler things:

  1. The code was choosing the location for the download as being in the root of the system tmpdir, and building a name for the file based on the project name, which was always the same. Using a varied name would have prevented this, as would using a unique directory under the $TMPDIR.
  2. It's hard to vary the name of the project, but it will help us a lot, especially when tests may be running concurrently and container names might collide. comment suggests that 'name' be optional in the config.yaml, so that it can be overridden by tests.
  3. The actual reason that this became so difficult was a lack of check on one error return, so the test continued and generally made a mess of everything and obscured the source of the problem. We have to check error returns even when it seems trivially obvious, IMO. build-tools 1.10.0 adds static analysis tools and the "errcheck" tool and target so we can actually add this checking to circle.yml.

@beeradb

beeradb commented Apr 14, 2017

Copy link
Copy Markdown
Contributor

Great work. Awesome write-up. So sorry for your pain 😢

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.

3 participants