python icon indicating copy to clipboard operation
python copied to clipboard

fake client for unit testing

Open kevingessner opened this issue 6 years ago • 31 comments

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.

kevingessner avatar Apr 27 '18 16:04 kevingessner

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.

AlexShemeshWix avatar Oct 02 '18 09:10 AlexShemeshWix

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

sayarg avatar Feb 07 '19 18:02 sayarg

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

fejta-bot avatar May 08 '19 19:05 fejta-bot

/remove-lifecycle stale

micw523 avatar May 09 '19 18:05 micw523

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?

sonnysideup avatar Aug 19 '19 22:08 sonnysideup

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

fejta-bot avatar Nov 17 '19 23:11 fejta-bot

/remove-lifecycle stale

NissesSenap avatar Nov 19 '19 22:11 NissesSenap

Any exact roadmap on this one?

irvifa avatar Jan 01 '20 05:01 irvifa

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.

darktempla avatar Jan 21 '20 02:01 darktempla

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

arielb135 avatar Mar 08 '20 11:03 arielb135

/assign

palnabarun avatar Mar 25 '20 21:03 palnabarun

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

fejta-bot avatar Jun 23 '20 21:06 fejta-bot

/remove-lifecycle stale

bguseman avatar Jul 09 '20 20:07 bguseman

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.

aflag avatar Aug 19 '20 16:08 aflag

Back to Go it is

NickLarsenNZ avatar Aug 25 '20 07:08 NickLarsenNZ

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

shiba24 avatar Oct 04 '20 10:10 shiba24

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.

askreet avatar Dec 16 '20 18:12 askreet

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

fejta-bot avatar Mar 16 '21 18:03 fejta-bot

@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?

kdebisschop avatar Apr 05 '21 11:04 kdebisschop

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

fejta-bot avatar May 05 '21 12:05 fejta-bot

+1 for this feature very useful

mayeuldalzon avatar May 12 '21 14:05 mayeuldalzon

/remove-lifecycle rotten

palnabarun avatar Jun 07 '21 07:06 palnabarun

@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 avatar Jun 07 '21 07:06 palnabarun

/lifecycle frozen

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

palnabarun avatar Jun 07 '21 07:06 palnabarun

That would be an awesome feature for operator testing.

TomaszKlosinski avatar Nov 22 '21 16:11 TomaszKlosinski

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?

bubthegreat avatar Aug 04 '22 18:08 bubthegreat

I would appreciate this feature too.

PatrikDubec avatar Aug 15 '22 11:08 PatrikDubec

Any progress on this?

allenmqcymp avatar Dec 06 '22 22:12 allenmqcymp

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

Pithikos avatar Jan 28 '23 09:01 Pithikos

+1 on this

daveortiz-ctct avatar May 18 '23 15:05 daveortiz-ctct