Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clientset unit tests #16

Merged
merged 14 commits into from
Aug 3, 2022
2 changes: 2 additions & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
go-version: ${{ env.GOVER }}
- name: kubebuilder tests
run: make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3

golang-ci-lint:
runs-on: ubuntu-20.04
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/linters/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ linters:
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- noctx
Expand All @@ -59,9 +58,6 @@ linters:
- nestif
- prealloc
- wsl
linters-settings:
lll:
line-length: 140

issues:
# https://github.com/golangci/golangci-lint/issues/2439#issuecomment-1002912465
Expand All @@ -74,4 +70,6 @@ issues:
# - structcheck
# - unused
# - unparam
- goerr113
- gochecknoglobals
- funlen
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate fmt vet ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); CGO_ENABLED=1 go test ./... -race -coverprofile coverage.out -covermode atomic

##@ Build

Expand Down
90 changes: 44 additions & 46 deletions api/clientset/v1alpha1/srlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,13 @@ var ErrUpdateFailed = errors.New("operation update failed")

// SrlinuxInterface provides access to the Srlinux CRD.
type SrlinuxInterface interface {
List(ctx context.Context, opts *metav1.ListOptions) (*typesv1alpha1.SrlinuxList, error)
Get(
ctx context.Context,
name string,
options *metav1.GetOptions,
) (*typesv1alpha1.Srlinux, error)
List(ctx context.Context, opts metav1.ListOptions) (*typesv1alpha1.SrlinuxList, error)
Get(ctx context.Context, name string, opts metav1.GetOptions) (*typesv1alpha1.Srlinux, error)
Create(ctx context.Context, srlinux *typesv1alpha1.Srlinux) (*typesv1alpha1.Srlinux, error)
Delete(ctx context.Context, name string, opts *metav1.DeleteOptions) error
Watch(ctx context.Context, opts *metav1.ListOptions) (watch.Interface, error)
Unstructured(
ctx context.Context,
name string,
opts *metav1.GetOptions,
subresources ...string,
) (*unstructured.Unstructured, error)
Update(
ctx context.Context,
obj *unstructured.Unstructured,
opts *metav1.UpdateOptions,
) (*typesv1alpha1.Srlinux, error)
Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error
Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error)
Unstructured(ctx context.Context, name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error)
Update(ctx context.Context, obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*typesv1alpha1.Srlinux, error)
}

// Interface is the clientset interface for srlinux.
Expand All @@ -58,16 +45,25 @@ type Clientset struct {
restClient rest.Interface
}

var gvr = schema.GroupVersionResource{ // nolint: gochecknoglobals
Group: typesv1alpha1.GroupVersion.Group,
Version: typesv1alpha1.GroupVersion.Version,
Resource: "srlinuxes",
func GVR() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: typesv1alpha1.GroupName,
Version: typesv1alpha1.GroupVersion,
Resource: "srlinuxes",
}
}

func GV() *schema.GroupVersion {
return &schema.GroupVersion{
Group: typesv1alpha1.GroupName,
Version: typesv1alpha1.GroupVersion,
}
}

// NewForConfig returns a new Clientset based on c.
func NewForConfig(c *rest.Config) (*Clientset, error) {
config := *c
config.ContentConfig.GroupVersion = &schema.GroupVersion{Group: gvr.Group, Version: gvr.Version}
config.ContentConfig.GroupVersion = &schema.GroupVersion{Group: GVR().Group, Version: GVR().Version}
config.APIPath = "/apis"
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
config.UserAgent = rest.DefaultKubernetesUserAgent()
Expand All @@ -77,7 +73,7 @@ func NewForConfig(c *rest.Config) (*Clientset, error) {
return nil, err
}

dInterface := dClient.Resource(gvr)
dInterface := dClient.Resource(GVR())

rClient, err := rest.RESTClientFor(&config)
if err != nil {
Expand Down Expand Up @@ -108,14 +104,14 @@ type srlinuxClient struct {
// List gets a list of SRLinux resources.
func (s *srlinuxClient) List(
ctx context.Context,
opts *metav1.ListOptions,
opts metav1.ListOptions, // skipcq: CRT-P0003
) (*typesv1alpha1.SrlinuxList, error) {
result := typesv1alpha1.SrlinuxList{}
err := s.restClient.
Get().
Namespace(s.ns).
Resource(gvr.Resource).
VersionedParams(opts, scheme.ParameterCodec).
Resource(GVR().Resource).
VersionedParams(&opts, scheme.ParameterCodec).
Do(ctx).
Into(&result)

Expand All @@ -126,15 +122,15 @@ func (s *srlinuxClient) List(
func (s *srlinuxClient) Get(
ctx context.Context,
name string,
opts *metav1.GetOptions,
opts metav1.GetOptions,
) (*typesv1alpha1.Srlinux, error) {
result := typesv1alpha1.Srlinux{}
err := s.restClient.
Get().
Namespace(s.ns).
Resource(gvr.Resource).
Resource(GVR().Resource).
Name(name).
VersionedParams(opts, scheme.ParameterCodec).
VersionedParams(&opts, scheme.ParameterCodec).
Do(ctx).
Into(&result)

Expand All @@ -150,7 +146,7 @@ func (s *srlinuxClient) Create(
err := s.restClient.
Post().
Namespace(s.ns).
Resource(gvr.Resource).
Resource(GVR().Resource).
Body(srlinux).
Do(ctx).
Into(&result)
Expand All @@ -160,24 +156,27 @@ func (s *srlinuxClient) Create(

func (s *srlinuxClient) Watch(
ctx context.Context,
opts *metav1.ListOptions,
opts metav1.ListOptions, // skipcq: CRT-P0003
) (watch.Interface, error) {
opts.Watch = true

return s.restClient.
Get().
Namespace(s.ns).
Resource(gvr.Resource).
VersionedParams(opts, scheme.ParameterCodec).
Resource(GVR().Resource).
VersionedParams(&opts, scheme.ParameterCodec).
Watch(ctx)
}

func (s *srlinuxClient) Delete(ctx context.Context, name string, opts *metav1.DeleteOptions) error {
func (s *srlinuxClient) Delete(ctx context.Context,
name string,
opts metav1.DeleteOptions, // skipcq: CRT-P0003
) error {
return s.restClient.
Delete().
Namespace(s.ns).
Resource(gvr.Resource).
VersionedParams(opts, scheme.ParameterCodec).
Resource(GVR().Resource).
VersionedParams(&opts, scheme.ParameterCodec).
Name(name).
Do(ctx).
Error()
Expand All @@ -186,11 +185,11 @@ func (s *srlinuxClient) Delete(ctx context.Context, name string, opts *metav1.De
func (s *srlinuxClient) Update(
ctx context.Context,
obj *unstructured.Unstructured,
_ *metav1.UpdateOptions,
opts metav1.UpdateOptions,
) (*typesv1alpha1.Srlinux, error) {
result := typesv1alpha1.Srlinux{}

obj, err := s.dInterface.Namespace(s.ns).UpdateStatus(ctx, obj, metav1.UpdateOptions{})
obj, err := s.dInterface.Namespace(s.ns).UpdateStatus(ctx, obj, opts)
if err != nil {
return nil, err
}
Expand All @@ -203,15 +202,14 @@ func (s *srlinuxClient) Update(
return &result, nil
}

func (s *srlinuxClient) Unstructured(
ctx context.Context,
name string,
opts *metav1.GetOptions,
func (s *srlinuxClient) Unstructured(ctx context.Context, name string, opts metav1.GetOptions,
subresources ...string,
) (*unstructured.Unstructured, error) {
return s.dInterface.Namespace(s.ns).Get(ctx, name, *opts, subresources...)
return s.dInterface.Namespace(s.ns).Get(ctx, name, opts, subresources...)
}

func init() {
_ = typesv1alpha1.AddToScheme(scheme.Scheme)
if err := typesv1alpha1.AddToScheme(scheme.Scheme); err != nil {
panic(err)
}
}
Loading