github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/client/resource_manager_client_test.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package client
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  
    20  	"github.com/golang/mock/gomock"
    21  	"github.com/pingcap/tiflow/engine/enginepb"
    22  	"github.com/pingcap/tiflow/engine/enginepb/mock"
    23  	"github.com/pingcap/tiflow/pkg/errors"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestCreateResource(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	mockCli := mock.NewMockResourceManagerClient(gomock.NewController(t))
    31  	resourceCli := NewResourceManagerClient(mockCli)
    32  
    33  	req := &enginepb.CreateResourceRequest{
    34  		ResourceId:      "/local/resource-1",
    35  		CreatorExecutor: "executor-1",
    36  		JobId:           "job-1",
    37  		CreatorWorkerId: "worker-1",
    38  	}
    39  	mockCli.EXPECT().CreateResource(gomock.Any(), gomock.Eq(req)).
    40  		Return(&enginepb.CreateResourceResponse{}, nil).Times(1)
    41  	require.NoError(t, resourceCli.CreateResource(context.Background(), req))
    42  
    43  	mockCli.EXPECT().CreateResource(gomock.Any(), gomock.Eq(req)).
    44  		Return(nil, errors.ErrInvalidArgument.GenWithStackByArgs("resource-id")).Times(1)
    45  	require.True(t, errors.Is(resourceCli.CreateResource(context.Background(), req), errors.ErrInvalidArgument))
    46  
    47  	mockCli.EXPECT().CreateResource(gomock.Any(), gomock.Eq(req)).
    48  		Return(nil, errors.ErrResourceAlreadyExists.GenWithStackByArgs(req.ResourceId)).Times(1)
    49  	mockCli.EXPECT().QueryResource(gomock.Any(), gomock.Eq(&enginepb.QueryResourceRequest{
    50  		ResourceKey: &enginepb.ResourceKey{
    51  			JobId:      "job-1",
    52  			ResourceId: "/local/resource-1",
    53  		},
    54  	})).Return(&enginepb.QueryResourceResponse{
    55  		CreatorExecutor: "executor-1",
    56  		JobId:           "job-1",
    57  		CreatorWorkerId: "worker-1",
    58  	}, nil).Times(1)
    59  	require.NoError(t, resourceCli.CreateResource(context.Background(), req))
    60  
    61  	mockCli.EXPECT().CreateResource(gomock.Any(), gomock.Eq(req)).
    62  		Return(nil, errors.ErrResourceAlreadyExists.GenWithStackByArgs(req.ResourceId)).Times(1)
    63  	mockCli.EXPECT().QueryResource(gomock.Any(), gomock.Eq(&enginepb.QueryResourceRequest{
    64  		ResourceKey: &enginepb.ResourceKey{
    65  			JobId:      "job-1",
    66  			ResourceId: "/local/resource-1",
    67  		},
    68  	})).Return(&enginepb.QueryResourceResponse{
    69  		CreatorExecutor: "executor-1",
    70  		JobId:           "job-1",
    71  		CreatorWorkerId: "worker-2",
    72  	}, nil).Times(1)
    73  	require.ErrorContains(t, resourceCli.CreateResource(context.Background(), req), "already exists")
    74  }
    75  
    76  func TestRemoveResource(t *testing.T) {
    77  	t.Parallel()
    78  
    79  	mockCli := mock.NewMockResourceManagerClient(gomock.NewController(t))
    80  	resourceCli := NewResourceManagerClient(mockCli)
    81  
    82  	req := &enginepb.RemoveResourceRequest{ResourceKey: &enginepb.ResourceKey{
    83  		JobId:      "job-1",
    84  		ResourceId: "/local/resource-1",
    85  	}}
    86  	mockCli.EXPECT().RemoveResource(gomock.Any(), gomock.Eq(req)).
    87  		Return(&enginepb.RemoveResourceResponse{}, nil).Times(1)
    88  	require.NoError(t, resourceCli.RemoveResource(context.Background(), req))
    89  
    90  	mockCli.EXPECT().RemoveResource(gomock.Any(), gomock.Eq(req)).
    91  		Return(nil, errors.ErrResourceDoesNotExist.GenWithStackByArgs("/local/resource-1")).Times(1)
    92  	require.NoError(t, resourceCli.RemoveResource(context.Background(), req))
    93  }