Skip to content

Commit 92d517f

Browse files
authored
e2e: add e2e tests for code generators (#458)
* e2e: add e2e tests for code generators * improve the verifying of java code generating --------- Co-authored-by: rick <[email protected]>
1 parent 1765d13 commit 92d517f

17 files changed

+191
-11
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Making sure that your local build is OK before committing will help you reduce d
44
and make it easier for maintainers to review.
55
-->
66

7-
> We highly recommend you read [the contributor's documentation](https://github.com/LinuxSuRen/api-testing/blob/master/CONTRIBUTION.md) before starting the review process especially since this is your first contribution to this project.
7+
> We highly recommend you read [the contributor's documentation](https://github.com/LinuxSuRen/api-testing/blob/master/CONTRIBUTING.md) before starting the review process especially since this is your first contribution to this project.
88
>
99
> It was updated on 2024/5/27
1010

.github/workflows/build.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ jobs:
105105
sudo curl -L https://github.com/docker/compose/releases/download/v2.23.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
106106
sudo chmod u+x /usr/local/bin/docker-compose
107107
make test-e2e
108-
# - name: Operator Image
109-
# run: cd operator && make docker-build
108+
- name: Code Generator Test
109+
run: cd e2e/code-generator && ./start.sh
110110

111111
BuildEmbedUI:
112112
runs-on: ubuntu-latest

e2e/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
You can build the image locally in the repository root directory:
2+
3+
```shell
4+
REGISTRY=ghcr.io TAG=master make image
5+
```

e2e/code-generator/Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ARG LAN_ENV=docker.io/library/golang:1.21
2+
3+
FROM ghcr.io/linuxsuren/api-testing:master AS atest
4+
FROM docker.io/stedolan/jq AS jq
5+
FROM $LAN_ENV
6+
7+
WORKDIR /workspace
8+
COPY . .
9+
COPY --from=jq /usr/local/bin/jq /usr/local/bin/jq
10+
COPY --from=atest /usr/local/bin/atest /usr/local/bin/atest
11+
12+
CMD [ "/workspace/entrypoint.sh" ]

e2e/code-generator/JavaScript.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -e
3+
4+
export sourcefile=$1
5+
# exit if no source file is specified
6+
if [ -z "$sourcefile" ]
7+
then
8+
echo "no source file is specified"
9+
exit 1
10+
fi
11+
12+
mv ${sourcefile} main.js
13+
node main.js

e2e/code-generator/compose.yaml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
services:
2+
golang:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
args:
7+
- LAN_ENV=docker.io/library/golang:1.21
8+
command:
9+
- /workspace/entrypoint.sh
10+
- golang
11+
java:
12+
build:
13+
context: .
14+
dockerfile: Dockerfile
15+
args:
16+
- LAN_ENV=docker.io/library/openjdk:23
17+
command:
18+
- /workspace/entrypoint.sh
19+
- java
20+
python:
21+
build:
22+
context: .
23+
dockerfile: Dockerfile
24+
args:
25+
- LAN_ENV=docker.io/library/python:3.8
26+
command:
27+
- /workspace/entrypoint.sh
28+
- python
29+
javascript:
30+
build:
31+
context: .
32+
dockerfile: Dockerfile
33+
args:
34+
- LAN_ENV=docker.io/library/node:22
35+
command:
36+
- /workspace/entrypoint.sh
37+
- JavaScript
38+
curl:
39+
build:
40+
context: .
41+
dockerfile: Dockerfile
42+
args:
43+
- LAN_ENV=docker.io/library/openjdk:23
44+
command:
45+
- /workspace/entrypoint.sh
46+
- curl

e2e/code-generator/curl.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -e
3+
4+
export sourcefile=$1
5+
# exit if no source file is specified
6+
if [ -z "$sourcefile" ]
7+
then
8+
echo "no source file is specified"
9+
exit 1
10+
fi
11+
12+
mv ${sourcefile} main.sh
13+
sh main.sh

e2e/code-generator/entrypoint.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
set -e
3+
4+
export lang=$1
5+
# exit if no language is specified
6+
if [ -z "$lang" ]
7+
then
8+
echo "no language is specified"
9+
exit 1
10+
fi
11+
12+
mkdir -p /root/.config/atest
13+
mkdir -p /var/data
14+
15+
nohup atest server --local-storage '/workspace/test-suites/*.yaml'&
16+
sleep 1
17+
18+
curl http://localhost:8080/server.Runner/GenerateCode -X POST \
19+
-d '{"TestSuite": "test", "TestCase": "requestWithHeader", "Generator": "'"$lang"'"}' > code.json
20+
21+
cat code.json | jq .message -r | sed 's/\\n/\n/g' | sed 's/\\t/\t/g' | sed 's/\\\"/"/g' > code.txt
22+
cat code.txt
23+
24+
sh /workspace/${lang}.sh code.txt
25+
26+
exit 0

e2e/code-generator/golang.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -e
3+
4+
export sourcefile=$1
5+
# exit if no source file is specified
6+
if [ -z "$sourcefile" ]
7+
then
8+
echo "no source file is specified"
9+
exit 1
10+
fi
11+
12+
mv ${sourcefile} main.go
13+
go run main.go

e2e/code-generator/java.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -e
3+
4+
export sourcefile=$1
5+
# exit if no source file is specified
6+
if [ -z "$sourcefile" ]
7+
then
8+
echo "no source file is specified"
9+
exit 1
10+
fi
11+
12+
mv ${sourcefile} Main.java
13+
java Main.java

e2e/code-generator/python.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -e
3+
4+
export sourcefile=$1
5+
# exit if no source file is specified
6+
if [ -z "$sourcefile" ]
7+
then
8+
echo "no source file is specified"
9+
exit 1
10+
fi
11+
12+
mv ${sourcefile} main.py
13+
pip install requests
14+
python main.py

e2e/code-generator/start.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
docker-compose version
4+
5+
targets=(golang java python javascript curl)
6+
for target in "${targets[@]}"
7+
do
8+
docker-compose down
9+
docker-compose up --build $target
10+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!api-testing
2+
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-schema.json
3+
# https://docs.gitlab.com/ee/api/api_resources.html
4+
name: test
5+
api: http://localhost:8080/server.Runner
6+
param:
7+
suiteName: test
8+
caseName: test
9+
items:
10+
- name: requestWithHeader
11+
request:
12+
api: /GetSuites
13+
method: POST
14+
header:
15+
auth: fake

pkg/generator/data/main.python.tpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
'''
22
Copyright 2024 API Testing Authors.
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -9,7 +9,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
99
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
12-
*/
12+
'''
1313
import io
1414
import requests
1515
from urllib.parse import urlencode

pkg/generator/testdata/expected_python_code.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
'''
22
Copyright 2024 API Testing Authors.
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -9,7 +9,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
99
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
12-
*/
12+
'''
1313
import io
1414
import requests
1515
from urllib.parse import urlencode

pkg/generator/testdata/expected_python_cookie_request_code.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
'''
22
Copyright 2024 API Testing Authors.
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -9,7 +9,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
99
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
12-
*/
12+
'''
1313
import io
1414
import requests
1515
from urllib.parse import urlencode

pkg/generator/testdata/expected_python_form_request_code.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
'''
22
Copyright 2024 API Testing Authors.
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -9,7 +9,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
99
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
12-
*/
12+
'''
1313
import io
1414
import requests
1515
from urllib.parse import urlencode

0 commit comments

Comments
 (0)