Merge ~pwlars/testflinger-cli:timeouts into testflinger-cli:master

Proposed by Paul Larson
Status: Merged
Merged at revision: 58759ab1940eb1d288e70bcb2f3fb7a10987893b
Proposed branch: ~pwlars/testflinger-cli:timeouts
Merge into: testflinger-cli:master
Diff against target: 142 lines (+38/-7)
2 files modified
testflinger-cli (+18/-2)
testflinger_cli/__init__.py (+20/-5)
Reviewer Review Type Date Requested Status
Maciej Kisielewski (community) Approve
Review via email: mp+316480@code.launchpad.net

Description of the change

Add some better error handling, and also handle timeouts better when trying to communicate with the testflinger server

To post a comment you must log in.
Revision history for this message
Maciej Kisielewski (kissiel) wrote :

Works great! +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/testflinger-cli b/testflinger-cli
2index 126332e..a845129 100755
3--- a/testflinger-cli
4+++ b/testflinger-cli
5@@ -1,4 +1,4 @@
6-#!/usr/bin/env python3 -u
7+#!/usr/bin/env python3
8 # Copyright (C) 2017 Canonical
9 #
10 # This program is free software: you can redistribute it and/or modify
11@@ -53,6 +53,9 @@ def status(ctx, job_id):
12 elif e.status == 400:
13 print('Invalid job id specified. Check the job id to be sure it '
14 'is correct')
15+ if e.status == 404:
16+ print('Received 404 error from server. Are you sure this '
17+ 'is a testflinger server?')
18 sys.exit(1)
19 print(job_state)
20
21@@ -71,10 +74,14 @@ def submit(ctx, filename, quiet):
22 if e.status == 400:
23 print('The job you submitted contained bad data or bad '
24 'formatting, or did not specify a job_queue.')
25+ if e.status == 404:
26+ print('Received 404 error from server. Are you sure this '
27+ 'is a testflinger server?')
28 else:
29 # This shouldn't happen, so let's get the full trace
30 print('Unexpected error status from testflinger '
31 'server: {}'.format(e.status))
32+ sys.exit(1)
33 if quiet:
34 print(job_id)
35 else:
36@@ -95,6 +102,9 @@ def results(ctx, job_id):
37 elif e.status == 400:
38 print('Invalid job id specified. Check the job id to be sure it '
39 'is correct')
40+ if e.status == 404:
41+ print('Received 404 error from server. Are you sure this '
42+ 'is a testflinger server?')
43 sys.exit(1)
44
45 print(json.dumps(results, sort_keys=True, indent=4))
46@@ -115,6 +125,9 @@ def artifacts(ctx, job_id, filename):
47 elif e.status == 400:
48 print('Invalid job id specified. Check the job id to be sure it '
49 'is correct')
50+ if e.status == 404:
51+ print('Received 404 error from server. Are you sure this '
52+ 'is a testflinger server?')
53 sys.exit(1)
54 print('Artifacts downloaded to {}'.format(filename))
55
56@@ -133,6 +146,9 @@ def poll(ctx, job_id):
57 elif e.status == 400:
58 print('Invalid job id specified. Check the job id to be sure it '
59 'is correct')
60+ if e.status == 404:
61+ print('Received 404 error from server. Are you sure this '
62+ 'is a testflinger server?')
63 sys.exit(1)
64 while True:
65 output = ''
66@@ -143,7 +159,7 @@ def poll(ctx, job_id):
67 # We are still waiting for the job to start
68 pass
69 if output:
70- print(output, end='')
71+ print(output, end='', flush=True)
72 job_state = conn.get_status(job_id)
73 if job_state == 'complete':
74 break
75diff --git a/testflinger_cli/__init__.py b/testflinger_cli/__init__.py
76index 842a62d..a5ba9d7 100644
77--- a/testflinger_cli/__init__.py
78+++ b/testflinger_cli/__init__.py
79@@ -16,6 +16,7 @@
80
81 import json
82 import requests
83+import sys
84 import urllib.parse
85 import yaml
86
87@@ -30,7 +31,7 @@ class Client():
88 def __init__(self, server):
89 self.server = server
90
91- def get(self, uri_frag):
92+ def get(self, uri_frag, timeout=5):
93 """Submit a GET request to the server
94 :param uri_frag:
95 endpoint for the GET request
96@@ -38,12 +39,19 @@ class Client():
97 String containing the response from the server
98 """
99 uri = urllib.parse.urljoin(self.server, uri_frag)
100- req = requests.get(uri)
101+ try:
102+ req = requests.get(uri, timeout=timeout)
103+ except requests.exceptions.ConnectTimeout as e:
104+ print('Timout while trying to communicate with the server.')
105+ sys.exit(1)
106+ except requests.exceptions.ConnectionError as e:
107+ print('Unable to communicate with specified server.')
108+ sys.exit(1)
109 if req.status_code != 200:
110 raise HTTPError(req.status_code)
111 return req.text
112
113- def put(self, uri_frag, data):
114+ def put(self, uri_frag, data, timeout=5):
115 """Submit a POST request to the server
116 :param uri_frag:
117 endpoint for the POST request
118@@ -51,7 +59,14 @@ class Client():
119 String containing the response from the server
120 """
121 uri = urllib.parse.urljoin(self.server, uri_frag)
122- req = requests.post(uri, json=data)
123+ try:
124+ req = requests.post(uri, json=data, timeout=timeout)
125+ except requests.exceptions.ConnectTimeout as e:
126+ print('Timout while trying to communicate with the server.')
127+ sys.exit(1)
128+ except requests.exceptions.ConnectionError as e:
129+ print('Unable to communicate with specified server.')
130+ sys.exit(1)
131 if req.status_code != 200:
132 raise HTTPError(req.status_code)
133 return req.text
134@@ -103,7 +118,7 @@ class Client():
135 """
136 endpoint = '/v1/result/{}/artifact'.format(job_id)
137 uri = urllib.parse.urljoin(self.server, endpoint)
138- req = requests.get(uri)
139+ req = requests.get(uri, timeout=5)
140 if req.status_code != 200:
141 raise HTTPError(req.status_code)
142 with open(path, 'wb') as artifact:

Subscribers

People subscribed via source and target branches