Skip to content

Commit d4398dc

Browse files
committed
add run_test_server command
1 parent 8f5f515 commit d4398dc

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: test lint cover mock_wire mock_gen swag migrate_local
1+
.PHONY: test lint cover mock_wire mock_gen swag migrate_local run_test_server
22

33
test:
44
go test ./...
@@ -31,3 +31,6 @@ swag:
3131

3232
migrate_local:
3333
go run cmd/csghub-server/main.go migration migrate --config local.toml
34+
35+
run_test_server:
36+
go run tests/main.go

tests/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ When `testinfra.StartTestEnv()` is called, the following actions are performed i
2626
8. **Start the CSGHub dataset viewer server**: The CSGHub dataset viewer server and its workflows are started.
2727
9. **Start the CSGHub main API server**: The CSGHub main API server and its workflows are started.
2828

29-
That’s all. 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.
29+
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.
3030

3131
After the test environment is started, always defer the call to `env.Shutdown(ctx)` to ensure all resources are properly cleaned up.
3232

@@ -51,7 +51,7 @@ This allows you to differentiate between unit tests and integration tests.
5151

5252
### What to Test in Integration Tests
5353

54-
Integration tests involve starting multiple services and interacting with real databases (as opposed to database unit tests, which use in-memory or transactional databases and roll back changes after the test). Writing too many integration tests can significantly slow down the testing process, so here are three key suggestions:
54+
Integration tests involve starting multiple services and save data to database (as opposed to database unit tests, which use in-memory transaction and roll back changes after the test). Writing too many integration tests can significantly slow down the testing process, so here are three key suggestions:
5555

5656
1. **Group related actions into single test cases**: For example, basic CRUD operations can be tested in a single test case. However, avoid overloading tests. Separate unrelated actions, such as API and Git operations, into different tests.
5757
2. **Prioritize the most used features**: Focus on testing the 80% of use cases that are most commonly used in your service. These are the most critical and should not fail.
@@ -62,5 +62,11 @@ Integration tests involve starting multiple services and interacting with real d
6262
You can also use the test environment to start a temporary test server. To do so, run the following command:
6363

6464
```
65-
go run main.go start-test-env
65+
make run_test_server
66+
```
67+
68+
To integrate the test server into CSGHub portal, change CSGHUB_PORTAL_STARHUB_BASE_URL env:
69+
70+
```
71+
CSGHUB_PORTAL_STARHUB_BASE_URL=http://localhost:9091 make
6672
```

tests/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
"os/signal"
8+
9+
"opencsg.com/csghub-server/tests/testinfra"
10+
)
11+
12+
func main() {
13+
ctx := context.Background()
14+
env, err := testinfra.StartTestEnv()
15+
defer func() { _ = env.Shutdown(ctx) }()
16+
if err != nil {
17+
panic(err)
18+
}
19+
quit := make(chan os.Signal, 1)
20+
signal.Notify(quit, os.Interrupt)
21+
<-quit
22+
log.Println("Shutdown Server ...")
23+
}

0 commit comments

Comments
 (0)