github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/resource_group_test.go (about) 1 // Copyright (C) 2019-2021 Zilliz. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance 4 // with the License. You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software distributed under the License 9 // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 10 // or implied. See the License for the specific language governing permissions and limitations under the License. 11 12 package client 13 14 import ( 15 "context" 16 "errors" 17 "fmt" 18 "math/rand" 19 "testing" 20 21 "github.com/stretchr/testify/mock" 22 "github.com/stretchr/testify/suite" 23 24 "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" 25 "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" 26 ) 27 28 type ResourceGroupSuite struct { 29 MockSuiteBase 30 } 31 32 func (s *ResourceGroupSuite) TestListResourceGroups() { 33 c := s.client 34 ctx, cancel := context.WithCancel(context.Background()) 35 defer cancel() 36 37 s.Run("normal_run", func() { 38 defer s.resetMock() 39 40 rgs := make([]string, 0, 5) 41 for i := 0; i < 5; i++ { 42 rgs = append(rgs, randStr(10)) 43 } 44 45 s.mock.EXPECT().ListResourceGroups(mock.Anything, mock.AnythingOfType("*milvuspb.ListResourceGroupsRequest")). 46 Return(&milvuspb.ListResourceGroupsResponse{Status: &commonpb.Status{}, ResourceGroups: rgs}, nil) 47 48 result, err := c.ListResourceGroups(ctx) 49 s.NoError(err) 50 s.ElementsMatch(rgs, result) 51 }) 52 53 s.Run("request_fails", func() { 54 defer s.resetMock() 55 56 s.mock.EXPECT().ListResourceGroups(mock.Anything, mock.AnythingOfType("*milvuspb.ListResourceGroupsRequest")). 57 Return(nil, errors.New("mocked grpc error")) 58 59 _, err := c.ListResourceGroups(ctx) 60 s.Error(err) 61 }) 62 63 s.Run("server_return_err", func() { 64 defer s.resetMock() 65 66 s.mock.EXPECT().ListResourceGroups(mock.Anything, mock.AnythingOfType("*milvuspb.ListResourceGroupsRequest")). 67 Return(&milvuspb.ListResourceGroupsResponse{Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_UnexpectedError}}, nil) 68 69 _, err := c.ListResourceGroups(ctx) 70 s.Error(err) 71 }) 72 } 73 74 func (s *ResourceGroupSuite) TestCreateResourceGroup() { 75 c := s.client 76 ctx, cancel := context.WithCancel(context.Background()) 77 defer cancel() 78 79 s.Run("normal_run", func() { 80 defer s.resetMock() 81 rgName := randStr(10) 82 83 s.mock.EXPECT().CreateResourceGroup(mock.Anything, mock.AnythingOfType("*milvuspb.CreateResourceGroupRequest")). 84 Run(func(_ context.Context, req *milvuspb.CreateResourceGroupRequest) { 85 s.Equal(rgName, req.GetResourceGroup()) 86 }). 87 Return(&commonpb.Status{}, nil) 88 89 err := c.CreateResourceGroup(ctx, rgName) 90 s.NoError(err) 91 }) 92 93 s.Run("request_fails", func() { 94 defer s.resetMock() 95 96 rgName := randStr(10) 97 98 s.mock.EXPECT().CreateResourceGroup(mock.Anything, mock.AnythingOfType("*milvuspb.CreateResourceGroupRequest")). 99 Run(func(_ context.Context, req *milvuspb.CreateResourceGroupRequest) { 100 s.Equal(rgName, req.GetResourceGroup()) 101 }). 102 Return(nil, errors.New("mocked grpc error")) 103 104 err := c.CreateResourceGroup(ctx, rgName) 105 s.Error(err) 106 }) 107 108 s.Run("server_return_err", func() { 109 defer s.resetMock() 110 111 rgName := randStr(10) 112 113 s.mock.EXPECT().CreateResourceGroup(mock.Anything, mock.AnythingOfType("*milvuspb.CreateResourceGroupRequest")). 114 Run(func(_ context.Context, req *milvuspb.CreateResourceGroupRequest) { 115 s.Equal(rgName, req.GetResourceGroup()) 116 }). 117 Return(&commonpb.Status{ErrorCode: commonpb.ErrorCode_UnexpectedError}, nil) 118 119 err := c.CreateResourceGroup(ctx, rgName) 120 s.Error(err) 121 }) 122 } 123 124 func (s *ResourceGroupSuite) TestDescribeResourceGroup() { 125 c := s.client 126 ctx, cancel := context.WithCancel(context.Background()) 127 defer cancel() 128 129 s.Run("normal_run", func() { 130 defer s.resetMock() 131 rgName := randStr(10) 132 capacity := rand.Int31n(5) + 1 133 available := rand.Int31n(capacity) 134 135 loadedReplica := make(map[string]int32) 136 l := rand.Intn(3) + 1 137 for i := 0; i < l; i++ { 138 loadedReplica[fmt.Sprintf("coll_%s", randStr(6))] = rand.Int31n(5) + 1 139 } 140 incomingNum := make(map[string]int32) 141 l = rand.Intn(3) + 1 142 for i := 0; i < l; i++ { 143 incomingNum[fmt.Sprintf("coll_%s", randStr(6))] = rand.Int31n(5) + 1 144 } 145 outgoingNum := make(map[string]int32) 146 l = rand.Intn(3) + 1 147 for i := 0; i < l; i++ { 148 outgoingNum[fmt.Sprintf("coll_%s", randStr(6))] = rand.Int31n(5) + 1 149 } 150 151 s.mock.EXPECT().DescribeResourceGroup(mock.Anything, mock.AnythingOfType("*milvuspb.DescribeResourceGroupRequest")). 152 Run(func(_ context.Context, req *milvuspb.DescribeResourceGroupRequest) { 153 s.Equal(rgName, req.GetResourceGroup()) 154 }). 155 Call.Return(func(_ context.Context, req *milvuspb.DescribeResourceGroupRequest) *milvuspb.DescribeResourceGroupResponse { 156 157 return &milvuspb.DescribeResourceGroupResponse{ 158 Status: &commonpb.Status{}, 159 ResourceGroup: &milvuspb.ResourceGroup{ 160 Name: rgName, 161 Capacity: capacity, 162 NumAvailableNode: available, 163 NumLoadedReplica: loadedReplica, 164 NumOutgoingNode: outgoingNum, 165 NumIncomingNode: incomingNum, 166 }, 167 } 168 }, nil) 169 170 result, err := c.DescribeResourceGroup(ctx, rgName) 171 s.NoError(err) 172 s.Equal(rgName, result.Name) 173 s.Equal(capacity, result.Capacity) 174 s.Equal(available, result.AvailableNodesNumber) 175 s.InDeltaMapValues(loadedReplica, result.LoadedReplica, 0) 176 s.InDeltaMapValues(incomingNum, result.IncomingNodeNum, 0) 177 s.InDeltaMapValues(outgoingNum, result.OutgoingNodeNum, 0) 178 }) 179 180 s.Run("request_fails", func() { 181 defer s.resetMock() 182 183 rgName := randStr(10) 184 185 s.mock.EXPECT().DescribeResourceGroup(mock.Anything, mock.AnythingOfType("*milvuspb.DescribeResourceGroupRequest")). 186 Run(func(_ context.Context, req *milvuspb.DescribeResourceGroupRequest) { 187 s.Equal(rgName, req.GetResourceGroup()) 188 }). 189 Return(nil, errors.New("mocked grpc error")) 190 191 _, err := c.DescribeResourceGroup(ctx, rgName) 192 s.Error(err) 193 }) 194 195 s.Run("server_return_err", func() { 196 defer s.resetMock() 197 198 rgName := randStr(10) 199 200 s.mock.EXPECT().DescribeResourceGroup(mock.Anything, mock.AnythingOfType("*milvuspb.DescribeResourceGroupRequest")). 201 Run(func(_ context.Context, req *milvuspb.DescribeResourceGroupRequest) { 202 s.Equal(rgName, req.GetResourceGroup()) 203 }). 204 Return(&milvuspb.DescribeResourceGroupResponse{Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_UnexpectedError}}, nil) 205 206 _, err := c.DescribeResourceGroup(ctx, rgName) 207 s.Error(err) 208 }) 209 } 210 211 func (s *ResourceGroupSuite) TestDropResourceGroup() { 212 c := s.client 213 ctx, cancel := context.WithCancel(context.Background()) 214 defer cancel() 215 216 s.Run("normal_run", func() { 217 defer s.resetMock() 218 rgName := randStr(10) 219 220 s.mock.EXPECT().DropResourceGroup(mock.Anything, mock.AnythingOfType("*milvuspb.DropResourceGroupRequest")). 221 Run(func(_ context.Context, req *milvuspb.DropResourceGroupRequest) { 222 s.Equal(rgName, req.GetResourceGroup()) 223 }). 224 Return(&commonpb.Status{}, nil) 225 226 err := c.DropResourceGroup(ctx, rgName) 227 s.NoError(err) 228 }) 229 230 s.Run("request_fails", func() { 231 defer s.resetMock() 232 233 rgName := randStr(10) 234 235 s.mock.EXPECT().DropResourceGroup(mock.Anything, mock.AnythingOfType("*milvuspb.DropResourceGroupRequest")). 236 Run(func(_ context.Context, req *milvuspb.DropResourceGroupRequest) { 237 s.Equal(rgName, req.GetResourceGroup()) 238 }). 239 Return(nil, errors.New("mocked grpc error")) 240 241 err := c.DropResourceGroup(ctx, rgName) 242 s.Error(err) 243 }) 244 245 s.Run("server_return_err", func() { 246 defer s.resetMock() 247 248 rgName := randStr(10) 249 250 s.mock.EXPECT().DropResourceGroup(mock.Anything, mock.AnythingOfType("*milvuspb.DropResourceGroupRequest")). 251 Run(func(_ context.Context, req *milvuspb.DropResourceGroupRequest) { 252 s.Equal(rgName, req.GetResourceGroup()) 253 }). 254 Return(&commonpb.Status{ErrorCode: commonpb.ErrorCode_UnexpectedError}, nil) 255 256 err := c.DropResourceGroup(ctx, rgName) 257 s.Error(err) 258 }) 259 } 260 261 func (s *ResourceGroupSuite) TestTransferNodes() { 262 c := s.client 263 ctx, cancel := context.WithCancel(context.Background()) 264 defer cancel() 265 266 s.Run("normal_run", func() { 267 defer s.resetMock() 268 sourceRg := randStr(10) 269 targetRg := randStr(10) 270 nodeNum := rand.Int31n(5) + 1 271 272 s.mock.EXPECT().TransferNode(mock.Anything, mock.AnythingOfType("*milvuspb.TransferNodeRequest")). 273 Run(func(_ context.Context, req *milvuspb.TransferNodeRequest) { 274 s.Equal(sourceRg, req.GetSourceResourceGroup()) 275 s.Equal(targetRg, req.GetTargetResourceGroup()) 276 s.Equal(nodeNum, req.GetNumNode()) 277 }). 278 Return(&commonpb.Status{}, nil) 279 280 err := c.TransferNode(ctx, sourceRg, targetRg, nodeNum) 281 s.NoError(err) 282 }) 283 284 s.Run("request_fails", func() { 285 defer s.resetMock() 286 287 sourceRg := randStr(10) 288 targetRg := randStr(10) 289 nodeNum := rand.Int31n(5) + 1 290 291 s.mock.EXPECT().TransferNode(mock.Anything, mock.AnythingOfType("*milvuspb.TransferNodeRequest")). 292 Run(func(_ context.Context, req *milvuspb.TransferNodeRequest) { 293 s.Equal(sourceRg, req.GetSourceResourceGroup()) 294 s.Equal(targetRg, req.GetTargetResourceGroup()) 295 s.Equal(nodeNum, req.GetNumNode()) 296 }). 297 Return(nil, errors.New("mocked grpc error")) 298 299 err := c.TransferNode(ctx, sourceRg, targetRg, nodeNum) 300 s.Error(err) 301 }) 302 303 s.Run("server_return_err", func() { 304 defer s.resetMock() 305 306 sourceRg := randStr(10) 307 targetRg := randStr(10) 308 nodeNum := rand.Int31n(5) + 1 309 310 s.mock.EXPECT().TransferNode(mock.Anything, mock.AnythingOfType("*milvuspb.TransferNodeRequest")). 311 Run(func(_ context.Context, req *milvuspb.TransferNodeRequest) { 312 s.Equal(sourceRg, req.GetSourceResourceGroup()) 313 s.Equal(targetRg, req.GetTargetResourceGroup()) 314 s.Equal(nodeNum, req.GetNumNode()) 315 }). 316 Return(&commonpb.Status{ErrorCode: commonpb.ErrorCode_UnexpectedError}, nil) 317 318 err := c.TransferNode(ctx, sourceRg, targetRg, nodeNum) 319 s.Error(err) 320 }) 321 } 322 323 func (s *ResourceGroupSuite) TestTransferReplica() { 324 c := s.client 325 ctx, cancel := context.WithCancel(context.Background()) 326 defer cancel() 327 328 s.Run("normal_run", func() { 329 defer s.resetMock() 330 collName := fmt.Sprintf("coll_%s", randStr(6)) 331 sourceRg := randStr(10) 332 targetRg := randStr(10) 333 replicaNum := rand.Int63n(5) + 1 334 335 s.mock.EXPECT().TransferReplica(mock.Anything, mock.AnythingOfType("*milvuspb.TransferReplicaRequest")). 336 Run(func(_ context.Context, req *milvuspb.TransferReplicaRequest) { 337 s.Equal(sourceRg, req.GetSourceResourceGroup()) 338 s.Equal(targetRg, req.GetTargetResourceGroup()) 339 s.Equal(replicaNum, req.GetNumReplica()) 340 }). 341 Return(&commonpb.Status{}, nil) 342 343 err := c.TransferReplica(ctx, sourceRg, targetRg, collName, replicaNum) 344 s.NoError(err) 345 }) 346 347 s.Run("request_fails", func() { 348 defer s.resetMock() 349 350 collName := fmt.Sprintf("coll_%s", randStr(6)) 351 sourceRg := randStr(10) 352 targetRg := randStr(10) 353 replicaNum := rand.Int63n(5) + 1 354 355 s.mock.EXPECT().TransferReplica(mock.Anything, mock.AnythingOfType("*milvuspb.TransferReplicaRequest")). 356 Run(func(_ context.Context, req *milvuspb.TransferReplicaRequest) { 357 s.Equal(sourceRg, req.GetSourceResourceGroup()) 358 s.Equal(targetRg, req.GetTargetResourceGroup()) 359 s.Equal(replicaNum, req.GetNumReplica()) 360 }). 361 Return(nil, errors.New("mocked grpc error")) 362 363 err := c.TransferReplica(ctx, sourceRg, targetRg, collName, replicaNum) 364 s.Error(err) 365 }) 366 367 s.Run("server_return_err", func() { 368 defer s.resetMock() 369 370 collName := fmt.Sprintf("coll_%s", randStr(6)) 371 sourceRg := randStr(10) 372 targetRg := randStr(10) 373 replicaNum := rand.Int63n(5) + 1 374 375 s.mock.EXPECT().TransferReplica(mock.Anything, mock.AnythingOfType("*milvuspb.TransferReplicaRequest")). 376 Run(func(_ context.Context, req *milvuspb.TransferReplicaRequest) { 377 s.Equal(sourceRg, req.GetSourceResourceGroup()) 378 s.Equal(targetRg, req.GetTargetResourceGroup()) 379 s.Equal(replicaNum, req.GetNumReplica()) 380 }). 381 Return(&commonpb.Status{ErrorCode: commonpb.ErrorCode_UnexpectedError}, nil) 382 383 err := c.TransferReplica(ctx, sourceRg, targetRg, collName, replicaNum) 384 s.Error(err) 385 }) 386 } 387 388 func TestResourceGroupSuite(t *testing.T) { 389 suite.Run(t, new(ResourceGroupSuite)) 390 }