Skip to content

Improve distribution tests in conformance for MeshHTTPRouteWeight #3855

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion conformance/tests/mesh/httproute-weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
"errors"
"fmt"
"math"
"math/rand"
"slices"
"strings"
"sync"
"testing"
"time"

"golang.org/x/sync/errgroup"

Expand Down Expand Up @@ -89,7 +91,9 @@ func testDistribution(t *testing.T, client echo.MeshPod, expected http.ExpectedR
g.SetLimit(concurrentRequests)
for i := 0.0; i < totalRequests; i++ {
g.Go(func() error {
_, cRes, err := client.CaptureRequestResponseAndCompare(t, expected)
uniqueExpected := expected
addEntropy(&uniqueExpected)
_, cRes, err := client.CaptureRequestResponseAndCompare(t, uniqueExpected)
if err != nil {
return fmt.Errorf("failed: %w", err)
}
Expand Down Expand Up @@ -141,3 +145,21 @@ func testDistribution(t *testing.T, client echo.MeshPod, expected http.ExpectedR
})
return errors.Join(errs...)
}

// addEntropy adds jitter to the request by adding either a delay up to 1 second, or a random header value, or both.
func addEntropy(exp *http.ExpectedResponse) {
delay := func() { time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond) } //nolint:gosec // This is test code to get random value.
randomHeader := func() {
Copy link
Member

Choose a reason for hiding this comment

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

why do we need this no lint?

Copy link
Author

Choose a reason for hiding this comment

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

I noticed that earlier before adding the no lint, prow test was failing here due to:
G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand) (gosec)

so i followed a similar example here, to ignore linter for testing purposes.

Copy link
Member

Choose a reason for hiding this comment

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

maybe using crypto.rand would make it happy?

Copy link
Author

Choose a reason for hiding this comment

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

fixed! slightly lengthier due to error handling 😃

also re-ran the tests after using crypto.rand, attaching the new output test logs here:
MeshHTTPRouteWeight_updated.log

exp.Request.Headers = make(map[string]string)
exp.Request.Headers["X-Jitter"] = fmt.Sprintf("%d", rand.Intn(9999)) //nolint:gosec // This is test code to get random value.
}
switch rand.Intn(3) { //nolint:gosec // This is test code to get random value
case 0:
delay()
case 1:
randomHeader()
case 2:
delay()
randomHeader()
}
}