go-micro.dev/v5@v5.12.0/cache/redis/options_test.go (about)

     1  package redis
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  
     8  	rclient "github.com/go-redis/redis/v8"
     9  	"go-micro.dev/v5/cache"
    10  )
    11  
    12  func Test_newUniversalClient(t *testing.T) {
    13  	type fields struct {
    14  		options cache.Options
    15  	}
    16  	type wantValues struct {
    17  		username string
    18  		password string
    19  		address  string
    20  	}
    21  
    22  	tests := []struct {
    23  		name   string
    24  		fields fields
    25  		want   wantValues
    26  	}{
    27  		{name: "No Url", fields: fields{options: cache.Options{}},
    28  			want: wantValues{
    29  				username: "",
    30  				password: "",
    31  				address:  "127.0.0.1:6379",
    32  			}},
    33  		{name: "legacy Url", fields: fields{options: cache.Options{Address: "127.0.0.1:6379"}},
    34  			want: wantValues{
    35  				username: "",
    36  				password: "",
    37  				address:  "127.0.0.1:6379",
    38  			}},
    39  		{name: "New Url", fields: fields{options: cache.Options{Address: "redis://127.0.0.1:6379"}},
    40  			want: wantValues{
    41  				username: "",
    42  				password: "",
    43  				address:  "127.0.0.1:6379",
    44  			}},
    45  		{name: "Url with Pwd", fields: fields{options: cache.Options{Address: "redis://:password@redis:6379"}},
    46  			want: wantValues{
    47  				username: "",
    48  				password: "password",
    49  				address:  "redis:6379",
    50  			}},
    51  		{name: "Url with username and Pwd", fields: fields{
    52  			options: cache.Options{Address: "redis://username:password@redis:6379"}},
    53  			want: wantValues{
    54  				username: "username",
    55  				password: "password",
    56  				address:  "redis:6379",
    57  			}},
    58  
    59  		{name: "Sentinel Failover client", fields: fields{
    60  			options: cache.Options{
    61  				Context: context.WithValue(
    62  					context.TODO(), redisOptionsContextKey{},
    63  					rclient.UniversalOptions{MasterName: "master-name"}),
    64  			}},
    65  			want: wantValues{
    66  				username: "",
    67  				password: "",
    68  				address:  "FailoverClient", // <- Placeholder set by NewFailoverClient
    69  			}},
    70  	}
    71  	for _, tt := range tests {
    72  		t.Run(tt.name, func(t *testing.T) {
    73  			univClient := newUniversalClient(tt.fields.options)
    74  			client, ok := univClient.(*rclient.Client)
    75  			if !ok {
    76  				t.Errorf("newUniversalClient() expect a *redis.Client")
    77  				return
    78  			}
    79  			if client.Options().Addr != tt.want.address {
    80  				t.Errorf("newUniversalClient() Address = %v, want address %v", client.Options().Addr, tt.want.address)
    81  			}
    82  			if client.Options().Password != tt.want.password {
    83  				t.Errorf("newUniversalClient() password = %v, want password %v", client.Options().Password, tt.want.password)
    84  			}
    85  			if client.Options().Username != tt.want.username {
    86  				t.Errorf("newUniversalClient() username = %v, want username %v", client.Options().Username, tt.want.username)
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func Test_newUniversalClientCluster(t *testing.T) {
    93  	type fields struct {
    94  		options cache.Options
    95  	}
    96  	type wantValues struct {
    97  		username string
    98  		password string
    99  		addrs    []string
   100  	}
   101  
   102  	tests := []struct {
   103  		name   string
   104  		fields fields
   105  		want   wantValues
   106  	}{
   107  		{name: "Addrs in redis options", fields: fields{
   108  			options: cache.Options{
   109  				Address: "127.0.0.1:6379", // <- ignored
   110  				Context: context.WithValue(
   111  					context.TODO(), redisOptionsContextKey{},
   112  					rclient.UniversalOptions{Addrs: []string{"127.0.0.1:6381", "127.0.0.1:6382"}}),
   113  			}},
   114  			want: wantValues{
   115  				username: "",
   116  				password: "",
   117  				addrs:    []string{"127.0.0.1:6381", "127.0.0.1:6382"},
   118  			}},
   119  	}
   120  	for _, tt := range tests {
   121  		t.Run(tt.name, func(t *testing.T) {
   122  			univClient := newUniversalClient(tt.fields.options)
   123  			client, ok := univClient.(*rclient.ClusterClient)
   124  			if !ok {
   125  				t.Errorf("newUniversalClient() expect a *redis.ClusterClient")
   126  				return
   127  			}
   128  			if !reflect.DeepEqual(client.Options().Addrs, tt.want.addrs) {
   129  				t.Errorf("newUniversalClient() Addrs = %v, want addrs %v", client.Options().Addrs, tt.want.addrs)
   130  			}
   131  			if client.Options().Password != tt.want.password {
   132  				t.Errorf("newUniversalClient() password = %v, want password %v", client.Options().Password, tt.want.password)
   133  			}
   134  			if client.Options().Username != tt.want.username {
   135  				t.Errorf("newUniversalClient() username = %v, want username %v", client.Options().Username, tt.want.username)
   136  			}
   137  		})
   138  	}
   139  }