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

Flaky test TestGetRoundTripperTLSConfig #4732

Closed
yurishkuro opened this issue Sep 6, 2023 · 8 comments · Fixed by #4738
Closed

Flaky test TestGetRoundTripperTLSConfig #4732

yurishkuro opened this issue Sep 6, 2023 · 8 comments · Fixed by #4738
Labels
bug good first issue Good for beginners help wanted Features that maintainers are willing to accept but do not have cycles to implement

Comments

@yurishkuro
Copy link
Member

yurishkuro commented Sep 6, 2023

assert.Equal(t, "Bearer foo", r.Header.Get("Authorization"))

=== RUN   TestGetRoundTripperTLSConfig/tls_tlsEnabled
    reader_test.go:542: 
        	Error Trace:	/home/runner/work/jaeger/jaeger/plugin/metrics/prometheus/metricsstore/reader_test.go:542
        	            				/opt/hostedtoolcache/go/1.21.0/x64/src/net/http/server.go:2136
        	            				/opt/hostedtoolcache/go/1.21.0/x64/src/net/http/server.go:2938
        	            				/opt/hostedtoolcache/go/1.21.0/x64/src/net/http/server.go:2009
        	            				/opt/hostedtoolcache/go/1.21.0/x64/src/runtime/asm_amd64.s:1650
        	Error:      	Not equal: 
        	            	expected: "***"
        	            	actual  : ""
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-***
        	            	+
        	Test:       	TestGetRoundTripperTLSConfig/tls_tlsEnabled
=== RUN   TestGetRoundTripperTLSConfig/tls_disabled
    reader_test.go:542: 
        	Error Trace:	/home/runner/work/jaeger/jaeger/plugin/metrics/prometheus/metricsstore/reader_test.go:542
        	            				/opt/hostedtoolcache/go/1.21.0/x64/src/net/http/server.go:2136
        	            				/opt/hostedtoolcache/go/1.21.0/x64/src/net/http/server.go:2938
        	            				/opt/hostedtoolcache/go/1.21.0/x64/src/net/http/server.go:2009
        	            				/opt/hostedtoolcache/go/1.21.0/x64/src/runtime/asm_amd64.s:1650
        	Error:      	Not equal: 
        	            	expected: "***"
        	            	actual  : ""
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-***
        	            	+
        	Test:       	TestGetRoundTripperTLSConfig/tls_disabled
--- FAIL: TestGetRoundTripperTLSConfig (0.11s)
    --- FAIL: TestGetRoundTripperTLSConfig/tls_tlsEnabled (0.11s)
    --- FAIL: TestGetRoundTripperTLSConfig/tls_disabled (0.00s)
@yurishkuro yurishkuro added bug help wanted Features that maintainers are willing to accept but do not have cycles to implement good first issue Good for beginners labels Sep 6, 2023
@CodesbyUnnati
Copy link

Hey, I would like to work on this issue.

@CodesbyUnnati
Copy link

CodesbyUnnati commented Sep 6, 2023

Hey @yurishkuro, I am unable to replicate this issue. When I'm running make test command or running this unit test it is passing the test case. Please help me replicate this issue. Thanks

@yurishkuro
Copy link
Member Author

Flaky tests are difficult to reproduce reliably. Sometimes you can run it multiple times with -n param and encounter a failure.

@CodesbyUnnati
Copy link

CodesbyUnnati commented Sep 8, 2023

I ran the make test -n command multiple times, but can't replicate the issue 😢 How can I fix this issue.

@albertteoh
Copy link
Contributor

I'm a little confused about the test error. The assertion is:

 assert.Equal(t, "Bearer foo", r.Header.Get("Authorization")) 

But the test fails with:

        	Error:      	Not equal: 
        	            	expected: "***"
        	            	actual  : ""

I expected the assertion error to contain something like expected: "Bearer foo", or have I misunderstood something?

@yurishkuro do you have the link to the error run or was it something you encountered from a local run of the test?

@yurishkuro
Copy link
Member Author

@albertteoh it wasn't local, it was from CI. I suspect that the stars are somehow substituted by GitHub runner. If you change the string to foo1 and run the test, the assertion fails with clear text as expected. The fact that actual is shown as empty string suggests that the Authorization header was not sent - I couldn't figure out how that could happen given the code of the test. One option we have is to add t.Logf("request %v", r) to the handler - in the other PR I merged today GitHub was not trying to mask the Authorization header:

    factory_test.go:286: request to fake ES server: &{HEAD / HTTP/1.1 1 1 map[Accept:[application/json] Authorization:[Basic OmZpcnN0IHBhc3N3b3Jk] Content-Type:[application/json] User-Agent:[elastic/6.2.37 (linux-amd64)]] {} <nil> 0 [] false 127.0.0.1:45253 map[] map[] <nil> map[] 127.0.0.1:59670 / <nil> <nil> <nil> 0xc000536b40}

@albertteoh
Copy link
Contributor

Yeah, that's a good idea. I'll put that in.

I'm wondering if httptest.NewServer will strictly guarantee isolation to this test. Just speculating if another test or another process is coincidentally hitting that same test server URL so the assertion is prematurely invoked before (or after?) the actual test makes the request.

As such, I wonder if it's worthwhile changing the test to have a boolean variable outside and setting this to true only if it's received a request where the auth header is Bearer foo? It has the added benefit of actually asserting we received the request. The current test can still pass even though we didn't receive that request.

So something like:

			var gotExpectedRequest bool
			server := httptest.NewServer(
				http.HandlerFunc(
					func(w http.ResponseWriter, r *http.Request) {
						if r.Header.Get("Authorization") == "Bearer foo" {
							gotExpectedRequest = true
						}
					},
				),
			)
...
			assert.True(t, gotExpectedRequest)

@yurishkuro
Copy link
Member Author

I believe each test server runs on a random port, so crossover should not happen.

I agree that it's better to have the handler store the data somewhere and then assert on that data in the main test. Example: https://github.com/jaegertracing/jaeger/pull/4342/files#diff-fb005680ccab1461b654fe14dcb0a53eb862af4f9bfc15b279731e287083b5daR285 - this one is a bit more complicated because basic auth is encoded and I didn't want to compare values in the encoded form (not informative if assertion fails). Note that you should use synchronized storage var, otherwise your test may cause race condition detector to flag it.

yurishkuro added a commit that referenced this issue Sep 9, 2023
## Which problem is this PR solving?
- Resolves #4732

## Description of the changes
- Introduces an atomic boolean outside of the HTTP test handler and only
set to `true` if the expected request is received.
- This should prevent cases where unrelated processes are hitting the
same test URL.

## How was this change tested?
- Ran `make test` to pass.
- Ran `go test -tags=memory_storage_integration -count 10 ./...` and
confirmed `metricsstore` tests are passing.
- Note that a number of unrelated tests were failing with the `-count
10` flag set, **not as a result of the changes from this PR**, namely:
```
--- FAIL: TestFactory (0.00s)
    --- FAIL: TestFactory/#00 (0.00s)
panic: Reuse of exported var name: test_1694287567920905000_counter_x
 [recovered]
	panic: Reuse of exported var name: test_1694287567920905000_counter_x
```
and
```
--- FAIL: TestPublishOpts (0.00s)
    builder_test.go:308:
        	Error Trace:	/Users/albertteoh/go/src/github.com/albertteoh/jaeger/cmd/agent/app/builder_test.go:308
        	Error:      	Received unexpected error:
        	            	cannot create processors: cannot create Thrift Processor: cannot create UDP Server: cannot create UDPServerTransport: listen udp :5775: bind: address already in use
        	Test:       	TestPublishOpts
```

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [x] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `yarn lint` and `yarn test`

---------

Signed-off-by: Albert Teoh <[email protected]>
Signed-off-by: Yuri Shkuro <[email protected]>
Co-authored-by: Albert Teoh <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue Good for beginners help wanted Features that maintainers are willing to accept but do not have cycles to implement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants