machine:
  services:
    - docker
  environment:
    GODIST: "go1.9.linux-amd64.tar.gz"
    IMPORT_PATH: "/home/ubuntu/.go_workspace/src/github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME"
  post:
    - mkdir -p download
    - test -e download/$GODIST || curl -o download/$GODIST https://storage.googleapis.com/golang/$GODIST
    - sudo rm -rf /usr/local/go
    - sudo tar -C /usr/local -xzf download/$GODIST

dependencies:
  pre:
    # clean the workspace
    - rm -Rf /home/ubuntu/.go_workspace/src/*
    # we should use an old docker-compose because CircleCI supports
    # only docker-engine==1.9
    - pip install docker-compose==1.7.1

  override:
    # put the package in the right $GOPATH
    - mkdir -p "$IMPORT_PATH"
    - rsync -azr --delete ./ "$IMPORT_PATH"
    - cd "$IMPORT_PATH" && rake init

test:
  override:
    # run the agent and backing services
    - docker-compose up -d | cat
    # wait for external services and execute tests
    - cd "$IMPORT_PATH" && ./wait-for-services.sh
    - cd "$IMPORT_PATH" && rake test:lint
    - cd "$IMPORT_PATH" && rake test:all
    - cd "$IMPORT_PATH" && rake test:race
    - cd "$IMPORT_PATH" && rake test:coverage

  post:
    # add the coverage HTML report as CircleCI artifact
    - cd "$IMPORT_PATH" && go tool cover -html=code.cov -o $CIRCLE_ARTIFACTS/coverage.html
