-
Notifications
You must be signed in to change notification settings - Fork 292
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
xueqingz
merged 2 commits into
xapi-project:feature/go_sdk
from
acefei:private/feis/CP-48777
May 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
1. Add test_id as the user agent in the request header. | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
## 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` | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"strconv" | ||
) | ||
|
||
const ServerURL = "http://127.0.0.1:5000" | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) { | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var p map[string]string | ||
if len(params) > 0 { | ||
p = params[0] | ||
} else { | ||
p = map[string]string{} | ||
} | ||
|
||
idStr := strings.TrimPrefix(test_id, "test_id") | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
id, err := strconv.Atoi(idStr) | ||
if err != nil { | ||
fmt.Printf("Failed to parse id: %v\n", err) | ||
return | ||
} | ||
|
||
requestBody, _ := json.Marshal(Request{ | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Jsonrpc: "2.0", | ||
Method: method, | ||
Params: p, | ||
ID: id, | ||
}) | ||
|
||
req, err := http.NewRequest("POST", ServerURL, bytes.NewBuffer(requestBody)) | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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)) | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
|
||
func testSessions() { | ||
sendJsonRpcRequest("test_id1", "login_session", map[string]string{"username": "admin", "password": "secret"}) | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sendJsonRpcRequest("test_id2", "login_session", map[string]string{"username": "admin", "password": "invalid"}) | ||
sendJsonRpcRequest("test_id3", "logout_session") | ||
} | ||
|
||
func main() { | ||
testSessions() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
aiohttp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not test_id: | ||
raise ValueError("Failed to get test_id in User-Agent.") | ||
test_data = spec.get(test_id, {}) | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
psafont marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Shut down the server to reduce future problems when testing other clients. | ||
kill $JSONRPC_SERVER_PID | ||
} | ||
|
||
main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
acefei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"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" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.