github.com/weaviate/weaviate@v1.24.6/usecases/scaler/mocks_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package scaler 13 14 import ( 15 "context" 16 "errors" 17 "io" 18 "sort" 19 20 "github.com/sirupsen/logrus" 21 "github.com/sirupsen/logrus/hooks/test" 22 "github.com/stretchr/testify/mock" 23 "github.com/weaviate/weaviate/entities/backup" 24 "github.com/weaviate/weaviate/usecases/sharding" 25 ) 26 27 const ( 28 localNode = "N1" 29 ) 30 31 var ( 32 anyVal = mock.Anything 33 errAny = errors.New("any error") 34 ) 35 36 type fakeFactory struct { 37 LocalNode string 38 Nodes []string 39 ShardingState fakeShardingState 40 NodeHostMap map[string]string 41 Source *fakeSource 42 Client *fakeClient 43 logger logrus.FieldLogger 44 } 45 46 func newFakeFactory() *fakeFactory { 47 nodeHostMap := map[string]string{"N1": "H1", "N2": "H2", "N3": "H3", "N4": "H4"} 48 nodes := []string{"N1", "N2", "N3", "N4"} 49 logger, _ := test.NewNullLogger() 50 return &fakeFactory{ 51 LocalNode: localNode, 52 Nodes: nodes, 53 ShardingState: fakeShardingState{ 54 LocalNode: localNode, 55 M: map[string][]string{ 56 "S1": {"N1"}, 57 "S3": {"N3", "N4"}, 58 }, 59 }, 60 NodeHostMap: nodeHostMap, 61 Source: &fakeSource{}, 62 Client: &fakeClient{}, 63 logger: logger, 64 } 65 } 66 67 func (f *fakeFactory) Scaler(dataPath string) *Scaler { 68 nodeResolver := newFakeNodeResolver(f.LocalNode, f.NodeHostMap) 69 scaler := New( 70 nodeResolver, 71 f.Source, 72 f.Client, 73 f.logger, 74 dataPath) 75 scaler.SetSchemaManager(&f.ShardingState) 76 return scaler 77 } 78 79 type fakeShardingState struct { 80 LocalNode string 81 M map[string][]string 82 } 83 84 func (f *fakeShardingState) CopyShardingState(class string) *sharding.State { 85 if len(f.M) == 0 { 86 return nil 87 } 88 state := sharding.State{} 89 state.Physical = make(map[string]sharding.Physical) 90 for shard, nodes := range f.M { 91 state.Physical[shard] = sharding.Physical{BelongsToNodes: nodes} 92 } 93 state.SetLocalName(f.LocalNode) 94 return &state 95 } 96 97 // func newShardingState(nShard, rf int, localNode string) fakeShardingState { 98 // m := make(map[string][]string) 99 // for i := 0; i < nShard; i++ { 100 // replicas := make([]string, rf) 101 // for j := 0; j < rf; j++ { 102 // replicas[j] = "N" + strconv.Itoa(j+1) 103 // } 104 // m["S"+strconv.Itoa(i+1)] = replicas 105 // } 106 // return fakeShardingState{M: m, LocalNode: localNode} 107 // } 108 109 // node resolver 110 type fakeNodeResolver struct { 111 NodeName string 112 M map[string]string 113 } 114 115 func newFakeNodeResolver(localNode string, nodeHostMap map[string]string) *fakeNodeResolver { 116 return &fakeNodeResolver{NodeName: localNode, M: nodeHostMap} 117 } 118 119 func (r *fakeNodeResolver) NodeHostname(nodeName string) (string, bool) { 120 host, ok := r.M[nodeName] 121 return host, ok 122 } 123 124 func (r *fakeNodeResolver) Candidates() []string { 125 xs := make([]string, 0, len(r.M)) 126 for k := range r.M { 127 xs = append(xs, k) 128 } 129 sort.Strings(xs) 130 return xs 131 } 132 133 func (r *fakeNodeResolver) LocalName() string { 134 return r.NodeName 135 } 136 137 type fakeSource struct { 138 mock.Mock 139 } 140 141 func (s *fakeSource) ReleaseBackup(ctx context.Context, id, class string) error { 142 args := s.Called(ctx, id, class) 143 return args.Error(0) 144 } 145 146 func (s *fakeSource) ShardsBackup( 147 ctx context.Context, id, class string, shards []string, 148 ) (_ backup.ClassDescriptor, err error) { 149 args := s.Called(ctx, id, class, shards) 150 return args.Get(0).(backup.ClassDescriptor), args.Error(1) 151 } 152 153 type fakeClient struct { 154 mock.Mock 155 } 156 157 func (f *fakeClient) PutFile(ctx context.Context, host, class, 158 shard, filename string, payload io.ReadSeekCloser, 159 ) error { 160 args := f.Called(ctx, host, class, shard, filename, payload) 161 return args.Error(0) 162 } 163 164 func (f *fakeClient) CreateShard(ctx context.Context, host, class, name string) error { 165 args := f.Called(ctx, host, class, name) 166 return args.Error(0) 167 } 168 169 func (f *fakeClient) ReInitShard(ctx context.Context, host, class, shard string, 170 ) error { 171 args := f.Called(ctx, host, class, shard) 172 return args.Error(0) 173 } 174 175 func (f *fakeClient) IncreaseReplicationFactor(ctx context.Context, 176 host, class string, dist ShardDist, 177 ) error { 178 args := f.Called(ctx, host, class, dist) 179 return args.Error(0) 180 }