redis_storage_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package remotecache
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. redis "gopkg.in/redis.v2"
  7. )
  8. func Test_parseRedisConnStr(t *testing.T) {
  9. cases := map[string]struct {
  10. InputConnStr string
  11. OutputOptions *redis.Options
  12. ShouldErr bool
  13. }{
  14. "all redis options should parse": {
  15. "addr=127.0.0.1:6379,pool_size=100,db=1,password=grafanaRocks",
  16. &redis.Options{
  17. Addr: "127.0.0.1:6379",
  18. PoolSize: 100,
  19. DB: 1,
  20. Password: "grafanaRocks",
  21. Network: "tcp",
  22. },
  23. false,
  24. },
  25. "subset of redis options should parse": {
  26. "addr=127.0.0.1:6379,pool_size=100",
  27. &redis.Options{
  28. Addr: "127.0.0.1:6379",
  29. PoolSize: 100,
  30. Network: "tcp",
  31. },
  32. false,
  33. },
  34. "trailing comma should err": {
  35. "addr=127.0.0.1:6379,pool_size=100,",
  36. nil,
  37. true,
  38. },
  39. "invalid key should err": {
  40. "addr=127.0.0.1:6379,puddle_size=100",
  41. nil,
  42. true,
  43. },
  44. "empty connection string should err": {
  45. "",
  46. nil,
  47. true,
  48. },
  49. }
  50. for reason, testCase := range cases {
  51. options, err := parseRedisConnStr(testCase.InputConnStr)
  52. if testCase.ShouldErr {
  53. assert.Error(t, err, fmt.Sprintf("error cases should return non-nil error for test case %v", reason))
  54. assert.Nil(t, options, fmt.Sprintf("error cases should return nil for redis options for test case %v", reason))
  55. continue
  56. }
  57. assert.NoError(t, err, reason)
  58. assert.EqualValues(t, testCase.OutputOptions, options, reason)
  59. }
  60. }