github.com/craicoverflow/tyk@v2.9.6-rc3+incompatible/storage/redis_cluster_test.go (about)

     1  package storage
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/TykTechnologies/tyk/config"
     7  )
     8  
     9  func TestRedisClusterGetMultiKey(t *testing.T) {
    10  	t.Skip()
    11  
    12  	keys := []string{"first", "second"}
    13  	r := RedisCluster{KeyPrefix: "test-cluster"}
    14  	for _, v := range keys {
    15  		r.DeleteKey(v)
    16  	}
    17  	_, err := r.GetMultiKey(keys)
    18  	if err != ErrKeyNotFound {
    19  		t.Errorf("expected %v got %v", ErrKeyNotFound, err)
    20  	}
    21  	err = r.SetKey(keys[0], keys[0], 0)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	v, err := r.GetMultiKey([]string{"first", "second"})
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	if v[0] != keys[0] {
    31  		t.Errorf("expected %s got %s", keys[0], v[0])
    32  	}
    33  }
    34  
    35  func TestRedisAddressConfiguration(t *testing.T) {
    36  
    37  	t.Run("Host but no port", func(t *testing.T) {
    38  		cfg := config.StorageOptionsConf{Host: "host"}
    39  		if len(getRedisAddrs(cfg)) != 0 {
    40  			t.Fatal("Port is 0, there is no valid addr")
    41  		}
    42  	})
    43  
    44  	t.Run("Port but no host", func(t *testing.T) {
    45  		cfg := config.StorageOptionsConf{Port: 30000}
    46  
    47  		addrs := getRedisAddrs(cfg)
    48  		if addrs[0] != ":30000" || len(addrs) != 1 {
    49  			t.Fatal("Port is valid, it is a valid addr")
    50  		}
    51  	})
    52  
    53  	t.Run("addrs parameter should have precedence", func(t *testing.T) {
    54  		cfg := config.StorageOptionsConf{Host: "host", Port: 30000}
    55  
    56  		addrs := getRedisAddrs(cfg)
    57  		if addrs[0] != "host:30000" || len(addrs) != 1 {
    58  			t.Fatal("Wrong address")
    59  		}
    60  
    61  		cfg.Addrs = []string{"override:30000"}
    62  
    63  		addrs = getRedisAddrs(cfg)
    64  		if addrs[0] != "override:30000" || len(addrs) != 1 {
    65  			t.Fatal("Wrong address")
    66  		}
    67  	})
    68  
    69  	t.Run("Default addresses", func(t *testing.T) {
    70  		opts := &RedisOpts{}
    71  		simpleOpts := opts.simple()
    72  
    73  		if simpleOpts.Addr != "127.0.0.1:6379" {
    74  			t.Fatal("Wrong default single node address")
    75  		}
    76  
    77  		opts.Addrs = []string{}
    78  		clusterOpts := opts.cluster()
    79  
    80  		if clusterOpts.Addrs[0] != "127.0.0.1:6379" || len(clusterOpts.Addrs) != 1 {
    81  			t.Fatal("Wrong default cluster mode address")
    82  		}
    83  
    84  		opts.Addrs = []string{}
    85  		failoverOpts := opts.failover()
    86  
    87  		if failoverOpts.SentinelAddrs[0] != "127.0.0.1:26379" || len(failoverOpts.SentinelAddrs) != 1 {
    88  			t.Fatal("Wrong default sentinel mode address")
    89  		}
    90  	})
    91  }