Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 6 additions & 4 deletions ocaml/sdk-gen/component-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ spec/
#### jsonrpc-client
jsonrpc-client is a client that imports the SDK and runs the functions, following these important details:

1. Add test_id as a customize request header.
1. Single test case in single test spec file

2. 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. Ensure that the function and params are aligned with the data defined in spec/ directory.

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.
4. In order to support test reports, practitioners should use the specific test framework to test SDK, eg: pytest, gotest, junit, xUnit and so on.

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:
5. 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
```
Expand Down
187 changes: 187 additions & 0 deletions ocaml/sdk-gen/component-test/jsonrpc-client/go/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package componenttest

import (
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
"xenapi"
)

func TestRpcError(t *testing.T) {
//Constuct an rpc client error
data, err := ReadJsonFile("../../spec/xapi-24/rpc_error.json")
if err != nil {
t.Log(err)
t.Fail()
return
}

testId, err := GetTestId(data)
if err != nil {
t.Log(err)
t.Fail()
return
}
session, err := GetSession(testId)
if err != nil {
t.Log(err)
t.Fail()
return
}

type ResultBody struct {
Result map[string]interface{} `json:"result,omitempty"`
Error map[string]interface{} `json:"error,omitempty"`
}

type MethodResult struct {
MethodName ResultBody `json:"VM.get_record,omitempty"`
}

type TestSpecBody struct {
Method []string `json:"method,omitempty"`
Params map[string][]interface{} `json:"params,omitempty"`
ExpectedResult MethodResult `json:"expected_result"`
}

type TestSpec struct {
Key TestSpecBody `json:"xapi-24/rpc_error_09"`
}

var spec TestSpec
if err := json.Unmarshal(data, &spec); err != nil {
t.Log(err)
t.Fail()
return
}

testMethod := "VM.get_record"
vmRef := spec.Key.Params[testMethod][1].(string)
_, err = xenapi.VM.GetRecord(session, xenapi.VMRef(vmRef))
if err == nil {
t.Log(err)
t.Fail()
return
}

errorString := "call " + testMethod + "() on " + ServerURL + "/jsonrpc status code: 200. Could not decode response body: json: cannot unmarshal string into Go struct field ResponseError.error.code of type int"
var rpcError = errors.New(errorString)
if err.Error() != rpcError.Error() {
t.Log("The expected error is not the same with the returned one!")
t.Fail()
return
}
}

func TestHttpError(t *testing.T) {
// Construct an error in RPC response that returned for method/parameter check not as expected
data, err := ReadJsonFile("../../spec/xapi-24/http_error.json")
if err != nil {
t.Log(err)
t.Fail()
return
}

testId, err := GetTestId(data)
if err != nil {
t.Log(err)
t.Fail()
return
}
session, err := GetSession(testId)
if err != nil {
t.Log(err)
t.Fail()
return
}

//Use a VMRef which is inconsist with in json file to trigger server check error then return HTTP Error
var vmRef xenapi.VMRef = "OpaqueRef:6ef08bce-0bf0-30ff-804f-5f0ee4bbdd13"
var httpError = errors.New("API error: code 500, message Rpc server failed to handle the client request!")

_, err = xenapi.VM.GetRecord(session, vmRef)
if err == nil {
t.Log("Expected to be error, but no error is detected")
t.Fail()
return
}

if !strings.Contains(err.Error(), httpError.Error()) {
t.Log("The expected error is not the same with the returned one!")
t.Fail()
return
}
}

func TestXapiError(t *testing.T) {
// Construct an error that returned from Xapi
data, err := ReadJsonFile("../../spec/xapi-24/xapi_error.json")
if err != nil {
t.Log(err)
t.Fail()
return
}

testId, err := GetTestId(data)
if err != nil {
t.Log(err)
t.Fail()
return
}
session, err := GetSession(testId)
if err != nil {
t.Log(err)
t.Fail()
return
}

type ResultBody struct {
Result map[string]interface{} `json:"result,omitempty"`
Error map[string]interface{} `json:"error,omitempty"`
}

type MethodResult struct {
MethodName ResultBody `json:"VM.get_record,omitempty"`
}

type TestSpecBody struct {
Method []string `json:"method,omitempty"`
Params map[string][]interface{} `json:"params,omitempty"`
ExpectedResult MethodResult `json:"expected_result"`
}

type TestSpec struct {
Key TestSpecBody `json:"xapi-24/xapi_error_13"`
}

var spec TestSpec
if err := json.Unmarshal(data, &spec); err != nil {
t.Log(err)
t.Fail()
return
}

inputParams := spec.Key.Params["VM.get_record"]
vmRef := inputParams[1].(string)

expectedError := spec.Key.ExpectedResult.MethodName.Error
errorCode := expectedError["code"].(float64)
errorMessage := expectedError["message"].(string)
errorData := expectedError["data"].(string)
xapiError := errors.New("API error: code " + fmt.Sprint(errorCode) + ", message " + errorMessage + ", data " + errorData)

_, err = xenapi.VM.GetRecord(session, xenapi.VMRef(vmRef))
if err == nil {
t.Log("Expected to be error, but no error is detected")
t.Fail()
return
}

if !strings.Contains(err.Error(), xapiError.Error()) {
t.Log("The expected error is not the same with the returned one!")
t.Fail()
return
}
}
91 changes: 91 additions & 0 deletions ocaml/sdk-gen/component-test/jsonrpc-client/go/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package componenttest

import (
"encoding/json"
"reflect"
"testing"
"xenapi"
)

func TestEventFrom(t *testing.T) {
data, err := ReadJsonFile("../../spec/xapi-24/event_from.json")
if err != nil {
t.Log(err)
t.Fail()
return
}

testId, err := GetTestId(data)
if err != nil {
t.Log(err)
t.Fail()
return
}
session, err := GetSession(testId)
if err != nil {
t.Log(err)
t.Fail()
return
}

type ResultBody struct {
Result xenapi.EventBatch `json:"result,omitempty"`
}

type MethodResult struct {
MethodName ResultBody `json:"event.from,omitempty"`
}

type TestSpecBody struct {
Method []string `json:"method,omitempty"`
Params map[string][]interface{} `json:"params,omitempty"`
ExpectedResult MethodResult `json:"expected_result"`
}

type TestSpec struct {
Key TestSpecBody `json:"xapi-24/event_from_11"`
}

var spec TestSpec
if err := json.Unmarshal(data, &spec); err != nil {
t.Log(err)
t.Fail()
return
}

expectedResult := spec.Key.ExpectedResult.MethodName.Result

inputParams := spec.Key.Params["event.from"]
const (
IndexClasses = 1
IndexToken = 2
IndexTimeout = 3
)
classesInterfaceSlice, ok1 := inputParams[IndexClasses].([]interface{})
token, ok2 := inputParams[IndexToken].(string)
timeout, ok3 := inputParams[IndexTimeout].(float64)
if !ok1 || !ok2 || !ok3 {
t.Log("Parameter get error from json file")
t.Fail()
return
}
classes, err := ConvertInterfaceSliceToStringSlice(classesInterfaceSlice)
if err != nil {
t.Log(err)
t.Fail()
return
}

result, err := xenapi.Event.From(session, classes, token, timeout)
if err != nil {
t.Log(err)
t.Fail()
return
}

if !reflect.DeepEqual(result, expectedResult) {
t.Log("The result returned not the same with expected -> The XAPI outcome diverges from the anticipated value.")
t.Fail()
return
}
}
Loading