Unit Tests with MySQL

Using this repository, we’ll help you get up to speed with basic functionality such as: compiling, testing and building Docker images.

This project uses Node.js, MySQL to build an application which will eventually become a distributable Docker image.

Looking around

In the root of this repository, you’ll find a file named codefresh.yml, this is our build descriptor and it describes the different steps that comprise our process. Let’s quickly review the contents of this file:

codefresh.yml

version: '1.0'
steps:
  build_step:
    type: build
    image_name: codefreshio/example-nodejs-mysql
    dockerfile: Dockerfile
    tag: ${{CF_BRANCH}}

  unit_test:
    type: composition
    working_directory: ${{main_clone}}
    composition: ./docker-compose.yml
    composition_candidates:
      test:
        image: ${{build_step}}
        links:
          - db
        command: bash -c '/usr/src/app/test-script.sh'
        environment:
          - MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD
          - MYSQL_USER=$MYSQL_USER
          - MYSQL_PASSWORD=$MYSQL_PASSWORD
          - MYSQL_DATABASE=$MYSQL_DATABASE
          - MYSQL_HOST=$MYSQL_HOST
    composition_variables:
      - MYSQL_ROOT_PASSWORD=admin
      - MYSQL_USER=my_user
      - MYSQL_PASSWORD=admin
      - MYSQL_DATABASE=nodejs
      - MYSQL_HOST=db

In this test script, we wait for mysql is ready, then we can run the tests

script.sh

#!/usr/bin/env bash
wait_for_db() {
  nslookup db
  if ! nc -z db 3306; then
    echo "Waiting for db..."
    sleep 2
    wait_for_db
  fi
}

wait_for_db

cd /usr/src/app
npm install
npm test
Example

Just head over to the example repository in Github.

Expected result

Codefresh unit test MySQL