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

Improve how we use git in eext and improve the code's testing facilities. #152

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

mkisielewski-arista
Copy link
Contributor

@mkisielewski-arista mkisielewski-arista commented Jan 8, 2025

Previously the tests for code that sourced packages with git was downloading actual repositories. This PR rearranges the code a bit to make it more testable, introduces a simple executor mock, and uses it, making the whole process more contained.

It also has a small fix (97b67a2) that properly handles commits used as revisions. The new tests also cover this logic path.

@mkisielewski-arista mkisielewski-arista force-pushed the mkisielewski-improve-git-shenanigans branch from 4a3212b to 7f5bb57 Compare January 8, 2025 21:42
"code.arista.io/eos/tools/eext/util"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
//"code.arista.io/eos/tools/eext/executor/mocked_executor"
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment can be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, sorry :D

// MockedExecutor returns the Response objects for each of the MockedExecutor
// calls. The responses are consumed in the natural order, meaning [0] is the
// first one to be returned.
Responses []Response
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we consider making this a struct of Call and Response.

type MockedExecutor struct {
    MockedEntries []struct{
        Response Response
        Call RecordedCall
    }
}

Currently the onus of maintaining the order of Response is on the dev, and I feel the mapping might get messier with longer Response slices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It took me a while, but I think I get what you mean now...
This is unpaired in my branch, because it's not guaranteed that the actual call that was made corresponds to given Response.

Example:
The dev writes tests for function foobar that is supposed to call add 2 times. Once with (1, 1), and second time with (2, 2). So the dev creates two Response objects - one that returns 2 and one that returns 4.
It turns out that somebody patched foobar and now it calls add 4 times - (1,1), (3,5), (2,2), and (5,0). This means that there are 4 recorded calls, and the test author expected only 2.

My approach is somewhat standard and follows approach from python:
https://docs.python.org/3/library/unittest.mock.html

In one of the examples they give this:

mock.side_effect = [5, 4, 3, 2, 1]
mock(), mock(), mock()
(5, 4, 3)

5 responses, and only 3 RecordedCalls.

I'm changing the name of Calls to RecordedCalls to hopefully make it less misleading.

if err != nil {
t.Fatal(err)
mex := executor.MockedExecutor{
Responses: []executor.Response{executor.NewResponse(0, "libpcap-1.10.1", nil)},
Copy link
Contributor

Choose a reason for hiding this comment

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

This Response is the mocked output for the call
rpmName, err := executor.Output("rpmspec", cmd...)
within the getRpmNameFromSpecFile().

But that isn't evident here.
Furthermore a reason to have a struct of Call and Response for easier readability.

@@ -15,7 +15,7 @@ type DryRunExecutor struct {
// extra abstractions feel unnecessary.

// human friendly description of what an invocation would do
invocations []string
Invocations []string
Copy link
Contributor

Choose a reason for hiding this comment

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

We aren't using this member outside this file. Do we need to export it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At this moment nope. Good idea. Let me "unexport" it.

The purpose of the function was missing, and the comment grammar implied that
only the pull is being done.
There was a lot of boiler plate that was hard to follow, so this patch
introduces a simple list of git commands to run. It should also yield
potentially better error messages in case of a failure.
@mkisielewski-arista mkisielewski-arista force-pushed the mkisielewski-improve-git-shenanigans branch from 5b6db92 to c8221bb Compare February 24, 2025 14:05
@mkisielewski-arista
Copy link
Contributor Author

I've updated the branch with improvements suggested by Manith

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.

2 participants