Open nam20485 opened 6 years ago
Yeah, this was earlier command, and actually removed from current setup: https://github.com/hackoregon/backend-examplar-2018/blob/b251b21d853f197d9cc7a202ee0d7d417b806dea/bin/test.sh#L16
We need to build out.
so general scaffold we will need, following discussion in #51:
the -p
command should be more along a build and run like this:
docker-compose -f travis-docker-compose.yml build
docker-compose -f travis-docker-compose.yml run \
--entrypoint /code/bin/test-entrypoint.sh $DOCKER_IMAGE
test-entrpoint.sh
- should roughly be:
#!/bin/bash
export PATH=$PATH:~/.local/bin
python manage.py test --no-input --keepdb
since we are using the --keepdb variable
, and assuming tests will be run against the prod database:
I made an attempt at the travis configuration and have something working now. I basically just followed the patterns I found in the budget and transporation backend repos you referenced as example, using the Docker images and bin/*.sh scripts that were already in our repos. I know it may not be exactly what we want to go with but I figured it would be worth getting the major components working.
If you look in the .travis.yml and the test-entrypoint.sh you will see that it is very similar to what you posted above.
...
script:
- bin/build.sh -p
- bin/test.sh -p
after_success:
- bin/deploy.sh
...
docker-compose -f production-docker-compose.yml run --entrypoint /code/bin/test-entrypoint.sh api_production -p 8000 --rm
...
python manage.py test --nomigrations
...
class NeighborhoodUnitsTestCase(TestCase):
def setUp(self):
#preexisting_models.NeighborhoodUnits.objects.create()
pass
def test_example(self):
self.assertTrue(True)
Travis builds the production API image and then runs any Django unit tests in found in api/tests/tests.py
(currently just one fake unit test case example). Additional unit tests that backend developers will want to create can be added easily to this file.
After that bin/deploy.sh
is run but it is mostly commented-out and blank at this point.
The only piece that is not accounted for are the database credentials, which I currently am resorting to hard-coding securely in the Travis build config settings.
Is this something generalizable and worth adding to the examplar? It seems like it would be.
awesome, going in the right direction.
A few things.
test.sh
, after our review of last year's deployment, it appears that we built the docker containers once, ran the tests, and then deployed these same containers to aws. As such it does not look like we will want to have the --rm
flag here, we do want to preserve the containers created.
"currently am resorting to hard-coding securely in the Travis build config settings." - if you are saying you are setting these within the settings in the Travis UI, with the lock activated to secure them, this is correct plan. From looking over your logs and github briefly, this seems to be the case.
"the patterns I found in the budget and transportation backend repos you referenced as example, using the Docker images"
so, in the end it turned out transportation from last year did not write any actual tests so should not be used as a base pattern, we cannot verify what actually worked.
it appears that you are not using the --keepdb
flag currently, where is the test db being spun up and destroyed? in other words, are you connecting to an aws hosted database server? if so, the current one or last year's emergency response ec2 instance, or something else?
at some point we will need to replace the 'api_production' hardcoded name to start using $DOCKER_IMAGE env var, it may be better though that we make the env var updates their own pr request and just note this for later.
Cool, once we settle on the details and have it working how you would like I was thinking that I would open a PR to merge this into the exemplar repo. Then generated quickstart API apps would take advantage of the travis functionality straight out of the box.
test.sh, after our review of last year's deployment, it appears that we built the docker containers once, ran the tests, and then deployed these same containers to aws. As such it does not look like we will want to have the --rm flag here, we do want to preserve the containers created.
I will remove the --rm
flag.
"currently am resorting to hard-coding securely in the Travis build config settings." - if you are saying you are setting these within the settings in the Travis UI, with the lock activated to secure them, this is correct plan. From looking over your logs and github briefly, this seems to be the case.
Correct, I have them in the Travis UI settings, secured with the privacy lock so they aren't advertised in the logs.
"the patterns I found in the budget and transportation backend repos you referenced as example, using the Docker images" so, in the end it turned out transportation from last year did not write any actual tests so should not be used as a base pattern, we cannot verify what actually worked.
I think it should still be okay. The only thing I really took from their pattern was how to structure the unit test directory. Everything else is either blank or laid out according to Django convention. What I have now is a tests/
directory in the Django application (api/
) directory with a file called tests.py
in it. Django convention is search for that file at that path and run any classes it finds in the file that extend the TestCase base class. I put one unit TestCase in the file to provide an example. Developers that have a backend repo with a generated quickstart app and filled-in API can then easily add and build out their api and data unit test cases by adding more cases in that file. I think this process for adding and running unit tests is pretty efficient for newcomers. Also, the bin/test.sh -d
command works to run the development images and their unit tests locally on the dev machine.
it appears that you are not using the --keepdb flag currently, where is the test db being spun up and destroyed? in other words, are you connecting to an aws hosted database server? if so, the current one or last year's emergency response ec2 instance, or something else?
I have since added the --keepdb
flag. Yes, currently it is pointed at the 2017 emergency-response postgres on AWS ec2, which has a copy of the disaster database. Here is the most current form I am invoking:
python manage.py test --nomigrations --no-input --keepdb
at some point we will need to replace the 'api_production' hardcoded name to start using $DOCKER_IMAGE env var, it may be better though that we make the env var updates their own pr request and just note this for later.
OK. @znmeb can probably handle changing the image name if necessary. Do you want me to create an issue so we can keep track of that requirement?
Definitely create an issue - I read GitHub more than Slack!
Created, see #57
@znmeb You seem to be proficient with the Docker images, would this be a good one for you to tackle?
Yeah ... If nobody else wants it I'll take it
I believe the issues discussed are resolved with #60
We will need to confirm this pattern works for projects connecting to the current database server, with read-only credentials, and running an actual test.
I had run into issues, which is why currently using py.test on transportation-systems-backend.
This issue will be resolved when related issue #82 becomes resolved and we settle on a read-only access DB unit testing solution. (Probably pytest)
Running
bin/test.sh -p
causes the following command to be run:docker-compose -p tests run --entrypoint /code/bin/test-entrypoint.sh -p 8000 --all -f
This command is not correct, and results in the following error output: