# Go tests can't reach mongo container

**URL:** <https://discuss.circleci.com/t/go-tests-cant-reach-mongo-container/23913>\
**Category:** Build Environment\
**Tags:** go, docker, mongo\
**Created:** [July 27, 2018, 1:59pm UTC](https://discuss.circleci.com/t/go-tests-cant-reach-mongo-container/23913 "2018-07-27T13:59:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucasmdrs](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@lucasmdrs](https://discuss.circleci.com/u/lucasmdrs)\
**Post date:** [July 27, 2018, 1:59pm UTC](https://discuss.circleci.com/t/go-tests-cant-reach-mongo-container/23913/1 "2018-07-27T13:59:34Z")

</div>

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

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/halfer/32/9083_2.png) [@halfer](https://discuss.circleci.com/u/halfer)\
**Post date:** [July 27, 2018, 6:06pm UTC](https://discuss.circleci.com/t/go-tests-cant-reach-mongo-container/23913/2 "2018-07-27T18:06:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Cormac](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/cormac/32/18955_2.png) [@Cormac](https://discuss.circleci.com/u/Cormac)\
**Post date:** [July 27, 2018, 6:27pm UTC](https://discuss.circleci.com/t/go-tests-cant-reach-mongo-container/23913/3 "2018-07-27T18:27:48Z")

</div>

Would agree with @halfer – could you have a look at [this support center article](https://support.circleci.com/hc/en-us/articles/360006773953-Race-Conditions-Wait-For-Database) and see if it’s any help?

---

<div class="post-metadata">

**Author:** ![lucasmdrs](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@lucasmdrs](https://discuss.circleci.com/u/lucasmdrs)\
**Post date:** [July 27, 2018, 8:56pm UTC](https://discuss.circleci.com/t/go-tests-cant-reach-mongo-container/23913/4 "2018-07-27T20:56:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/halfer/32/9083_2.png) [@halfer](https://discuss.circleci.com/u/halfer)\
**Post date:** [July 28, 2018, 7:52am UTC](https://discuss.circleci.com/t/go-tests-cant-reach-mongo-container/23913/5 "2018-07-28T07:52:47Z")

</div>

> [@lucasmdrs](#):
>
> Anyway, I’ve add a check just for the sake of it, using netcat and the output was, as expected, the service was up!

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.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [August 7, 2018, 7:52am UTC](https://discuss.circleci.com/t/go-tests-cant-reach-mongo-container/23913/6 "2018-08-07T07:52:49Z")

</div>

Closed on August 7, 2018, 7:52am UTC

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