Operator-sdk: How do I actually test my operator?

Created on 21 May 2019  路  8Comments  路  Source: operator-framework/operator-sdk

Hi,

we are using this SDK for deploying some services to Kubernetes but we are lacking tests. We test it all manually by running it.

What are the best practices for testing my operator? There is not any documentation about operator testing. I was looking into this repository, in test/e2e/memcached_test.go, but honestly I dont have a clue how that test is run. Travis build does not seem to run that and by simply running it from IDE (GoLand), it says that:

time="2019-05-21T13:22:35+10:00" level=fatal 
msg="Failed to change directory to project root: chdir : no such file or directory"

How am I supposed to actually run this?

EDIT

Ok so this is something:

[smiklosovic@E091-FED operator-sdk]$ ./hack/tests/e2e-go.sh 
+ go test ./test/e2e/... -root=. -globalMan=testdata/empty.yaml
--- FAIL: TestMemcached (189.76s)
    memcached_test.go:95: Creating new operator project
    memcached_test.go:312: Failed to read go.mod: open go.mod: no such file or directory
FAIL
FAIL    github.com/operator-framework/operator-sdk/test/e2e     196.646s

What to do now?

triagsupport

Most helpful comment

Cool, please leave this open for a while, maybe I ll have a couple of other questions, I was successful in coding e2e tests and they run fine.

All 8 comments

Hi @smiklosovic,

The unit test documentation is: https://github.com/operator-framework/operator-sdk/blob/master/doc/user/unit-testing.md

Also, it has a PR to be improved see: ( I hope that you find easier to check the info in this PR) : https://github.com/operator-framework/operator-sdk/blob/8fb5317317d58a46eb28ca11cdf3e0ba2eb39c93/doc/user/unit-testing.md

The e2e-tests is here: https://github.com/operator-framework/operator-sdk/blob/2f772d1dc2340dd19bdc3ec8c2dc9f0f77cc8297/doc/test-framework/writing-e2e-tests.md

NOTE: I'd suggest you start by the unit test them after working on in the integration.

Please, let us know if it is the info that you are looking for.

HI @camilamacedo86 ,

thanks for links, definitely helpful.

What are the practical differences between local and cluster testing and when would one choose the former approach instead of the latter? Any pros and cons? Can you elaborate on this little bit?

Thanks

More to it, is it possible to unit test e.g stateful sets?

I understand that I can use fake client for my reconciler so it would be "as if" that client interacted with a cluster but it does not so there are not any pods running or similar.

On the other hand, our reconciliation logic does make some assumptions about the state of a cluster of services before scaling up or down hence we need to call something like:

func existingPods(c client.Client) ([]corev1.Pod, error) {

    podList := corev1.PodList{}
    listOps := &client.ListOptions{
        Namespace:     cdc.Namespace,
    }

    if err := c.List(context.TODO(), listOps, &podList); err != nil {
        return nil, err
    }

    return podList.Items, nil
}

Is there any way how to mock this c.List() ?

Hi @smiklosovic,

My suggestion is because I believe that is easier getting started by the unit test to know how you will do it. Regards the other question raised, if your reconcile is creating these objects I don't think you need to mock them. You can check this unit-test file in a project that I have been working on.

I hope that this info helps you.

Cool, please leave this open for a while, maybe I ll have a couple of other questions, I was successful in coding e2e tests and they run fine.

Closing this as it's been open for a while, but if you run into any other problems you can always open a new issue.

I have similar error message as @smiklosovic in 1st post -

go test ./...
?       metrics-operator/cmd/manager    [no test files]
time="2019-09-19T08:31:26+02:00" level=fatal msg="Failed to change directory to project root: chdir : The filename, directory name, or volume label syntax is incorrect."

All I did is added main_test.go file in e2e directory as described in:
https://github.com/operator-framework/operator-sdk/blob/master/doc/test-framework/writing-e2e-tests.md#main-test

It is windows 10 so it may be issue here ; /

HI @michalkarolik,

Could you please open a new issue and provide all details which will be asked for its template? ( SO, operator-sk version and etc .. ) Also, note that it is regarded as ./hack/tests/e2e-go.sh and not go test as you executed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

surajssd picture surajssd  路  3Comments

nrvnrvn picture nrvnrvn  路  5Comments

danielsig727 picture danielsig727  路  4Comments

ricardozanini picture ricardozanini  路  4Comments

camilamacedo86 picture camilamacedo86  路  4Comments