Skip to content

Makefile: add -gcflags=all=-l to gnmi_server tests for gomonkey compatibility#604

Open
sigabrtv1-ui wants to merge 1 commit intosonic-net:masterfrom
sigabrtv1-ui:fix/gnsi-authz-gomonkey
Open

Makefile: add -gcflags=all=-l to gnmi_server tests for gomonkey compatibility#604
sigabrtv1-ui wants to merge 1 commit intosonic-net:masterfrom
sigabrtv1-ui:fix/gnsi-authz-gomonkey

Conversation

@sigabrtv1-ui
Copy link

Problem

Tests in gnmi_server that use gomonkey.ApplyFunc() to mock OS/auth functions (e.g. user.Lookup, UserPwAuth) silently fail to patch because the binary is built with inlining enabled. gomonkey patches function pointers at the machine-code level and requires inlining to be disabled to work correctly.

Without -gcflags=all=-l, ApplyFunc() patches are no-ops at runtime — the real functions execute instead — causing tests to get real authentication failures:

E0305 14:42:56 gnsi_authz.go:66] authentication failed in Rotate RPC: rpc error: code = Unauthenticated desc = Unauthenticated
    gnsi_authz_test.go:60: Unexpected error: rpc error: code = Unauthenticated desc = Unauthenticated
--- FAIL: TestGnsiAuthzRotation/RotateOpenClose
--- FAIL: TestGnsiAuthzRotation/RotateStreamRecvError
... (all 11 subtests fail)
--- FAIL: TestGnsiAuthzRotation

This is blocking PR #549 (gNSI Authz frontend implementation) which has failed on every CI run.

Fix

Add -gcflags=all=-l to the go test invocations for gnmi_server (in check_gotest) and INTEGRATION_ENV_PKGS (in check_gotest_junit). This flag disables inlining so gomonkey can insert its patches at the function entry points.

The flag has no effect on production binaries and does not break any other tests — it only makes the test binary slightly larger and marginally slower to compile.

References

…tibility

gomonkey patches function pointers at the machine-code level and requires
inlining to be disabled (-gcflags=all=-l) to work correctly. Without this
flag, ApplyFunc() patches are silently ignored at runtime — the real
functions run instead, causing tests that mock OS/auth calls (e.g.
user.Lookup, UserPwAuth) to fail with Unauthenticated errors.

Add the flag to both check_gotest and check_gotest_junit targets for the
gnmi_server / INTEGRATION_ENV_PKGS invocations so that tests using
gomonkey (e.g. TestGnsiAuthzRotation in PR sonic-net#549) work as expected.

Signed-off-by: sigabrtv1-ui <sig.abrt.v1@gmail.com>
@mssonicbld
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@sigabrtv1-ui
Copy link
Author

@zbud-msft @FengPan-Frank — companion to #603. Adds -gcflags=all=-l to gnmi_server test invocations so gomonkey patches work correctly. Without this flag, ApplyFunc patches are silently no-ops, causing auth mocks to be ignored and every test to fail with Unauthenticated.

sigabrtv1-ui pushed a commit to sigabrtv1-ui/sonic-gnmi that referenced this pull request Mar 9, 2026
Go 1.23's compiler inlines more aggressively than 1.19, breaking
gomonkey patches in sonic_service_client tests (TestGetFileStat panics
with nil pointer dereference because the method patch is bypassed).

Add -gcflags=all=-l to INTEGRATION_BASIC_PKGS test run to disable
inlining and allow gomonkey to patch reliably.

Related: PR sonic-net#604 adds the same flag to gnmi_server tests.

Signed-off-by: Dawei Huang <daweihuang@microsoft.com>
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