github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/debug/test/test.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package debugtest contains test helper methods
    22  package debugtest
    23  
    24  import (
    25  	"testing"
    26  
    27  	clusterclient "github.com/m3db/m3/src/cluster/client"
    28  	"github.com/m3db/m3/src/cluster/generated/proto/placementpb"
    29  	"github.com/m3db/m3/src/cluster/kv"
    30  	clusterplacement "github.com/m3db/m3/src/cluster/placement"
    31  	"github.com/m3db/m3/src/cluster/placementhandler"
    32  	"github.com/m3db/m3/src/cluster/services"
    33  	"github.com/m3db/m3/src/query/api/v1/handler/namespace"
    34  	"github.com/m3db/m3/src/x/instrument"
    35  
    36  	"github.com/golang/mock/gomock"
    37  	"github.com/stretchr/testify/require"
    38  )
    39  
    40  // NewTestHandlerOptsAndClient returns a new test handler.
    41  func NewTestHandlerOptsAndClient(
    42  	t *testing.T,
    43  ) (
    44  	placementhandler.HandlerOptions,
    45  	*kv.MockStore,
    46  	*clusterclient.MockClient,
    47  ) {
    48  	ctrl := gomock.NewController(t)
    49  
    50  	placementProto := &placementpb.Placement{
    51  		Instances: map[string]*placementpb.Instance{
    52  			"host1": {
    53  				Id:             "host1",
    54  				IsolationGroup: "rack1",
    55  				Zone:           "test",
    56  				Weight:         1,
    57  				Endpoint:       "http://host1:1234",
    58  				Hostname:       "host1",
    59  				Port:           1234,
    60  			},
    61  			"host2": {
    62  				Id:             "host2",
    63  				IsolationGroup: "rack1",
    64  				Zone:           "test",
    65  				Weight:         1,
    66  				Endpoint:       "http://host2:1234",
    67  				Hostname:       "host2",
    68  				Port:           1234,
    69  			},
    70  		},
    71  	}
    72  
    73  	mockPlacement := clusterplacement.NewMockPlacement(ctrl)
    74  	mockPlacement.EXPECT().Proto().Return(placementProto, nil).AnyTimes()
    75  	mockPlacement.EXPECT().Version().Return(0).AnyTimes()
    76  
    77  	return NewTestHandlerOptsAndClientWithPlacement(t, ctrl, mockPlacement)
    78  }
    79  
    80  // NewTestHandlerOptsAndClientWithPlacement returns a new test handler with supplied placement.
    81  func NewTestHandlerOptsAndClientWithPlacement(
    82  	t *testing.T,
    83  	ctrl *gomock.Controller,
    84  	placement clusterplacement.Placement,
    85  ) (
    86  	placementhandler.HandlerOptions,
    87  	*kv.MockStore,
    88  	*clusterclient.MockClient,
    89  ) {
    90  	mockClient := clusterclient.NewMockClient(ctrl)
    91  	require.NotNil(t, mockClient)
    92  
    93  	mockKV := kv.NewMockStore(ctrl)
    94  	require.NotNil(t, mockKV)
    95  
    96  	mockServices := services.NewMockServices(ctrl)
    97  	require.NotNil(t, mockServices)
    98  
    99  	mockPlacementService := clusterplacement.NewMockService(ctrl)
   100  	require.NotNil(t, mockPlacementService)
   101  
   102  	mockClient.EXPECT().Services(gomock.Not(nil)).Return(mockServices, nil).AnyTimes()
   103  	mockServices.EXPECT().PlacementService(gomock.Not(nil), gomock.Not(nil)).Return(mockPlacementService, nil).AnyTimes()
   104  	mockPlacementService.EXPECT().Placement().Return(placement, nil).AnyTimes()
   105  
   106  	mockClient.EXPECT().KV().Return(mockKV, nil).AnyTimes()
   107  	mockKV.EXPECT().Get(namespace.M3DBNodeNamespacesKey).Return(nil, kv.ErrNotFound).AnyTimes()
   108  
   109  	handlerOpts, err := placementhandler.NewHandlerOptions(
   110  		mockClient, clusterplacement.Configuration{}, nil, instrument.NewOptions())
   111  	require.NoError(t, err)
   112  
   113  	return handlerOpts, mockKV, mockClient
   114  }