From 697a1dc3dd207d44e9828309ca564ae4f7e8a305 Mon Sep 17 00:00:00 2001 From: Fei Su Date: Thu, 18 Apr 2024 16:11:11 +0800 Subject: [PATCH 1/2] CP-48777 Set up Github Action for go sdk component test Signed-off-by: Fei Su --- .github/workflows/go-ci/action.yml | 21 +++++ .github/workflows/go-lint/action.yml | 14 ---- .github/workflows/sdk-ci/action.yml | 22 +++++- ocaml/sdk-gen/component-test/README.md | 60 ++++++++++++++ .../jsonrpc-client/go/client.go | 78 +++++++++++++++++++ .../jsonrpc-server/requirements.txt | 1 + .../component-test/jsonrpc-server/server.py | 64 +++++++++++++++ ocaml/sdk-gen/component-test/run-tests.sh | 34 ++++++++ .../sdk-gen/component-test/spec/session.json | 29 +++++++ 9 files changed, 305 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/go-ci/action.yml delete mode 100644 .github/workflows/go-lint/action.yml create mode 100644 ocaml/sdk-gen/component-test/README.md create mode 100644 ocaml/sdk-gen/component-test/jsonrpc-client/go/client.go create mode 100644 ocaml/sdk-gen/component-test/jsonrpc-server/requirements.txt create mode 100644 ocaml/sdk-gen/component-test/jsonrpc-server/server.py create mode 100644 ocaml/sdk-gen/component-test/run-tests.sh create mode 100644 ocaml/sdk-gen/component-test/spec/session.json diff --git a/.github/workflows/go-ci/action.yml b/.github/workflows/go-ci/action.yml new file mode 100644 index 00000000000..244aca0f35f --- /dev/null +++ b/.github/workflows/go-ci/action.yml @@ -0,0 +1,21 @@ +name: 'Run CI for Go SDK' +runs: + using: 'composite' + steps: + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.22.2' + + - name: Lint for Go SDK + uses: golangci/golangci-lint-action@v4 + with: + version: v1.57.2 + working-directory: ${{ github.workspace }}/_build/install/default/xapi/sdk/go/src + args: --config=${{ github.workspace }}/.golangci.yml + + - name: Run CI for Go SDK + shell: bash + run: | + cd ./ocaml/sdk-gen/component-test/ + bash run-tests.sh \ No newline at end of file diff --git a/.github/workflows/go-lint/action.yml b/.github/workflows/go-lint/action.yml deleted file mode 100644 index d041aa4627a..00000000000 --- a/.github/workflows/go-lint/action.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: Go Lint -runs: - using: composite - steps: - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version: 1.22.2 - - name: golangci-lint - uses: golangci/golangci-lint-action@v4 - with: - version: v1.57.2 - working-directory: ${{ github.workspace }}/_build/install/default/xapi/sdk/go/src - args: --config=${{ github.workspace }}/.golangci.yml diff --git a/.github/workflows/sdk-ci/action.yml b/.github/workflows/sdk-ci/action.yml index 3113b1b91b4..f20b59ee8d6 100644 --- a/.github/workflows/sdk-ci/action.yml +++ b/.github/workflows/sdk-ci/action.yml @@ -1,6 +1,20 @@ -name: Continuous Integration for SDKs +name: 'Run CI for Go SDK' runs: - using: composite + using: 'composite' steps: - - name: Lint for Go SDK - uses: ./.github/workflows/go-lint + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '^3.12' + + - name: Install dependencies for JsonRPC Server + shell: bash + run: | + python -m pip install --upgrade pip + cd ./ocaml/sdk-gen/component-test/jsonrpc-server + pip install -r requirements.txt + + - name: Run CI for Go SDK + uses: ./.github/workflows/go-ci + + # Run other tests here \ No newline at end of file diff --git a/ocaml/sdk-gen/component-test/README.md b/ocaml/sdk-gen/component-test/README.md new file mode 100644 index 00000000000..422badb2483 --- /dev/null +++ b/ocaml/sdk-gen/component-test/README.md @@ -0,0 +1,60 @@ +## How Component Testing Works + +#### jsonrpc-server +The jsonrpc-server is a mock HTTP server created to emulate the XAPI (eXtensible Application Programming Interface) environment. It operates by executing the method specified within incoming requests and subsequently providing a response that includes the outcome of that execution. To facilitate comprehensive testing, the server extracts test data from JSON files located in the spec/ directory. To enhance the organization and scalability of test cases, especially when simulating various API versions for the purpose of ensuring backwards compatibility, the following structure is recommended: + +- Base Directory: All test cases should be housed within a dedicated directory, conventionally named spec/. + +- Sub-directories: As the number of test cases grows or when differentiating between API versions, it becomes advantageous to categorize them into sub-directories. These sub-directories can represent different versions or modules of the API. +``` +spec/ +├── v1/ +│ ├── test_id_1.json +│ ├── test_id_2.json +│ └── ... +├── v2/ +│ ├── test_id_3.json +│ ├── test_id_4.json +│ └── ... +└── ... +``` +- Test Case Files: Each test case is represented by a JSON file. These files should be named uniquely, often corresponding to a test ID, to avoid conflicts and to make it easier to reference specific test cases. + +- JSON Object Structure: Within each JSON file, the test data should be structured to include essential fields such as test_id, method, params, and expect_result. This structure allows for clear definition and expectation of each test case. +```json +{ + "test_id": "test_id_1", + "method": "methodName", + "params": { + // ... parameters required for the method + + }, + "expect_result": { + // ... expected result of the method execution + + } + +} +``` +- Test Execution: The jsonrpc-server should be designed to iterate through the spec/ directory and its sub-directories, executing each test case and comparing the actual results against the expect_result as defined in the JSON files. + +- Compatiblity: When organizing test cases for multiple API versions, ensure that the sub-directory structure reflects these versions. This allows for running version-specific tests or comprehensive tests that cover multiple versions. + +- Maintenance and Evolution: As the API evolves, the test cases should be updated or extended to reflect new methods, parameters, or expected results. The directory structure should facilitate easy updates and additions to the test cases. + +#### jsonrpc-client + +jsonrpc-client is a client that imports the SDK and runs the functions, following these important details: + +1. Add test_id as the user agent in the request header. + +2. Ensure that the function and params are aligned with the data defined in spec/ directory. + +3. The test results/reports need to be collected from the client side. + +#### github actions +For a ci step in the generate sdk sources job, it should involve performing lint, component testing. For instance, in the case of the go sdk, there is a docker compose which launches a jsonrpc server and client and executes the testing procedure in the go-ct step. + + +## Run test locally +Install python 3.11+ with requirements and go 1.22+ and go to ocaml/sdk-gen/component-test and run `bash run-tests.sh` diff --git a/ocaml/sdk-gen/component-test/jsonrpc-client/go/client.go b/ocaml/sdk-gen/component-test/jsonrpc-client/go/client.go new file mode 100644 index 00000000000..fc6f5c3efbe --- /dev/null +++ b/ocaml/sdk-gen/component-test/jsonrpc-client/go/client.go @@ -0,0 +1,78 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + "strconv" +) + +const ServerURL = "http://127.0.0.1:5000" + +type Request struct { + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + Params interface{} `json:"params"` + ID int `json:"id"` +} + +func sendJsonRpcRequest(test_id string, method string, params ...map[string]string) { + var p map[string]string + if len(params) > 0 { + p = params[0] + } else { + p = map[string]string{} + } + + idStr := strings.TrimPrefix(test_id, "test_id") + id, err := strconv.Atoi(idStr) + if err != nil { + fmt.Printf("Failed to parse id: %v\n", err) + return + } + + requestBody, _ := json.Marshal(Request{ + Jsonrpc: "2.0", + Method: method, + Params: p, + ID: id, + }) + + req, err := http.NewRequest("POST", ServerURL, bytes.NewBuffer(requestBody)) + if err != nil { + fmt.Printf("Failed to create request: %v\n", err) + return + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", test_id) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + fmt.Printf("Failed to send JSON-RPC request: %v\n", err) + return + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Printf("Failed to read response body: %v\n", err) + return + } + fmt.Println(string(body)) +} + + +func testSessions() { + sendJsonRpcRequest("test_id1", "login_session", map[string]string{"username": "admin", "password": "secret"}) + sendJsonRpcRequest("test_id2", "login_session", map[string]string{"username": "admin", "password": "invalid"}) + sendJsonRpcRequest("test_id3", "logout_session") +} + +func main() { + testSessions() +} diff --git a/ocaml/sdk-gen/component-test/jsonrpc-server/requirements.txt b/ocaml/sdk-gen/component-test/jsonrpc-server/requirements.txt new file mode 100644 index 00000000000..e9feb68da3d --- /dev/null +++ b/ocaml/sdk-gen/component-test/jsonrpc-server/requirements.txt @@ -0,0 +1 @@ + aiohttp diff --git a/ocaml/sdk-gen/component-test/jsonrpc-server/server.py b/ocaml/sdk-gen/component-test/jsonrpc-server/server.py new file mode 100644 index 00000000000..d605cac306c --- /dev/null +++ b/ocaml/sdk-gen/component-test/jsonrpc-server/server.py @@ -0,0 +1,64 @@ +""" +Module Name: + jsonrpc_server +Description: + This module provides a simple JSON-RPC server implementation using aiohttp. +""" + +import json +import os + +from aiohttp import web # pytype: disable=import-error + + +def load_json_files(): + """ + Load all JSON files from the 'spec' directory and merge their contents + into a dictionary. + + Returns: + dict: A dictionary containing the merged contents of all JSON files. + """ + data = {} + for filename in os.listdir("spec/"): + if filename.endswith(".json"): + with open(f"spec/{filename}", "r", encoding="utf-8") as f: + data.update(json.load(f)) + return data + + +async def handle(request): + """ + Handle incoming requests and execute methods based on the test ID provided + in the request headers. + + Args: + request (aiohttp.web.Request): The incoming HTTP request. + + Returns: + aiohttp.web.Response: The HTTP response containing the JSON-RPC result. + """ + spec = load_json_files() + test_id = request.headers.get("User-Agent") + if not test_id: + raise ValueError("Failed to get test_id in User-Agent.") + test_data = spec.get(test_id, {}) + data = await request.json() + + assert test_data.get("method") == data.get("method") + assert test_data.get("params") == data.get("params") + + response = { + "jsonrpc": "2.0", + "id": data.get("id"), + **test_data.get("expected_result", {}), + } + + return web.json_response(response) + + +app = web.Application() +app.router.add_post("/", handle) + +if __name__ == "__main__": + web.run_app(app, port=5000) diff --git a/ocaml/sdk-gen/component-test/run-tests.sh b/ocaml/sdk-gen/component-test/run-tests.sh new file mode 100644 index 00000000000..1e30faebebc --- /dev/null +++ b/ocaml/sdk-gen/component-test/run-tests.sh @@ -0,0 +1,34 @@ +#!/bin/bash +set -e + +ROOT_PATH=$(cd "$(dirname "$0")" && pwd) + +start_jsonrpc_server() { + echo "Starting JSONRPC server" + python3 jsonrpc-server/server.py & + JSONRPC_SERVER_PID=$! + sleep 1 +} + +start_jsonrpc_go_client() { + echo "Starting JSONRPC Go client" + # build client.go and run it + go run jsonrpc-client/go/client.go & + JSONRPC_GO_CLIENT_PID=$! +} + +trap 'kill $JSONRPC_SERVER_PID $JSONRPC_GO_CLIENT_PID 2>/dev/null' EXIT + +main() { + cd "$ROOT_PATH" + start_jsonrpc_server + start_jsonrpc_go_client + + # Wait for the component test to finish + wait $JSONRPC_GO_CLIENT_PID + + # Shut down the server to reduce future problems when testing other clients. + kill $JSONRPC_SERVER_PID +} + +main diff --git a/ocaml/sdk-gen/component-test/spec/session.json b/ocaml/sdk-gen/component-test/spec/session.json new file mode 100644 index 00000000000..06b4f6adc06 --- /dev/null +++ b/ocaml/sdk-gen/component-test/spec/session.json @@ -0,0 +1,29 @@ +{ + "test_id1": { + "method": "login_session", + "params": { + "username": "admin", + "password": "secret" + }, + "expected_result": { + "result": "login successfully with username: admin" + } + }, + "test_id2": { + "method": "login_session", + "params": { + "username": "admin", + "password": "invalid" + }, + "expected_result": { + "error": "login failed with username: admin" + } + }, + "test_id3": { + "method": "logout_session", + "params": {}, + "expected_result": { + "result": "logout successfully" + } + } +} From 85d1e342156f94ce03ddd8d9e96427d70b295419 Mon Sep 17 00:00:00 2001 From: Fei Su Date: Fri, 17 May 2024 13:48:31 +0800 Subject: [PATCH 2/2] update for using the generated sdk Signed-off-by: Fei Su --- .github/workflows/go-ci/action.yml | 1 + ocaml/sdk-gen/component-test/README.md | 33 ++++---- .../jsonrpc-client/go/client.go | 78 ------------------- .../component-test/jsonrpc-client/go/go.mod | 5 ++ .../jsonrpc-client/go/main_test.go | 52 +++++++++++++ .../component-test/jsonrpc-server/server.py | 34 ++++---- ocaml/sdk-gen/component-test/run-tests.sh | 12 ++- .../sdk-gen/component-test/spec/session.json | 55 +++++++------ 8 files changed, 140 insertions(+), 130 deletions(-) delete mode 100644 ocaml/sdk-gen/component-test/jsonrpc-client/go/client.go create mode 100644 ocaml/sdk-gen/component-test/jsonrpc-client/go/go.mod create mode 100644 ocaml/sdk-gen/component-test/jsonrpc-client/go/main_test.go diff --git a/.github/workflows/go-ci/action.yml b/.github/workflows/go-ci/action.yml index 244aca0f35f..6dc66224fe0 100644 --- a/.github/workflows/go-ci/action.yml +++ b/.github/workflows/go-ci/action.yml @@ -18,4 +18,5 @@ runs: shell: bash run: | cd ./ocaml/sdk-gen/component-test/ + cp -r ${{ github.workspace }}/_build/install/default/xapi/sdk/go/src jsonrpc-client/go/goSDK bash run-tests.sh \ No newline at end of file diff --git a/ocaml/sdk-gen/component-test/README.md b/ocaml/sdk-gen/component-test/README.md index 422badb2483..d60b597c25d 100644 --- a/ocaml/sdk-gen/component-test/README.md +++ b/ocaml/sdk-gen/component-test/README.md @@ -1,7 +1,9 @@ ## How Component Testing Works #### jsonrpc-server -The jsonrpc-server is a mock HTTP server created to emulate the XAPI (eXtensible Application Programming Interface) environment. It operates by executing the method specified within incoming requests and subsequently providing a response that includes the outcome of that execution. To facilitate comprehensive testing, the server extracts test data from JSON files located in the spec/ directory. To enhance the organization and scalability of test cases, especially when simulating various API versions for the purpose of ensuring backwards compatibility, the following structure is recommended: +The jsonrpc-server is a mock HTTP server which aligns with [jsonrpc 2.0 specification](https://www.jsonrpc.org/specification) and to emulate the xen-api server. It parses the test data from JSON files located in the spec/ directory, executes the method and parameters specified in spec files with incoming requests and then sends back a jsonrpc response that includes the expect_result. + +For the purpose of backwards and forwards compatibility, the following structure is recommended: - Base Directory: All test cases should be housed within a dedicated directory, conventionally named spec/. @@ -18,9 +20,8 @@ spec/ │ └── ... └── ... ``` -- Test Case Files: Each test case is represented by a JSON file. These files should be named uniquely, often corresponding to a test ID, to avoid conflicts and to make it easier to reference specific test cases. -- JSON Object Structure: Within each JSON file, the test data should be structured to include essential fields such as test_id, method, params, and expect_result. This structure allows for clear definition and expectation of each test case. +- Test Data Specification: Within each JSON file, the test data should be structured to include essential fields such as test_id, method, params, and expect_result. This structure allows for clear definition and expectation of each test case. ```json { "test_id": "test_id_1", @@ -36,25 +37,31 @@ spec/ } ``` -- Test Execution: The jsonrpc-server should be designed to iterate through the spec/ directory and its sub-directories, executing each test case and comparing the actual results against the expect_result as defined in the JSON files. - -- Compatiblity: When organizing test cases for multiple API versions, ensure that the sub-directory structure reflects these versions. This allows for running version-specific tests or comprehensive tests that cover multiple versions. - -- Maintenance and Evolution: As the API evolves, the test cases should be updated or extended to reflect new methods, parameters, or expected results. The directory structure should facilitate easy updates and additions to the test cases. #### jsonrpc-client - jsonrpc-client is a client that imports the SDK and runs the functions, following these important details: -1. Add test_id as the user agent in the request header. +1. Add test_id as a customize request header. 2. Ensure that the function and params are aligned with the data defined in spec/ directory. -3. The test results/reports need to be collected from the client side. +3. In order to support test reports, practitioners should use the specific test framework to test SDK, eg: pytest, gotest, junit, xUnit and so on. -#### github actions -For a ci step in the generate sdk sources job, it should involve performing lint, component testing. For instance, in the case of the go sdk, there is a docker compose which launches a jsonrpc server and client and executes the testing procedure in the go-ct step. +4. To support the SDK component test, it recommended to move the SDK generated to a sub directory as a local module for import purposes, eg: +``` +cp -r ${{ github.workspace }}/_build/install/default/xapi/sdk/go/src jsonrpc-client/go/goSDK +``` +then, import the local module. +``` +module github.com/xapi-project/xen-api/sdk-gen/component-test/jsonrpc-client/go + +go 1.22.2 +replace xenapi => ./goSDK +``` + +#### github actions +For a CI step in the generate sdk sources job, it should involve performing lint and component testing after sdk generation. ## Run test locally Install python 3.11+ with requirements and go 1.22+ and go to ocaml/sdk-gen/component-test and run `bash run-tests.sh` diff --git a/ocaml/sdk-gen/component-test/jsonrpc-client/go/client.go b/ocaml/sdk-gen/component-test/jsonrpc-client/go/client.go deleted file mode 100644 index fc6f5c3efbe..00000000000 --- a/ocaml/sdk-gen/component-test/jsonrpc-client/go/client.go +++ /dev/null @@ -1,78 +0,0 @@ -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - "strings" - "strconv" -) - -const ServerURL = "http://127.0.0.1:5000" - -type Request struct { - Jsonrpc string `json:"jsonrpc"` - Method string `json:"method"` - Params interface{} `json:"params"` - ID int `json:"id"` -} - -func sendJsonRpcRequest(test_id string, method string, params ...map[string]string) { - var p map[string]string - if len(params) > 0 { - p = params[0] - } else { - p = map[string]string{} - } - - idStr := strings.TrimPrefix(test_id, "test_id") - id, err := strconv.Atoi(idStr) - if err != nil { - fmt.Printf("Failed to parse id: %v\n", err) - return - } - - requestBody, _ := json.Marshal(Request{ - Jsonrpc: "2.0", - Method: method, - Params: p, - ID: id, - }) - - req, err := http.NewRequest("POST", ServerURL, bytes.NewBuffer(requestBody)) - if err != nil { - fmt.Printf("Failed to create request: %v\n", err) - return - } - - req.Header.Set("Content-Type", "application/json") - req.Header.Set("User-Agent", test_id) - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - fmt.Printf("Failed to send JSON-RPC request: %v\n", err) - return - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - fmt.Printf("Failed to read response body: %v\n", err) - return - } - fmt.Println(string(body)) -} - - -func testSessions() { - sendJsonRpcRequest("test_id1", "login_session", map[string]string{"username": "admin", "password": "secret"}) - sendJsonRpcRequest("test_id2", "login_session", map[string]string{"username": "admin", "password": "invalid"}) - sendJsonRpcRequest("test_id3", "logout_session") -} - -func main() { - testSessions() -} diff --git a/ocaml/sdk-gen/component-test/jsonrpc-client/go/go.mod b/ocaml/sdk-gen/component-test/jsonrpc-client/go/go.mod new file mode 100644 index 00000000000..f1ee4c8ee52 --- /dev/null +++ b/ocaml/sdk-gen/component-test/jsonrpc-client/go/go.mod @@ -0,0 +1,5 @@ +module github.com/xapi-project/xen-api/sdk-gen/component-test/jsonrpc-client/go + +go 1.22.2 + +replace xenapi => ./goSDK \ No newline at end of file diff --git a/ocaml/sdk-gen/component-test/jsonrpc-client/go/main_test.go b/ocaml/sdk-gen/component-test/jsonrpc-client/go/main_test.go new file mode 100644 index 00000000000..466c037deed --- /dev/null +++ b/ocaml/sdk-gen/component-test/jsonrpc-client/go/main_test.go @@ -0,0 +1,52 @@ +package main + +import ( + "flag" + "fmt" + "testing" + "xenapi" +) + +const ServerURL = "http://localhost:5000" + +var session *xenapi.Session + +var USERNAME_FLAG = flag.String("root", "", "the username of the host (e.g. root)") +var PASSWORD_FLAG = flag.String("secret", "", "the password of the host") + +func TestLoginSuccess(t *testing.T) { + session = xenapi.NewSession(&xenapi.ClientOpts{ + URL: ServerURL, + Headers: map[string]string{ + "Test-ID": "test_id1", + }, + }) + if session == nil { + fmt.Printf("Failed to get the session") + return + } + _, err := session.LoginWithPassword(*USERNAME_FLAG, *PASSWORD_FLAG, "1.0", "Go sdk component test") + if err != nil { + t.Log(err) + t.Fail() + return + } + + expectedXapiVersion := "1.20" + getXapiVersion := session.XAPIVersion + if expectedXapiVersion != getXapiVersion { + t.Errorf("Unexpected result. Expected: %s, Got: %s", expectedXapiVersion, getXapiVersion) + } + var expectedAPIVersion xenapi.APIVersion = xenapi.APIVersion2_21 + getAPIVersion := session.APIVersion + if expectedAPIVersion != getAPIVersion { + t.Errorf("Unexpected result. Expected: %s, Got: %s", expectedAPIVersion, getAPIVersion) + } + + err = session.Logout() + if err != nil { + t.Log(err) + t.Fail() + return + } +} diff --git a/ocaml/sdk-gen/component-test/jsonrpc-server/server.py b/ocaml/sdk-gen/component-test/jsonrpc-server/server.py index d605cac306c..8fff806eac1 100644 --- a/ocaml/sdk-gen/component-test/jsonrpc-server/server.py +++ b/ocaml/sdk-gen/component-test/jsonrpc-server/server.py @@ -39,26 +39,34 @@ async def handle(request): aiohttp.web.Response: The HTTP response containing the JSON-RPC result. """ spec = load_json_files() - test_id = request.headers.get("User-Agent") - if not test_id: - raise ValueError("Failed to get test_id in User-Agent.") + test_id = request.headers.get("Test-ID") test_data = spec.get(test_id, {}) data = await request.json() - assert test_data.get("method") == data.get("method") - assert test_data.get("params") == data.get("params") - - response = { - "jsonrpc": "2.0", - "id": data.get("id"), - **test_data.get("expected_result", {}), - } - + try: + assert data.get("method") in test_data.get("method") + assert test_data.get("params")[data.get("method")] == data.get("params") + except Exception: + response = { + "jsonrpc": "2.0", + "id": data.get("id"), + "error": { + "code": 500, + "message": "Rpc server failed to handle the client request!", + "data": str(data), + } + } + else: + response = { + "jsonrpc": "2.0", + "id": data.get("id"), + **test_data.get("expected_result")[data.get("method")], + } return web.json_response(response) app = web.Application() -app.router.add_post("/", handle) +app.router.add_post("/jsonrpc", handle) if __name__ == "__main__": web.run_app(app, port=5000) diff --git a/ocaml/sdk-gen/component-test/run-tests.sh b/ocaml/sdk-gen/component-test/run-tests.sh index 1e30faebebc..0eed34f0e0c 100644 --- a/ocaml/sdk-gen/component-test/run-tests.sh +++ b/ocaml/sdk-gen/component-test/run-tests.sh @@ -1,7 +1,7 @@ #!/bin/bash -set -e +set -ex -ROOT_PATH=$(cd "$(dirname "$0")" && pwd) +SCRIPT_PATH=$(cd "$(dirname "$0")" && pwd) start_jsonrpc_server() { echo "Starting JSONRPC server" @@ -12,15 +12,19 @@ start_jsonrpc_server() { start_jsonrpc_go_client() { echo "Starting JSONRPC Go client" + + cd jsonrpc-client/go + # ensure that all dependencies are satisfied + go mod tidy # build client.go and run it - go run jsonrpc-client/go/client.go & + go test main_test.go -v & JSONRPC_GO_CLIENT_PID=$! } trap 'kill $JSONRPC_SERVER_PID $JSONRPC_GO_CLIENT_PID 2>/dev/null' EXIT main() { - cd "$ROOT_PATH" + cd "$SCRIPT_PATH" start_jsonrpc_server start_jsonrpc_go_client diff --git a/ocaml/sdk-gen/component-test/spec/session.json b/ocaml/sdk-gen/component-test/spec/session.json index 06b4f6adc06..4916448f3d9 100644 --- a/ocaml/sdk-gen/component-test/spec/session.json +++ b/ocaml/sdk-gen/component-test/spec/session.json @@ -1,29 +1,40 @@ { "test_id1": { - "method": "login_session", + "method": [ + "session.login_with_password", + "pool.get_all", + "pool.get_record", + "host.get_record", + "session.logout" + ], "params": { - "username": "admin", - "password": "secret" + "session.login_with_password": ["", "", "1.0", "Go sdk component test"], + "pool.get_all": ["login successfully"], + "pool.get_record": ["login successfully", "poolref0"], + "host.get_record": ["login successfully", ""], + "session.logout": ["login successfully"] }, "expected_result": { - "result": "login successfully with username: admin" - } - }, - "test_id2": { - "method": "login_session", - "params": { - "username": "admin", - "password": "invalid" - }, - "expected_result": { - "error": "login failed with username: admin" - } - }, - "test_id3": { - "method": "logout_session", - "params": {}, - "expected_result": { - "result": "logout successfully" + "session.login_with_password": { + "result": "login successfully" + }, + "pool.get_all": { + "result": ["poolref0"] + }, + "pool.get_record": { + "result": {"name_label": "pool0"} + }, + "host.get_record": { + "result": { + "name_label": "host0", + "software_version": {"xapi": "1.20"}, + "API_version_major": "2", + "API_version_minor": "21" + } + }, + "session.logout": { + "result": "logout successfully" + } } } -} +} \ No newline at end of file