-
Notifications
You must be signed in to change notification settings - Fork 162
[POC] CSGHub server integration testing framework #283
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
base: main
Are you sure you want to change the base?
Conversation
Linter Issue ReportDuring the code review, a list issues were found. These issues could affect the code quality, maintainability, and consistency. Below is the detailed Linter issue report: common/tests/testutils.goLint Issue: Undefined: testcontainersCode Context and Location: ...
\treuse := testcontainers.CustomizeRequestOption(
\t\tfunc(req *testcontainers.GenericContainerRequest) error {
\t\t\treq.Reuse = true
\t\t\treq.Name = cname
\t\t\treturn nil
\t\t},
\t)
... Actionable Suggestions: To resolve this issue, please ensure that the Here is an example of how to import the package: import (
"github.com/testcontainers/testcontainers-go"
) Please make sure to replace the import path with the correct one if it's different. tests/testinfra/testinfra.goLint Issue: Undefined: workflowservice
err = nsclient.Register(ctx, &workflowservice.RegisterNamespaceRequest{
Namespace: "default",
WorkflowExecutionRetentionPeriod: &durationpb.Duration{Seconds: 1000000000},
}) This issue is located at line 200, column 32.
Please make the suggested changes to improve the code quality. |
func main() { | ||
ctx := context.Background() | ||
env, err := testinfra.StartTestEnv() | ||
defer func() { _ = env.Shutdown(ctx) }() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Comments:
- The error handling for
testinfra.StartTestEnv()
is placed after the function call, which will cause a panic iferr
is not nil. This should be checked immediately after the call.
- The error handling for
- Suggestions:
ctx := context.Background() env, err := testinfra.StartTestEnv() if err != nil { panic(err) } defer func() { _ = env.Shutdown(ctx) }()
for _, b := range rp.bodys { | ||
require.Equal(t, "test1", gjson.GetBytes(b, "data.name").String()) | ||
// FIXME: user email leak | ||
require.Equal(t, "[email protected]", gjson.GetBytes(b, "data.user.email").String()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Comments:
- The test for model detail exposes user email, which could lead to privacy issues.
- Suggestions:
// Consider removing or masking the user email in the response require.NotEmpty(t, gjson.GetBytes(b, "data.user.email").String())
err = exec.Command("cp", "Makefile", "model_clone_1/Makefile").Run() | ||
require.NoError(t, err) | ||
err = gitCommitAndPush("model_clone_1") | ||
require.Error(t, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Comments:
- The
gitCommitAndPush
function does not handle errors correctly; it should return an error if the command fails.
- The
func (t *TestEnv) Shutdown(ctx context.Context) error { | ||
var err error | ||
if t.temporalServer != nil { | ||
t.temporalServer.Stop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Comments:
- The
Shutdown
method does not handle errors fromt.temporalServer.Stop()
properly; it should check for errors.
- The
- Suggestions:
if err := t.temporalServer.Stop(); err != nil { return err }
// from the command line. | ||
// This is a temporary fix. We should avoid relying on a global config and instead use DI | ||
// to pass the config wherever it's needed. | ||
once = sync.OnceValues(func() (*Config, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Comments:
- The use of
sync.OnceValues
is incorrect; it should besync.Once
instead. This could lead to unexpected behavior.
- The use of
- Suggestions:
once = &sync.Once{}
@@ -69,21 +69,26 @@ func chProjectRoot() { | |||
|
|||
// Init a test db, must call `defer db.Close()` in the test | |||
func InitTestDB() *database.DB { | |||
db, _ := CreateTestDB("csghub_test") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Comments:
- The
CreateTestDB
function should handle errors fromCreateTestDB
instead of ignoring them. This could lead to silent failures.
- The
- Suggestions:
db, err := CreateTestDB("csghub_test") if err != nil { log.Fatalf("failed to create test DB: %v", err) }
@@ -51,6 +51,10 @@ type Operator struct { | |||
Core bun.IDB | |||
} | |||
|
|||
func GetDB() *DB { | |||
return defaultDB |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Comments:
- The
GetDB
function does not handle the case wheredefaultDB
is nil, which could lead to a nil pointer dereference.
- The
- Suggestions:
if defaultDB == nil { return nil, errors.New("defaultDB is not initialized") } return defaultDB
@@ -42,6 +42,7 @@ func newWorkflowTester(t *testing.T) (*workflowTester, error) { | |||
scanner := mock_component.NewMockRuntimeArchitectureComponent(t) | |||
|
|||
cfg := &config.Config{} | |||
cfg.MultiSync.SaasAPIDomain = "http://foo.com" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Comments:
- The configuration for
cfg.MultiSync.SaasAPIDomain
should be validated to ensure it is not empty before use.
- The configuration for
- Suggestions:
if cfg.MultiSync.SaasAPIDomain == "" { t.Fatal("SaasAPIDomain must be set") }
Possible Issues And Suggestions:
MR Evaluation:This feature is still under test, evaluation are given by AI and might be inaccurate. After evaluation, the code changes in the Merge Request get score: 100. TipsCodeReview Commands (invoked as MR or PR comments)
CodeReview Discussion ChatThere are 2 ways to chat with Starship CodeReview:
Note: Be mindful of the bot's finite context window. CodeReview Documentation and Community
About Us:Visit the OpenCSG StarShip website for the Dashboard and detailed information on CodeReview, CodeGen, and other StarShip modules. |
Starting and Stopping the Test Environment
Note: The test server requires Docker and an internet connection (if the images have not been pulled yet) to run correctly, as we use Testcontainers to start various services and components.
Setting up the test environment for integration testing is simple. Follow the steps below:
However, before proceeding with writing your tests, please read the following explanation to fully understand what happens in these three lines of code. A lot is happening behind the scenes.
When
testinfra.StartTestEnv()
is called, the following actions are performed in sequence:common/config/test.toml
configuration file is loaded. This config is used during integration tests.tests/gitaly.toml
ortests/gitaly_github.toml
(used when running on GitHub). Please see the comment in thetestinfra.go
explaining why two config files are required. The Gitaly server configuration is updated once the container is started.That’s all. All docker containers are started using Testcontainers, so all data will be removed after test done.Note that not all services are started by default. For example, the NATS server or runner server is not started. If you need to test functionality related to these services, be sure to add them to the environment startup function.
After the test environment is started, always defer the call to
env.Shutdown(ctx)
to ensure all resources are properly cleaned up.Writing Tests
There are two example test files:
The test code in these files clearly demonstrates how to test the API, Git, and workflows. There’s no need to repeat this here.
One important thing to remember is that for all integration tests, you should add the following snippet at the beginning of your test function:
This allows you to differentiate between unit tests and integration tests.
MR Summary:
The summary is added by @codegpt.
This Merge Request introduces an integration testing framework for the CSGHub server, utilizing Docker and Testcontainers to set up various services. Key updates include:
Shutdown
method to theGracefulServer
for proper server shutdown.testinfra
for consistent testing.Overall, this MR significantly enhances the testing capabilities of the CSGHub server.