简体   繁体   中英

Starting docker-compose via npm

I have a small node app against which I want to run some browsertests. I am wondering what the best approach was. First I was thinking to put everything in my package.json but I do not see how I would spin up docker-compose, run the tests, and kill docker-compose.

For example, I would make something like:

"scripts": {
    "test": "docker-compose up && mocha --ui tdd test/**/*.test.js --reporter spec && docker-compose down

But I am not sure that this is possible.

What is used nowadays to accomplish this? Thanks in advance.

Cheers

It appears you have a synchronization issue where your tests try to access services before your test fixture has reached the point where the services are available. (It may be something like a node app with an HTTP server that listens for API requests, for example.)

When you do docker-compose up -d , the docker-compose process exits before the containerized services within have created their sockets and started listening for connections. That means that if the next thing you do is run your tests that try to open a connection to the services started by docker-compose, there is a race condition where the service may not be available yet.

You'll have to make your test so that when it tries to open a connection, if it encounters a timeout, then it retries a few times (allowing enough time and retries for your containers to reach a state where they are listening and ready to service requests before giving up.)

You can usually just have a simple standalone app that repeatedly tries to open up the service port and returns success when it succeeds (or retries if it times out up to some maximum where it returns an error). Insert this before you run your tests to ensure your services have reached a listening state.

I strongly suggest reading Docker Compose: Wait for Dependencies

That article describes a nice little docker-image you can use that will repeatedly try to open a TCP port until it succeeds. You can use it to synchronize your test with the test fixture services the test requires.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM