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

experimental matchers for integration testing #1653

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ripienaar
Copy link
Member

Signed-off-by: R.I.Pienaar [email protected]

@@ -137,7 +137,7 @@ var _ = Describe("rpcutil agent", func() {
It("Should fetch correct collective info", func() {
res, err := rpcutilClient.OptionTargets([]string{"localhost"}).CollectiveInfo().Do(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(res.Stats().OKCount()).To(Equal(1))
Expect(res).To(HaveOnlySuccessfulResponses())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vjanelle this is what I was talking about on slack, give me some more ideas of what you want to be testable and I can see.

Ultimately I want to expand this out enough that I can stand up a infra using docker compose and write integration tests against that asserting things like did all servers get correctly provisioned etc

Copy link
Member Author

@ripienaar ripienaar Apr 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other things I am just kind of thinking about - for the context of testing a deployed infra

  • HaveDiscoverableNodes(count) - discovers using the generated rpc client and asserts against them
  • HaveOutputWithValue(output, key, value) - takes any generated client result looking for key and val, those found with res.AllOutputs(), I might add some accessors to find more like by name or something
  • HaveAllOutputsWithValue(res, key, value) - loops all the outputs and checks they all have the same key and value, this from any generated client reply

If I can't make this work - or I dont want to require generics - we could look at a generic thing like

Expect(RPC("agent", "action", input, filter)).To(HaveAllRepliesWithValue(key, value)

or

res,err:=RPC("agent", "action", input, filter)
Expect(err).ToNot(HaveOccured())
Expect(res).To(HaveAllRepliesWithValue(key, value)

ie. a small RPC caller helper that doesnt use the generated client - this for testing agents. But I suspect you want to test the clients right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative approach would be to add an assert package to each generated client with things like:

package assert

import (
	"testing"

	"github.com/choria-io/go-choria/client/rpcutilclient"
)

type StatsProvider interface {
	Stats() rpcutilclient.Stats
}

func HaveUnexpectedResponders(t *testing.T, res StatsProvider, count int) {
	ur := len(res.Stats().UnexpectedResponseFrom())
	switch {
	case count < 0:
		if ur == 0 {
			t.Fatalf("Expected >0 unexpected responders")
		}
	case count == 0:
		if ur > 0 {
			t.Fatalf("Expected no unexpected responders")
		}
	default:
		if ur != count {
			t.Fatalf("Expected %d unexpected responders but had %d", count, ur)
		}
	}
}
res, _ := rpcutilClient.AgentInventory().Do(ctx)
assert.HaveUnexpectedResponders(t, res, 0)

This would be usable in any testing framework for go and less casting and weird stuff since it would just use the package internals?

I could even put this in the package directory as AssertHaveUnexpectedResponders() (ie. not on the instance) since then it would have access to the private variables in the client, I think I quite like this it avoids having to pollute the clients with weird accessors

Signed-off-by: R.I.Pienaar <[email protected]>
Signed-off-by: R.I.Pienaar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant