diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index c44452a..ce9c868 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -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 diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index ea014d4..fa4b07f 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -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 diff --git a/e2e/README.md b/e2e/README.md new file mode 100644 index 0000000..a816081 --- /dev/null +++ b/e2e/README.md @@ -0,0 +1,5 @@ +You can build the image locally in the repository root directory: + +```shell +REGISTRY=ghcr.io TAG=master make image +``` diff --git a/e2e/code-generator/Dockerfile b/e2e/code-generator/Dockerfile new file mode 100644 index 0000000..c315dbd --- /dev/null +++ b/e2e/code-generator/Dockerfile @@ -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" ] diff --git a/e2e/code-generator/JavaScript.sh b/e2e/code-generator/JavaScript.sh new file mode 100644 index 0000000..f44e447 --- /dev/null +++ b/e2e/code-generator/JavaScript.sh @@ -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 diff --git a/e2e/code-generator/compose.yaml b/e2e/code-generator/compose.yaml new file mode 100644 index 0000000..b52d117 --- /dev/null +++ b/e2e/code-generator/compose.yaml @@ -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 diff --git a/e2e/code-generator/curl.sh b/e2e/code-generator/curl.sh new file mode 100755 index 0000000..f1ce778 --- /dev/null +++ b/e2e/code-generator/curl.sh @@ -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 diff --git a/e2e/code-generator/entrypoint.sh b/e2e/code-generator/entrypoint.sh new file mode 100755 index 0000000..67ecd40 --- /dev/null +++ b/e2e/code-generator/entrypoint.sh @@ -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 diff --git a/e2e/code-generator/golang.sh b/e2e/code-generator/golang.sh new file mode 100755 index 0000000..e4083e3 --- /dev/null +++ b/e2e/code-generator/golang.sh @@ -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 diff --git a/e2e/code-generator/java.sh b/e2e/code-generator/java.sh new file mode 100644 index 0000000..3fc37ad --- /dev/null +++ b/e2e/code-generator/java.sh @@ -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 diff --git a/e2e/code-generator/python.sh b/e2e/code-generator/python.sh new file mode 100644 index 0000000..521a340 --- /dev/null +++ b/e2e/code-generator/python.sh @@ -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 diff --git a/e2e/code-generator/start.sh b/e2e/code-generator/start.sh new file mode 100755 index 0000000..f8257c1 --- /dev/null +++ b/e2e/code-generator/start.sh @@ -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 diff --git a/e2e/code-generator/test-suites/test-suite.yaml b/e2e/code-generator/test-suites/test-suite.yaml new file mode 100644 index 0000000..83c82bf --- /dev/null +++ b/e2e/code-generator/test-suites/test-suite.yaml @@ -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 diff --git a/pkg/generator/data/main.python.tpl b/pkg/generator/data/main.python.tpl index 1259f42..a0d617d 100644 --- a/pkg/generator/data/main.python.tpl +++ b/pkg/generator/data/main.python.tpl @@ -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. @@ -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 diff --git a/pkg/generator/testdata/expected_python_code.txt b/pkg/generator/testdata/expected_python_code.txt index 0e53287..bd758d3 100644 --- a/pkg/generator/testdata/expected_python_code.txt +++ b/pkg/generator/testdata/expected_python_code.txt @@ -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. @@ -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 diff --git a/pkg/generator/testdata/expected_python_cookie_request_code.txt b/pkg/generator/testdata/expected_python_cookie_request_code.txt index d05f3f5..a61470b 100644 --- a/pkg/generator/testdata/expected_python_cookie_request_code.txt +++ b/pkg/generator/testdata/expected_python_cookie_request_code.txt @@ -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. @@ -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 diff --git a/pkg/generator/testdata/expected_python_form_request_code.txt b/pkg/generator/testdata/expected_python_form_request_code.txt index 2180c44..cde6e31 100644 --- a/pkg/generator/testdata/expected_python_form_request_code.txt +++ b/pkg/generator/testdata/expected_python_form_request_code.txt @@ -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. @@ -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