diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml new file mode 100644 index 0000000..68605a7 --- /dev/null +++ b/.github/workflows/pull-request.yml @@ -0,0 +1,12 @@ +name: Test Pull Request +on: + pull_request: + branches: + - 'dev' + - 'staging' + - 'master' + workflow_dispatch: +jobs: + test-pr: + uses: ./.github/workflows/run_tests.yml + secrets: inherit diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml new file mode 100644 index 0000000..eef646b --- /dev/null +++ b/.github/workflows/run_tests.yml @@ -0,0 +1,67 @@ +name: Run tests +on: + workflow_dispatch: + workflow_call: +jobs: + test: + runs-on: ubuntu-latest + env: + ELASTIC_PASSWORD: "changeme" + ELASTIC7_HOST: "localhost" + ELASTIC7_PORT: "9200" + DB_HOST: 127.0.0.1 + AWS_REGION: ${{ secrets.AWS_REGION }} + GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }} + LAUNCH_DARKLY_KEY: ${{ secrets.LAUNCH_DARKLY_KEY_DEV }} + TEST_DATA_DUMP_FILE: ${{ vars.TEST_DATA_DUMP_FILE }} + services: + elasticsearch7: + image: docker.elastic.co/elasticsearch/elasticsearch:7.10.0 + env: + discovery.type: single-node + ES_JAVA_OPTS: -Xms512m -Xmx512m + ELASTIC_PASSWORD: changeme + xpack.security.enabled: "false" + http.cors.enabled: "true" + http.cors.allow-origin: "*" + ports: + - 9200:9200 + db: + image: mysql:8.0 + env: + MYSQL_DATABASE: "rorapi" + MYSQL_USER: "ror_user" + MYSQL_PASSWORD: "password" + MYSQL_ROOT_PASSWORD: "password" + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + steps: + - name: Checkout ror-api code + uses: actions/checkout@v2 + with: + path: ror-api + - name: Set up Python environment + uses: actions/setup-python@v6 + with: + python-version: "3.8" + cache: "pip" + - name: Install requirements + working-directory: ./ror-api + run: | + # python -m pip install --upgrade pip + pip install -r requirements.txt + pip install yapf + + python manage.py collectstatic --noinput + - name: Load test data + working-directory: ./ror-api + run: | + python manage.py setup ${{ env.TEST_DATA_DUMP_FILE }} -t + - name: Run Tests + working-directory: ./ror-api + run: | + python manage.py test rorapi.tests.tests_unit + # TODO fix these tests running in GitHub Action + # python manage.py test rorapi.tests.tests_integration + # python manage.py test rorapi.tests.tests_functional diff --git a/docker-compose.yml b/docker-compose.yml index 253e3c3..698ea39 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,7 @@ services: interval: 10s timeout: 1s volumes: - - ./esdata:/usr/share/elasticsearch/data + - esdata:/usr/share/elasticsearch/data db: image: mysql:8.0 volumes: @@ -46,3 +46,5 @@ services: - db volumes: mysql_data: + esdata: + driver: local diff --git a/rorapi/settings.py b/rorapi/settings.py index 5fb4230..1ccf72e 100644 --- a/rorapi/settings.py +++ b/rorapi/settings.py @@ -159,14 +159,19 @@ 'BULK_SIZE': 500 } -# use AWS4Auth for AWS Elasticsearch unless running locally via docker -if os.environ.get('ELASTIC7_HOST', 'elasticsearch7') != 'elasticsearch7': - http_auth = AWS4Auth(os.environ.get('AWS_ACCESS_KEY_ID'), - os.environ.get('AWS_SECRET_ACCESS_KEY'), - os.environ.get('AWS_REGION'), 'es') +# use AWS4Auth for AWS Elasticsearch unless running locally via docker or localhost +if os.environ.get('ELASTIC7_HOST', 'elasticsearch7') not in ['elasticsearch7', 'localhost']: + aws_access_key = os.environ.get('AWS_ACCESS_KEY_ID') + aws_secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY') + aws_region = os.environ.get('AWS_REGION') + if aws_access_key and aws_secret_key and aws_region: + http_auth = AWS4Auth(aws_access_key, aws_secret_key, aws_region, 'es') + else: + http_auth = ('elastic', os.environ.get('ELASTIC_PASSWORD', 'changeme')) else: http_auth = ('elastic', os.environ.get('ELASTIC_PASSWORD', 'changeme')) + ES7 = Elasticsearch([{ 'host': os.environ.get('ELASTIC7_HOST', 'elasticsearch7'), 'port': int(os.environ.get('ELASTIC7_PORT', '9200'))