test.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "./index.coffee"
  7. expect = require("chai").expect
  8. describe "index", ->
  9. beforeEach ->
  10. @settings = {}
  11. @sentinelClient = set:->
  12. @normalRedisClient = get: ->
  13. @sentinel =
  14. createClient: sinon.stub().returns(@sentinelClient)
  15. @normalRedis =
  16. createClient: sinon.stub().returns(@normalRedisClient)
  17. @redis = SandboxedModule.require modulePath, requires:
  18. "redis-sentinel":@sentinel
  19. "redis":@normalRedis
  20. @auth_pass = "1234 pass"
  21. @endpoints = [
  22. {host: '127.0.0.1', port: 26379},
  23. {host: '127.0.0.1', port: 26380}
  24. ]
  25. describe "sentinel", ->
  26. beforeEach ->
  27. @masterName = "my master"
  28. @sentinelOptions =
  29. endpoints:@endpoints
  30. masterName:@masterName
  31. auth_pass:@auth_pass
  32. it "should use sentinal if the first argument in an array", ->
  33. client = @redis.createClient @sentinelOptions
  34. @sentinel.createClient.called.should.equal true
  35. @normalRedis.createClient.called.should.equal false
  36. client.should.equal @sentinelClient
  37. it "should pass the options correctly though", ->
  38. client = @redis.createClient @sentinelOptions
  39. @sentinel.createClient.calledWith(@endpoints, @masterName, auth_pass:@auth_pass).should.equal true
  40. client.should.equal @sentinelClient
  41. describe "normal redis", ->
  42. beforeEach ->
  43. @standardOpts =
  44. auth_pass: @auth_pass
  45. port: 1234
  46. host: "redis.mysite.env"
  47. it "should use the normal redis driver if a non array is passed", ->
  48. client = @redis.createClient @standardOpts
  49. @sentinel.createClient.called.should.equal false
  50. @normalRedis.createClient.called.should.equal true
  51. client.should.equal @normalRedisClient
  52. it "should use the normal redis driver if a non array is passed", ->
  53. client = @redis.createClient @standardOpts
  54. @normalRedis.createClient.calledWith(@standardOpts.port, @standardOpts.host, auth_pass:@auth_pass).should.equal true
  55. describe "setting the password", ->
  56. beforeEach ->
  57. @standardOpts =
  58. password: @auth_pass
  59. port: 1234
  60. host: "redis.mysite.env"
  61. @sentinelOptions =
  62. endpoints:@endpoints
  63. masterName:@masterName
  64. password: @auth_pass
  65. it "should set the auth_pass from password if password exists for normal redis", ->
  66. client = @redis.createClient @standardOpts
  67. @normalRedis.createClient.calledWith(@standardOpts.port, @standardOpts.host, auth_pass:@auth_pass).should.equal true
  68. it "should set the auth_pass from password if password exists for sentinal", ->
  69. client = @redis.createClient @sentinelOptions
  70. @sentinel.createClient.calledWith(@endpoints, @masterName, auth_pass:@auth_pass).should.equal true