0

i'm try to use wercker, but i don't know my testing can't connect into my mongodb.

i'm using sails + sails mongo, and when npm test...i'm always get error can connect into mongo db, this is my wercker.yml :

box: nodesource/trusty:0.12.7
services:
  - id: mongo:2.6
# Build definition
build:
  # The steps that will be executed on build
  steps:
    - script:
        name: set NODE_ENV
        code: export NODE_ENV=development
    # A step that executes `npm install` command
    - npm-install
    # A step that executes `npm test` command
    - npm-test
    # A custom script step, name value is used in the UI
    # and the code value contains the command that get executed
    - script:
        name: echo nodejs information
        code: |
          echo "node version $(node -v) running"
          echo "npm version $(npm -v) running"

this is my error message :

warn: `sails.config.express` is deprecated; use `sails.config.http` instead.
Express midleware for passport
error: A hook (`orm`) failed to load!
  1) "before all" hook
  2) "after all" hook

  0 passing (2s)
  2 failing

  1)  "before all" hook:
     Uncaught Error: Failed to connect to MongoDB.  Are you sure your configured Mongo instance is running?
 Error details:
{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
      at net.js:459:14

  2)  "after all" hook:
     Uncaught Error: Failed to connect to MongoDB.  Are you sure your configured Mongo instance is running?
 Error details:
{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
      at net.js:459:14
monoy suronoy
  • 881
  • 5
  • 10
  • 19

1 Answers1

1

While out of the box, MongoDB has no authentication so you just have to provide to sails the right host and port.

  1. Define a new connection in your sails app in config/connection.js:
    mongodbTestingServer: {
        adapter: 'sails-mongo',
        host:    process.env.MONGO_PORT_27017_TCP_ADDR,
        port:    process.env.MONGO_PORT_27017_TCP_PORT
    },

Concerning MONGO_PORT_27017_TCP_ADDR and MONGO_PORT_27017_TCP_PORT, these 2 environment variable are created by Wercker when you declared a mongo service. Like That, you will be able to connected your application to your database with the right host and port.

  1. Add a new environment in your sails sails app in config/env/testing.js. It will be used by Wercker :
module.exports = {
    models: {
        connection: 'mongodbTestingServer'
    }
};
  1. In your wercker file wercker.yml. I recommend you to use the ewok stack (based on Docker), you can active it in the settings of your application. Here is some useful informations concerning migration to Ewok stack. My example use a box based on a Docker image.
# use the latest official stable node image hosted on DockerHub 
box: node
# use the mongo (v2.6) image hosted on DockerHub 
services:
  - id: mongo:2.6
# Build definition
build:
    steps:
    # Print node and npm version
    - script:
        name: echo nodejs information
        code: |
            echo "node version $(node -v) running"
            echo "npm version $(npm -v) running"
    - script:
        name: set NODE_ENV
        code: |
            export NODE_ENV=testing
    # install npm dependencies of your project
    - npm-install
    # run tests
    - npm-test

To see all environment variables in your Wercker build, add this line :

- script:
    name: show all environment variables
    code: |
        env

It should work.

Peter
  • 13,733
  • 11
  • 75
  • 122
Louis Barranqueiro
  • 10,058
  • 6
  • 42
  • 52