kubernetes-client / python

Official Python client library for kubernetes
http://kubernetes.io/
Apache License 2.0
6.55k stars 3.24k forks source link

fake client for unit testing #524

Open kevingessner opened 6 years ago

kevingessner commented 6 years ago

Kubernetes's client-go includes a powerful "fake" library (https://godoc.org/k8s.io/client-go/kubernetes/fake), which provides a mock API client that handles reads and writes without a cluster. It enables unit testing of code that uses client-go. This blog post has examples of how it's used: https://medium.com/@e_frogers/unit-testing-with-kubernetes-client-go-283b11aaa7db

Could this library provide a similar mock interface? I think a fake kubernetes.client.ApiClient that works in-process instead of over HTTP (like the go fake client) would be perfect: it would be injectable into the existing clients to allow full testing.

AlexShemeshWix commented 5 years ago

That would be a great feature. Im using go-client for k8s and im able to do integration tests in memory. Saves a lot of time.

sayarg commented 5 years ago

is this on the radar / roadmap? it would be extremely useful!

fejta-bot commented 5 years ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle stale

micw523 commented 5 years ago

/remove-lifecycle stale

sonnysideup commented 4 years ago

I have to write some automation using Python (I wish it could be in Golang) and a fake client or, at the very least, some documentation on how to write tests when using this library would be helpful.

Can a maintainer at least touch on this subject?

fejta-bot commented 4 years ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle stale

NissesSenap commented 4 years ago

/remove-lifecycle stale

irvifa commented 4 years ago

Any exact roadmap on this one?

darktempla commented 4 years ago

Totally agree. Usually I just look at a repos unit tests to see how the contributors have mocked out their class etc but blank stubs when look in the test folder.

Any update on this one.

arielb135 commented 4 years ago

just saying that i need that too, i'm currently mocking my usages of the API and this is alot of work

palnabarun commented 4 years ago

/assign

fejta-bot commented 4 years ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle stale

bguseman commented 3 years ago

/remove-lifecycle stale

aflag commented 3 years ago

Just been bitten by this too. I wrote an ad-hoc fake for the things I use. But not very happy with that. Looking online I couldn't find any evidence of anyone doing anything better 😢 In some cases I'd just write integration tests, but my current scenario isn't really easy to run as an integration test.

NickLarsenNZ commented 3 years ago

Back to Go it is

shiba24 commented 3 years ago

+1 for this feature - one option may be like moto library for AWS services, create a decorator-based one? https://github.com/spulec/moto

askreet commented 3 years ago

I agree this would be useful if provided first-hand by the client. I've been able to streamline mocked responses by doing things like:

class FakeResponse:
    def __init__(self, filename):
        with open(filename) as file:
            self.data = file.read()

configmaps = client.CoreV1Api().api_client.deserialize(FakeResponse('my-fixture.json'), 'V1ConfigMapList')

I build the fixtures by using kubectl:

kubectl get configmaps --namespace my-namespace -o yaml > my-fixture.json

Hope this helps someone fill the gap until this library has a better solution.

fejta-bot commented 3 years ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale

kdebisschop commented 3 years ago

@palnabarun it looks you self-assigned this. It seems like a tough nut to crack. Have you made any progress? Is there anything I can do to help?

fejta-bot commented 3 years ago

Stale issues rot after 30d of inactivity. Mark the issue as fresh with /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten

mayeuldalzon commented 3 years ago

+1 for this feature very useful

palnabarun commented 3 years ago

/remove-lifecycle rotten

palnabarun commented 3 years ago

@kdebisschop -- No, I haven't been able to devote time to this. In the meanwhile, if you or anyone wants to take a dig at it, please feel free to do so and update the findings here.

palnabarun commented 3 years ago

/lifecycle frozen

(applying the label to not mark it as stale again until we have some mock designs)

TomaszKlosinski commented 2 years ago

That would be an awesome feature for operator testing.

bubthegreat commented 1 year ago

kubectl get configmaps --namespace my-namespace -o yaml > my-fixture.json

@askreet Am I reading this right that you're loading the yaml into a json or was that a typo?

PatrikDubec commented 1 year ago

I would appreciate this feature too.

allenmqcymp commented 1 year ago

Any progress on this?

Pithikos commented 1 year ago

Yes, this would be great if it's in progress

daveortiz-ctct commented 1 year ago

+1 on this

olsenbudanur commented 1 year ago

another +1 on this