From b25a04837835e09081949c1ba2e0ba50f582577f Mon Sep 17 00:00:00 2001 From: Dawid Rusnak Date: Fri, 18 Oct 2024 10:17:41 +0200 Subject: [PATCH] feat: add missing client for TestTriggers (#302) --- .../testtriggers/v1/mock_testtriggers.go | 137 ++++++++++++++++++ pkg/client/testtriggers/v1/testtriggers.go | 120 +++++++++++++++ 2 files changed, 257 insertions(+) create mode 100644 pkg/client/testtriggers/v1/mock_testtriggers.go create mode 100644 pkg/client/testtriggers/v1/testtriggers.go diff --git a/pkg/client/testtriggers/v1/mock_testtriggers.go b/pkg/client/testtriggers/v1/mock_testtriggers.go new file mode 100644 index 00000000..3f52fa1b --- /dev/null +++ b/pkg/client/testtriggers/v1/mock_testtriggers.go @@ -0,0 +1,137 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/kubeshop/testkube-operator/pkg/client/testtriggers/v1 (interfaces: Interface) + +// Package v1 is a generated GoMock package. +package v1 + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + v1 "github.com/kubeshop/testkube-operator/api/testtriggers/v1" +) + +// MockInterface is a mock of Interface interface. +type MockInterface struct { + ctrl *gomock.Controller + recorder *MockInterfaceMockRecorder +} + +// MockInterfaceMockRecorder is the mock recorder for MockInterface. +type MockInterfaceMockRecorder struct { + mock *MockInterface +} + +// NewMockInterface creates a new mock instance. +func NewMockInterface(ctrl *gomock.Controller) *MockInterface { + mock := &MockInterface{ctrl: ctrl} + mock.recorder = &MockInterfaceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockInterface) EXPECT() *MockInterfaceMockRecorder { + return m.recorder +} + +// Create mocks base method. +func (m *MockInterface) Create(arg0 *v1.TestTrigger) (*v1.TestTrigger, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Create", arg0) + ret0, _ := ret[0].(*v1.TestTrigger) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Create indicates an expected call of Create. +func (mr *MockInterfaceMockRecorder) Create(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockInterface)(nil).Create), arg0) +} + +// Delete mocks base method. +func (m *MockInterface) Delete(arg0, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Delete", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// Delete indicates an expected call of Delete. +func (mr *MockInterfaceMockRecorder) Delete(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockInterface)(nil).Delete), arg0, arg1) +} + +// DeleteAll mocks base method. +func (m *MockInterface) DeleteAll(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteAll", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteAll indicates an expected call of DeleteAll. +func (mr *MockInterfaceMockRecorder) DeleteAll(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteAll", reflect.TypeOf((*MockInterface)(nil).DeleteAll), arg0) +} + +// DeleteByLabels mocks base method. +func (m *MockInterface) DeleteByLabels(arg0, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteByLabels", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteByLabels indicates an expected call of DeleteByLabels. +func (mr *MockInterfaceMockRecorder) DeleteByLabels(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteByLabels", reflect.TypeOf((*MockInterface)(nil).DeleteByLabels), arg0, arg1) +} + +// Get mocks base method. +func (m *MockInterface) Get(arg0, arg1 string) (*v1.TestTrigger, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Get", arg0, arg1) + ret0, _ := ret[0].(*v1.TestTrigger) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Get indicates an expected call of Get. +func (mr *MockInterfaceMockRecorder) Get(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockInterface)(nil).Get), arg0, arg1) +} + +// List mocks base method. +func (m *MockInterface) List(arg0, arg1 string) (*v1.TestTriggerList, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "List", arg0, arg1) + ret0, _ := ret[0].(*v1.TestTriggerList) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// List indicates an expected call of List. +func (mr *MockInterfaceMockRecorder) List(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "List", reflect.TypeOf((*MockInterface)(nil).List), arg0, arg1) +} + +// Update mocks base method. +func (m *MockInterface) Update(arg0 *v1.TestTrigger) (*v1.TestTrigger, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Update", arg0) + ret0, _ := ret[0].(*v1.TestTrigger) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Update indicates an expected call of Update. +func (mr *MockInterfaceMockRecorder) Update(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockInterface)(nil).Update), arg0) +} diff --git a/pkg/client/testtriggers/v1/testtriggers.go b/pkg/client/testtriggers/v1/testtriggers.go new file mode 100644 index 00000000..26677261 --- /dev/null +++ b/pkg/client/testtriggers/v1/testtriggers.go @@ -0,0 +1,120 @@ +package v1 + +import ( + "context" + + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/labels" + "sigs.k8s.io/controller-runtime/pkg/client" + + testtriggersv1 "github.com/kubeshop/testkube-operator/api/testtriggers/v1" +) + +//go:generate mockgen -destination=./mock_testtriggers.go -package=v1 "github.com/kubeshop/testkube-operator/pkg/client/testtriggers/v1" Interface +type Interface interface { + List(selector, namespace string) (*testtriggersv1.TestTriggerList, error) + Get(name, namespace string) (*testtriggersv1.TestTrigger, error) + Create(trigger *testtriggersv1.TestTrigger) (*testtriggersv1.TestTrigger, error) + Update(trigger *testtriggersv1.TestTrigger) (*testtriggersv1.TestTrigger, error) + Delete(name, namespace string) error + DeleteAll(namespace string) error + DeleteByLabels(selector, namespace string) error +} + +// NewClient creates new TestTrigger client +func NewClient(client client.Client, namespace string) *TestTriggersClient { + return &TestTriggersClient{ + Client: client, + Namespace: namespace, + } +} + +// TestTriggersClient implements methods to work with TestTriggers +type TestTriggersClient struct { + Client client.Client + Namespace string +} + +// List lists TestTriggers +func (s TestTriggersClient) List(selector, namespace string) (*testtriggersv1.TestTriggerList, error) { + list := &testtriggersv1.TestTriggerList{} + reqs, err := labels.ParseToRequirements(selector) + if err != nil { + return list, err + } + if namespace == "" { + namespace = s.Namespace + } + + options := &client.ListOptions{ + Namespace: namespace, + LabelSelector: labels.NewSelector().Add(reqs...), + } + + if err = s.Client.List(context.Background(), list, options); err != nil { + return list, err + } + + return list, nil +} + +// Get returns TestTrigger +func (s TestTriggersClient) Get(name, namespace string) (*testtriggersv1.TestTrigger, error) { + if namespace == "" { + namespace = s.Namespace + } + trigger := &testtriggersv1.TestTrigger{} + err := s.Client.Get(context.Background(), client.ObjectKey{Namespace: namespace, Name: name}, trigger) + if err != nil { + return nil, err + } + return trigger, nil +} + +// Create creates new TestTrigger +func (s TestTriggersClient) Create(trigger *testtriggersv1.TestTrigger) (*testtriggersv1.TestTrigger, error) { + return trigger, s.Client.Create(context.Background(), trigger) +} + +// Update updates existing TestTrigger +func (s TestTriggersClient) Update(trigger *testtriggersv1.TestTrigger) (*testtriggersv1.TestTrigger, error) { + return trigger, s.Client.Update(context.Background(), trigger) +} + +// Delete deletes existing TestTrigger +func (s TestTriggersClient) Delete(name, namespace string) error { + trigger, err := s.Get(name, namespace) + if err != nil { + return err + } + return s.Client.Delete(context.Background(), trigger) +} + +// DeleteAll delete all TestTriggers +func (s TestTriggersClient) DeleteAll(namespace string) error { + if namespace == "" { + namespace = s.Namespace + } + u := &unstructured.Unstructured{} + u.SetKind("TestTrigger") + u.SetAPIVersion(testtriggersv1.GroupVersion.String()) + return s.Client.DeleteAllOf(context.Background(), u, client.InNamespace(namespace)) +} + +// DeleteByLabels deletes TestTriggers by labels +func (s TestTriggersClient) DeleteByLabels(selector, namespace string) error { + reqs, err := labels.ParseToRequirements(selector) + if err != nil { + return err + } + if namespace == "" { + namespace = s.Namespace + } + + u := &unstructured.Unstructured{} + u.SetKind("TestTrigger") + u.SetAPIVersion(testtriggersv1.GroupVersion.String()) + err = s.Client.DeleteAllOf(context.Background(), u, client.InNamespace(namespace), + client.MatchingLabelsSelector{Selector: labels.NewSelector().Add(reqs...)}) + return err +}