diff --git a/testutil/k8sutils/mock_kubectl_factory.go b/testutil/k8sutils/mock_kubectl_factory.go index de97e05bc..f6698d709 100644 --- a/testutil/k8sutils/mock_kubectl_factory.go +++ b/testutil/k8sutils/mock_kubectl_factory.go @@ -61,67 +61,104 @@ type MockKubectlFactory struct { func (f *MockKubectlFactory) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) { return f.MockToDiscoveryClient() } + +// DynamicClient implements Factory interface +// Returns a mock dynamic client ready for use func (f *MockKubectlFactory) DynamicClient() (dynamic.Interface, error) { return f.MockDynamicClient() } + +// OpenAPISchema implements Factory interface +// Returns a mock openapi schema definition. Schema definition includes metadata and structural information about +// Kubernetes object definitions func (f *MockKubectlFactory) OpenAPISchema() (openapi.Resources, error) { return f.MockOpenAPISchema() } + +// Validator implements Factory interface +// Returns a mock schema that can validate objects stored on disk func (f *MockKubectlFactory) Validator(bool) (validation.Schema, error) { return f.MockValidator() } + +// ToRESTMapper implements Factory interface +// Returns a mock RESTMapper +// RESTMapper allows clients to map resources to kind, and map kind and version to interfaces for manipulating +// those objects. It is primarily intended for consumers of Kubernetes compatible REST APIs func (f *MockKubectlFactory) ToRESTMapper() (meta.RESTMapper, error) { return f.MockToRESTMapper() } -func (f *MockKubectlFactory) ToRESTConfig() (*rest.Config, error) { return f.MockToRESTConfig() } -func (f *MockKubectlFactory) NewBuilder() *resource.Builder { return f.MockNewBuilder() } + +// ToRESTConfig implements Factory interface +// Returns a mock Config +// Config holds the common attributes that can be passed to a Kubernetes client on initialization +func (f *MockKubectlFactory) ToRESTConfig() (*rest.Config, error) { return f.MockToRESTConfig() } + +// NewBuilder implements Factory interface +// Returns a mock object that assists in loading objects from both disk and the server +func (f *MockKubectlFactory) NewBuilder() *resource.Builder { return f.MockNewBuilder() } + +// ToRawKubeConfigLoader implements Factory interface func (f *MockKubectlFactory) ToRawKubeConfigLoader() clientcmd.ClientConfig { return f.MockToRawKubeConfigLoader() } + +// ClientForMapping implements Factory interface +// Returns a mock RESTClient for working with the specified RESTMapping or an error func (f *MockKubectlFactory) ClientForMapping(*meta.RESTMapping) (resource.RESTClient, error) { return f.MockClientForMapping() } +// WithToDiscoveryClientByError returns mock discovery client with its respective error func (f *MockKubectlFactory) WithToDiscoveryClientByError(d discovery.CachedDiscoveryInterface, err error) *MockKubectlFactory { f.MockToDiscoveryClient = func() (discovery.CachedDiscoveryInterface, error) { return d, err } return f } +// WithOpenAPISchemaByError returns mock openAPISchema with its respective error func (f *MockKubectlFactory) WithOpenAPISchemaByError(r openapi.Resources, err error) *MockKubectlFactory { f.MockOpenAPISchema = func() (openapi.Resources, error) { return r, err } return f } +// WithDynamicClientByError returns mock dynamic client with its respective error func (f *MockKubectlFactory) WithDynamicClientByError(d dynamic.Interface, err error) *MockKubectlFactory { f.MockDynamicClient = func() (dynamic.Interface, error) { return d, err } return f } +// WithValidatorByError returns mock validator with its respective error func (f *MockKubectlFactory) WithValidatorByError(v validation.Schema, err error) *MockKubectlFactory { f.MockValidator = func() (validation.Schema, error) { return v, err } return f } +// WithToRESTMapperByError returns mock RESTMapper with its respective error func (f *MockKubectlFactory) WithToRESTMapperByError(r meta.RESTMapper, err error) *MockKubectlFactory { f.MockToRESTMapper = func() (meta.RESTMapper, error) { return r, err } return f } +// WithToRESTConfigByError returns mock RESTConfig with its respective error func (f *MockKubectlFactory) WithToRESTConfigByError(r *rest.Config, err error) *MockKubectlFactory { f.MockToRESTConfig = func() (*rest.Config, error) { return r, err } return f } +// WithNewBuilderByError returns mock resource builder with its respective error func (f *MockKubectlFactory) WithNewBuilderByError(r *resource.Builder) *MockKubectlFactory { f.MockNewBuilder = func() *resource.Builder { return r } return f } +// WithToRawKubeConfigLoaderByError returns mock raw kubeconfig loader with its respective error func (f *MockKubectlFactory) WithToRawKubeConfigLoaderByError(c clientcmd.ClientConfig) *MockKubectlFactory { f.MockToRawKubeConfigLoader = func() clientcmd.ClientConfig { return c } return f } +// WithClientForMappingByError returns mock client mapping with its respective error func (f *MockKubectlFactory) WithClientForMappingByError(r resource.RESTClient, err error) *MockKubectlFactory { f.MockClientForMapping = func() (resource.RESTClient, error) { return r, err } return f } +// NewMockKubectlFactory defines the functions of MockKubectlFactory with nil values for testing purpose func NewMockKubectlFactory() *MockKubectlFactory { return &MockKubectlFactory{MockDynamicClient: func() (dynamic.Interface, error) { return nil, nil }, MockToDiscoveryClient: func() (discovery.CachedDiscoveryInterface, error) { return nil, nil }, @@ -135,24 +172,31 @@ func NewMockKubectlFactory() *MockKubectlFactory { } } +// MockClientConfig implements DirectClientConfig interface +// Returns mock client config for testing type MockClientConfig struct { clientcmd.DirectClientConfig MockNamespace func() (string, bool, error) } +// Namespace returns mock namespace for testing func (c MockClientConfig) Namespace() (string, bool, error) { return c.MockNamespace() } +// WithNamespace returns mock namespace with its respective error func (c *MockClientConfig) WithNamespace(s string, b bool, err error) *MockClientConfig { c.MockNamespace = func() (string, bool, error) { return s, b, err } return c } +// NewMockClientConfig returns mock client config for testing func NewMockClientConfig() *MockClientConfig { return &MockClientConfig{ MockNamespace: func() (string, bool, error) { return "test", false, nil }, } } +// NewFakeFactoryForRC returns a fake Factory object for testing +// It is used to mock network interactions via a rest.Request func NewFakeFactoryForRC(t *testing.T, filenameRC string) *cmdtesting.TestFactory { c := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)