Adding MongoDB Replica Set Service in Drone for E2E Tests

Hi y’all! I have a stage in Drone that runs end-to-end tests for my backend application and since it depends on a database, I have Drone launch a MongoDB service. I have a few tests that use transactions, and that requires a replica set member.

What I need help with is launching a MongoDB replica set in the Drone pipeline. Looking over some other resources, I see that it requires the command rs.initiate(...) to initiate the replica set, but I’m not sure how I can run that command in the Drone pipeline.

I’m thinking I could run this command in a different stage (e.g. initiate-mongo-clusters), but I’m not quite sure if it’s possible to do this in the “test” stage.

Here is what I have so far:

kind: pipeline
type: docker
name: test-pipeline

  disable: true

  - name: clone
    image: alpine/git
      - git clone .
      - git checkout $DRONE_COMMIT

  - name: initiate-mongo-clusters
    image: mongo:latest
      - mongosh --host mongo1:27017 --eval "rs.initiate({ _id: \"e2e\", members: [{_id: 0, host: \"mongo1\"}, {_id: 1, host: \"mongo2\"}, {_id: 2, host: \"mongo3\"}]})"

   - name: test
     image: node:16.13.0-alpine
       - npm install
       - npm run test:e2e
       DB_URL: mongodb://mongodb1:27017/test?replicaSet=e2e

  - name: mongo1
    image: mongo:latest
    command: '--replSet e2e'
  - name: mongo2
    image: mongo:latest
    command: '--replSet e2e'
  - name: mongo3
    image: mongo:latest
    command: '--replSet e2e'

When running this Drone pipeline, I get the error “yaml: line : mapping values are not allowed in this context” when initiate-mongo-clusters stage runs. I assume this is related to the --eval command.

Any thoughts? Thanks in advance!

I believe the command attribute is an array

-    command: '--replSet e2e'
+    command: [ '--replSet e2e' ]

Hey @brad , thanks for the reply! I changed the command attribute to be an array. I also found the solution to the error yaml: line : mapping values are not allowed in this context. This was related to some spacing between after the colons in rs.initiate. I had to make sure those spaces were removed.

"rs.initiate({ _id:\"e2e\",members:[{_id: 0, host:\"mongo1\"},{_id: 1, host:\"mongo2\"},{_id: 2, host:\"mongo3\"}]})"

It’s bit of a weird issue, but finally got replica sets running!

Though… I did face some issues related to my test stage. In most cases, my tests would pass. But in some cases, some of my tests would fail due to a 500 Internal Server Error, and I believe this is related to the replica sets because this issue doesn’t come up in my local environment. Any thoughts on this?

Actually, I found a solution to the 500 Internal Server error. For anyone that is curious, I added another stage that checks the status of the MongoDB clusters, which gives the MongoDB clusters time to be fully set up.

- name: check-mongo-cluster-status
   image: mongo:latest
      - sleep 10
      - mongosh --host mongo1:27017 --eval "rs.status()"
      - mongosh --host mongo2:27017 --eval "rs.status()"
      - mongosh --host mongo3:27017 --eval "rs.status()"