From a43127ad734428d6d5ec66d6ff524001579eb086 Mon Sep 17 00:00:00 2001 From: tttttangTH Date: Mon, 19 Oct 2020 14:47:20 -0400 Subject: [PATCH 1/2] [web] fix rest-test issues --- src/rest/resource.cpp | 28 +++++++--------------------- src/rest/resource.hpp | 7 ------- src/rest/rest_web_server.cpp | 2 -- tests/rest/CMakeLists.txt | 4 ++++ tests/rest/test-rest-server | 6 ++++++ tests/rest/test_rest.py | 13 +++++++++---- 6 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/rest/resource.cpp b/src/rest/resource.cpp index 865e1e6728b..def6a00ab23 100644 --- a/src/rest/resource.cpp +++ b/src/rest/resource.cpp @@ -123,11 +123,6 @@ Resource::Resource(ControllerOpenThread *aNcp) mResourceCallbackMap.emplace(OT_REST_RESOURCE_PATH_DIAGNOETIC, &Resource::HandleDiagnosticCallback); } -void Resource::Init(void) -{ - otThreadSetReceiveDiagnosticGetCallback(mInstance, &Resource::DiagnosticResponseHandler, this); -} - void Resource::Handle(Request &aRequest, Response &aResponse) const { std::string url = aRequest.GetUrl(); @@ -518,31 +513,22 @@ void Resource::UpdateDiag(std::string aKey, std::vector &aDiag void Resource::Diagnostic(const Request &aRequest, Response &aResponse) const { - otbrError error = OTBR_ERROR_NONE; + otThreadSetReceiveDiagnosticGetCallback(mInstance, &Resource::DiagnosticResponseHandler, + const_cast(this)); OT_UNUSED_VARIABLE(aRequest); struct otIp6Address rloc16address = *otThreadGetRloc(mInstance); struct otIp6Address multicastAddress; VerifyOrExit(otThreadSendDiagnosticGet(mInstance, &rloc16address, kAllTlvTypes, sizeof(kAllTlvTypes)) == - OT_ERROR_NONE, - error = OTBR_ERROR_REST); - VerifyOrExit(otIp6AddressFromString(kMulticastAddrAllRouters, &multicastAddress) == OT_ERROR_NONE, - error = OTBR_ERROR_REST); + OT_ERROR_NONE); + VerifyOrExit(otIp6AddressFromString(kMulticastAddrAllRouters, &multicastAddress) == OT_ERROR_NONE); VerifyOrExit(otThreadSendDiagnosticGet(mInstance, &multicastAddress, kAllTlvTypes, sizeof(kAllTlvTypes)) == - OT_ERROR_NONE, - error = OTBR_ERROR_REST); + OT_ERROR_NONE); exit: - if (error == OTBR_ERROR_NONE) - { - aResponse.SetStartTime(steady_clock::now()); - aResponse.SetCallback(); - } - else - { - ErrorHandler(aResponse, HttpStatusCode::kStatusInternalServerError); - } + aResponse.SetStartTime(steady_clock::now()); + aResponse.SetCallback(); } void Resource::DiagnosticResponseHandler(otMessage *aMessage, const otMessageInfo *aMessageInfo, void *aContext) diff --git a/src/rest/resource.hpp b/src/rest/resource.hpp index 80c9aca79b5..b818d7c913f 100644 --- a/src/rest/resource.hpp +++ b/src/rest/resource.hpp @@ -65,13 +65,6 @@ class Resource */ Resource(ControllerOpenThread *aNcp); - /** - * This method initialize the Resource handler. - * - * - */ - void Init(void); - /** * This method is the main entry of resource handler, which find corresponding handler according to request url * find the resource and set the content of response. diff --git a/src/rest/rest_web_server.cpp b/src/rest/rest_web_server.cpp index a9c7964b481..120519016e2 100644 --- a/src/rest/rest_web_server.cpp +++ b/src/rest/rest_web_server.cpp @@ -61,8 +61,6 @@ otbrError RestWebServer::Init(void) { otbrError error = OTBR_ERROR_NONE; - mResource.Init(); - error = InitializeListenFd(); return error; diff --git a/tests/rest/CMakeLists.txt b/tests/rest/CMakeLists.txt index e0899440b9d..f6ba89dc634 100644 --- a/tests/rest/CMakeLists.txt +++ b/tests/rest/CMakeLists.txt @@ -39,3 +39,7 @@ set_tests_properties(rest-server PROPERTIES set_tests_properties(rest-server PROPERTIES LABELS "TESTREST" ) + +set_tests_properties(rest-server PROPERTIES + TIMEOUT 60 +) diff --git a/tests/rest/test-rest-server b/tests/rest/test-rest-server index 159c2348d50..1c30ca62013 100755 --- a/tests/rest/test-rest-server +++ b/tests/rest/test-rest-server @@ -48,6 +48,12 @@ main() sudo "${CMAKE_BINARY_DIR}"/src/agent/otbr-agent -d 7 -v -I wpan0 "spinel+hdlc+forkpty://$(command -v ot-rcp)?forkpty-arg=1" & trap on_exit EXIT sleep 5 + sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl factoryreset + sleep 1 + sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl ifconfig up + sleep 1 + sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl thread start + sleep 3 sudo python3 "${CMAKE_CURRENT_SOURCE_DIR}"/test_rest.py } diff --git a/tests/rest/test_rest.py b/tests/rest/test_rest.py index dde32dd9752..df4f769cd6b 100644 --- a/tests/rest/test_rest.py +++ b/tests/rest/test_rest.py @@ -37,11 +37,16 @@ def get_data_from_url(url, result, index): - response = urllib.request.urlopen(urllib.request.Request(url)) - body = response.read() - data = json.loads(body) - result[index] = data + try: + response = urllib.request.urlopen(urllib.request.Request(url)) + body = response.read() + data = json.loads(body) + result[index] = data + + + except urllib.error.HTTPError as e: + print(e.code) def get_error_from_url(url, result, index): try: From 7a4769c8cb17db9f13d2efdd8066b8744eda60b5 Mon Sep 17 00:00:00 2001 From: tttttangTH Date: Tue, 20 Oct 2020 05:26:38 -0400 Subject: [PATCH 2/2] resolve comment --- tests/rest/test_rest.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/rest/test_rest.py b/tests/rest/test_rest.py index df4f769cd6b..2d67cf1bffa 100644 --- a/tests/rest/test_rest.py +++ b/tests/rest/test_rest.py @@ -43,8 +43,6 @@ def get_data_from_url(url, result, index): body = response.read() data = json.loads(body) result[index] = data - - except urllib.error.HTTPError as e: print(e.code)