Go tests can't reach mongo container

I’ve been trying to improve my test job in one of my Go projects, which tries to connect to a mongo database. Here’s the config yml

test: &test
  working_directory: /go/src/project
  docker:
    - image: ${AWS_REGISTRY}/docker-image-citools:go-dep
      aws_auth:
        aws_access_key_id: $AWS_ACCESS_KEY_ID
        aws_secret_access_key: $AWS_SECRET_ACCESS_KEY
    - image: ${AWS_REGISTRY}/rabbitmq:latest
      aws_auth:
        aws_access_key_id: $AWS_ACCESS_KEY_ID
        aws_secret_access_key: $AWS_SECRET_ACCESS_KEY
      environment:
        RABBITMQ_PUBLISHER_PASSWD: publisher
    - image: circleci/mongo:3.4-jessie-ram
      command: [mongod, --smallfiles]
      ports:
        - 27017:27017


version: 2
jobs:
  tests:
    <<: *test
    steps:
      - checkout:
          path: /go/src/project
      - attach_workspace:
          at: /go/src/project
      - run:
          name: Go dep
          command: $GOPATH/bin/dep ensure
      - run:
          name: Wait for rabbitmq user settings
          command: |
            LOGIN="failed"
            while [[ "${LOGIN}" =~ failed ]]; do
              LOGIN=$(curl -s -u publisher:publisher http://localhost:15672/api/whoami | jq -r '.reason')
              sleep 3 && echo $LOGIN
            done;
      - run:
          name: Go test
          command: go test -v ./...
      - persist_to_workspace:
          root: .

I’ve tried to run locally using circleci cli, and it worked fine, but when I pushed the job fails with:

panic: no reachable servers [recovered]
	panic: no reachable servers

At a guess, your test container is not waiting until Mongo is ready to service requests - I would imagine your three containers start up in parallel and there’s a race-condition introduced as to which starts first.

It looks like you are waiting for RabbitMQ - so do the same for MongoDB?

Would agree with @halfer – could you have a look at this support center article and see if it’s any help?

Thanks for the quick reply guys. The rabbitmq service takes a longtime to start so that’s why I check if it’s up or not, mongo in the other hand it’s mutch faster and the job logs showed me that it’s was ready before the start of the tests.

Anyway, I’ve add a check just for the sake of it, using netcat and the output was, as expected, the service was up!

TL;DR I was missing a environment variable 'couse my test wasn’t getting the default value. Curiously, as I said earlier, when I’ve tried to run locally the job ran successfully, not sure what happend there.

Don’t rely on this. The times when it works may be when a race condition is in your favour. It might still lose the race sometimes, and crash your tests.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.