8000 STF REST API (v2.0.0) by vbanthia-zz · Pull Request #219 · openstf/stf · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

STF REST API (v2.0.0) #219

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 37 commits into from
Jul 28, 2016
Merged

STF REST API (v2.0.0) #219

merged 37 commits into from
Jul 28, 2016

Conversation

vbanthia-zz
Copy link
Member

This PR implements RESTful APIs for STF which will make possible to use STF for test automation.
Detailed discussion can be seen here

In order to implement APIs, I did following things.

API Documentation can be found here

How to use these API's for test automation?

Basic idea behind test automation is by using STF's remote debugging feature. By this feature you can remotely connect STF device to you machine(client). More about this feature

How to connect device

  1. Get all the devices using GET /devices/ endpoint. You can also use fields query parameter to get only useful information such as GET /devices?fields=owner,ready. You can filter the usable device by filtering devices whose owner==null
  2. Next step will be to request a usable device from STF server. You can do this by sending a POST request to /user/devices endpoint with serial in request body. Since this request is going to be asynchronous, STF will return you a polling URL. You can use this polling url to check if device is added into your devices (group).
  3. Once device is added into your devices(/user/devices). You can ask STF server to start remote debugging using POST /user/devices/{serial}/remoteConnect endpoint. This will again return a polling url like /user/devices/{serial}. You can check if remoteConnect is finished by GET /user/devices/{serial}?fields=remoteConnect. If it is true, you can get remoteDebugUrl from device information i.e /user/devices/{serial}?fields=remoteDebugUrl. After getting this url you can use this url for remote connection and then you can run most of the automated test such as appium, calabash etc.
  4. Once test are over, don't forget to disconnect remote debug and release the device by DELETE /user/devices/{serial}/remoteConnect and DELETE /user/devices/{serial}

NOTE

Those who wants to try this branch, they can use npm install stf@2.0.0-beta or docker image docker pull openstf/stf:v2.0.0-beta

8000
@HelderVSousa
Copy link

I have a issues with your branch, i am testing your rest api is working fine but at same time i get a problem wiht the gui from stf, the user interface don't start give errors missing modules or can't locate some of them.

@vbanthia-zz
Copy link
Member Author

@HelderVSousa

I merged some recent UI fix from master to this branch. Check once again. In case if it is not working, please also provide errors detail with your environment info which you can get from stf doctor

@dhartwich1991
Copy link
dhartwich1991 commented Jun 3, 2016

@vbanthia

Hey I tried to build my own version of stf (cloned this repo) with the API enabled. However when I try to access the API I get a 302 response redirecting me to /auth/mock and I can't access the API... Since the tool is behind an internal VPN it's fine to not have to authenticate and use the mock version. But I can't access the API, what can I do?
The server is running with stf -local
Below you'll find the logs from the request I made through curl. Hope you can help me.

Request
curl -H "Authorization: Bearer 26de8268b133418fa8ad87b2e404c60f8ba865f3655b444a83e9bead1157d80c" -i http://172.31.10.33:7100/api/v1/devices

Response
HTTP/1.1 302 Found x-powered-by: Express location: http://172.31.10.33:7100/auth/mock/ vary: Accept content-type: text/plain; charset=utf-8 content-length: 57 date: Fri, 03 Jun 2016 09:32:16 GMT connection: close

@vbanthia-zz
Copy link
Member Author
vbanthia-zz commented Jun 3, 2016

@dhartwich1991

I tried reproducing this but could not. Most probably you are still using master branch. Can you confirm this?

In case problem is not resolved, please post stf logs too.

@dhartwich1991
Copy link

@vbanthia

I ran stf -V and it returned 2.0.0 so I guess I am running from the correct branch.

@fbatogo
Copy link
fbatogo commented Jun 9, 2016

@vbanthia @dhartwich1991

I hit the same problem @dhartwich1991 did. The problem was that I had not looked at the changes to the deployment guide, so I didn't have nginx set up, or the api service running.

Is it possible that when running "stf local" that it isn't starting the api service, and nginx isn't configured correctly?

Also, I've found some issues with the deployment document. The same stf-api@.service appears to tells you to run "stf app", instead of "stf api". However, once you change it to "stf api" (and set your secret), the "stf api" command complains that there isn't a "--connect-sub" or "--connect-push" parameter provided.

I'm not sure which socket "stf api" wants to connect to, but when I try either 7170 or 7270 for the "--connect-push" and then attempt to query the devices endpoint, the docker for "stf api" terminates with no log messages. (I may need to crank up the logging level, that is the next thing I am going to look in to.)

So, the deployment doc probably needs to be updated a little bit to account for these issues. (Unless I am doing everything wrong, which is entirely possible!)

BTW - @vbanthia Thanks for doing this! It'll make my integration with Jenkins a LOT easier! (I am planning on looking in to what is involved in building a Jenkins plugin to work with these patches.)

@fbatogo
Copy link
fbatogo commented Jun 9, 2016

@vbanthia
I tried port 7270 for the push, and 7250 for the sub, and managed to query the devices endpoint! (So, anyone else struggling with this should use those ports to get everything working.)

@fbatogo
Copy link
fbatogo commented Jun 14, 2016

@vbanthia - I've been working on a small Jenkins plugin, but hit an issue with the API. When I try to add a device to a user, I keep getting back a 504 response with the JSON '{"success":false,"description":"Device is not responding"}'.

From looking at the logs for the API docker, I don't see any error messages when I run the query. So, it is unclear what the problem might be.

FWIW - This happens with both curl and my Java code. The command I am using is :

curl -v -X POST --header "Content-Type:application/json" --data '{"serial":"HT254W302991"}' -H "Authorization: Bearer ea06b6215c844409bdee5cfb970dcee8a702e72c67e1461492576b84c4749114" http://192.168.64.56/api/v1/user/devices

(And, yes, I am really running it over http, not https. I know there are some issues with that, but I have not had a chance to rebuild my test environment yet. I'll probably start on that today, in case it helps with this issue.)

FWIW - I'll post this over on the Google group as well, just in case this discussion isn't monitored.

@vbanthia-zz
Copy link
Member Author

Github is the right place for discussion.

Are you able to connect same device from STF UI? In case yes, how much time does it take to get device connected after pressing "Use" button.

This error is raised when stf api service is not getting JoinGroupMessage (or device connected) message from backend within 5 second. Code related to this error is here

@vbanthia-zz
Copy link
Member Author

@fbatogo

One more thing, if you want STF Java Client for Jenkins plugin, you can generate one using Swagger online codegen and STF swagger.yaml

@fbatogo
Copy link
fbatogo commented Jun 16, 2016

Hi @vbanthia -

Per your suggestion, I moved my development from my own home-grown interface to the API to the swagger generated Java class. Seems like it will be easier to maintain in the long run.

I moved my setup over to using HTTPS to try to rule that out as a problem. However, the issue still remains. If I use the web interface I am able to claim a device and even control it as expected. So, I either have something messed up in my setup (wouldn't be the first time), or there is a bug in the API.

I'm going to spend an hour or two trying to dig in to the API code and see if I can figure out what is going on, but if you have any thoughts on things I can check, I would appreciate it.

If I manage to figure out what is going on, I'll post a follow-up here.

Thanks again for your work! It is appreciated!

@fbatogo
Copy link
fbatogo commented Jun 17, 2016

Hi @vbanthia -

I am completely at a loss as to what is going on. I set up a local OpenSTF on my development machine, and was able to access the device over the API even though I couldn't actually use it through the UI. But, on the set up that I have where I can use devices through the UI, I can't access the devices.

On the set up that times out trying to take control of a device, I tried changing the time out in the code you linked to. I set it to 30 seconds, figuring that should be more than enough for a response, even if everything is running really slowly. The end result is still the same. (Except now I have to wait 30 seconds for it to happen.)

When I access devices through the UI, there isn't any noticeable lag, so it doesn't seem that lag is the issue. It seems more like the response that the API is waiting for isn't getting where it is supposed to go.

Since I was just guessing as to how the API hooked in to everything else, is it possible that I got something hooked up wrong? (i.e. Are the ports not 7270 and 7250?)

The only other thing I can think of that could be a problem is that the devices are connected to one machine, and the rest of the OpenSTF processes run on another machine. (The machine with the devices has the adb and provider service running.) But, that seems like a dead end too since the UI is working.

Is there any kind of verbose debugging that I can turn on to try to track down what is failing? Or is there anything else you can think of that might be a problem?

Thanks!

@vbanthia-zz
Copy link
Member Author

@fbatogo
Sorry, I could not look into your issue, will try to check this sometime next week.

@dhartwich1991
Copy link

@fbatogo @vbanthia
Somehow I don't really get if I am doing everything right in order to start up the API service, on what port it is running etc. could you be so kind to give me a short list on how to start up the service correctly keeping the stf local etc.

Currently we do:
~/stf/bin/stf local --public-ip 172.31.10.33

and:

rethinkdb

what else needs to be called in order to get access to the API, please help me out since I never worked with Docker / node before and I don't know how to get started :(

@fbatogo
Copy link
fbatogo commented Jun 21, 2016

@vbanthia - No worries. I've done a fair bit of open source in the past, so I don't expect you guys to work on the weekends or do anything too wildly special for me. Which is why I am mostly asking for enough help to hopefully figure out the issue and post back here what it was. Knowing how to enable more verbose logging would be REALLY helpful, as I have found it kinda hard to debug issues with OpenSTF. (Which may well be a comment on me not understanding the technology as anything. ;)

@dhartwich1991 -

I'm not sure that the order you start the various services matters much. However, I do always try to start up rethinkdb before starting anything else since it seems to be the core of everything.

If you are trying to start OpenSTF using the "local" command, you don't really need to know anything about docker. (That said, when I started with OpenSTF I was a total docker n00b. I spent a little bit of time trying to understand docker a bit more, and was surprised at how handy it is. One thing that messed me up was a belief that docker was something akin to a VM. In reality, it is more akin to a chroot environment. Once I understood that, things started to make a fair bit more sense!)

I suspect the problem you are running in to has to do with following the instructions on Github to get OpenSTF running. This will work just fine, but you will end up with an OpenSTF instance that doesn't contain the API functionality. You can verify that pretty easily using two different methods. One is to run "stf -V" and make sure it outputs "2.0.0". The other is to run "stf --help" and look in the output to see if there is a line that says "api [options] start api" in the help output. If the version isn't 2.0.0, and the "api" module isn't listed in the help output, then you aren't running the branch that has the API functionality.

I would bet that there is a better way to do this, but I basically downloaded the 2.0.0 branch to a directory and then copied it over the files that were downloaded using the instructions from the GitHub instruction page. (For me, the downloaded version of OpenSTF ended up in the /usr/local/lib/node_modules/stf directory.) You will probably want to make a backup of that directory before you overwrite the files. Also note that when I did this, I was able to get the API working, but I had some issues with the devices being accessible through the UI. For my needs at the time, this wasn't an issue, but it may be for you. That said, I suspect that the bulk of my problem with the UI came from the fact that I had some development processes on my machine that were also using adb to talk to the device. So, you may not have any problems, but YMMV.

@fbatogo
Copy link
fbatogo commented Jun 23, 2016

After MUCH mucking around, I finally figured out that the problem I was running in to is that I actually had the ports wrong. (Seems odd that it half way works when the ports are wrong. And that there doesn't seem to be any error messages when they are wrong either.)

For the --connect-push, I had to use 7170. For the --connect-sub, I had to use 7150. So, my stf-api@.service file ends up looking like this :

[Unit]
Description=STF api
After=rethinkdb-proxy-28015.service
BindsTo=rethinkdb-proxy-28015.service

[Service]
EnvironmentFile=/etc/environment
TimeoutStartSec=0
Restart=always
ExecStartPre=/usr/bin/docker pull openstf/stf:v2.0.0-beta
ExecStartPre=-/usr/bin/docker kill %p-%i
ExecStartPre=-/usr/bin/docker rm %p-%i
ExecStart=/usr/bin/docker run --rm
--name %p-%i
--link rethinkdb-proxy-28015:rethinkdb
-e "SECRET="
-p %i:3000
stfapi/chris
stf api --port 3000
--connect-push tcp://:7170
--connect-sub tcp://:7150
ExecStop=-/usr/bin/docker stop -t 10 %p-%i

Hopefully this helps someone else!

@vbanthia-zz
Copy link
Member Author

@fbatogo

Good to know that its working now.

The reason why it was running and not raising any error was because of ZeroMQ behaviour. As you can see API unit uses 7150 and 7170 port to connect with stf's app side sub and push sockets respectively. Unlike HTTP, zeromq's "client" socket does not raise any error if zeromq's "server" are not running. So, previously API unit was listening at 7250 and 7270 port but it was not getting any device connected response since those message were sent on 7150 and 7170 ports.

I will try to figure out much better way to log the proper error message in case client is using wrong port. Thanks for troubleshooting.

@tadesushilgithub
Copy link

How to use this API? Can I use API with having default authentication with local STF? When I do curl I will get request is redirect to mock URL. Please help on this.

@vbanthia-zz
Copy link
Member Author

Read this https://github.com/openstf/stf/blob/2.0.0/doc/API.md and use 2.0.0 branch

@tadesushilgithub
Copy link

You mean that, I need to install STF 2.0.0. Is this STF new release? I went through same link.

On 05-Jul-2016, at 12:22 AM, Vishal Banthia notifications@github.com wrote:

Read this https://github.com/openstf/stf/blob/2.0.0/doc/API.md and use 2.0.0 branch


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

@fbatogo
Copy link
fbatogo commented Jul 7, 2016

@tadesushilgithub

I don't believe the 2.0.0 branch is a full release yet. So, you do have to do things a little different to get it up and running. However, if you look at my comments above, and use the documentation included in the new branch, it isn't too hard to get going.

And, yes, it should work fine with a local STF. I debugg 8000 ed my Jenkins plug-in using a local STF.

If you need a stable release, you'll probably want to wait for the official release.

@tadesushilgithub
Copy link

I am getting error while running command npm install stf@2.0.0-beta

npm ERR! Error: version not found: stf@2.0.0-beta

@deg0nz
Copy link
deg0nz commented Jul 12, 2016

@tadesushilgithub
version 2.0.0-beta is not present in npm, you have to remove the "-beta".
Just try npm install stf@2.0.0
This should work

@tadesushilgithub
Copy link

Hi,
After executing curl
>curl -H "Authorization: Bearer a3fc22e88ba74fb3b67cfcaf0154da
138714a5fd764f4021a08f49b8bd03209f" -i http://ip:7100/api/v1/devices

HTTP/1.1 500 Internal Server Error
x-powered-by: Express
content-type: application/json; charset=utf-8
content-length: 17
etag: W/"11-wKQin2UUhiiybkUTBN2saA"
date: Tue, 26 Jul 2016 11:33:45 GMT
connection: close

{"success":false}

I am getting HTTP/1.1 500 Internal Server Error. What could be the cause?

@vbanthia-zz
Copy link
Member Author

Without STF logs, I cant interpret anything.

@tadesushilgithub
Copy link

Unhandled rejection TimeoutError: operation timed out
at tryOnTimeout (timers.js:224:11)
at Timer.listOnTimeout (timers.js:198:5)

@vbanthia-zz vbanthia-zz merged commit 5a2af57 into master Jul 28, 2016
@vbanthia-zz vbanthia-zz deleted the 2.0.0 branch July 28, 2016 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0