From 705980af9c34ade2efebecf49b517496df058db1 Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 20 Nov 2023 16:57:24 +0800 Subject: [PATCH] test: add e2e testing base on docker-compose --- .github/workflows/testing.yaml | 22 ++++++ test/e2e/Dockerfile | 6 ++ test/e2e/data/cluster-alibaba.json | 65 +++++++++++++++++ test/e2e/data/cluster-k3d.json | 31 ++++++++ test/e2e/data/cluster-native.json | 46 ++++++++++++ test/e2e/data/credential-alibaba.json | 7 ++ test/e2e/data/credential-aws.json | 8 +++ test/e2e/data/credential-google.json | 7 ++ test/e2e/data/credential-tencent.json | 7 ++ test/e2e/data/sshkey.json | 6 ++ test/e2e/docker-compose.yaml | 16 +++++ test/e2e/entrypoint.sh | 4 ++ test/e2e/start.sh | 31 ++++++++ test/e2e/testcase.yaml | 100 ++++++++++++++++++++++++++ 14 files changed, 356 insertions(+) create mode 100644 .github/workflows/testing.yaml create mode 100644 test/e2e/Dockerfile create mode 100644 test/e2e/data/cluster-alibaba.json create mode 100644 test/e2e/data/cluster-k3d.json create mode 100644 test/e2e/data/cluster-native.json create mode 100644 test/e2e/data/credential-alibaba.json create mode 100644 test/e2e/data/credential-aws.json create mode 100644 test/e2e/data/credential-google.json create mode 100644 test/e2e/data/credential-tencent.json create mode 100644 test/e2e/data/sshkey.json create mode 100644 test/e2e/docker-compose.yaml create mode 100755 test/e2e/entrypoint.sh create mode 100755 test/e2e/start.sh create mode 100644 test/e2e/testcase.yaml diff --git a/.github/workflows/testing.yaml b/.github/workflows/testing.yaml new file mode 100644 index 00000000..f123d9a5 --- /dev/null +++ b/.github/workflows/testing.yaml @@ -0,0 +1,22 @@ +name: Testing + +on: + - pull_request + +jobs: + Test: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v4 + with: + fetch-tags: true + fetch-depth: 0 + - name: Build Image + run: | + make autok3s + docker build . -t cnrancher/autok3s:dev + - name: Run e2e + run: | + 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 + ./test/e2e/start.sh diff --git a/test/e2e/Dockerfile b/test/e2e/Dockerfile new file mode 100644 index 00000000..9359fe9c --- /dev/null +++ b/test/e2e/Dockerfile @@ -0,0 +1,6 @@ +FROM docker.io/linuxsuren/api-testing:v0.0.14 +WORKDIR /workspace +COPY testcase.yaml . +COPY data data +COPY entrypoint.sh entrypoint.sh +CMD [ "/workspace/entrypoint.sh" ] diff --git a/test/e2e/data/cluster-alibaba.json b/test/e2e/data/cluster-alibaba.json new file mode 100644 index 00000000..5c71bb90 --- /dev/null +++ b/test/e2e/data/cluster-alibaba.json @@ -0,0 +1,65 @@ +{ + "cluster": false, + "datastore": "", + "datastore-cafile": "", + "datastore-cafile-content": "", + "datastore-certfile": "", + "datastore-certfile-content": "", + "datastore-keyfile": "", + "datastore-keyfile-content": "", + "docker-arg": "", + "docker-script": "https://get.docker.com", + "enable": null, + "k3s-channel": "stable", + "k3s-install-mirror": "INSTALL_K3S_MIRROR=cn", + "k3s-install-script": "https://rancher-mirror.rancher.cn/k3s/k3s-install.sh", + "k3s-version": "", + "manifests": "", + "master": "1", + "master-extra-args": "", + "name": "{{randAlpha 4}}", + "package-name": "", + "package-path": "", + "registry": "", + "registry-content": "", + "rollback": true, + "set": null, + "ssh-agent-auth": false, + "ssh-cert-path": "", + "ssh-key-name": "", + "ssh-key-passphrase": "", + "ssh-key-path": "", + "ssh-password": "", + "ssh-port": "22", + "ssh-user": "root", + "system-default-registry": "", + "tls-sans": null, + "token": "", + "worker": "0", + "worker-extra-args": "", + "ssh-key": "", + "ssh-cert": "", + "provider": "alibaba", + "options": { + "access-key": "cc", + "access-secret": "ccc", + "cloud-controller-manager": false, + "disk-category": "cloud_essd", + "disk-size": "40", + "eip": false, + "image": "ubuntu_22_04_x64_20G_alibase_20230613.vhd", + "instance-type": "ecs.c6.large", + "internet-max-bandwidth-out": "5", + "key-pair": "", + "region": "cn-hangzhou", + "security-group": "", + "spot-duration": 1, + "spot-price-limit": 0, + "spot-strategy": "NoSpot", + "tags": null, + "user-data-content": "", + "user-data-path": "", + "v-switch": "", + "zone": "cn-hangzhou-i" + } +} diff --git a/test/e2e/data/cluster-k3d.json b/test/e2e/data/cluster-k3d.json new file mode 100644 index 00000000..1e14b4c5 --- /dev/null +++ b/test/e2e/data/cluster-k3d.json @@ -0,0 +1,31 @@ +{ + "datastore-cafile-content": "", + "datastore-certfile-content": "", + "datastore-keyfile-content": "", + "master": "1", + "master-extra-args": "", + "masters-memory": "", + "name": "{{randAlpha 4}}", + "network": "", + "registry": "", + "registry-content": "", + "token": "", + "worker": "0", + "worker-extra-args": "", + "workers-memory": "", + "rollback": true, + "provider": "k3d", + "options": { + "api-port": "0.0.0.0:0", + "envs": null, + "gpus": "", + "image": "docker.io/rancher/k3s:v1.27.6-k3s1", + "labels": null, + "masters-memory": "", + "no-image-volume": false, + "no-lb": false, + "ports": null, + "volumes": null, + "workers-memory": "" + } +} diff --git a/test/e2e/data/cluster-native.json b/test/e2e/data/cluster-native.json new file mode 100644 index 00000000..fa057d79 --- /dev/null +++ b/test/e2e/data/cluster-native.json @@ -0,0 +1,46 @@ +{ + "cluster": false, + "datastore": "", + "datastore-cafile": "", + "datastore-cafile-content": "", + "datastore-certfile": "", + "datastore-certfile-content": "", + "datastore-keyfile": "", + "datastore-keyfile-content": "", + "docker-arg": "", + "docker-script": "https://get.docker.com", + "enable": [], + "k3s-channel": "stable", + "k3s-install-mirror": "", + "k3s-install-script": "https://get.k3s.io", + "k3s-version": "", + "manifests": "", + "master-extra-args": "", + "name": "{{randAlpha 4}}", + "package-name": "", + "package-path": "", + "registry": "", + "registry-content": "", + "rollback": true, + "set": null, + "ssh-agent-auth": false, + "ssh-cert-path": "", + "ssh-key-name": "", + "ssh-key-passphrase": "", + "ssh-key-path": "", + "ssh-password": "", + "ssh-port": "22", + "ssh-user": "root", + "system-default-registry": "", + "tls-sans": null, + "token": "", + "worker-extra-args": "", + "ssh-key": "", + "ssh-cert": "", + "values": {}, + "provider": "native", + "options": { + "master-ips": "0.0.0.0", + "worker-ips": "" + } +} diff --git a/test/e2e/data/credential-alibaba.json b/test/e2e/data/credential-alibaba.json new file mode 100644 index 00000000..12b680a0 --- /dev/null +++ b/test/e2e/data/credential-alibaba.json @@ -0,0 +1,7 @@ +{ + "provider": "alibaba", + "secrets": { + "access-key": "sdfsf", + "access-secret": "sdfsfd" + } +} diff --git a/test/e2e/data/credential-aws.json b/test/e2e/data/credential-aws.json new file mode 100644 index 00000000..091187dc --- /dev/null +++ b/test/e2e/data/credential-aws.json @@ -0,0 +1,8 @@ +{ + "provider": "aws", + "secrets": { + "access-key": "test", + "secret-key": "ttest", + "session-token": "" + } +} diff --git a/test/e2e/data/credential-google.json b/test/e2e/data/credential-google.json new file mode 100644 index 00000000..9e74ee2c --- /dev/null +++ b/test/e2e/data/credential-google.json @@ -0,0 +1,7 @@ +{ + "provider": "google", + "secrets": { + "service-account": "ss", + "service-account-file": "ss" + } +} diff --git a/test/e2e/data/credential-tencent.json b/test/e2e/data/credential-tencent.json new file mode 100644 index 00000000..30f1aadd --- /dev/null +++ b/test/e2e/data/credential-tencent.json @@ -0,0 +1,7 @@ +{ + "provider": "tencent", + "secrets": { + "secret-id": "sdfs", + "secret-key": "fsfds" + } +} diff --git a/test/e2e/data/sshkey.json b/test/e2e/data/sshkey.json new file mode 100644 index 00000000..f5168593 --- /dev/null +++ b/test/e2e/data/sshkey.json @@ -0,0 +1,6 @@ +{ + "name": "{{randAlpha 4}}", + "generate-key": true, + "bits": 2048, + "ssh-passphrase": "" +} diff --git a/test/e2e/docker-compose.yaml b/test/e2e/docker-compose.yaml new file mode 100644 index 00000000..e59bea26 --- /dev/null +++ b/test/e2e/docker-compose.yaml @@ -0,0 +1,16 @@ +services: + testing: + build: . + depends_on: + autok3s: + condition: service_healthy + links: + - autok3s + autok3s: + image: cnrancher/autok3s:dev + healthcheck: + test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/8080"] + interval: 3s + timeout: 60s + retries: 10 + start_period: 3s diff --git a/test/e2e/entrypoint.sh b/test/e2e/entrypoint.sh new file mode 100755 index 00000000..d4223008 --- /dev/null +++ b/test/e2e/entrypoint.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -e + +atest run -p testcase.yaml diff --git a/test/e2e/start.sh b/test/e2e/start.sh new file mode 100755 index 00000000..35e1fc94 --- /dev/null +++ b/test/e2e/start.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +file=$1 +if [ "$file" == "" ] +then + file=docker-compose.yaml +fi + +docker-compose version +docker-compose -f "$file" up --build -d + +while true +do + docker-compose -f "$file" ps | grep testing + if [ $? -eq 1 ] + then + code=-1 + docker-compose -f "$file" logs | grep e2e-testing + docker-compose -f "$file" logs | grep e2e-testing | grep Usage + if [ $? -eq 1 ] + then + code=0 + echo "successed" + fi + + docker-compose -f "$file" down + set -e + exit $code + fi + sleep 1 +done diff --git a/test/e2e/testcase.yaml b/test/e2e/testcase.yaml new file mode 100644 index 00000000..06b115f8 --- /dev/null +++ b/test/e2e/testcase.yaml @@ -0,0 +1,100 @@ +name: autok3s +param: + server: http://autok3s:8080 +spec: + kind: HTTP +api: http://autok3s:8080/v1 +items: +- name: createAlibabaCredential + request: + api: /credentials + method: POST + header: + Content-Type: application/json + bodyFromFile: data/credential-alibaba.json + expect: + statusCode: 201 +- name: createAwsCredential + request: + api: /credentials + method: POST + bodyFromFile: data/credential-aws.json + expect: + statusCode: 201 +- name: createGoogleCredential + request: + api: /credentials + method: POST + bodyFromFile: data/credential-google.json + expect: + statusCode: 201 +- name: createTencentCredential + request: + api: /credentials + method: POST + bodyFromFile: data/credential-tencent.json + expect: + statusCode: 201 + +- expect: + statusCode: 201 + name: createK3dCluster + request: + api: /clusters + bodyFromFile: data/cluster-k3d.json + method: POST +- expect: + statusCode: 201 + name: createNativeCluster + request: + api: /clusters + bodyFromFile: data/cluster-native.json + method: POST +- name: createAlibabaCluster + request: + api: /clusters + bodyFromFile: data/cluster-alibaba.json + method: POST + expect: + statusCode: 422 +- name: clusters + request: + api: /clusters + method: GET + expect: + verify: + - len(data.data) == 2 +- name: getCluster + request: + api: /clusters/{{(index .clusters.data 0).id}}?link=nodes + method: GET +- name: deleteCluster + request: + api: /clusters/{{(index .clusters.data 0).id}} + method: DELETE + +## ssh key related APIs +- name: generateSSHKey + request: + api: /sshKeys + method: POST + bodyFromFile: data/sshkey.json + expect: + statusCode: 201 +- name: getSSHKeyList + request: + api: /sshKeys + expect: + verify: + - len(data.data) == 1 +- name: deleteSSHKey + request: + api: /sshKeys/{{(index .getSSHKeyList.data 0).id}} + +## addon related APIs +- name: getAdonList + request: + api: /addons + expect: + verify: + - len(data.data) == 1