github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/topic/update_test.go (about)

     1  // Copyright (c) 2020 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 topic
    22  
    23  import (
    24  	"bytes"
    25  	"io/ioutil"
    26  	"net/http"
    27  	"net/http/httptest"
    28  	"testing"
    29  
    30  	"github.com/m3db/m3/src/cmd/services/m3query/config"
    31  	"github.com/m3db/m3/src/msg/generated/proto/topicpb"
    32  	"github.com/m3db/m3/src/msg/topic"
    33  	"github.com/m3db/m3/src/query/generated/proto/admin"
    34  	"github.com/m3db/m3/src/x/instrument"
    35  
    36  	"github.com/golang/mock/gomock"
    37  	"github.com/stretchr/testify/assert"
    38  	"github.com/stretchr/testify/require"
    39  )
    40  
    41  const (
    42  	testTopicName = "test-topic"
    43  )
    44  
    45  func TestPlacementUpdateHandler(t *testing.T) {
    46  	ctrl := gomock.NewController(t)
    47  	defer ctrl.Finish()
    48  
    49  	mockService := setupTest(t, ctrl)
    50  	handler := newUpdateHandler(nil, config.Configuration{}, instrument.NewOptions())
    51  	handler.(*UpdateHandler).serviceFn = testServiceFn(mockService)
    52  
    53  	consumerSvc := &topicpb.ConsumerService{
    54  		ServiceId: &topicpb.ServiceID{
    55  			Name:        "svc",
    56  			Environment: "env",
    57  			Zone:        "zone",
    58  		},
    59  		ConsumptionType: topicpb.ConsumptionType_SHARED,
    60  	}
    61  
    62  	// Test topic init success
    63  	updateProto := admin.TopicUpdateRequest{
    64  		ConsumerServices: []*topicpb.ConsumerService{consumerSvc},
    65  		Version:          2,
    66  	}
    67  	w := httptest.NewRecorder()
    68  	b := bytes.NewBuffer(nil)
    69  	require.NoError(t, jsonMarshaler.Marshal(b, &updateProto))
    70  	req := httptest.NewRequest("PUT", "/topic/update", b)
    71  	req.Header.Add("topic-name", testTopicName)
    72  	require.NotNil(t, req)
    73  
    74  	returnTopic := topic.NewTopic().
    75  		SetName(testTopicName).
    76  		SetNumberOfShards(256).
    77  		SetVersion(1).SetConsumerServices([]topic.ConsumerService{
    78  		func() topic.ConsumerService {
    79  			svc, err := topic.NewConsumerServiceFromProto(consumerSvc)
    80  			assert.NoError(t, err)
    81  			return svc
    82  		}(),
    83  	})
    84  
    85  	mockService.EXPECT().
    86  		Get(testTopicName).
    87  		Return(returnTopic, nil)
    88  
    89  	mockService.
    90  		EXPECT().
    91  		CheckAndSet(returnTopic, 2).
    92  		Return(
    93  			returnTopic.SetVersion(3),
    94  			nil,
    95  		)
    96  
    97  	handler.ServeHTTP(w, req)
    98  	resp := w.Result()
    99  	body, err := ioutil.ReadAll(resp.Body)
   100  	require.NoError(t, err)
   101  	require.Equal(t, http.StatusOK, resp.StatusCode)
   102  
   103  	var respProto admin.TopicGetResponse
   104  	require.NoError(t, jsonUnmarshaler.Unmarshal(bytes.NewBuffer(body), &respProto))
   105  
   106  	validateEqualTopicProto(t, topicpb.Topic{
   107  		Name:             testTopicName,
   108  		NumberOfShards:   256,
   109  		ConsumerServices: []*topicpb.ConsumerService{consumerSvc},
   110  	}, *respProto.Topic)
   111  
   112  	require.Equal(t, uint32(3), respProto.Version)
   113  
   114  	// Test removing all consumers.
   115  	updateProto = admin.TopicUpdateRequest{
   116  		ConsumerServices: []*topicpb.ConsumerService{},
   117  		Version:          3,
   118  	}
   119  	w = httptest.NewRecorder()
   120  	b = bytes.NewBuffer(nil)
   121  	require.NoError(t, jsonMarshaler.Marshal(b, &updateProto))
   122  	req = httptest.NewRequest("PUT", "/topic/update", b)
   123  	req.Header.Add("topic-name", testTopicName)
   124  	require.NotNil(t, req)
   125  
   126  	returnTopic = returnTopic.SetConsumerServices([]topic.ConsumerService{})
   127  
   128  	mockService.EXPECT().
   129  		Get(testTopicName).
   130  		Return(returnTopic, nil)
   131  
   132  	mockService.
   133  		EXPECT().
   134  		CheckAndSet(returnTopic, 3).
   135  		Return(
   136  			returnTopic.SetVersion(4),
   137  			nil,
   138  		)
   139  
   140  	handler.ServeHTTP(w, req)
   141  	resp = w.Result()
   142  	body, err = ioutil.ReadAll(resp.Body)
   143  	require.NoError(t, err)
   144  	require.Equal(t, http.StatusOK, resp.StatusCode)
   145  
   146  	require.NoError(t, jsonUnmarshaler.Unmarshal(bytes.NewBuffer(body), &respProto))
   147  
   148  	validateEqualTopicProto(t, topicpb.Topic{
   149  		Name:             testTopicName,
   150  		NumberOfShards:   256,
   151  		ConsumerServices: []*topicpb.ConsumerService{},
   152  	}, *respProto.Topic)
   153  
   154  	require.Equal(t, uint32(4), respProto.Version)
   155  }