Skip to content

Commit

Permalink
Add full test for all APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jul 31, 2023
1 parent d3c5d8e commit 9d24941
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 73 deletions.
37 changes: 21 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ jobs:
uses: actions/checkout@v3
- name: Build image for platform
run: |
docker build -t platform:latest -f Dockerfile . &&
docker tag platform:latest ossrs/srs-cloud:$SRS_TAG &&
docker tag platform:latest registry.cn-hangzhou.aliyuncs.com/ossrs/srs-cloud:$SRS_TAG &&
docker build -t platform:latest -f Dockerfile .
docker tag platform:latest ossrs/srs-cloud:$SRS_TAG
docker tag platform:latest registry.cn-hangzhou.aliyuncs.com/ossrs/srs-cloud:$SRS_TAG
docker images
- name: Build package
run: |
Expand All @@ -75,30 +75,35 @@ jobs:
du -sh build/*
- name: Install package
run: |
sudo bash build/srs-cloud/scripts/setup-ubuntu/install.sh --verbose &&
du -sh /usr/local/srs-cloud/*
sudo bash build/srs-cloud/scripts/setup-ubuntu/install.sh --verbose
echo "" && echo "/usr/local/srs-cloud/" && du -sh /usr/local/srs-cloud/*
echo "" && ls -lha /data /data/config
- name: Check service
run: |
# Wait for service ready.
echo "Wait for service ready."
make -j -C test
./test/srs-cloud.test -test.v -srs-log -endpoint http://localhost:2022 \
-wait-ready -check-api-secret=false \
-test.run TestApi_Empty
./test/srs-cloud.test -test.v -endpoint http://localhost:2022 \
-srs-log=true -wait-ready=true -init-password=true \
-check-api-secret=false -test.run TestApi_Empty
echo "Record log of services."
docker ps -a
echo "Show service status."
systemctl status srs-cloud
journalctl -u srs-cloud -f >journalctl.log 2>&1 & pid_journalctl=$!
docker logs -f srs-cloud >docker.log 2>&1 & pid_docker=$!
docker ps -a
echo "Record log of services."
journalctl -u srs-cloud > journalctl.log
journalctl -u srs-cloud -f >>journalctl.log 2>&1 & pid_journalctl=$!
docker logs srs-cloud >docker.log
docker logs -f srs-cloud >>docker.log 2>&1 & pid_docker=$!
- name: Test service
run: |
# We will handle the error by ourselves.
set +e
SRS_PLATFORM_SECRET=$(docker exec srs-cloud redis-cli hget SRS_PLATFORM_SECRET token)
./test/srs-cloud.test -test.v -wait-ready -srs-log -endpoint http://localhost:2022 \
-wait-ready -check-api-secret=true -api-secret=$SRS_PLATFORM_SECRET \
-init-password
./test/srs-cloud.test -test.v -wait-ready -endpoint http://localhost:2022 \
-srs-log=true -wait-ready=true -init-password=false \
-check-api-secret=true -api-secret=$SRS_PLATFORM_SECRET \
ret=$?; echo "Test with ${SRS_PLATFORM_SECRET} result: $ret"
echo "Stop service"
Expand Down
11 changes: 4 additions & 7 deletions DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@ resources, and ports, as well as development on Mac or using Docker.

## Develop All in macOS

Start redis by docker:

```bash
docker run --name redis --rm -it -v $HOME/db/redis:/data -p 6379:6379 -d redis
```

Start SRS in macOS:
Start redis and SRS by docker:

```bash
docker run --name redis --rm -it -v $HOME/db/redis:/data -p 6379:6379 -d redis &&
docker run --name srs --rm -it \
-v $(pwd)/platform/containers/conf/srs.release-mac.conf:/usr/local/srs/conf/srs.conf \
-v $(pwd)/platform/containers/objs/nginx:/usr/local/srs/objs/nginx \
-p 1935:1935/tcp -p 1985:1985/tcp -p 8080:8080/tcp -p 8000:8000/udp -p 10080:10080/udp \
-d ossrs/srs:5
```

> Note: Stop service by `docker rm -f redis srs`
> Note: Also, you can run SRS by `(cd platform && ~/git/srs/trunk/objs/srs -c containers/conf/srs.release-local.conf)`
Run the platform backend, or run in GoLand:
Expand Down
9 changes: 3 additions & 6 deletions scripts/setup-ubuntu/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,9 @@ fi

echo "Start to install files"
mkdir -p ${SRS_HOME} ${DATA_HOME} && rm -rf ${SRS_HOME}/* &&
files=$(find . -type f |grep -vE '(.git|.idea|.run|node_modules|nginx/html/live)') &&
files="${files} $(find . -name .gitkeep)"
for file in $files; do
mkdir -p ${SRS_HOME}/$(dirname ${file}) &&
cp $file ${SRS_HOME}/${file}
done
cp -r ${WORK_DIR}/usr ${SRS_HOME}/usr &&
cp -r ${WORK_DIR}/mgmt ${SRS_HOME}/mgmt &&
cp -r ${WORK_DIR}/LICENSE ${SRS_HOME}/LICENSE
ret=$?; if [[ $ret -ne 0 ]]; then echo "Copy files failed, ret=$ret"; exit $ret; fi
echo "Install files at ${SRS_HOME} ok"

Expand Down
14 changes: 12 additions & 2 deletions test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ func TestApi_Empty(t *testing.T) {
logger.Tf(ctx, "test done")
}

func TestApi_Ready(t *testing.T) {
ctx, cancel := context.WithTimeout(logger.WithContext(context.Background()), time.Duration(*srsTimeout)*time.Millisecond)
defer cancel()

if err := waitForServiceReady(ctx); err != nil {
t.Errorf("Fail for err %+v", err)
} else {
logger.Tf(ctx, "test done")
}
}

func TestApi_QuerySecret(t *testing.T) {
ctx, cancel := context.WithTimeout(logger.WithContext(context.Background()), time.Duration(*srsTimeout)*time.Millisecond)
logger.Tf(ctx, "TestApi_QuerySecret with %v", options())
defer cancel()

var r0 error
defer func(ctx context.Context) {
Expand All @@ -26,7 +37,6 @@ func TestApi_QuerySecret(t *testing.T) {
logger.Tf(ctx, "test done with err %+v", err)
}
}(ctx)
defer cancel()

var publishSecret string
if err := apiRequest(ctx, "/terraform/v1/hooks/srs/secret/query", nil, &struct {
Expand Down
89 changes: 47 additions & 42 deletions test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ func options() string {
}

func prepareTest(ctx context.Context) (err error) {
envFile := "../platform/containers/data/config/.env"
if _, err := os.Stat(envFile); err == nil {
if err := godotenv.Load(envFile); err != nil {
return errors.Wrapf(err, "load %v", envFile)
// Try to load the .env file.
for _, envFile := range []string{
"../platform/containers/data/config/.env",
"/data/config/.env",
} {
if _, err := os.Stat(envFile); err == nil {
if err := godotenv.Load(envFile); err == nil {
break
}
}
}

// Parse the options.
srsLog = flag.Bool("srs-log", false, "Whether enable the detail log")
srsTimeout = flag.Int("srs-timeout", 5000, "For each case, the timeout in ms")
apiSecret = flag.String("api-secret", os.Getenv("SRS_PLATFORM_SECRET"), "The secret for api")
Expand All @@ -54,6 +60,7 @@ func prepareTest(ctx context.Context) (err error) {

// Should parse it first.
flag.Parse()
logger.Tf(ctx, "Test with %v", options())

if *checkApiSecret && *apiSecret == "" {
return errors.Errorf("empty api secret")
Expand All @@ -62,9 +69,44 @@ func prepareTest(ctx context.Context) (err error) {
return nil
}

func TestMain(m *testing.M) {
ctx := logger.WithContext(context.Background())

if err := prepareTest(ctx); err != nil {
logger.Ef(ctx, "Prepare test fail, err %+v", err)
os.Exit(-1)
}

// Disable the logger during all tests.
if *srsLog == false {
olw := logger.Switch(ioutil.Discard)
defer func() {
logger.Switch(olw)
}()
}

// Init rand seed.
rand.Seed(time.Now().UnixNano())

// Wait for the service ready.
if *waitReady {
if err := waitForServiceReady(ctx); err != nil {
os.Exit(-1)
}
}

if *initPassword {
if err := initSystemPassword(ctx); err != nil {
logger.Ef(ctx, "Init system fail, err %+v", err)
os.Exit(-1)
}
}

os.Exit(m.Run())
}

func waitForServiceReady(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, time.Duration(*apiReadyimeout)*time.Millisecond)
logger.Tf(ctx, "Wait for API ready with %v, apiReadyimeout=%vms", options(), *apiReadyimeout)
defer cancel()

for {
Expand All @@ -88,7 +130,6 @@ func waitForServiceReady(ctx context.Context) error {

func initSystemPassword(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, time.Duration(*srsTimeout)*time.Millisecond)
logger.Tf(ctx, "Wait for API ready with %v", options())
defer cancel()

// Initialize the system by password.
Expand Down Expand Up @@ -129,42 +170,6 @@ func initSystemPassword(ctx context.Context) error {
return nil
}

func TestMain(m *testing.M) {
ctx := logger.WithContext(context.Background())

if err := prepareTest(ctx); err != nil {
logger.Ef(ctx, "Prepare test fail, err %+v", err)
os.Exit(-1)
}

// Disable the logger during all tests.
if *srsLog == false {
olw := logger.Switch(ioutil.Discard)
defer func() {
logger.Switch(olw)
}()
}

// Init rand seed.
rand.Seed(time.Now().UnixNano())

// Wait for the service ready.
if *waitReady {
if err := waitForServiceReady(ctx); err != nil {
os.Exit(-1)
}
}

if *initPassword {
if err := initSystemPassword(ctx); err != nil {
logger.Ef(ctx, "Init system fail, err %+v", err)
os.Exit(-1)
}
}

os.Exit(m.Run())
}

// Filter the test error, ignore context.Canceled
func filterTestError(errs ...error) error {
var filteredErrors []error
Expand Down

0 comments on commit 9d24941

Please sign in to comment.