redis_storage_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package remotecache
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. redis "gopkg.in/redis.v5"
  8. )
  9. func Test_parseRedisConnStr(t *testing.T) {
  10. cases := map[string]struct {
  11. InputConnStr string
  12. OutputOptions *redis.Options
  13. ShouldErr bool
  14. }{
  15. "all redis options should parse": {
  16. "addr=127.0.0.1:6379,pool_size=100,db=1,password=grafanaRocks,ssl=false",
  17. &redis.Options{
  18. Addr: "127.0.0.1:6379",
  19. PoolSize: 100,
  20. DB: 1,
  21. Password: "grafanaRocks",
  22. Network: "tcp",
  23. TLSConfig: nil,
  24. },
  25. false,
  26. },
  27. "subset of redis options should parse": {
  28. "addr=127.0.0.1:6379,pool_size=100",
  29. &redis.Options{
  30. Addr: "127.0.0.1:6379",
  31. PoolSize: 100,
  32. Network: "tcp",
  33. },
  34. false,
  35. },
  36. "ssl set to true should result in default TLS configuration with tls set to addr's host": {
  37. "addr=grafana.com:6379,ssl=true",
  38. &redis.Options{
  39. Addr: "grafana.com:6379",
  40. Network: "tcp",
  41. TLSConfig: &tls.Config{ServerName: "grafana.com"},
  42. },
  43. false,
  44. },
  45. "ssl to insecure should result in TLS configuration with InsecureSkipVerify": {
  46. "addr=127.0.0.1:6379,ssl=insecure",
  47. &redis.Options{
  48. Addr: "127.0.0.1:6379",
  49. Network: "tcp",
  50. TLSConfig: &tls.Config{InsecureSkipVerify: true},
  51. },
  52. false,
  53. },
  54. "invalid SSL option should err": {
  55. "addr=127.0.0.1:6379,ssl=dragons",
  56. nil,
  57. true,
  58. },
  59. "invalid pool_size value should err": {
  60. "addr=127.0.0.1:6379,pool_size=seven",
  61. nil,
  62. true,
  63. },
  64. "invalid db value should err": {
  65. "addr=127.0.0.1:6379,db=seven",
  66. nil,
  67. true,
  68. },
  69. "trailing comma should err": {
  70. "addr=127.0.0.1:6379,pool_size=100,",
  71. nil,
  72. true,
  73. },
  74. "invalid key should err": {
  75. "addr=127.0.0.1:6379,puddle_size=100",
  76. nil,
  77. true,
  78. },
  79. "empty connection string should err": {
  80. "",
  81. nil,
  82. true,
  83. },
  84. }
  85. for reason, testCase := range cases {
  86. options, err := parseRedisConnStr(testCase.InputConnStr)
  87. if testCase.ShouldErr {
  88. assert.Error(t, err, fmt.Sprintf("error cases should return non-nil error for test case %v", reason))
  89. assert.Nil(t, options, fmt.Sprintf("error cases should return nil for redis options for test case %v", reason))
  90. continue
  91. }
  92. assert.NoError(t, err, reason)
  93. assert.EqualValues(t, testCase.OutputOptions, options, reason)
  94. }
  95. }