re-applying changes from reverted Pull #3684#5118
re-applying changes from reverted Pull #3684#5118sudhirakondi wants to merge 4 commits intoopenconfig:mainfrom
Conversation
Summary of ChangesHello @sudhirakondi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on re-establishing robust testing for gNSI client certificate functionality. It introduces a new, detailed test suite that covers various scenarios for client certificate validation and rotation across multiple gRPC services. The changes also include refinements to test data generation and the introduction of a dedicated utility package to streamline test setup and verification processes, ensuring the stability and correctness of certificate handling. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new test for gNSI client certificates and updates related test data generation scripts. The changes look good overall, but there are several areas for improvement.
Notably, the PR appears to include duplicated test files under both feature/gnsi/certz and feature/security/gnsi/certz. The files under feature/security seem to be an older version and contain unresolved merge conflicts, which is a critical issue. Please clarify if this is intentional, and if not, remove the duplicated files.
In the test implementation, there are some issues with global variables, including one that causes stale timestamps in logs and another that should be a local variable. The logic for waiting for a gRPC response is flawed and can lead to test hangs, but the suggested fix aligns with best practices for handling critical test failures. Additionally, several verification functions have an unused mismatch parameter, indicating incomplete logic for negative test cases. I've also pointed out a violation of the style guide regarding the use of time.Sleep.
| //RotateRequest receive channel to receive the next response message. | ||
| ch := make(chan struct{}, 1) | ||
| rotateResponse := &certzpb.RotateCertificateResponse{} | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| rotateResponse, err = rotateRequestClient.Recv() | ||
| if err != nil { | ||
| t.Errorf("Error fetching rotate certificate response: %v", err) | ||
| return | ||
| } | ||
| close(ch) | ||
| }() | ||
| for i := range retries { | ||
| wg.Add(1) | ||
| go func(i int) { | ||
| defer wg.Done() | ||
| for { | ||
| select { | ||
| case <-ch: | ||
| //Exiting receive channel. | ||
| return | ||
| default: | ||
| //Did not receive response after sending rotate request.Sleeping 10s to retry. | ||
| time.Sleep(10 * time.Second) | ||
| } | ||
| } | ||
| }(i) | ||
| } | ||
| wg.Wait() | ||
| t.Logf("Received Rotate certificate response: %v", rotateResponse) |
There was a problem hiding this comment.
The current implementation for waiting for the Recv() response is overly complex, inefficient, and contains a bug. It spawns multiple goroutines that spin-wait, which is not a standard or clear way to implement a timeout. Furthermore, if Recv() returns an error, the channel ch is never closed, causing the waiting goroutines to hang indefinitely. A much simpler and more idiomatic Go approach is to use a select statement with a time.After channel for the timeout.
// Wait for the rotate response with a timeout.
respCh := make(chan *certzpb.RotateCertificateResponse)
errCh := make(chan error)
go func() {
resp, err := rotateRequestClient.Recv()
if err != nil {
errCh <- err
return
}
respCh <- resp
}()
var rotateResponse *certzpb.RotateCertificateResponse
select {
case rotateResponse = <-respCh:
t.Logf("Received Rotate certificate response: %v", rotateResponse)
case err := <-errCh:
t.Fatalf("Error fetching rotate certificate response: %v", err)
case <-time.After(time.Duration(retries) * 10 * time.Second): // e.g. 12 * 10s = 2 minutes timeout
t.Fatalf("Timed out waiting for rotate certificate response")
}References
- When monitoring critical processes in a test, use
t.Fatalfif a process is not found, as its absence should be considered a fatal test failure.
| var ( | ||
| serverAddr string | ||
| creds DUTCredentialer //an interface for getting credentials from a DUT binding | ||
| testProfile string = "newprofile" //sslProfileId name |
There was a problem hiding this comment.
This variable is used as a constant. It should be declared as a const to follow Go conventions and prevent accidental changes.
const testProfile = "newprofile"References
- Per Effective Go, compile-time constants should be declared using the
constkeyword. (link)
| prevClientCertFile string = "" | ||
| prevClientKeyFile string = "" | ||
| prevTrustBundleFile string = "" | ||
| logTime string = time.Now().String() //Timestamp |
There was a problem hiding this comment.
The logTime global variable is initialized only once when the package is loaded. This means all log messages throughout the test execution will have the same stale timestamp. It is recommended to remove this variable and rely on the test runner's built-in timestamping. If custom timestamping is required, time.Now() should be called within each logging statement to get a fresh timestamp.
| servers []string | ||
| retries int = 12 | ||
| wg sync.WaitGroup | ||
| success bool |
There was a problem hiding this comment.
| } | ||
| batch.Set(t, dut.GNMIOpts().WithClient(gnmiClient)) | ||
| t.Logf("gNMI config is replaced with new ssl profile %s successfully.", profileID) | ||
| time.Sleep(30 * time.Second) //waiting 30s for gnmi config propagation// |
There was a problem hiding this comment.
The style guide recommends avoiding time.Sleep and using gnmi.Watch with .Await to wait for conditions to be met. This time.Sleep should be replaced with a watch on the gNMI config to confirm propagation.
References
- The repository style guide specifies to avoid
time.Sleepand instead usegnmi.Watchwith.Awaitfor waiting on conditions. (link)
| // TestdataMakeCleanup function to create/cleanup test data for use in TLS tests. | ||
| // This function executes the certificate generate/cleanup script "mk_cas.sh" and "cleanup.sh" | ||
| // located in the specified dir at the start and end of the tests repectively. | ||
| func TestdataMakeCleanup(t *testing.T, dirPath string, timeout time.Duration, args string) error { |
There was a problem hiding this comment.
The function name TestdataMakeCleanup is a bit misleading as it's a generic function to run shell scripts, used for both setup (mk_cas.sh) and cleanup (cleanup.sh). A more descriptive name like RunScriptInDir would better reflect its purpose.
func RunScriptInDir(t *testing.T, dirPath string, timeout time.Duration, script string) error {
Changes merged via PR #3684 were reverted by #5114. As I understood from Amritha, there were some issues faced due to the changes in the PR 3684. Raising this PR as recommended, with same changes as in PR 3684.