2015-10-01 102 views
0

我嘗試使用wercker, 但我不知道我的測試無法連接到我的mongodb。wercker for sails + mongo db

我使用帆+帆蒙戈,當NPM測試...我總是得到錯誤可以連接到蒙戈分貝,這是我的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" 

這是我的錯誤消息:

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 

回答

1

雖然開箱即用,但MongoDB沒有身份驗證,所以您只需提供適當的主機和端口即可。

  1. 定義在config/connection.js在你的船帆應用一個新的連接:
mongodbTestingServer: { 
     adapter: 'sails-mongo', 
     host: process.env.MONGO_PORT_27017_TCP_ADDR, 
     port: process.env.MONGO_PORT_27017_TCP_PORT 
    }, 

關於MONGO_PORT_27017_TCP_ADDRMONGO_PORT_27017_TCP_PORT,通過Wercker被創建時,你聲明的蒙戈服務這兩個環境變量。 就像這樣,您將能夠通過正確的主機和端口將您的應用程序連接到您的數據庫。

  1. 在您的sails sails應用程序中添加新環境config/env/testing.js。它將被用於通過Wercker:
module.exports = { 
    models: { 
     connection: 'mongodbTestingServer' 
    } 
}; 
  • 在你wercker文件wercker.yml。我建議您使用ewok堆棧(基於Docker),您可以在應用程序的設置中激活它。以下是有關migration to Ewok stack的一些有用信息。我的例子使用基於Docker鏡像的盒子。
  • # 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 
    

    要查看您的Wercker所有環境變量建立,加入這一行:

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

    它應該工作。