8000 feat: tests for cli.go SignReq by PabloHiro · Pull Request #1346 · ansible/receptor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: tests for cli.go SignReq #1346

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&rdqu 8000 o;, 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

Merged
merged 1 commit into from
Jun 20, 2025

Conversation

PabloHiro
Copy link
Contributor
@PabloHiro PabloHiro commented Jun 18, 2025

Changes:

  1. Make SignReq mockable
  2. Move the logic of SignReqConfig.Run() into a separate method named ValidateAndSign

New coverage can be seen here:
https://app.codecov.io/gh/ansible/receptor/pull/1346/blob/pkg/certificates/cli.go?dropdown=coverage

github.com/ansible/receptor/pkg/certificates/cli.go:173:        SignReq                 89.4%
github.com/ansible/receptor/pkg/certificates/cli.go:247:        ValidateAndSign         100.0%
github.com/ansible/receptor/pkg/certificates/cli.go:267:        Run                     0.0%

Run is 0% but does not contain any logic.

@PabloHiro PabloHiro force-pushed the feature/tests-signreq-cli branch 2 times, most recently from b40757a to 678031d Compare June 18, 2025 09:39
Copy link
codecov bot commented Jun 18, 2025

Codecov Report

Attention: Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.

Please upload report for BASE (devel@334b724). Learn more about missing BASE report.
Report is 2 commits behind head on devel.

Files with missing lines Patch % Lines
pkg/certificates/cli.go 60.00% 2 Missing ⚠️
@@           Coverage Diff            @@
##             devel    #1346   +/-   ##
========================================
  Coverage         ?   47.97%           
========================================
  Files            ?       62           
  Lines            ?    10360           
  Branches         ?        0           
========================================
  Hits             ?     4970           
  Misses           ?     5071           
  Partials         ?      319           
Files with missing lines Coverage Δ
pkg/certificates/cli.go 62.29% <60.00%> (ø)
Components Coverage Δ
Go 47.89% <60.00%> (?)
Receptorctl 49.31% <ø> (?)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@PabloHiro PabloHiro force-pushed the feature/tests-signreq-cli branch 2 times, most recently from 239a187 to 7ddd0a6 Compare June 18, 2025 11:24
@PabloHiro PabloHiro force-pushed the feature/tests-signreq-cli branch from 7ddd0a6 to 0e44a67 Compare June 18, 2025 11:51
Copy link

@@ -314,10 +409,14 @@ func TestSignReq(t *testing.T) {
EXPECT().
ReadFile(gomock.Eq(positiveCaCrtPath)).
Return(setupGoodCaCertificatePEMData(), nil).
Times(1)
AnyTimes()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can I ask, why the move to AnyTimes() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because in certain test scenarios, the path is not even reached. Example:

		{
			name: "Error in CA Path",
			args: args{
				opts:      &positiveCertOptions,
				caCrtPath: invalidPath,
				caKeyPath: positiveCaKeyPath,
				reqPath:   positiveReqPath,
				certOut:   positiveCertOut,
				verify:    true,
			},
			wantErr: true,
		},

In this scenario, we don't get to read from caKeyPath or reqPath, because we already failed at caCrtPath. We could mock conditionally based on the scenario, but it adds complexity to the test and makes it really hard to read and extend it with new test cases, I tried it before ending up using AnyTimes().

The trade-off between being strict vs having an easy to read and maintain tests, is not worth it imho.

@AaronH88
Copy link
Contributor

Overall looking good,
One question around the use of AnyTimes vs Times(1)

@PabloHiro PabloHiro requested a review from AaronH88 June 19, 2025 15:32
Copy link
Contributor
@AaronH88 AaronH88 left a comment

Choose a reason for hiding this comment

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

LGTM

Thanks for this 🎉

@arrestle arrestle self-requested a review June 20, 2025 15:56
Copy link
Contributor
@arrestle arrestle left a comment

Choose a reason for hiding this comment

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

YES! Came to the same conclusion on the AnyTimes()

@PabloHiro PabloHiro merged commit c88d880 into ansible:devel Jun 20, 2025
31 of 32 checks passed
@PabloHiro PabloHiro deleted the feature/tests-signreq-cli branch June 20, 2025 16:06
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.

3 participants
0