github.com/mailru/activerecord@v1.12.2/pkg/activerecord/cluster_test.go (about) 1 package activerecord 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/google/go-cmp/cmp/cmpopts" 8 "github.com/stretchr/testify/mock" 9 "gotest.tools/assert" 10 "gotest.tools/assert/cmp" 11 ) 12 13 func TestGetClusterInfoFromCfg(t *testing.T) { 14 ctx := context.Background() 15 16 type args struct { 17 ctx context.Context 18 path string 19 globs MapGlobParam 20 optionCreator func(ShardInstanceConfig) (OptionInterface, error) 21 } 22 tests := []struct { 23 name string 24 args args 25 mocks func(*testing.T, *MockConfig) 26 want *Cluster 27 wantErr bool 28 }{ 29 { 30 name: "cluster hosts from root path (no master or replica keys)", 31 mocks: func(t *testing.T, mockConfig *MockConfig) { 32 mockConfig.EXPECT().GetIntIfExists(mock.Anything, mock.Anything).Return(0, false) 33 mockConfig.EXPECT().GetDuration(mock.Anything, mock.Anything, mock.Anything).Return(0) 34 mockConfig.EXPECT().GetInt(mock.Anything, mock.Anything, mock.Anything).Return(0) 35 mockConfig.EXPECT().GetDurationIfExists(mock.Anything, mock.Anything).Return(0, false) 36 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/master").Return("", false) 37 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig").Return("host1,host2", true) 38 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/replica").Return("", false) 39 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/user").Return("", false) 40 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/password").Return("", false) 41 }, 42 args: args{ 43 ctx: ctx, 44 path: "testconfig", 45 globs: MapGlobParam{}, 46 optionCreator: func(c ShardInstanceConfig) (OptionInterface, error) { 47 return &TestOptions{hash: c.Addr}, nil 48 }, 49 }, 50 want: &Cluster{ 51 shards: []Shard{ 52 { 53 Masters: []ShardInstance{ 54 { 55 ParamsID: "host1", 56 Config: ShardInstanceConfig{ 57 Mode: ModeMaster, 58 Addr: "host1", 59 }, 60 Options: &TestOptions{hash: "host1"}, 61 }, 62 { 63 ParamsID: "host2", 64 Config: ShardInstanceConfig{ 65 Mode: ModeMaster, 66 Addr: "host2", 67 }, 68 Options: &TestOptions{hash: "host2"}, 69 }, 70 }, 71 Replicas: []ShardInstance{}, 72 }, 73 }, 74 }, 75 }, 76 { 77 name: "cluster hosts from master and replica keys path", 78 mocks: func(t *testing.T, mockConfig *MockConfig) { 79 mockConfig.EXPECT().GetIntIfExists(mock.Anything, mock.Anything).Return(0, false) 80 mockConfig.EXPECT().GetDuration(mock.Anything, mock.Anything, mock.Anything).Return(0) 81 mockConfig.EXPECT().GetInt(mock.Anything, mock.Anything, mock.Anything).Return(0) 82 mockConfig.EXPECT().GetDurationIfExists(mock.Anything, mock.Anything).Return(0, false) 83 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/master").Return("host2", true) 84 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/replica").Return("host1", true) 85 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/user").Return("", false) 86 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/password").Return("", false) 87 }, 88 args: args{ 89 ctx: ctx, 90 path: "testconfig", 91 globs: MapGlobParam{}, 92 optionCreator: func(c ShardInstanceConfig) (OptionInterface, error) { 93 return &TestOptions{hash: c.Addr}, nil 94 }, 95 }, 96 want: &Cluster{ 97 shards: []Shard{ 98 { 99 Masters: []ShardInstance{ 100 { 101 ParamsID: "host2", 102 Config: ShardInstanceConfig{ 103 Mode: ModeMaster, 104 Addr: "host2", 105 }, 106 Options: &TestOptions{hash: "host2"}, 107 }, 108 }, 109 Replicas: []ShardInstance{ 110 { 111 ParamsID: "host1", 112 Config: ShardInstanceConfig{ 113 Mode: ModeReplica, 114 Addr: "host1", 115 }, 116 Options: &TestOptions{hash: "host1", mode: ModeReplica}, 117 }, 118 }, 119 }, 120 }, 121 }, 122 }, 123 { 124 name: "cluster hosts from root path and replica keys path", 125 mocks: func(t *testing.T, mockConfig *MockConfig) { 126 mockConfig.EXPECT().GetIntIfExists(mock.Anything, mock.Anything).Return(0, false) 127 mockConfig.EXPECT().GetDuration(mock.Anything, mock.Anything, mock.Anything).Return(0) 128 mockConfig.EXPECT().GetInt(mock.Anything, mock.Anything, mock.Anything).Return(0) 129 mockConfig.EXPECT().GetDurationIfExists(mock.Anything, mock.Anything).Return(0, false) 130 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/master").Return("", false) 131 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig").Return("host1", true) 132 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/replica").Return("host2", true) 133 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/user").Return("", false) 134 mockConfig.EXPECT().GetStringIfExists(mock.Anything, "testconfig/password").Return("", false) 135 }, 136 args: args{ 137 ctx: ctx, 138 path: "testconfig", 139 globs: MapGlobParam{}, 140 optionCreator: func(c ShardInstanceConfig) (OptionInterface, error) { 141 return &TestOptions{hash: c.Addr}, nil 142 }, 143 }, 144 want: &Cluster{ 145 shards: []Shard{ 146 { 147 Masters: []ShardInstance{ 148 { 149 ParamsID: "host1", 150 Config: ShardInstanceConfig{ 151 Mode: ModeMaster, 152 Addr: "host1", 153 }, 154 Options: &TestOptions{hash: "host1"}, 155 }, 156 }, 157 Replicas: []ShardInstance{ 158 { 159 ParamsID: "host2", 160 Config: ShardInstanceConfig{ 161 Mode: ModeReplica, 162 Addr: "host2", 163 }, 164 Options: &TestOptions{hash: "host2", mode: ModeReplica}, 165 }, 166 }, 167 }, 168 }, 169 }, 170 }, 171 } 172 for _, tt := range tests { 173 t.Run(tt.name, func(t *testing.T) { 174 mockConfig := NewMockConfig(t) 175 176 ReinitActiveRecord( 177 WithConfig(mockConfig), 178 ) 179 180 if tt.mocks != nil { 181 tt.mocks(t, mockConfig) 182 } 183 184 got, err := GetClusterInfoFromCfg(tt.args.ctx, tt.args.path, tt.args.globs, tt.args.optionCreator) 185 if (err != nil) != tt.wantErr { 186 t.Errorf("GetClusterInfoFromCfg() error = %v, wantErr %v", err, tt.wantErr) 187 return 188 } 189 assert.Check(t, cmp.DeepEqual(got.ShardInstances(0), tt.want.ShardInstances(0), cmpopts.IgnoreUnexported(Shard{}, TestOptions{})), "GetClusterInfoFromCfg() got = %v, want %v", got, tt.want) 190 }) 191 } 192 }