Skip to content

Set up Github Action for go SDK component test #5588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/go-ci/action.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions .github/workflows/go-lint/action.yml

This file was deleted.

22 changes: 18 additions & 4 deletions .github/workflows/sdk-ci/action.yml
Original file line number Diff line number Diff line change
@@ -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
60 changes: 60 additions & 0 deletions ocaml/sdk-gen/component-test/README.md
Original file line number Diff line number Diff line change
@@ -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`
78 changes: 78 additions & 0 deletions ocaml/sdk-gen/component-test/jsonrpc-client/go/client.go
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aiohttp
64 changes: 64 additions & 0 deletions ocaml/sdk-gen/component-test/jsonrpc-server/server.py
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions ocaml/sdk-gen/component-test/run-tests.sh
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions ocaml/sdk-gen/component-test/spec/session.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Loading