argo-cd icon indicating copy to clipboard operation
argo-cd copied to clipboard

test: add unit test for read and write function for websocket

Open nitishfy opened this issue 1 year ago • 3 comments

Checklist:

Closes #19550

  • [ ] Either (a) I've created an enhancement proposal and discussed it with the community, (b) this is a bug fix, or (c) this does not need to be in the release notes.
  • [x] The title of the PR states what changed and the related issues number (used for the release note).
  • [x] The title of the PR conforms to the Toolchain Guide
  • [x] I've included "Closes [ISSUE #]" or "Fixes [ISSUE #]" in the description to automatically close the associated issue.
  • [ ] I've updated both the CLI and UI to expose my feature, or I plan to submit a second PR with them.
  • [ ] Does this PR require documentation updates?
  • [ ] I've updated documentation as required by this PR.
  • [x] I have signed off all my commits as required by DCO
  • [x] I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
  • [ ] My build is green (troubleshooting builds).
  • [ ] My new feature complies with the feature status guidelines.
  • [ ] I have added a brief description of why this PR is necessary and/or what this PR solves.
  • [ ] Optional. My organization is added to USERS.md.
  • [ ] Optional. For bug fixes, I've indicated what older releases this fix should be cherry-picked into (this may or may not happen depending on risk/complexity).

nitishfy avatar Aug 20 '24 09:08 nitishfy

:x: Preview Environment deleted from Bunnyshell

Available commands (reply to this comment):

  • :rocket: /bns:deploy to deploy the environment

bunnyshell[bot] avatar Aug 20 '24 09:08 bunnyshell[bot]

:x: Preview Environment deleted from Bunnyshell

Available commands (reply to this comment):

  • :rocket: /bns:deploy to deploy the environment

bunnyshell[bot] avatar Aug 20 '24 09:08 bunnyshell[bot]

Putting this PR on hold since the test cases are failing due to nil pointer dereference error inspite of passing the correct values. Can i get some hints to fix this and simplify the test cases?

On running the unit test for Read

=== RUN   TestTerminalSession_Read
=== RUN   TestTerminalSession_Read/stdin_operation
    websocket_test.go:197: Failed to write message: write tcp 127.0.0.1:37227->127.0.0.1:48764: write: broken pipe
--- FAIL: TestTerminalSession_Read (0.00s)
    --- FAIL: TestTerminalSession_Read/stdin_operation (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x32bf29b]

goroutine 14 [running]:
testing.tRunner.func1.2({0x35fbf40, 0x572b6c0})
	/usr/local/go/src/testing/testing.go:1545 +0x238
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1548 +0x397
panic({0x35fbf40?, 0x572b6c0?})
	/usr/local/go/src/runtime/panic.go:914 +0x21f
github.com/argoproj/argo-cd/v2/server/application.(*terminalSession).performValidationsAndReconnect(0xc000bf79e0?, {0xc000bf7af8?, 0x5883d40?, 0xc000bf7a48?})
	/home/nitish/Documents/Work/Open-Source/argo-cd/server/application/websocket.go:162 +0x1b
github.com/argoproj/argo-cd/v2/server/application.(*terminalSession).Read(0xc00035a000, {0xc000bf7af8, 0x400, 0x0?})
	/home/nitish/Documents/Work/Open-Source/argo-cd/server/application/websocket.go:183 +0x37
github.com/argoproj/argo-cd/v2/server/application.TestTerminalSession_Read.func2(0xc0002dd380)
	/home/nitish/Documents/Work/Open-Source/argo-cd/server/application/websocket_test.go:250 +0x250
testing.tRunner(0xc0002dd380, 0xc000694d50)
	/usr/local/go/src/testing/testing.go:1595 +0xff
created by testing.(*T).Run in goroutine 13
	/usr/local/go/src/testing/testing.go:1648 +0x3ad


Process finished with the exit code 1

On running the unit test for Write:

=== RUN   TestTerminalSession_Write
=== RUN   TestTerminalSession_Write/simple_message
=== RUN   TestTerminalSession_Write/empty_message
2024/08/20 15:25:28 http: panic serving 127.0.0.1:40110: Fail in goroutine after TestTerminalSession_Write/simple_message has completed
goroutine 66 [running]:
net/http.(*conn).serve.func1()
	/usr/local/go/src/net/http/server.go:1868 +0xb9
panic({0x345fbc0?, 0xc000c0a160?})
	/usr/local/go/src/runtime/panic.go:920 +0x270
testing.(*common).Fail(0xc000503380)
	/usr/local/go/src/testing/testing.go:952 +0xd4
testing.(*common).Errorf(0xc000503380, {0x39da1c7?, 0x131cbe5?}, {0xc000c0a120?, 0x34603c0?, 0x201?})
	/usr/local/go/src/testing/testing.go:1069 +0x5e
github.com/stretchr/testify/assert.Fail({0x40aae20, 0xc000503380}, {0xc000c1a0e0, 0x64}, {0x0, 0x0, 0x0})
	/home/nitish/go/pkg/mod/github.com/stretchr/[email protected]/assert/assertions.go:363 +0x350
github.com/stretchr/testify/assert.NoError({0x40aae20, 0xc000503380}, {0x40abba0, 0xc0003f0320}, {0x0, 0x0, 0x0})
	/home/nitish/go/pkg/mod/github.com/stretchr/[email protected]/assert/assertions.go:1545 +0xfc
github.com/stretchr/testify/require.NoError({0x40b3a38, 0xc000503380}, {0x40abba0, 0xc0003f0320}, {0x0, 0x0, 0x0})
	/home/nitish/go/pkg/mod/github.com/stretchr/[email protected]/require/require.go:1354 +0x8f
github.com/argoproj/argo-cd/v2/server/application.TestTerminalSession_Write.func1.1({0x40c4220, 0xc000c1c000}, 0xc000560160?)
	/home/nitish/Documents/Work/Open-Source/argo-cd/server/application/websocket_test.go:316 +0x28a
net/http.HandlerFunc.ServeHTTP(0x5881b60?, {0x40c4220?, 0xc000c1c000?}, 0xc000b91b50?)
	/usr/local/go/src/net/http/server.go:2136 +0x29
net/http.serverHandler.ServeHTTP({0xc000c08030?}, {0x40c4220?, 0xc000c1c000?}, 0x6?)
	/usr/local/go/src/net/http/server.go:2938 +0x8e
net/http.(*conn).serve(0xc000596120, {0x40d1aa0, 0xc000b980f0})
	/usr/local/go/src/net/http/server.go:2009 +0x5f4
created by net/http.(*Server).Serve in goroutine 29
	/usr/local/go/src/net/http/server.go:3086 +0x5cb
=== RUN   TestTerminalSession_Write/long_message
2024/08/20 15:25:28 http: panic serving 127.0.0.1:47286: Fail in goroutine after TestTerminalSession_Write/empty_message has completed
goroutine 67 [running]:
net/http.(*conn).serve.func1()
	/usr/local/go/src/net/http/server.go:1868 +0xb9
panic({0x345fbc0?, 0xc000c0a220?})
	/usr/local/go/src/runtime/panic.go:920 +0x270
testing.(*common).Fail(0xc00024c680)
	/usr/local/go/src/testing/testing.go:952 +0xd4
testing.(*common).Errorf(0xc00024c680, {0x39da1c7?, 0x131cbe5?}, {0xc000c0a1e0?, 0x34603c0?, 0x101?})
	/usr/local/go/src/testing/testing.go:1069 +0x5e
github.com/stretchr/testify/assert.Fail({0x40aae20, 0xc00024c680}, {0xc000c18240, 0x53}, {0x0, 0x0, 0x0})
	/home/nitish/go/pkg/mod/github.com/stretchr/[email protected]/assert/assertions.go:363 +0x350
github.com/stretchr/testify/assert.NoError({0x40aae20, 0xc00024c680}, {0x40ac0e0, 0x5730710}, {0x0, 0x0, 0x0})
	/home/nitish/go/pkg/mod/github.com/stretchr/[email protected]/assert/assertions.go:1545 +0xfc
github.com/stretchr/testify/require.NoError({0x40b3a38, 0xc00024c680}, {0x40ac0e0, 0x5730710}, {0x0, 0x0, 0x0})
	/home/nitish/go/pkg/mod/github.com/stretchr/[email protected]/require/require.go:1354 +0x8f
github.com/argoproj/argo-cd/v2/server/application.TestTerminalSession_Write.func1.1({0x40c4220, 0xc000ca20e0}, 0xc0000209a0?)
	/home/nitish/Documents/Work/Open-Source/argo-cd/server/application/websocket_test.go:316 +0x28a
net/http.HandlerFunc.ServeHTTP(0x5881b60?, {0x40c4220?, 0xc000ca20e0?}, 0xc000ba1b50?)
	/usr/local/go/src/net/http/server.go:2136 +0x29
net/http.serverHandler.ServeHTTP({0xc000c82450?}, {0x40c4220?, 0xc000ca20e0?}, 0x6?)
	/usr/local/go/src/net/http/server.go:2938 +0x8e
net/http.(*conn).serve(0xc0005961b0, {0x40d1aa0, 0xc000b982a0})
	/usr/local/go/src/net/http/server.go:2009 +0x5f4
created by net/http.(*Server).Serve in goroutine 83
	/usr/local/go/src/net/http/server.go:3086 +0x5cb
=== RUN   TestTerminalSession_Write/special_chars
2024/08/20 15:25:28 http: panic serving 127.0.0.1:42126: Fail in goroutine after TestTerminalSession_Write/long_message has completed
goroutine 86 [running]:
net/http.(*conn).serve.func1()
	/usr/local/go/src/net/http/server.go:1868 +0xb9
panic({0x345fbc0?, 0xc00077e320?})
	/usr/local/go/src/runtime/panic.go:920 +0x270
testing.(*common).Fail(0xc0004eaea0)
	/usr/local/go/src/testing/testing.go:952 +0xd4
testing.(*common).Errorf(0xc0004eaea0, {0x39da1c7?, 0x131cbe5?}, {0xc00077e2e0?, 0x34603c0?, 0x201?})
	/usr/local/go/src/testing/testing.go:1069 +0x5e
github.com/stretchr/testify/assert.Fail({0x40aae20, 0xc0004eaea0}, {0xc000bb4070, 0x64}, {0x0, 0x0, 0x0})
	/home/nitish/go/pkg/mod/github.com/stretchr/[email protected]/assert/assertions.go:363 +0x350
github.com/stretchr/testify/assert.NoError({0x40aae20, 0xc0004eaea0}, {0x40abba0, 0xc0009864b0}, {0x0, 0x0, 0x0})
	/home/nitish/go/pkg/mod/github.com/stretchr/[email protected]/assert/assertions.go:1545 +0xfc
github.com/stretchr/testify/require.NoError({0x40b3a38, 0xc0004eaea0}, {0x40abba0, 0xc0009864b0}, {0x0, 0x0, 0x0})
	/home/nitish/go/pkg/mod/github.com/stretchr/[email protected]/require/require.go:1354 +0x8f
github.com/argoproj/argo-cd/v2/server/application.TestTerminalSession_Write.func1.1({0x40c4220, 0xc000c1d260}, 0xc000560420?)
	/home/nitish/Documents/Work/Open-Source/argo-cd/server/application/websocket_test.go:316 +0x28a
net/http.HandlerFunc.ServeHTTP(0x5881b60?, {0x40c4220?, 0xc000c1d260?}, 0xc000b9ab50?)
	/usr/local/go/src/net/http/server.go:2136 +0x29
net/http.serverHandler.ServeHTTP({0xc000c827b0?}, {0x40c4220?, 0xc000c1d260?}, 0x6?)
	/usr/local/go/src/net/http/server.go:2938 +0x8e
net/http.(*conn).serve(0xc000c8c360, {0x40d1aa0, 0xc000c82690})
	/usr/local/go/src/net/http/server.go:2009 +0x5f4
created by net/http.(*Server).Serve in goroutine 69
	/usr/local/go/src/net/http/server.go:3086 +0x5cb
--- FAIL: TestTerminalSession_Write (0.00s)
    --- PASS: TestTerminalSession_Write/simple_message (0.00s)
    assertions.go:363: 
        	Error Trace:	/home/nitish/Documents/Work/Open-Source/argo-cd/server/application/websocket_test.go:316
        	            				/usr/local/go/src/net/http/server.go:2136
        	            				/usr/local/go/src/net/http/server.go:2938
        	            				/usr/local/go/src/net/http/server.go:2009
        	            				/usr/local/go/src/runtime/asm_amd64.s:1650
        	Error:      	Received unexpected error:
        	            	read tcp 127.0.0.1:34035->127.0.0.1:40110: read: connection reset by peer
        	Test:       	TestTerminalSession_Write/simple_message
    --- PASS: TestTerminalSession_Write/empty_message (0.00s)
    assertions.go:363: 
        	Error Trace:	/home/nitish/Documents/Work/Open-Source/argo-cd/server/application/websocket_test.go:316
        	            				/usr/local/go/src/net/http/server.go:2136
        	            				/usr/local/go/src/net/http/server.go:2938
        	            				/usr/local/go/src/net/http/server.go:2009
        	            				/usr/local/go/src/runtime/asm_amd64.s:1650
        	Error:      	Received unexpected error:
        	            	websocket: close 1006 (abnormal closure): unexpected EOF
        	Test:       	TestTerminalSession_Write/empty_message
    --- PASS: TestTerminalSession_Write/long_message (0.00s)
    assertions.go:363: 
        	Error Trace:	/home/nitish/Documents/Work/Open-Source/argo-cd/server/application/websocket_test.go:316
        	            				/usr/local/go/src/net/http/server.go:2136
        	            				/usr/local/go/src/net/http/server.go:2938
        	            				/usr/local/go/src/net/http/server.go:2009
        	            				/usr/local/go/src/runtime/asm_amd64.s:1650
        	Error:      	Received unexpected error:
        	            	read tcp 127.0.0.1:42767->127.0.0.1:42126: read: connection reset by peer
        	Test:       	TestTerminalSession_Write/long_message
    --- PASS: TestTerminalSession_Write/special_chars (0.00s)

FAIL

cc @reggie-k @pasha-codefresh

nitishfy avatar Aug 20 '24 09:08 nitishfy

@crenshaw-dev can you please take a look?

nitishfy avatar Aug 24 '24 12:08 nitishfy

@leoluz can you provide feedback to fix the unit tests?

nitishfy avatar Oct 01 '24 15:10 nitishfy

Codecov Report

Attention: Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Project coverage is 55.23%. Comparing base (8841b0d) to head (71e655e). Report is 430 commits behind head on master.

Files with missing lines Patch % Lines
server/application/websocket.go 0.00% 5 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #19598      +/-   ##
==========================================
+ Coverage   55.15%   55.23%   +0.07%     
==========================================
  Files         337      337              
  Lines       57056    57059       +3     
==========================================
+ Hits        31472    31516      +44     
+ Misses      22879    22846      -33     
+ Partials     2705     2697       -8     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Dec 19 '24 06:12 codecov[bot]