github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/operations/fake.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package operations
    21  
    22  import "context"
    23  
    24  type FakeFuncType string
    25  
    26  const (
    27  	FakeInit       FakeFuncType = "fake-init"
    28  	FakeIsReadOnly FakeFuncType = "fake-is-read-only"
    29  	FakePreCheck   FakeFuncType = "fake-pre-check"
    30  	FakeDo         FakeFuncType = "fake-do"
    31  	FakeDefault    FakeFuncType = "fake-default"
    32  )
    33  
    34  type FakeOperations struct {
    35  	InitFunc       func(ctx context.Context) error
    36  	IsReadOnlyFunc func(ctx context.Context) bool
    37  	PreCheckFunc   func(ctx context.Context, request *OpsRequest) error
    38  	DoFunc         func(ctx context.Context, request *OpsRequest) (*OpsResponse, error)
    39  }
    40  
    41  func NewFakeOperations(funcType FakeFuncType, fakeFunc interface{}) *FakeOperations {
    42  	op := &FakeOperations{
    43  		InitFunc: func(ctx context.Context) error {
    44  			return nil
    45  		},
    46  		IsReadOnlyFunc: func(ctx context.Context) bool {
    47  			return false
    48  		},
    49  		PreCheckFunc: func(ctx context.Context, request *OpsRequest) error {
    50  			return nil
    51  		},
    52  		DoFunc: func(ctx context.Context, request *OpsRequest) (*OpsResponse, error) {
    53  			return nil, nil
    54  		},
    55  	}
    56  
    57  	switch funcType {
    58  	case FakeInit:
    59  		op.InitFunc = fakeFunc.(func(ctx context.Context) error)
    60  	case FakeIsReadOnly:
    61  		op.IsReadOnlyFunc = fakeFunc.(func(ctx context.Context) bool)
    62  	case FakePreCheck:
    63  		op.PreCheckFunc = fakeFunc.(func(ctx context.Context, request *OpsRequest) error)
    64  	case FakeDo:
    65  		op.DoFunc = fakeFunc.(func(ctx context.Context, request *OpsRequest) (*OpsResponse, error))
    66  	}
    67  	return op
    68  }
    69  
    70  func (f *FakeOperations) Init(ctx context.Context) error {
    71  	return f.InitFunc(ctx)
    72  }
    73  
    74  func (f *FakeOperations) IsReadonly(ctx context.Context) bool {
    75  	return f.IsReadOnlyFunc(ctx)
    76  }
    77  
    78  func (f *FakeOperations) PreCheck(ctx context.Context, request *OpsRequest) error {
    79  	return f.PreCheckFunc(ctx, request)
    80  }
    81  
    82  func (f *FakeOperations) Do(ctx context.Context, request *OpsRequest) (*OpsResponse, error) {
    83  	return f.DoFunc(ctx, request)
    84  }