github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/builder/template/mock_client.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 template
    21  
    22  import (
    23  	"context"
    24  
    25  	corev1 "k8s.io/api/core/v1"
    26  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    27  	"k8s.io/apimachinery/pkg/api/meta"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	"k8s.io/apimachinery/pkg/runtime/schema"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  
    32  	cfgcore "github.com/1aal/kubeblocks/pkg/configuration/core"
    33  	testutil "github.com/1aal/kubeblocks/pkg/testutil/k8s"
    34  )
    35  
    36  type ResourceMatcher = func(obj runtime.Object) bool
    37  type Handler = func(obj runtime.Object) error
    38  
    39  type ResourceHandler struct {
    40  	Matcher []ResourceMatcher
    41  	Handler Handler
    42  }
    43  
    44  type mockClient struct {
    45  	objects        map[client.ObjectKey]client.Object
    46  	kindObjectList map[string][]runtime.Object
    47  
    48  	hander *ResourceHandler
    49  }
    50  
    51  func newMockClient(objs []client.Object) *mockClient {
    52  	return &mockClient{
    53  		objects:        fromObjects(objs),
    54  		kindObjectList: splitRuntimeObject(objs),
    55  	}
    56  }
    57  
    58  func (m *mockClient) SetResourceHandler(resourceHandler *ResourceHandler) {
    59  	m.hander = resourceHandler
    60  }
    61  
    62  func fromObjects(objs []client.Object) map[client.ObjectKey]client.Object {
    63  	r := make(map[client.ObjectKey]client.Object)
    64  	for _, obj := range objs {
    65  		if obj != nil {
    66  			r[client.ObjectKeyFromObject(obj)] = obj
    67  		}
    68  	}
    69  	return r
    70  }
    71  
    72  func (m *mockClient) AppendMockObjects(obj client.Object) {
    73  	objKey := client.ObjectKeyFromObject(obj)
    74  	if _, ok := m.objects[objKey]; ok {
    75  		return
    76  	}
    77  	m.objects[objKey] = obj
    78  }
    79  
    80  func splitRuntimeObject(objects []client.Object) map[string][]runtime.Object {
    81  	r := make(map[string][]runtime.Object)
    82  	for _, object := range objects {
    83  		kind := object.GetObjectKind().GroupVersionKind().Kind
    84  		if _, ok := r[kind]; !ok {
    85  			r[kind] = make([]runtime.Object, 0)
    86  		}
    87  		r[kind] = append(r[kind], object)
    88  	}
    89  	return r
    90  }
    91  
    92  func (m *mockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
    93  	objKey := key
    94  	if object, ok := m.objects[objKey]; ok {
    95  		testutil.SetGetReturnedObject(obj, object)
    96  		return nil
    97  	}
    98  	objKey.Namespace = ""
    99  	if object, ok := m.objects[objKey]; ok {
   100  		testutil.SetGetReturnedObject(obj, object)
   101  		return nil
   102  	}
   103  	return apierrors.NewNotFound(corev1.SchemeGroupVersion.WithResource("mock_resource").GroupResource(), key.String())
   104  }
   105  
   106  func (m *mockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
   107  	r := m.kindObjectList[list.GetObjectKind().GroupVersionKind().Kind]
   108  	if r != nil {
   109  		return testutil.SetListReturnedObjects(list, r)
   110  	}
   111  	return nil
   112  }
   113  
   114  func (m *mockClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
   115  	if m.hander == nil || len(m.hander.Matcher) == 0 {
   116  		return nil
   117  	}
   118  
   119  	for _, matcher := range m.hander.Matcher {
   120  		if !matcher(obj) {
   121  			return nil
   122  		}
   123  	}
   124  	return m.hander.Handler(obj)
   125  }
   126  
   127  func (m *mockClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
   128  	return nil
   129  }
   130  
   131  func (m *mockClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
   132  	return nil
   133  }
   134  
   135  func (m *mockClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
   136  	return nil
   137  }
   138  
   139  func (m *mockClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
   140  	return cfgcore.MakeError("not support")
   141  }
   142  
   143  func (m *mockClient) Status() client.SubResourceWriter {
   144  	panic("implement me")
   145  }
   146  
   147  func (m *mockClient) SubResource(subResource string) client.SubResourceClient {
   148  	panic("implement me")
   149  }
   150  
   151  func (m *mockClient) Scheme() *runtime.Scheme {
   152  	panic("implement me")
   153  }
   154  
   155  func (m *mockClient) RESTMapper() meta.RESTMapper {
   156  	panic("implement me")
   157  }
   158  
   159  func (m *mockClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
   160  	panic("implement me")
   161  }
   162  
   163  func (m *mockClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
   164  	panic("implement me")
   165  }