github.com/oam-dev/kubevela@v1.9.11/pkg/oam/mock/mocks.go (about)

     1  /*
     2  Copyright 2019 The Crossplane Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package mock provides fake OAM resources for use in tests.
    18  package mock
    19  
    20  import (
    21  	"encoding/json"
    22  	"reflect"
    23  
    24  	corev1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/apimachinery/pkg/runtime/schema"
    28  	"k8s.io/client-go/rest"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  	"sigs.k8s.io/controller-runtime/pkg/manager"
    31  
    32  	"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
    33  )
    34  
    35  // Conditioned is a mock that implements Conditioned interface.
    36  type Conditioned struct{ Conditions []condition.Condition }
    37  
    38  // SetConditions sets the Conditions.
    39  func (m *Conditioned) SetConditions(c ...condition.Condition) { m.Conditions = c }
    40  
    41  // GetCondition get the Condition with the given ConditionType.
    42  func (m *Conditioned) GetCondition(ct condition.ConditionType) condition.Condition {
    43  	return condition.Condition{Type: ct, Status: corev1.ConditionUnknown}
    44  }
    45  
    46  // ManagedResourceReferencer is a mock that implements ManagedResourceReferencer interface.
    47  type ManagedResourceReferencer struct {
    48  	Ref *corev1.ObjectReference `json:"ref"`
    49  }
    50  
    51  // SetResourceReference sets the ResourceReference.
    52  func (m *ManagedResourceReferencer) SetResourceReference(r *corev1.ObjectReference) { m.Ref = r }
    53  
    54  // GetResourceReference gets the ResourceReference.
    55  func (m *ManagedResourceReferencer) GetResourceReference() *corev1.ObjectReference { return m.Ref }
    56  
    57  // A WorkloadReferencer references an OAM Workload type.
    58  type WorkloadReferencer struct {
    59  	Ref corev1.ObjectReference `json:"ref"`
    60  }
    61  
    62  // GetWorkloadReference gets the WorkloadReference.
    63  func (w *WorkloadReferencer) GetWorkloadReference() corev1.ObjectReference {
    64  	return w.Ref
    65  }
    66  
    67  // SetWorkloadReference sets the WorkloadReference.
    68  func (w *WorkloadReferencer) SetWorkloadReference(r corev1.ObjectReference) {
    69  	w.Ref = r
    70  }
    71  
    72  // Object is a mock that implements Object interface.
    73  type Object struct {
    74  	metav1.ObjectMeta
    75  	runtime.Object
    76  }
    77  
    78  // GetObjectKind returns schema.ObjectKind.
    79  func (o *Object) GetObjectKind() schema.ObjectKind {
    80  	return schema.EmptyObjectKind
    81  }
    82  
    83  // DeepCopyObject returns a copy of the object as runtime.Object
    84  func (o *Object) DeepCopyObject() runtime.Object {
    85  	out := &Object{}
    86  	j, err := json.Marshal(o)
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	_ = json.Unmarshal(j, out)
    91  	return out
    92  }
    93  
    94  // Trait is a mock that implements Trait interface.
    95  type Trait struct {
    96  	metav1.ObjectMeta
    97  	runtime.Object
    98  	condition.ConditionedStatus
    99  	WorkloadReferencer
   100  }
   101  
   102  // GetObjectKind returns schema.ObjectKind.
   103  func (t *Trait) GetObjectKind() schema.ObjectKind {
   104  	return schema.EmptyObjectKind
   105  }
   106  
   107  // DeepCopyObject returns a copy of the object as runtime.Object
   108  func (t *Trait) DeepCopyObject() runtime.Object {
   109  	out := &Trait{}
   110  	j, err := json.Marshal(t)
   111  	if err != nil {
   112  		panic(err)
   113  	}
   114  	_ = json.Unmarshal(j, out)
   115  	return out
   116  }
   117  
   118  // Workload is a mock that implements Workload interface.
   119  type Workload struct {
   120  	metav1.ObjectMeta
   121  	runtime.Object
   122  	condition.ConditionedStatus
   123  }
   124  
   125  // GetObjectKind returns schema.ObjectKind.
   126  func (w *Workload) GetObjectKind() schema.ObjectKind {
   127  	return schema.EmptyObjectKind
   128  }
   129  
   130  // DeepCopyObject returns a copy of the object as runtime.Object
   131  func (w *Workload) DeepCopyObject() runtime.Object {
   132  	out := &Workload{}
   133  	j, err := json.Marshal(w)
   134  	if err != nil {
   135  		panic(err)
   136  	}
   137  	_ = json.Unmarshal(j, out)
   138  	return out
   139  }
   140  
   141  // Manager is a mock object that satisfies manager.Manager interface.
   142  type Manager struct {
   143  	manager.Manager
   144  
   145  	Client client.Client
   146  	Scheme *runtime.Scheme
   147  }
   148  
   149  // GetClient returns the client.
   150  func (m *Manager) GetClient() client.Client { return m.Client }
   151  
   152  // GetScheme returns the scheme.
   153  func (m *Manager) GetScheme() *runtime.Scheme { return m.Scheme }
   154  
   155  // GetConfig returns the config for test.
   156  func (m *Manager) GetConfig() *rest.Config {
   157  	return &rest.Config{}
   158  }
   159  
   160  // GV returns a mock schema.GroupVersion.
   161  var GV = schema.GroupVersion{Group: "g", Version: "v"}
   162  
   163  // GVK returns the mock GVK of the given object.
   164  func GVK(o runtime.Object) schema.GroupVersionKind {
   165  	return GV.WithKind(reflect.TypeOf(o).Elem().Name())
   166  }
   167  
   168  // SchemeWith returns a scheme with list of `runtime.Object`s registered.
   169  func SchemeWith(o ...runtime.Object) *runtime.Scheme {
   170  	s := runtime.NewScheme()
   171  	s.AddKnownTypes(GV, o...)
   172  	return s
   173  }
   174  
   175  // NotFoundErr describes NotFound Resource error
   176  type NotFoundErr struct {
   177  	NotFoundStatus metav1.Status
   178  }
   179  
   180  // NewMockNotFoundErr return a mock NotFoundErr
   181  func NewMockNotFoundErr() NotFoundErr {
   182  	return NotFoundErr{
   183  		NotFoundStatus: metav1.Status{
   184  			Reason: metav1.StatusReasonNotFound,
   185  		},
   186  	}
   187  }
   188  
   189  // Status returns the Status Reason
   190  func (mock NotFoundErr) Status() metav1.Status {
   191  	return mock.NotFoundStatus
   192  }
   193  
   194  // Error return error info
   195  func (mock NotFoundErr) Error() string {
   196  	return "Not Found Resource"
   197  }
   198  
   199  // A LocalSecretReference is a reference to a secret in the same namespace as
   200  // the referencer.
   201  type LocalSecretReference struct {
   202  	// Name of the secret.
   203  	Name string `json:"name"`
   204  }
   205  
   206  // LocalConnectionSecretWriterTo is a mock that implements LocalConnectionSecretWriterTo interface.
   207  type LocalConnectionSecretWriterTo struct {
   208  	Ref *LocalSecretReference `json:"local_secret_ref"`
   209  }
   210  
   211  // SetWriteConnectionSecretToReference sets the WriteConnectionSecretToReference.
   212  func (m *LocalConnectionSecretWriterTo) SetWriteConnectionSecretToReference(r *LocalSecretReference) {
   213  	m.Ref = r
   214  }
   215  
   216  // GetWriteConnectionSecretToReference gets the WriteConnectionSecretToReference.
   217  func (m *LocalConnectionSecretWriterTo) GetWriteConnectionSecretToReference() *LocalSecretReference {
   218  	return m.Ref
   219  }
   220  
   221  // Target is a mock that implements Target interface.
   222  type Target struct {
   223  	metav1.ObjectMeta
   224  	ManagedResourceReferencer
   225  	LocalConnectionSecretWriterTo
   226  	condition.ConditionedStatus
   227  }
   228  
   229  // GetObjectKind returns schema.ObjectKind.
   230  func (m *Target) GetObjectKind() schema.ObjectKind {
   231  	return schema.EmptyObjectKind
   232  }
   233  
   234  // DeepCopyObject returns a deep copy of Target as runtime.Object.
   235  func (m *Target) DeepCopyObject() runtime.Object {
   236  	out := &Target{}
   237  	j, err := json.Marshal(m)
   238  	if err != nil {
   239  		panic(err)
   240  	}
   241  	_ = json.Unmarshal(j, out)
   242  	return out
   243  }