This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding curl client for ambari requests
- Loading branch information
Showing
4 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,2 +1,3 @@ | ||
nose | ||
coverage | ||
coverage | ||
mock |
This file contains 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,90 @@ | ||
import json | ||
from shell import Shell | ||
|
||
|
||
class CurlClient: | ||
username = '' | ||
password = '' | ||
server = '' | ||
port = '' | ||
proto = '' | ||
cmd = Shell() | ||
|
||
def set_username(self, username): | ||
self.username = username | ||
|
||
def set_password(self, password): | ||
self.password = password | ||
|
||
# Set HTTP protocol (http or https) | ||
def set_proto(self, proto): | ||
if proto == 'http' or proto == 'https': | ||
self.proto = proto | ||
else: | ||
raise ValueError('Protocol must be http or https') | ||
|
||
def set_server(self, server): | ||
self.server = server | ||
|
||
# A number between 0 and 65535 | ||
def set_port(self, port): | ||
if not type(port) is int: | ||
raise ValueError('Server port was not of type: int') | ||
if port > 0 and port <= 65535: | ||
self.port = port | ||
else: | ||
raise ValueError('Server port must be between 0 and 65535. Value was ' + str(port)) | ||
|
||
|
||
# Make a request via cURL | ||
# Must pass an HTTP verb GET|PUT|POST|DELETE | ||
# The request to the server (possibly something like /api/v1/resource...) | ||
# A list of query parameters | ||
# ['param1=value1', 'param2=value2'] | ||
|
||
def make_request(self, verb, request, query=''): | ||
|
||
if not (verb == 'GET' or verb == 'POST' or verb == 'PUT' or verb == 'DELETE'): | ||
raise ValueError('HTTP Verb must be one of GET|PUT|POST|DELETE') | ||
|
||
query = '&'.join(query) | ||
url = ''.join([self.proto, '://', self.server, ':', str(self.port), request]) | ||
url = url + '?' | ||
url = url + query | ||
|
||
|
||
credentials = ':'.join([self.username, self.password]) | ||
credentials = '-u ' + credentials | ||
|
||
method = '-X ' + verb | ||
|
||
call = ' '.join(['curl', credentials, method, url]) | ||
output = self.cmd.run(call) | ||
return output | ||
|
||
|
||
def __init__(self, username='', password='', proto='http', server='127.0.0.1', port=8080): | ||
self.set_username(username) | ||
self.set_password(password) | ||
self.set_server(server) | ||
self.set_port(port) | ||
self.set_proto(proto) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains 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
This file contains 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,101 @@ | ||
import unittest | ||
import mock | ||
from mock import MagicMock, Mock | ||
#from unittest.mock import MagicMock | ||
from env import scripts | ||
from scripts.curl_client import CurlClient | ||
|
||
res1 = ['\'key1\':value1', ''] | ||
res2 = ['\'key2\':value2', ''] | ||
res3 = ['\'key3\':value3', ''] | ||
resErr = ['', '\'msg\':\'err\''] | ||
|
||
def mocked_request(*args, **kwargs): | ||
if args[0] == 'curl -u admin:admin -X GET http://demo-server:9090/api/v1/test?': | ||
return res1 | ||
elif args[0] == 'curl -u admin:admin -X GET http://demo-server:9090/api/v1/bad?': | ||
return res2 | ||
else: | ||
return res3 | ||
return resErr | ||
|
||
class TestCurlClient(unittest.TestCase): | ||
|
||
@mock.patch('scripts.shell.Shell.run', side_effect=mocked_request) | ||
def test_basic_command(self, mock_get): | ||
un = 'admin' | ||
pw = 'admin' | ||
pt = 9090 | ||
serv = 'demo-server' | ||
prtc = 'http' | ||
client = CurlClient(username=un, password=pw, port=pt, server=serv, proto=prtc) | ||
assert client.username == un | ||
assert client.password == pw | ||
assert client.port == pt | ||
assert client.server == serv | ||
assert client.proto == prtc | ||
output = client.make_request('GET', '/api/v1/test') | ||
assert output == res1 | ||
output = client.make_request('GET', '/api/v1/bad') | ||
assert output == res2 | ||
output = client.make_request('GET', '') | ||
assert output == res3 | ||
|
||
|
||
def test_bad_port(self): | ||
ports = [-1, 0, 65536] | ||
for pt in ports: | ||
try: | ||
client = CurlClient(port=pt) | ||
self.fail('Cannot set port to ' + str(port)) | ||
except ValueError: | ||
pass | ||
|
||
def test_bad_proto(self): | ||
protos = ['http', 'https'] | ||
|
||
for prtc in protos: | ||
try: | ||
client = CurlClient(proto=prtc) | ||
except ValueError: | ||
self.fail('Cannot set protocol to ' +proto) | ||
|
||
bad_protos = ['htp', 'thrift', 'odbc'] | ||
for prtc in bad_protos: | ||
try: | ||
client = CurlClient(proto=prtc) | ||
self.fail('Cannot set protocol to ' +proto) | ||
except ValueError: | ||
pass | ||
|
||
# @mock.patch('scripts.shell.Shell.run', side_effect=mocked_request) | ||
def test_http_verbs(self): | ||
client = CurlClient() | ||
|
||
verbs = ['GET', 'PUT', 'POST', 'DELETE'] | ||
bad_verbs = ['BAD', 'IAMBAD', 'IAMSUPERBAD'] | ||
|
||
for v in verbs: | ||
try: | ||
client.make_request(v, '/') | ||
except: | ||
self.fail('Should not throw error on verb: ' + v) | ||
|
||
for v in bad_verbs: | ||
try: | ||
client.make_request(v, '/') | ||
self.fail('Should throw error on verb: ' + v) | ||
except: | ||
pass | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|