Unit Tests with Redis

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 Python, Redis 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_prj:
    type: build
    dockerfile: Dockerfile
    image_name: web
    tag: ${{CF_BRANCH}}

  build_test:
    type: build
    dockerfile: Dockerfile.test
    image_name: test
    tag: ${{CF_BRANCH}}

  unit_test:
    type: composition
    composition:
      version: '2'
      services:
        web:
          image: ${{build_prj}}
          links:
            - redis
          ports:
            - 80
        redis:
          image: redis
    composition_candidates:
      test:
        image: ${{build_test}}
Example

Just head over to the example repository in Github.

In this test script, we wait for web service and database is ready for testing

script.sh

#!bin/bash

wait_for_db() {
  nslookup redis
  if ! nc -z redis 6379; then
    echo "Waiting for db..."
    sleep 2
    wait_for_db
  fi
}

wait_for_web() {
  nslookup web
  if ! nc -z web 80; then
    echo "Waiting for web..."
    sleep 2
    wait_for_web
  fi
}

wait_for_db
wait_for_web

if curl web | grep -q '<b>Visits:</b> '; then
  echo "Tests passed!"
  exit 0
else
  echo "Tests failed!"
  exit 1
fi

Expected result

Codefresh unit test Redis