Skip to content

Commit

Permalink
e2e: add e2e tests for code generators (#458)
Browse files Browse the repository at this point in the history
* e2e: add e2e tests for code generators

* improve the verifying of java code generating

---------

Co-authored-by: rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored May 28, 2024
1 parent 1765d13 commit 92d517f
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Making sure that your local build is OK before committing will help you reduce d
and make it easier for maintainers to review.
-->

> 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.
> 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.
>
> It was updated on 2024/5/27
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ jobs:
sudo curl -L https://github.com/docker/compose/releases/download/v2.23.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo chmod u+x /usr/local/bin/docker-compose
make test-e2e
# - name: Operator Image
# run: cd operator && make docker-build
- name: Code Generator Test
run: cd e2e/code-generator && ./start.sh

BuildEmbedUI:
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
You can build the image locally in the repository root directory:

```shell
REGISTRY=ghcr.io TAG=master make image
```
12 changes: 12 additions & 0 deletions e2e/code-generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ARG LAN_ENV=docker.io/library/golang:1.21

FROM ghcr.io/linuxsuren/api-testing:master AS atest
FROM docker.io/stedolan/jq AS jq
FROM $LAN_ENV

WORKDIR /workspace
COPY . .
COPY --from=jq /usr/local/bin/jq /usr/local/bin/jq
COPY --from=atest /usr/local/bin/atest /usr/local/bin/atest

CMD [ "/workspace/entrypoint.sh" ]
13 changes: 13 additions & 0 deletions e2e/code-generator/JavaScript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e

export sourcefile=$1
# exit if no source file is specified
if [ -z "$sourcefile" ]
then
echo "no source file is specified"
exit 1
fi

mv ${sourcefile} main.js
node main.js
46 changes: 46 additions & 0 deletions e2e/code-generator/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
services:
golang:
build:
context: .
dockerfile: Dockerfile
args:
- LAN_ENV=docker.io/library/golang:1.21
command:
- /workspace/entrypoint.sh
- golang
java:
build:
context: .
dockerfile: Dockerfile
args:
- LAN_ENV=docker.io/library/openjdk:23
command:
- /workspace/entrypoint.sh
- java
python:
build:
context: .
dockerfile: Dockerfile
args:
- LAN_ENV=docker.io/library/python:3.8
command:
- /workspace/entrypoint.sh
- python
javascript:
build:
context: .
dockerfile: Dockerfile
args:
- LAN_ENV=docker.io/library/node:22
command:
- /workspace/entrypoint.sh
- JavaScript
curl:
build:
context: .
dockerfile: Dockerfile
args:
- LAN_ENV=docker.io/library/openjdk:23
command:
- /workspace/entrypoint.sh
- curl
13 changes: 13 additions & 0 deletions e2e/code-generator/curl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e

export sourcefile=$1
# exit if no source file is specified
if [ -z "$sourcefile" ]
then
echo "no source file is specified"
exit 1
fi

mv ${sourcefile} main.sh
sh main.sh
26 changes: 26 additions & 0 deletions e2e/code-generator/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -e

export lang=$1
# exit if no language is specified
if [ -z "$lang" ]
then
echo "no language is specified"
exit 1
fi

mkdir -p /root/.config/atest
mkdir -p /var/data

nohup atest server --local-storage '/workspace/test-suites/*.yaml'&
sleep 1

curl http://localhost:8080/server.Runner/GenerateCode -X POST \
-d '{"TestSuite": "test", "TestCase": "requestWithHeader", "Generator": "'"$lang"'"}' > code.json

cat code.json | jq .message -r | sed 's/\\n/\n/g' | sed 's/\\t/\t/g' | sed 's/\\\"/"/g' > code.txt
cat code.txt

sh /workspace/${lang}.sh code.txt

exit 0
13 changes: 13 additions & 0 deletions e2e/code-generator/golang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e

export sourcefile=$1
# exit if no source file is specified
if [ -z "$sourcefile" ]
then
echo "no source file is specified"
exit 1
fi

mv ${sourcefile} main.go
go run main.go
13 changes: 13 additions & 0 deletions e2e/code-generator/java.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e

export sourcefile=$1
# exit if no source file is specified
if [ -z "$sourcefile" ]
then
echo "no source file is specified"
exit 1
fi

mv ${sourcefile} Main.java
java Main.java
14 changes: 14 additions & 0 deletions e2e/code-generator/python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -e

export sourcefile=$1
# exit if no source file is specified
if [ -z "$sourcefile" ]
then
echo "no source file is specified"
exit 1
fi

mv ${sourcefile} main.py
pip install requests
python main.py
10 changes: 10 additions & 0 deletions e2e/code-generator/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

docker-compose version

targets=(golang java python javascript curl)
for target in "${targets[@]}"
do
docker-compose down
docker-compose up --build $target
done
15 changes: 15 additions & 0 deletions e2e/code-generator/test-suites/test-suite.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!api-testing
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-schema.json
# https://docs.gitlab.com/ee/api/api_resources.html
name: test
api: http://localhost:8080/server.Runner
param:
suiteName: test
caseName: test
items:
- name: requestWithHeader
request:
api: /GetSuites
method: POST
header:
auth: fake
4 changes: 2 additions & 2 deletions pkg/generator/data/main.python.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
'''
Copyright 2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -9,7 +9,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'''
import io
import requests
from urllib.parse import urlencode
Expand Down
4 changes: 2 additions & 2 deletions pkg/generator/testdata/expected_python_code.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
'''
Copyright 2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -9,7 +9,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'''
import io
import requests
from urllib.parse import urlencode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
'''
Copyright 2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -9,7 +9,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'''
import io
import requests
from urllib.parse import urlencode
Expand Down
4 changes: 2 additions & 2 deletions pkg/generator/testdata/expected_python_form_request_code.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
'''
Copyright 2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -9,7 +9,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'''
import io
import requests
from urllib.parse import urlencode
Expand Down

0 comments on commit 92d517f

Please sign in to comment.