kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
8.79k stars 2.91k forks source link

Unit testing using fake client limited when your code is using RESTClient call for CoreV1 #1233

Closed ezavgorodniy closed 5 months ago

ezavgorodniy commented 1 year ago

Our code is doing

    err := clientset.
        CoreV1().
        RESTClient().
        Post().
        AbsPath(absPath).
        Body(tj).
        Do(context.TODO()).
        Into(tj)

When I'm using fake client it's failing with panic because clientset.CoreV1().RESTClient() is returning nil here: https://github.com/kubernetes/client-go/blob/089d04441d989ab5cd9538e69c0e0c4e33700173/kubernetes/typed/core/v1/fake/fake_core_client.go#L95-L100

It's preventing me from writing unit test for this bit of functionality

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

mikechesterwang commented 11 months ago

This is the workaround I used. Hope it help.

You can customize the behavior of the fake rest client in func (c *FakeExtendedCoreV1) RESTClient()

package fake

import (
    "k8s.io/apimachinery/pkg/runtime"
    "k8s.io/client-go/kubernetes"
    kfake "k8s.io/client-go/kubernetes/fake"
    "k8s.io/client-go/rest"
    restfake "k8s.io/client-go/rest/fake"

    typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

type FakeExtendedCoreV1 struct {
    typedcorev1.CoreV1Interface
}

func (c *FakeExtendedCoreV1) RESTClient() rest.Interface {
    return &restfake.RESTClient{}
}

type FakeExtendedClientset struct {
    *kfake.Clientset
}

func (f *FakeExtendedClientset) CoreV1() typedcorev1.CoreV1Interface {
    return &FakeExtendedCoreV1{f.Clientset.CoreV1()}
}

func NewFakeClientset(objs ...runtime.Object) kubernetes.Interface {
    return &FakeExtendedClientset{kfake.NewSimpleClientset(objs...)}
}
k8s-triage-robot commented 5 months ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-ci-robot commented 5 months ago

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to [this](https://github.com/kubernetes/client-go/issues/1233#issuecomment-1901778253): >The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. > >This bot triages issues according to the following rules: >- After 90d of inactivity, `lifecycle/stale` is applied >- After 30d of inactivity since `lifecycle/stale` was applied, `lifecycle/rotten` is applied >- After 30d of inactivity since `lifecycle/rotten` was applied, the issue is closed > >You can: >- Reopen this issue with `/reopen` >- Mark this issue as fresh with `/remove-lifecycle rotten` >- Offer to help out with [Issue Triage][1] > >Please send feedback to sig-contributor-experience at [kubernetes/community](https://github.com/kubernetes/community). > >/close not-planned > >[1]: https://www.kubernetes.dev/docs/guide/issue-triage/ Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.