Change httplib to requests

Change-Id: Id6f10be2b9f68507ff9e579fc3e5ebdfd71ee1f2
This commit is contained in:
Vincent Fournier 2015-08-06 10:54:29 -04:00
parent a7e07a6c84
commit 10633bb397
47 changed files with 1362 additions and 1547 deletions

View File

@ -15,7 +15,6 @@
import requests
import requests.exceptions
from six.moves import http_client as httplib
from surveilclient import exc
from surveilclient.openstack.common.py3kcompat import urlutils
@ -79,18 +78,15 @@ class HTTPClient(object):
self.auth_token = access['access']['token']
return self.auth_token['id']
def get_connection(self):
def _create_complete_url(self, url):
# TODO(aviau): https
con = httplib.HTTPConnection(
self.endpoint_hostname,
self.endpoint_port
)
return con
return ('http://' + self.endpoint_hostname + ":"
+ str(self.endpoint_port) + self.endpoint_path + url)
def _http_request(self, url, method, **kwargs):
"""Send an http request with the specified characteristics.
Wrapper around httplib.HTTP(S)Connection.request to handle tasks such
Wrapper around requests to handle tasks such
as setting headers and error handling.
"""
kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {}))
@ -99,38 +95,27 @@ class HTTPClient(object):
if self.authenticated:
kwargs['headers']['X-Auth-Token'] = self._get_auth_token()
conn = self.get_connection()
request_params = urlutils.urlencode(
kwargs.pop("params", {})
)
request_url = self.endpoint_path + url + '?' + request_params
url = self._create_complete_url(url)
for attempt in range(3):
try:
conn.request(method, request_url, **kwargs)
resp = getattr(requests, method.lower())(url, **kwargs)
break
except (
httplib.BadStatusLine,
httplib.IncompleteRead
requests.Timeout,
requests.ConnectionError
) as exp:
if attempt == 2:
raise exp
time.sleep(1)
resp = conn.getresponse()
body_str = resp.read()
if 400 <= resp.status < 600:
if 400 <= resp.status_code < 600:
raise exc.from_response(
response=resp, body=body_str, method=method, url=url)
elif resp.status == 300:
response=resp, body=resp.content, method=method, url=url)
elif resp.status_code == 300:
raise exc.from_response(
response=resp, body=body_str, method=method, url=url)
response=resp, body=resp.content, method=method, url=url)
return resp, body_str
return resp
def json_request(self, url, method, **kwargs):
"""Send an http request with the specified characteristics.
@ -139,14 +124,11 @@ class HTTPClient(object):
kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {}))
kwargs['headers'].setdefault('Content-Type', 'application/json')
if 'body' in kwargs:
kwargs['body'] = json.dumps(kwargs['body'])
if 'data' in kwargs:
kwargs['data'] = json.dumps(kwargs['data'])
resp, body = self.request(url, method, **kwargs)
if body != "":
body = json.loads(body)
return resp, body
resp, content = self.request(url, method, **kwargs)
return resp, resp.json() if content != '' else ''
def request(self, url, method, **kwargs):
"""Send an http request with the specified characteristics.
@ -154,5 +136,5 @@ class HTTPClient(object):
"""
kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {}))
resp, body = self._http_request(url, method, **kwargs)
return resp, body.decode()
resp = self._http_request(url, method, **kwargs)
return resp, resp.content.decode()

View File

@ -15,7 +15,7 @@
import json
import unittest
import httpretty
import requests_mock
from surveilclient.common import http
@ -26,18 +26,17 @@ class TestHttp(unittest.TestCase):
self.surveil_url = 'http://surveil:5311/v1'
self.client = http.HTTPClient(self.surveil_url, authenticated=False)
@httpretty.activate
def test_json_request_get(self):
example_result = {'hello': 'surveil'}
httpretty.register_uri(httpretty.GET,
self.surveil_url + "/test",
body=json.dumps(example_result))
with requests_mock.mock() as m:
example_result = {'hello': 'surveil'}
m.get(self.surveil_url + "/test",
text=json.dumps(example_result))
resp, body = self.client.json_request('/test', 'GET')
self.assertEqual(httpretty.last_request().method, 'GET')
self.assertEqual(body, example_result)
resp, body = self.client.json_request('/test', 'GET')
self.assertEqual(m.last_request.method, 'GET')
self.assertEqual(body, example_result)
self.assertEqual(
httpretty.last_request().headers['Content-Type'],
'application/json'
)
self.assertEqual(
m.last_request.headers['Content-Type'],
'application/json'
)

View File

@ -12,23 +12,22 @@
# License for the specific language governing permissions and limitations
# under the License.
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestAcknowledge(clienttest.ClientTest):
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/actions/acknowledge",
body='{"message": "Ack received!"}')
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/actions/acknowledge",
text='{"message": "Ack received!"}')
self.client.actions.acknowledge.create(
host_name="somehost"
)
self.assertEqual(
httpretty.last_request().body.decode(),
u'{"host_name": "somehost"}'
)
self.client.actions.acknowledge.create(
host_name="somehost"
)
self.assertEqual(
m.last_request.body,
u'{"host_name": "somehost"}'
)

View File

@ -12,23 +12,22 @@
# License for the specific language governing permissions and limitations
# under the License.
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestDowntime(clienttest.ClientTest):
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/actions/downtime",
body='{"message": "Ack received!"}')
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/actions/downtime",
text='{"message": "Ack received!"}')
self.client.actions.downtime.create(
host_name="somehost"
)
self.assertEqual(
httpretty.last_request().body.decode(),
u'{"host_name": "somehost"}'
)
self.client.actions.downtime.create(
host_name="somehost"
)
self.assertEqual(
m.last_request.body,
u'{"host_name": "somehost"}'
)

View File

@ -11,27 +11,27 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestRecheck(clienttest.ClientTest):
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/actions/recheck",
body='{"message": "Ack received!"}')
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/actions/recheck",
text='{"message": "Ack received!"}')
self.client.actions.recheck.create(
host_name="somehost",
service_description="someservice"
)
self.client.actions.recheck.create(
host_name="somehost",
service_description="someservice"
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{"host_name": "somehost", "service_description": "someservice"}
)
self.assertEqual(
json.loads(m.last_request.body),
{"host_name": "somehost", "service_description": "someservice"}
)

View File

@ -14,122 +14,114 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestBusinessImpactModulations(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/"
"businessimpactmodulations",
body='[{"business_impact": 1,'
'"business_impact_modulation_name": "LowImpactOnDay",'
'"modulation_period": "day"},'
'{"business_impact": 1,'
'"business_impact_modulation_name": "LowImpactOnNight",'
'"modulation_period": "night"}]'
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/"
"businessimpactmodulations",
text='[{"business_impact": 1,'
'"business_impact_modulation_name": "LowImpactOnDay",'
'"modulation_period": "day"},'
'{"business_impact": 1,'
'"business_impact_modulation_name": '
'"LowImpactOnNight",'
'"modulation_period": "night"}]'
)
)
businessimpactmodulations = (self.client.config.
businessimpactmodulations.list())
businessimpactmodulations = (self.client.config.
businessimpactmodulations.list())
self.assertEqual(
businessimpactmodulations,
[{"business_impact": 1,
"business_impact_modulation_name": "LowImpactOnDay",
"modulation_period": "day"},
{"business_impact": 1,
"business_impact_modulation_name": "LowImpactOnNight",
"modulation_period": "night"}, ]
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/"
"businessimpactmodulations",
body='{"business_impact": 1,'
'"business_impact_modulation_name": "testtt",'
'"modulation_period": "day"}'
)
self.client.config.businessimpactmodulations.create(
business_impact=1,
business_impact_modulation_name="testtt",
modulation_period="day"
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"business_impact": 1,
"business_impact_modulation_name": "testtt",
"modulation_period": "day"
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
'http://localhost:5311/v2/config/businessimpactmodulations/'
'LowImpactOnDay',
body='{"business_impact": 1,'
'"business_impact_modulation_name": "LowImpactOnDay",'
'"modulation_period": "day"}'
)
businessimpactmodulation = (
self.client.config.businessimpactmodulations.get(
businessimpactmodulation_name='LowImpactOnDay')
self.assertEqual(
businessimpactmodulations,
[{"business_impact": 1,
"business_impact_modulation_name": "LowImpactOnDay",
"modulation_period": "day"},
{"business_impact": 1,
"business_impact_modulation_name": "LowImpactOnNight",
"modulation_period": "night"}, ]
)
self.assertEqual(
businessimpactmodulation,
{"business_impact": 1,
"business_impact_modulation_name": "LowImpactOnDay",
"modulation_period": "day"}
)
def test_create(self):
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/"
"businessimpactmodulations",
text='{"business_impact": 1,'
'"business_impact_modulation_name": "testtt",'
'"modulation_period": "day"}'
)
self.client.config.businessimpactmodulations.create(
business_impact=1,
business_impact_modulation_name="testtt",
modulation_period="day"
)
self.assertEqual(
json.loads(m.last_request.body),
{
"business_impact": 1,
"business_impact_modulation_name": "testtt",
"modulation_period": "day"
}
)
def test_get(self):
with requests_mock.mock() as m:
m.get('http://localhost:5311/v2/config/businessimpactmodulations/'
'LowImpactOnDay',
text='{"business_impact": 1,'
'"business_impact_modulation_name": "LowImpactOnDay",'
'"modulation_period": "day"}'
)
businessimpactmodulation = (
self.client.config.businessimpactmodulations.get(
businessimpactmodulation_name='LowImpactOnDay')
)
self.assertEqual(
businessimpactmodulation,
{"business_impact": 1,
"business_impact_modulation_name": "LowImpactOnDay",
"modulation_period": "day"}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
'http://localhost:5311/v2/config/businessimpactmodulations/'
'LowImpactOnNight',
body='{"test": "test"}'
)
with requests_mock.mock() as m:
m.put('http://localhost:5311/v2/config/'
'businessimpactmodulations/LowImpactOnNight',
text='{"test": "test"}'
)
self.client.config.businessimpactmodulations.update(
businessimpactmodulation_name="LowImpactOnNight",
businessimpactmodulation={'modulation_period': 'night'}
)
self.client.config.businessimpactmodulations.update(
businessimpactmodulation_name="LowImpactOnNight",
businessimpactmodulation={'modulation_period': 'night'}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"modulation_period": u"night"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"modulation_period": u"night"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/businessimpactmodulations/"
"name_to_delete",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/"
"businessimpactmodulations/name_to_delete",
text="body"
)
body = self.client.config.businessimpactmodulations.delete(
businessimpactmodulation_name="name_to_delete",
)
body = self.client.config.businessimpactmodulations.delete(
businessimpactmodulation_name="name_to_delete",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -13,98 +13,93 @@
# under the License.
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestCheckModulations(clienttest.ClientTest):
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/checkmodulations",
body='{"message": "Ack received!"}')
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/checkmodulations",
text='{"message": "Ack received!"}')
self.client.config.checkmodulations.create(
check_command='test',
check_period='test',
checkmodulation_name='test'
)
self.client.config.checkmodulations.create(
check_command='test',
check_period='test',
checkmodulation_name='test'
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{"checkmodulation_name": "test",
"check_command": "test",
"check_period": "test"}
)
self.assertEqual(
json.loads(m.last_request.body),
{"checkmodulation_name": "test",
"check_command": "test",
"check_period": "test"}
)
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/checkmodulations",
body='[{"checkmodulation_name": "test","check_command": "test",'
'"check_period": "test"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/checkmodulations",
text='[{"checkmodulation_name": "test",'
'"check_command": "test",'
'"check_period": "test"}]'
)
self.assertEqual(
self.client.config.checkmodulations.list(),
[{"checkmodulation_name": "test",
"check_command": "test", "check_period": "test"}]
)
self.assertEqual(
self.client.config.checkmodulations.list(),
[{"checkmodulation_name": "test",
"check_command": "test", "check_period": "test"}]
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE, "http://localhost:5311/v2/config/"
"checkmodulations/checkmodulation_to_delete",
body='body')
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/"
"checkmodulations/checkmodulation_to_delete",
text='body')
body = self.client.config.checkmodulations.delete(
checkmodulation_name='checkmodulation_to_delete'
)
body = self.client.config.checkmodulations.delete(
checkmodulation_name='checkmodulation_to_delete'
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
'http://localhost:5311/v2/config/checkmodulations/ping_night',
body='{"checkmodulation_name": "ping_night",'
'"check_command": "check_ping_night",'
'"check_period": "night"}'
)
with requests_mock.mock() as m:
m.get('http://localhost:5311/v2/config/checkmodulations/' +
'ping_night',
text='{"checkmodulation_name": "ping_night",'
'"check_command": "check_ping_night",'
'"check_period": "night"}'
)
checkmodulation = self.client.config.checkmodulations.get(
checkmodulation_name='ping_night'
)
checkmodulation = self.client.config.checkmodulations.get(
checkmodulation_name='ping_night'
)
self.assertEqual(
checkmodulation,
{"checkmodulation_name": "ping_night",
"check_command": "check_ping_night",
"check_period": "night"}
)
self.assertEqual(
checkmodulation,
{"checkmodulation_name": "ping_night",
"check_command": "check_ping_night",
"check_period": "night"}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
'http://localhost:5311/v2/config/checkmodulations/ping_night',
body='{"check_command": "updated"}'
)
with requests_mock.mock() as m:
m.put('http://localhost:5311/v2/config/checkmodulations/' +
'ping_night',
text='{"check_command": "updated"}')
self.client.config.checkmodulations.update(
checkmodulation_name='ping_night',
checkmodulation={"check_command": "updated"}
)
self.client.config.checkmodulations.update(
checkmodulation_name='ping_night',
checkmodulation={"check_command": "updated"}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"check_command": u"updated"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"check_command": u"updated"
}
)

View File

@ -14,99 +14,93 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestCommands(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST,
"http://localhost:5311/v2/config/commands",
body='[{"command_name":"myCommand"}]'
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/commands",
text='[{"command_name":"myCommand"}]'
)
self.assertEqual(
self.client.config.commands.list(),
[{"command_name": "myCommand"}]
)
self.assertEqual(
self.client.config.commands.list(),
[{"command_name": "myCommand"}]
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/commands",
body='{"command_name": "new_command", "command_line": "new_line"}'
)
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/commands",
text='{"command_name": "new_command",'
' "command_line": "new_line"}'
)
self.client.config.commands.create(
command_name="new_command",
command_line="new_line"
)
self.client.config.commands.create(
command_name="new_command",
command_line="new_line"
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"command_name": "new_command",
"command_line": "new_line"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"command_name": "new_command",
"command_line": "new_line"
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
"http://localhost:5311/v2/config/commands/command_to_show",
body='{"command_name": "command_to_show", "command_line": "line"}'
)
with requests_mock.mock() as m:
m.get("http://localhost:5311/v2/config/commands/command_to_show",
text='{"command_name": "command_to_show",'
'"command_line": "line"}'
)
command = self.client.config.commands.get(
command_name="command_to_show"
)
command = self.client.config.commands.get(
command_name="command_to_show"
)
self.assertEqual(
command,
{
"command_name": "command_to_show",
"command_line": "line"
}
)
self.assertEqual(
command,
{
"command_name": "command_to_show",
"command_line": "line"
}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
"http://localhost:5311/v2/config/commands/command_to_update",
body='{"command_line": "updated command_line"}'
)
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/commands/command_to_update",
text='{"command_line": "updated command_line"}'
)
self.client.config.commands.update(
command_name="command_to_update",
command={'command_line': "updated command_line"}
)
self.client.config.commands.update(
command_name="command_to_update",
command={'command_line': "updated command_line"}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"command_line": "updated command_line"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"command_line": "updated command_line"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/commands/command_to_delete",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/commands/"
"command_to_delete",
text="body"
)
body = self.client.config.commands.delete(
command_name="command_to_delete",
)
body = self.client.config.commands.delete(
command_name="command_to_delete",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -14,108 +14,103 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestContactGroups(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/contactgroups",
body='[{"contactgroup_name": "novell-admins",'
'"members": "jdoe,rtobert,tzach"},'
'{"contactgroup_name": "linux-adminx",'
'"members": "linus,richard"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/contactgroups",
text='[{"contactgroup_name": "novell-admins",'
'"members": "jdoe,rtobert,tzach"},'
'{"contactgroup_name": "linux-adminx",'
'"members": "linus,richard"}]'
)
contactgroups = self.client.config.contactgroups.list()
contactgroups = self.client.config.contactgroups.list()
self.assertEqual(
contactgroups,
[{"contactgroup_name": "novell-admins",
"members": "jdoe,rtobert,tzach"},
{"contactgroup_name": "linux-adminx",
"members": "linus,richard"},
]
)
self.assertEqual(
contactgroups,
[{"contactgroup_name": "novell-admins",
"members": "jdoe,rtobert,tzach"},
{"contactgroup_name": "linux-adminx",
"members": "linus,richard"},
]
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/contactgroups",
body='{"contactgroup_name": "John",'
'"members": "marie,bob,joe"}'
)
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/contactgroups",
text='{"contactgroup_name": "John",'
'"members": "marie,bob,joe"}'
)
self.client.config.contactgroups.create(
contactgroup_name='John',
members="marie,bob,joe"
)
self.client.config.contactgroups.create(
contactgroup_name='John',
members="marie,bob,joe"
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"contactgroup_name": "John",
"members": "marie,bob,joe"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"contactgroup_name": "John",
"members": "marie,bob,joe"
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
'http://localhost:5311/v2/config/contactgroups/novell-admins',
body='{"contactgroup_name": "novell-admins",'
'"members": "jdoe,rtobert,tzach"}'
)
with requests_mock.mock() as m:
m.get('http://localhost:5311/v2/'
'config/contactgroups/novell-admins',
text='{"contactgroup_name": "novell-admins",'
'"members": "jdoe,rtobert,tzach"}'
)
contactgroup = self.client.config.contactgroups.get(
contactgroup_name='novell-admins'
)
contactgroup = self.client.config.contactgroups.get(
contactgroup_name='novell-admins'
)
self.assertEqual(
contactgroup,
{
'contactgroup_name': 'novell-admins',
'members': 'jdoe,rtobert,tzach'
}
)
self.assertEqual(
contactgroup,
{
'contactgroup_name': 'novell-admins',
'members': 'jdoe,rtobert,tzach'
}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
'http://localhost:5311/v2/config/contactgroups/novell-admins',
body='{"test": "test"}'
)
with requests_mock.mock() as m:
m.put('http://localhost:5311/v2/config/contactgroups/'
'novell-admins',
text='{"test": "test"}'
)
self.client.config.contactgroups.update(
contactgroup_name="novell-admins",
contactgroup={"members": "updated"}
)
self.client.config.contactgroups.update(
contactgroup_name="novell-admins",
contactgroup={"members": "updated"}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"members": u"updated"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"members": u"updated"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/contactgroups/novell-admins",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/contactgroups/"
"novell-admins",
text="body"
)
body = self.client.config.contactgroups.delete(
contactgroup_name="novell-admins",
)
body = self.client.config.contactgroups.delete(
contactgroup_name="novell-admins",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -14,111 +14,98 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestContacts(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/contacts",
body='[{"contact_name": "bobby",'
'"email": "bob@bob.com"},'
'{"contact_name": "marie",'
'"email": "marie@marie.com"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/contacts",
text='[{"contact_name": "bobby",'
'"email": "bob@bob.com"},'
'{"contact_name": "marie",'
'"email": "marie@marie.com"}]'
)
contacts = self.client.config.contacts.list()
contacts = self.client.config.contacts.list()
self.assertEqual(
contacts,
[
self.assertEqual(
contacts,
[
{
'contact_name': 'bobby',
'email': 'bob@bob.com'
},
{
'contact_name': 'marie',
'email': 'marie@marie.com'
},
]
)
def test_create(self):
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/contacts",
text='{"contact_name": "John"}')
self.client.config.contacts.create(
contact_name='John'
)
self.assertEqual(
json.loads(m.last_request.body),
{
"contact_name": "John"
}
)
def test_get(self):
with requests_mock.mock() as m:
m.get('http://localhost:5311/v2/config/contacts/bobby',
text='{"contact_name": "bobby",'
'"email": "bob@bob.com"}')
contact = self.client.config.contacts.get(
contact_name='bobby'
)
self.assertEqual(
contact,
{
'contact_name': 'bobby',
'email': 'bob@bob.com'
},
{
'contact_name': 'marie',
'email': 'marie@marie.com'
},
]
}
)
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/contacts",
body='{"contact_name": "John"}'
)
self.client.config.contacts.create(
contact_name='John'
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"contact_name": "John"
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
'http://localhost:5311/v2/config/contacts/bobby',
body='{"contact_name": "bobby",'
'"email": "bob@bob.com"}'
)
contact = self.client.config.contacts.get(
contact_name='bobby'
)
self.assertEqual(
contact,
{
'contact_name': 'bobby',
'email': 'bob@bob.com'
}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
'http://localhost:5311/v2/config/contacts/bob',
body='{"test": "test"}'
)
with requests_mock.mock() as m:
m.put('http://localhost:5311/v2/config/contacts/bob',
text='{"test": "test"}')
self.client.config.contacts.update(
contact_name="bob",
contact={"email": "updated"}
)
self.client.config.contacts.update(
contact_name="bob",
contact={"email": "updated"}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"email": u"updated"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"email": u"updated"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/contacts/bob",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/contacts/bob",
text="body")
body = self.client.config.contacts.delete(
contact_name="bob",
)
body = self.client.config.contacts.delete(
contact_name="bob",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -14,113 +14,101 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestHostGroups(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/hostgroups",
body='[{"hostgroup_name": "novell-servers",'
'"members": "netware1,netware2,netware3,netware4"},'
'{"hostgroup_name": "otherservers",'
'"members": "googul,sfl"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/hostgroups",
text='[{"hostgroup_name": "novell-servers",'
'"members": "netware1,netware2,netware3,netware4"},'
'{"hostgroup_name": "otherservers",'
'"members": "googul,sfl"}]'
)
hostgroups = self.client.config.hostgroups.list()
hostgroups = self.client.config.hostgroups.list()
self.assertEqual(
hostgroups,
[
self.assertEqual(
hostgroups,
[
{
'hostgroup_name': 'novell-servers',
'members': 'netware1,netware2,netware3,netware4',
},
{
'hostgroup_name': 'otherservers',
'members': 'googul,sfl',
},
]
)
def test_create(self):
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/hostgroups",
text='{"hostgroup_name": "John",'
'"members": "marie,bob,joe"}')
self.client.config.hostgroups.create(
hostgroup_name='John',
members="marie,bob,joe"
)
self.assertEqual(
json.loads(m.last_request.body),
{
"hostgroup_name": "John",
"members": "marie,bob,joe"
}
)
def test_get(self):
with requests_mock.mock() as m:
m.get('http://localhost:5311/v2/config/hostgroups/novell-servers',
text='{"hostgroup_name": "novell-servers",'
'"members": "netware1,netware2,netware3,netware4"}')
hostgroup = self.client.config.hostgroups.get(
hostgroup_name='novell-servers'
)
self.assertEqual(
hostgroup,
{
'hostgroup_name': 'novell-servers',
'members': 'netware1,netware2,netware3,netware4',
},
{
'hostgroup_name': 'otherservers',
'members': 'googul,sfl',
},
]
)
'members': 'netware1,netware2,netware3,netware4'
}
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/hostgroups",
body='{"hostgroup_name": "John",'
'"members": "marie,bob,joe"}'
)
self.client.config.hostgroups.create(
hostgroup_name='John',
members="marie,bob,joe"
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"hostgroup_name": "John",
"members": "marie,bob,joe"
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
'http://localhost:5311/v2/config/hostgroups/novell-servers',
body='{"hostgroup_name": "novell-servers",'
'"members": "netware1,netware2,netware3,netware4"}'
)
hostgroup = self.client.config.hostgroups.get(
hostgroup_name='novell-servers'
)
self.assertEqual(
hostgroup,
{
'hostgroup_name': 'novell-servers',
'members': 'netware1,netware2,netware3,netware4'
}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
'http://localhost:5311/v2/config/hostgroups/novell-servers',
body='{"test": "test"}'
)
with requests_mock.mock() as m:
m.put('http://localhost:5311/v2/config/hostgroups/novell-servers')
self.client.config.hostgroups.update(
hostgroup_name="novell-servers",
hostgroup={"members": "updated"}
)
self.client.config.hostgroups.update(
hostgroup_name="novell-servers",
hostgroup={"members": "updated"}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"members": u"updated"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"members": u"updated"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/hostgroups/novell-servers",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/"
"config/hostgroups/novell-servers",
text="body")
body = self.client.config.hostgroups.delete(
hostgroup_name="novell-servers",
)
body = self.client.config.hostgroups.delete(
hostgroup_name="novell-servers",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -14,120 +14,106 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestHosts(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/hosts",
body='[{"host_name": "host1"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/hosts",
text='[{"host_name": "host1"}]')
hosts = self.client.config.hosts.list()
hosts = self.client.config.hosts.list()
self.assertEqual(
hosts,
[{u"host_name": u"host1"}]
)
self.assertEqual(
hosts,
[{u"host_name": u"host1"}]
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"filters": '{"isnot": {"register": ["0"]}}'
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"filters": '{"isnot": {"register": ["0"]}}'
}
)
@httpretty.activate
def test_list_templates(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/hosts",
body='[]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/hosts",
text='[]')
self.client.config.hosts.list(templates=True)
self.assertEqual(
httpretty.last_request().path,
'/v2/config/hosts?'
)
self.client.config.hosts.list(templates=True)
self.assertEqual(
m.last_request.path,
'/v2/config/hosts'
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/hosts",
body='{"host_name": "new_host", "address": "192.168.2.1"}'
)
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/hosts",
text='{"host_name": "new_host", "address": "192.168.2.1"}')
self.client.config.hosts.create(
host_name="new_host",
address="192.168.2.1"
)
self.client.config.hosts.create(
host_name="new_host",
address="192.168.2.1"
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"host_name": "new_host",
"address": "192.168.2.1"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"host_name": "new_host",
"address": "192.168.2.1"
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
"http://localhost:5311/v2/config/hosts/host_name_to_show",
body='{"host_name": "host_name_to_show"}'
)
with requests_mock.mock() as m:
m.get("http://localhost:5311/v2/config/hosts/host_name_to_show",
text='{"host_name": "host_name_to_show"}')
host = self.client.config.hosts.get(
host_name="host_name_to_show"
)
host = self.client.config.hosts.get(
host_name="host_name_to_show"
)
self.assertEqual(
host,
{"host_name": "host_name_to_show"}
)
self.assertEqual(
host,
{"host_name": "host_name_to_show"}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
"http://localhost:5311/v2/config/hosts/host_name_to_update",
body='{"test": "test"}'
)
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/hosts/host_name_to_update",
text='{"test": "test"}')
self.client.config.hosts.update(
host_name="host_name_to_update",
host={'address': "192.168.0.1",
'check_period': "24x7"
}
)
self.client.config.hosts.update(
host_name="host_name_to_update",
host={'address': "192.168.0.1",
'check_period': "24x7"
}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"check_period": u"24x7",
"address": u"192.168.0.1"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"check_period": u"24x7",
"address": u"192.168.0.1"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/hosts/host_name_to_delete",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/"
"config/hosts/host_name_to_delete",
text="body")
body = self.client.config.hosts.delete(
host_name="host_name_to_delete",
)
body = self.client.config.hosts.delete(
host_name="host_name_to_delete",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -14,121 +14,110 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestMacroModulations(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/macromodulations",
body='[{"macromodulation_name": "HighDuringNight",'
'"modulation_period": "night",'
'"_CRITICAL": 20,'
'"_WARNING": 10},'
'{"macromodulation_name": "LowDuringNight",'
'"modulation_period": "night",'
'"_CRITICAL": 10,'
'"_WARNING": 20}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/macromodulations",
text='[{"macromodulation_name": "HighDuringNight",'
'"modulation_period": "night",'
'"_CRITICAL": 20,'
'"_WARNING": 10},'
'{"macromodulation_name": "LowDuringNight",'
'"modulation_period": "night",'
'"_CRITICAL": 10,'
'"_WARNING": 20}]')
contacts = self.client.config.macromodulations.list()
contacts = self.client.config.macromodulations.list()
self.assertEqual(
contacts,
[
self.assertEqual(
contacts,
[
{
'macromodulation_name': 'HighDuringNight',
'modulation_period': 'night',
'_CRITICAL': 20,
'_WARNING': 10,
},
{
'macromodulation_name': 'LowDuringNight',
'modulation_period': 'night',
'_CRITICAL': 10,
'_WARNING': 20,
}
]
)
def test_create(self):
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/macromodulations",
text='{"macromodulation_name": "TEST_CREATE_MODULATION",'
'"modulation_period": "night"}')
self.client.config.macromodulations.create(
macromodulation_name='TEST_CREATE_MODULATION',
modulation_period='night'
)
self.assertEqual(
json.loads(m.last_request.body),
{
"macromodulation_name": "TEST_CREATE_MODULATION",
"modulation_period": "night"
}
)
def test_get(self):
with requests_mock.mock() as m:
m.get('http://localhost:5311/v2/config/'
'macromodulations/HighDuringNight',
text='{"macromodulation_name": "HighDuringNight",'
'"modulation_period": "night"}')
macromodulation = self.client.config.macromodulations.get(
macromodulation_name='HighDuringNight'
)
self.assertEqual(
macromodulation,
{
'macromodulation_name': 'HighDuringNight',
'modulation_period': 'night',
'_CRITICAL': 20,
'_WARNING': 10,
},
{
'macromodulation_name': 'LowDuringNight',
'modulation_period': 'night',
'_CRITICAL': 10,
'_WARNING': 20,
'modulation_period': 'night'
}
]
)
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/macromodulations",
body='{"macromodulation_name": "TEST_CREATE_MODULATION",'
'"modulation_period": "night"}'
)
self.client.config.macromodulations.create(
macromodulation_name='TEST_CREATE_MODULATION',
modulation_period='night'
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"macromodulation_name": "TEST_CREATE_MODULATION",
"modulation_period": "night"
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
'http://localhost:5311/v2/config/macromodulations/HighDuringNight',
body='{"macromodulation_name": "HighDuringNight",'
'"modulation_period": "night"}'
)
macromodulation = self.client.config.macromodulations.get(
macromodulation_name='HighDuringNight'
)
self.assertEqual(
macromodulation,
{
'macromodulation_name': 'HighDuringNight',
'modulation_period': 'night'
}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
'http://localhost:5311/v2/config/macromodulations/HighDuringNight',
body='{"test": "test"}'
)
with requests_mock.mock() as m:
m.put('http://localhost:5311/v2/config/'
'macromodulations/HighDuringNight',
text='{"test": "test"}')
self.client.config.macromodulations.update(
macromodulation_name="HighDuringNight",
macromodulation={"modulation_period": "updated"}
)
self.client.config.macromodulations.update(
macromodulation_name="HighDuringNight",
macromodulation={"modulation_period": "updated"}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"modulation_period": u"updated"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"modulation_period": u"updated"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/macromodulations/test",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/macromodulations/test",
text="body")
body = self.client.config.macromodulations.delete(
macromodulation_name="test",
)
body = self.client.config.macromodulations.delete(
macromodulation_name="test",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -14,52 +14,123 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestNotificationWays(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/notificationways",
body='[{'
'"notificationway_name": "email_in_day",'
'"host_notification_period": "24x7",'
'"service_notification_period": "24x7",'
'"host_notification_options": "d,u",'
'"service_notification_options": "w,c,r",'
'"host_notification_commands": "notify-service",'
'"service_notification_commands": "notify-host"'
'},'
'{'
'"notificationway_name": "email_all_time",'
'"host_notification_period": "24x7",'
'"service_notification_period": "24x7",'
'"host_notification_options": "d,r,f,u",'
'"service_notification_options": "w,f,c,r",'
'"host_notification_commands": "notify-service",'
'"service_notification_commands": "notify-host",'
'"min_business_impact": 5'
'}'
']'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/notificationways",
text='[{'
'"notificationway_name": "email_in_day",'
'"host_notification_period": "24x7",'
'"service_notification_period": "24x7",'
'"host_notification_options": "d,u",'
'"service_notification_options": "w,c,r",'
'"host_notification_commands": "notify-service",'
'"service_notification_commands": "notify-host"'
'},'
'{'
'"notificationway_name": "email_all_time",'
'"host_notification_period": "24x7",'
'"service_notification_period": "24x7",'
'"host_notification_options": "d,r,f,u",'
'"service_notification_options": "w,f,c,r",'
'"host_notification_commands": "notify-service",'
'"service_notification_commands": "notify-host",'
'"min_business_impact": 5'
'}'
']')
notificationways = self.client.config.notificationways.list()
notificationways = self.client.config.notificationways.list()
self.assertEqual(
notificationways,
[
self.assertEqual(
notificationways,
[
{
'notificationway_name': 'email_in_day',
'host_notification_period': '24x7',
'service_notification_period': '24x7',
'host_notification_options': 'd,u',
'service_notification_options': 'w,c,r',
'host_notification_commands': 'notify-service',
'service_notification_commands': 'notify-host'
},
{
'notificationway_name': 'email_all_time',
'host_notification_period': '24x7',
'service_notification_period': '24x7',
'host_notification_options': 'd,r,f,u',
'service_notification_options': 'w,f,c,r',
'host_notification_commands': 'notify-service',
'service_notification_commands': 'notify-host',
'min_business_impact': 5
}
]
)
def test_create(self):
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/notificationways",
text='{'
'"notificationway_name": "email_in_day",'
'"host_notification_period": "24x7",'
'"service_notification_period": "24x7",'
'"host_notification_options": "d,u",'
'"service_notification_options": "w,c,r",'
'"host_notification_commands": "notify-service",'
'"service_notification_commands": "notify-host"'
'}')
self.client.config.notificationways.create(
notificationway_name='test_create_notification',
host_notification_period='24x7',
service_notification_period='24x7',
host_notification_options='d,r,f,u',
service_notification_options='w,f,c,r',
host_notification_commands='notify-service',
service_notification_commands='notify-host',
min_business_impact=5
)
self.assertEqual(
json.loads(m.last_request.body),
{
'notificationway_name': 'email_in_day',
'notificationway_name': 'test_create_notification',
'host_notification_period': '24x7',
'service_notification_period': '24x7',
'host_notification_options': 'd,u',
'service_notification_options': 'w,c,r',
'host_notification_options': 'd,r,f,u',
'service_notification_options': 'w,f,c,r',
'host_notification_commands': 'notify-service',
'service_notification_commands': 'notify-host'
},
'service_notification_commands': 'notify-host',
'min_business_impact': 5
}
)
def test_get(self):
with requests_mock.mock() as m:
m.get('http://localhost:5311/v2/config/'
'notificationways/email_all_time',
text='{'
'"notificationway_name": "email_all_time",'
'"host_notification_period": "24x7",'
'"service_notification_period": "24x7",'
'"host_notification_options": "d,r,f,u",'
'"service_notification_options": "w,f,c,r",'
'"host_notification_commands": "notify-service",'
'"service_notification_commands": "notify-host",'
'"min_business_impact": 5'
'}')
notificationway = self.client.config.notificationways.get(
notificationway_name='email_all_time'
)
self.assertEqual(
notificationway,
{
'notificationway_name': 'email_all_time',
'host_notification_period': '24x7',
@ -70,118 +141,36 @@ class TestNotificationWays(clienttest.ClientTest):
'service_notification_commands': 'notify-host',
'min_business_impact': 5
}
]
)
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/notificationways",
body='{'
'"notificationway_name": "email_in_day",'
'"host_notification_period": "24x7",'
'"service_notification_period": "24x7",'
'"host_notification_options": "d,u",'
'"service_notification_options": "w,c,r",'
'"host_notification_commands": "notify-service",'
'"service_notification_commands": "notify-host"'
'}'
)
self.client.config.notificationways.create(
notificationway_name='test_create_notification',
host_notification_period='24x7',
service_notification_period='24x7',
host_notification_options='d,r,f,u',
service_notification_options='w,f,c,r',
host_notification_commands='notify-service',
service_notification_commands='notify-host',
min_business_impact=5
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
'notificationway_name': 'test_create_notification',
'host_notification_period': '24x7',
'service_notification_period': '24x7',
'host_notification_options': 'd,r,f,u',
'service_notification_options': 'w,f,c,r',
'host_notification_commands': 'notify-service',
'service_notification_commands': 'notify-host',
'min_business_impact': 5
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
'http://localhost:5311/v2/config/notificationways/email_all_time',
body='{'
'"notificationway_name": "email_all_time",'
'"host_notification_period": "24x7",'
'"service_notification_period": "24x7",'
'"host_notification_options": "d,r,f,u",'
'"service_notification_options": "w,f,c,r",'
'"host_notification_commands": "notify-service",'
'"service_notification_commands": "notify-host",'
'"min_business_impact": 5'
'}'
)
notificationway = self.client.config.notificationways.get(
notificationway_name='email_all_time'
)
self.assertEqual(
notificationway,
{
'notificationway_name': 'email_all_time',
'host_notification_period': '24x7',
'service_notification_period': '24x7',
'host_notification_options': 'd,r,f,u',
'service_notification_options': 'w,f,c,r',
'host_notification_commands': 'notify-service',
'service_notification_commands': 'notify-host',
'min_business_impact': 5
}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
'http://localhost:5311/v2/config/notificationways/email_all_time',
body='{"test": "test"}'
)
with requests_mock.mock() as m:
m.put('http://localhost:5311/v2/'
'config/notificationways/email_all_time',
text='{"test": "test"}')
self.client.config.notificationways.update(
notificationway_name="email_all_time",
notificationway={"host_notification_period": "updated"}
)
self.client.config.notificationways.update(
notificationway_name="email_all_time",
notificationway={"host_notification_period": "updated"}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"host_notification_period": u"updated"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"host_notification_period": u"updated"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/notificationways/bob",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/notificationways/bob",
text="body")
body = self.client.config.notificationways.delete(
notificationway_name="bob",
)
body = self.client.config.notificationways.delete(
notificationway_name="bob",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -14,131 +14,118 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestRealms(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/realms",
body='['
'{'
'"realm_name": "World",'
'"realm_members": "Europe,America,Asia",'
'"default": 0'
'},'
'{'
'"realm_name": "Anti-world",'
'"realm_members": "void,black-hole",'
'"default": 1'
'}'
']'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/realms",
text='['
'{'
'"realm_name": "World",'
'"realm_members": "Europe,America,Asia",'
'"default": 0'
'},'
'{'
'"realm_name": "Anti-world",'
'"realm_members": "void,black-hole",'
'"default": 1'
'}'
']')
realms = self.client.config.realms.list()
realms = self.client.config.realms.list()
self.assertEqual(
realms,
[
self.assertEqual(
realms,
[
{
'realm_name': 'World',
'realm_members': 'Europe,America,Asia',
'default': 0
},
{
'realm_name': 'Anti-world',
'realm_members': 'void,black-hole',
'default': 1
},
]
)
def test_create(self):
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/realms",
text='{"realm_name": "John",'
'"realm_members":"marie,bob,joe",'
'"default":1}')
self.client.config.realms.create(
realm_name='John',
realm_members="marie,bob,joe",
default=1
)
self.assertEqual(
json.loads(m.last_request.body),
{
"realm_name": "John",
"realm_members": "marie,bob,joe",
"default": 1
}
)
def test_get(self):
with requests_mock.mock() as m:
m.get('http://localhost:5311/v2/config/realms/bobby',
text='{'
'"realm_name": "World",'
'"realm_members": "Europe,America,Asia",'
'"default": 0'
'}')
realm = self.client.config.realms.get(
realm_name='bobby'
)
self.assertEqual(
realm,
{
'realm_name': 'World',
'realm_members': 'Europe,America,Asia',
'default': 0
},
{
'realm_name': 'Anti-world',
'realm_members': 'void,black-hole',
'default': 1
},
]
}
)
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/realms",
body='{"realm_name": "John",'
'"realm_members":"marie,bob,joe",'
'"default":1}'
)
self.client.config.realms.create(
realm_name='John',
realm_members="marie,bob,joe",
default=1
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"realm_name": "John",
"realm_members": "marie,bob,joe",
"default": 1
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
'http://localhost:5311/v2/config/realms/bobby',
body='{'
'"realm_name": "World",'
'"realm_members": "Europe,America,Asia",'
'"default": 0'
'}'
)
realm = self.client.config.realms.get(
realm_name='bobby'
)
self.assertEqual(
realm,
{
'realm_name': 'World',
'realm_members': 'Europe,America,Asia',
'default': 0
}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
'http://localhost:5311/v2/config/realms/World',
body='{"test": "test"}'
)
with requests_mock.mock() as m:
m.put('http://localhost:5311/v2/config/realms/World',
text='{"test": "test"}')
self.client.config.realms.update(
realm_name="World",
realm={"realm_members": "updated"}
)
self.client.config.realms.update(
realm_name="World",
realm={"realm_members": "updated"}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"realm_members": u"updated"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"realm_members": u"updated"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/realms/bob",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/realms/bob",
text="body")
body = self.client.config.realms.delete(
realm_name="bob",
)
body = self.client.config.realms.delete(
realm_name="bob",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -14,116 +14,105 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestServiceGroups(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/servicegroups",
body='[{"servicegroup_name": "dbservices",'
'"members": "ms1,SQL Server,ms1,'
'SQL Serverc Agent,ms1,SQL DTC"},'
'{"servicegroup_name": "otherservices",'
'"members": "some,other,member"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/servicegroups",
text='[{"servicegroup_name": "dbservices",'
'"members": "ms1,SQL Server,ms1,'
'SQL Serverc Agent,ms1,SQL DTC"},'
'{"servicegroup_name": "otherservices",'
'"members": "some,other,member"}]')
servicegroups = self.client.config.servicegroups.list()
servicegroups = self.client.config.servicegroups.list()
self.assertEqual(
servicegroups,
[
self.assertEqual(
servicegroups,
[
{
'servicegroup_name': 'dbservices',
'members': 'ms1,SQL Server,ms1,'
'SQL Serverc Agent,ms1,SQL DTC',
},
{
'servicegroup_name': 'otherservices',
'members': 'some,other,member',
},
]
)
def test_create(self):
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/servicegroups",
text='{"servicegroup_name": "John",'
'"members": "marie,bob,joe"}')
self.client.config.servicegroups.create(
servicegroup_name='John',
members="marie,bob,joe"
)
self.assertEqual(
json.loads(m.last_request.body),
{
"servicegroup_name": "John",
"members": "marie,bob,joe"
}
)
def test_get(self):
with requests_mock.mock() as m:
m.get('http://localhost:5311/v2/config/servicegroups/dbservices',
text='{"servicegroup_name": "dbservices",'
'"members": "ms1,SQL Server,ms1,'
'SQL Serverc Agent,ms1,SQL DTC"}')
servicegroup = self.client.config.servicegroups.get(
servicegroup_name='dbservices'
)
self.assertEqual(
servicegroup,
{
'servicegroup_name': 'dbservices',
'members': 'ms1,SQL Server,ms1,'
'SQL Serverc Agent,ms1,SQL DTC',
},
{
'servicegroup_name': 'otherservices',
'members': 'some,other,member',
},
]
)
'SQL Serverc Agent,ms1,SQL DTC'
}
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/servicegroups",
body='{"servicegroup_name": "John",'
'"members": "marie,bob,joe"}'
)
self.client.config.servicegroups.create(
servicegroup_name='John',
members="marie,bob,joe"
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"servicegroup_name": "John",
"members": "marie,bob,joe"
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
'http://localhost:5311/v2/config/servicegroups/dbservices',
body='{"servicegroup_name": "dbservices",'
'"members": "ms1,SQL Server,ms1,'
'SQL Serverc Agent,ms1,SQL DTC"}'
)
servicegroup = self.client.config.servicegroups.get(
servicegroup_name='dbservices'
)
self.assertEqual(
servicegroup,
{
'servicegroup_name': 'dbservices',
'members': 'ms1,SQL Server,ms1,SQL Serverc Agent,ms1,SQL DTC'
}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
'http://localhost:5311/v2/config/servicegroups/dbservices',
body='{"test": "test"}'
)
with requests_mock.mock() as m:
m.put('http://localhost:5311/v2/config/servicegroups/dbservices',
text='{"test": "test"}')
self.client.config.servicegroups.update(
servicegroup_name="dbservices",
servicegroup={"members": "updated"}
)
self.client.config.servicegroups.update(
servicegroup_name="dbservices",
servicegroup={"members": "updated"}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"members": u"updated"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"members": u"updated"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/servicegroups/dbservices",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/"
"servicegroups/dbservices",
text="body")
body = self.client.config.servicegroups.delete(
servicegroup_name="dbservices",
)
body = self.client.config.servicegroups.delete(
servicegroup_name="dbservices",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -14,96 +14,84 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestServices(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/services",
body='[{"service_name": "service1"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/services",
text='[{"service_name": "service1"}]')
services = self.client.config.services.list()
services = self.client.config.services.list()
self.assertEqual(
services,
[{"service_name": "service1"}]
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"filters": '{"isnot": {"register": ["0"]}}'
}
)
self.assertEqual(
services,
[{"service_name": "service1"}]
)
self.assertEqual(
json.loads(m.last_request.body),
{
"filters": '{"isnot": {"register": ["0"]}}'
}
)
@httpretty.activate
def test_list_templates(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/services",
body='[{"service_name": "service1"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/services",
text='[{"service_name": "service1"}]')
self.client.config.services.list(templates=True)
self.assertEqual(
httpretty.last_request().path,
'/v2/config/services?'
)
self.client.config.services.list(templates=True)
self.assertEqual(
m.last_request.path,
'/v2/config/services'
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/services",
body='{"service_name": "new_service"}'
)
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/services",
text='{"service_name": "new_service"}')
self.client.config.services.create(
service_name="new_service"
)
self.client.config.services.create(
service_name="new_service"
)
self.assertEqual(
httpretty.last_request().body.decode(),
'{"service_name": "new_service"}'
)
self.assertEqual(
m.last_request.body,
'{"service_name": "new_service"}'
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/hosts/host_name/" +
"services/service_description",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/hosts/host_name/" +
"services/service_description",
text="body")
body = self.client.config.services.delete(
host_name="host_name",
service_description="service_description"
)
body = self.client.config.services.delete(
host_name="host_name",
service_description="service_description"
)
self.assertEqual(
body,
"body"
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
"http://localhost:5311/v2/config/hosts/host_name/" +
"services/service_description",
body="{}"
)
with requests_mock.mock() as m:
m.get("http://localhost:5311/v2/config/hosts/host_name/" +
"services/service_description",
text="{}")
body = self.client.config.services.get(
host_name="host_name",
service_description="service_description"
)
body = self.client.config.services.get(
host_name="host_name",
service_description="service_description"
)
self.assertEqual(
body,
{}
)
self.assertEqual(
body,
{}
)

View File

@ -14,126 +14,112 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestTimePeriods(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/config/timeperiods",
body='['
'{'
'"timeperiod_name": "nonworkhours",'
'"sunday": "00:00-24:00",'
'"monday": "00:00-09:00,17:00-24:00"'
'},'
'{'
'"timeperiod_name": "misc-single-days",'
'"1999-01-28": "00:00-24:00",'
'"day 2": "00:00-24:00"'
'}'
']'
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/config/timeperiods",
text='['
'{'
'"timeperiod_name": "nonworkhours",'
'"sunday": "00:00-24:00",'
'"monday": "00:00-09:00,17:00-24:00"'
'},'
'{'
'"timeperiod_name": "misc-single-days",'
'"1999-01-28": "00:00-24:00",'
'"day 2": "00:00-24:00"'
'}'
']')
)
timeperiods = self.client.config.timeperiods.list()
timeperiods = self.client.config.timeperiods.list()
self.assertEqual(
timeperiods,
[
{
'timeperiod_name': 'nonworkhours',
'sunday': '00:00-24:00',
'monday': '00:00-09:00,17:00-24:00'
},
{
'timeperiod_name': 'misc-single-days',
'1999-01-28': '00:00-24:00',
'day 2': '00:00-24:00',
},
]
self.assertEqual(
timeperiods,
[
)
def test_create(self):
with requests_mock.mock() as m:
m.put("http://localhost:5311/v2/config/timeperiods",
text='{"timeperiod_name": "John"}')
self.client.config.timeperiods.create(
timeperiod_name='new_periods'
)
self.assertEqual(
json.loads(m.last_request.body),
{
"timeperiod_name": "new_periods"
}
)
def test_get(self):
with requests_mock.mock() as m:
m.get('http://localhost:5311/v2/config/timeperiods/nonworkhours',
text='{'
'"timeperiod_name": "nonworkhours",'
'"sunday": "00:00-24:00",'
'"monday": "00:00-09:00,17:00-24:00"'
'}')
timeperiod = self.client.config.timeperiods.get(
timeperiod_name='nonworkhours'
)
self.assertEqual(
timeperiod,
{
'timeperiod_name': 'nonworkhours',
'sunday': '00:00-24:00',
'monday': '00:00-09:00,17:00-24:00'
},
{
'timeperiod_name': 'misc-single-days',
'1999-01-28': '00:00-24:00',
'day 2': '00:00-24:00',
},
]
}
)
)
@httpretty.activate
def test_create(self):
httpretty.register_uri(
httpretty.PUT, "http://localhost:5311/v2/config/timeperiods",
body='{"timeperiod_name": "John"}'
)
self.client.config.timeperiods.create(
timeperiod_name='new_periods'
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"timeperiod_name": "new_periods"
}
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET,
'http://localhost:5311/v2/config/timeperiods/nonworkhours',
body='{'
'"timeperiod_name": "nonworkhours",'
'"sunday": "00:00-24:00",'
'"monday": "00:00-09:00,17:00-24:00"'
'}'
)
timeperiod = self.client.config.timeperiods.get(
timeperiod_name='nonworkhours'
)
self.assertEqual(
timeperiod,
{
'timeperiod_name': 'nonworkhours',
'sunday': '00:00-24:00',
'monday': '00:00-09:00,17:00-24:00'
}
)
@httpretty.activate
def test_update(self):
httpretty.register_uri(
httpretty.PUT,
'http://localhost:5311/v2/config/timeperiods/nonworkhours',
body='{"test": "test"}'
)
with requests_mock.mock() as m:
m.put('http://localhost:5311/v2/config/timeperiods/nonworkhours',
text='{"test": "test"}')
self.client.config.timeperiods.update(
timeperiod_name="nonworkhours",
timeperiod={"timeperiod_name": "updated"}
)
self.client.config.timeperiods.update(
timeperiod_name="nonworkhours",
timeperiod={"timeperiod_name": "updated"}
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{
"timeperiod_name": u"updated"
}
)
self.assertEqual(
json.loads(m.last_request.body),
{
"timeperiod_name": u"updated"
}
)
@httpretty.activate
def test_delete(self):
httpretty.register_uri(
httpretty.DELETE,
"http://localhost:5311/v2/config/timeperiods/bob",
body="body"
)
with requests_mock.mock() as m:
m.delete("http://localhost:5311/v2/config/timeperiods/bob",
text="body")
body = self.client.config.timeperiods.delete(
timeperiod_name="bob",
)
body = self.client.config.timeperiods.delete(
timeperiod_name="bob",
)
self.assertEqual(
body,
"body"
)
self.assertEqual(
body,
"body"
)

View File

@ -12,28 +12,28 @@
# License for the specific language governing permissions and limitations
# under the License.
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestEvents(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/status/events",
body='[{"host_name": "sfl.com", "service_description": "cpu", '
'"event_type": "ALERT", "output": "Ok"},'
'{"host_name": "sfl.com", "service_description": "cpu", '
'"event_type": "ALERT", "output": "Not Ok"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/status/events",
text='[{"host_name": "sfl.com", '
'"service_description": "cpu", '
'"event_type": "ALERT", "output": "Ok"},'
'{"host_name": "sfl.com", '
'"service_description": "cpu", '
'"event_type": "ALERT", "output": "Not Ok"}]')
events = self.client.status.events.list()
self.assertEqual(
events,
[{"host_name": "sfl.com", "service_description": "cpu",
"event_type": "ALERT", "output": "Ok"},
{"host_name": "sfl.com", "service_description": "cpu",
"event_type": "ALERT", "output": "Not Ok"}]
)
events = self.client.status.events.list()
self.assertEqual(
events,
[{"host_name": "sfl.com", "service_description": "cpu",
"event_type": "ALERT", "output": "Ok"},
{"host_name": "sfl.com", "service_description": "cpu",
"event_type": "ALERT", "output": "Not Ok"}]
)

View File

@ -14,54 +14,48 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestHosts(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/status/hosts",
body='[{"host_name": "host1"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/status/hosts",
text='[{"host_name": "host1"}]')
hosts = self.client.status.hosts.list()
hosts = self.client.status.hosts.list()
self.assertEqual(
hosts,
[{"host_name": "host1"}]
)
self.assertEqual(
hosts,
[{"host_name": "host1"}]
)
@httpretty.activate
def test_get(self):
httpretty.register_uri(
httpretty.GET, "http://localhost:5311/v2/status/hosts/hostname",
body='{"host_name": "host1"}'
)
with requests_mock.mock() as m:
m.get("http://localhost:5311/v2/status/hosts/hostname",
text='{"host_name": "host1"}')
host = self.client.status.hosts.get("hostname")
host = self.client.status.hosts.get("hostname")
self.assertEqual(
host,
{"host_name": "host1"}
)
self.assertEqual(
host,
{"host_name": "host1"}
)
@httpretty.activate
def test_submit_host_check_result(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/status/hosts/localhost"
"/results",
body=''
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/status/hosts/localhost"
"/results",
text='')
self.client.status.hosts.submit_check_result(
"localhost", output="someoutput"
)
self.client.status.hosts.submit_check_result(
"localhost", output="someoutput"
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{"output": u"someoutput"}
)
self.assertEqual(
json.loads(m.last_request.body),
{"output": u"someoutput"}
)

View File

@ -12,101 +12,99 @@
# License for the specific language governing permissions and limitations
# under the License.
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestMetrics(clienttest.ClientTest):
@httpretty.activate
def test_list_host_metrics(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/status/"
"hosts/localhost/metrics/load1",
body='[{"min": "2", "warning": "15", "value": "3"},'
'{"min": "5", "warning": "200", "value": "150"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/status/"
"hosts/localhost/metrics/load1",
text='[{"min": "2", "warning": "15", "value": "3"},'
'{"min": "5", "warning": "200", "value": "150"}]'
)
metrics = self.client.status.hosts.metrics.list(
'localhost',
'load1'
)
metrics = self.client.status.hosts.metrics.list(
'localhost',
'load1'
)
self.assertEqual(
metrics,
[{"min": "2", "warning": "15", "value": "3"},
{"min": "5", "warning": "200", "value": "150"}]
)
self.assertEqual(
metrics,
[{"min": "2", "warning": "15", "value": "3"},
{"min": "5", "warning": "200", "value": "150"}]
)
@httpretty.activate
def test_list_host_metrics_service(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/status/hosts/localhost"
"/services/load/metrics/load1",
body='[{"min": "2", "warning": "15", "value": "3"},'
'{"min": "5", "warning": "200", "value": "150"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/status/hosts/localhost"
"/services/load/metrics/load1",
text='[{"min": "2", "warning": "15", "value": "3"},'
'{"min": "5", "warning": "200", "value": "150"}]'
)
live_query = ('{"time_interval": { "start_time": '
'"2015-05-22T13:38:08Z",'
'"end_time": "2015-05-22T13:38:08Z"}}')
live_query = ('{"time_interval": { "start_time": '
'"2015-05-22T13:38:08Z",'
'"end_time": "2015-05-22T13:38:08Z"}}')
metrics = self.client.status.hosts.metrics.list('localhost', 'load1',
'load',
query=live_query
)
metrics = self.client.status.hosts.metrics.list('localhost',
'load1',
'load',
query=live_query
)
self.assertEqual(
metrics,
[{"min": "2", "warning": "15", "value": "3"},
{"min": "5", "warning": "200", "value": "150"}]
)
self.assertEqual(
metrics,
[{"min": "2", "warning": "15", "value": "3"},
{"min": "5", "warning": "200", "value": "150"}]
)
@httpretty.activate
def test_show_host_metrics(self):
httpretty.register_uri(
httpretty.GET, "http://localhost:5311/v2/status/hosts/localhost"
"/metrics/load1",
body='{"min": "2", "warning": "15", "value": "3"}'
)
with requests_mock.mock() as m:
m.get("http://localhost:5311/v2/status/hosts/localhost"
"/metrics/load1",
text='{"min": "2", "warning": "15", "value": "3"}'
)
metrics = self.client.status.hosts.metrics.get('localhost', 'load1')
metrics = self.client.status.hosts.metrics.get('localhost',
'load1')
self.assertEqual(
metrics,
{"min": "2", "warning": "15", "value": "3"}
)
self.assertEqual(
metrics,
{"min": "2", "warning": "15", "value": "3"}
)
@httpretty.activate
def test_show_host_service_metrics(self):
httpretty.register_uri(
httpretty.GET, "http://localhost:5311/v2/status/hosts/localhost"
"/services/load/metrics/load1",
body='{"value": "3"}'
)
with requests_mock.mock() as m:
m.get("http://localhost:5311/v2/status/hosts/localhost"
"/services/load/metrics/load1",
text='{"value": "3"}'
)
metrics = self.client.status.hosts.metrics.get('localhost', 'load1',
'load')
metrics = self.client.status.hosts.metrics.get('localhost',
'load1',
'load')
self.assertEqual(
metrics,
{"value": "3"}
)
self.assertEqual(
metrics,
{"value": "3"}
)
@httpretty.activate
def test_show_host_service(self):
httpretty.register_uri(
httpretty.GET, "http://localhost:5311/v2/status/hosts/localhost"
"/services/load/metrics",
body='[{"metric_name": "load1"},{"metric_name": "load5"}]'
)
with requests_mock.mock() as m:
m.get("http://localhost:5311/v2/status/hosts/localhost"
"/services/load/metrics",
text='[{"metric_name": "load1"},{"metric_name": "load5"}]'
)
metrics = self.client.status.hosts.metrics.get(
'localhost',
service_description='load')
metrics = self.client.status.hosts.metrics.get(
'localhost',
service_description='load')
self.assertEqual(
metrics,
[{"metric_name": "load1"}, {"metric_name": "load5"}]
)
self.assertEqual(
metrics,
[{"metric_name": "load1"}, {"metric_name": "load5"}]
)

View File

@ -14,43 +14,38 @@
import json
import httpretty
import requests_mock
from surveilclient.tests.v2_0 import clienttest
class TestServices(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:5311/v2/status/services",
body='[{"service_name": "service1"}]'
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/status/services",
text='[{"service_name": "service1"}]')
services = self.client.status.services.list()
services = self.client.status.services.list()
self.assertEqual(
services,
[{"service_name": "service1"}]
)
self.assertEqual(
services,
[{"service_name": "service1"}]
)
@httpretty.activate
def test_submit_service_check_result(self):
httpretty.register_uri(
httpretty.POST,
"http://localhost:5311/v2/status/hosts/localhost"
"/services/testservice/results",
body=''
)
with requests_mock.mock() as m:
m.post("http://localhost:5311/v2/status/hosts/localhost/"
+ "services/testservice/results",
text='')
self.client.status.services.submit_check_result(
"localhost",
'testservice',
output="someoutputt"
)
self.client.status.services.submit_check_result(
"localhost",
'testservice',
output="someoutput"
)
self.assertEqual(
json.loads(httpretty.last_request().body.decode()),
{"output": u"someoutputt"}
)
self.assertEqual(
json.loads(m.last_request.body),
{"output": u"someoutput"}
)

View File

@ -33,6 +33,6 @@ class Client(object):
resp, body = self.http_client.json_request(
'/reload_config',
'POST',
body='' # Must send empty body
data='' # Must send empty body
)
return body

View File

@ -29,6 +29,6 @@ class HostsManager(surveil_manager.SurveilManager):
"""Create a new host."""
resp, body = self.http_client.json_request(
HostsManager.base_url, 'POST',
body=kwargs
data=kwargs
)
return body

View File

@ -29,6 +29,6 @@ class ServicesManager(surveil_manager.SurveilManager):
"""Create a new host."""
resp, body = self.http_client.json_request(
ServicesManager.base_url, 'POST',
body=kwargs
data=kwargs
)
return body

View File

@ -22,6 +22,6 @@ class AcknowledgeManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
self.base_url,
'POST',
body=kwargs,
data=kwargs,
)
return body

View File

@ -22,6 +22,6 @@ class DowntimeManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
self.base_url,
'POST',
body=kwargs,
data=kwargs,
)
return body

View File

@ -22,6 +22,6 @@ class RecheckManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
self.base_url,
'POST',
body=kwargs,
data=kwargs,
)
return body

View File

@ -58,6 +58,6 @@ class ConfigManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
self.base_url + '/reload_config',
'POST',
body='' # Must send empty body
data='' # Must send empty body
)
return body

View File

@ -23,7 +23,7 @@ class BusinessImpactModulationsManager(surveil_manager.SurveilManager):
query = query or {}
resp, body = self.http_client.json_request(
BusinessImpactModulationsManager.base_url, 'POST',
body=query
data=query
)
return body
@ -31,7 +31,7 @@ class BusinessImpactModulationsManager(surveil_manager.SurveilManager):
"""Create a new businessimpactmodulation."""
resp, body = self.http_client.json_request(
BusinessImpactModulationsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -40,7 +40,7 @@ class BusinessImpactModulationsManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
BusinessImpactModulationsManager.base_url + '/' +
businessimpactmodulation_name, 'GET',
body=''
data=''
)
return body
@ -49,16 +49,15 @@ class BusinessImpactModulationsManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
BusinessImpactModulationsManager.base_url + '/' +
businessimpactmodulation_name, 'PUT',
body=businessimpactmodulation
data=businessimpactmodulation
)
return body
def delete(self, businessimpactmodulation_name):
"""Delete a businessimpactmodulation."""
resp, body = self.http_client.request(
BusinessImpactModulationsManager.base_url+"/" +
BusinessImpactModulationsManager.base_url + "/" +
businessimpactmodulation_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -22,7 +22,7 @@ class CheckModulationsManager(surveil_manager.SurveilManager):
"""Get a list of checkmodulations."""
query = query or {}
resp, body = self.http_client.json_request(
CheckModulationsManager.base_url, 'POST', body=query
CheckModulationsManager.base_url, 'POST', data=query
)
return body
@ -30,7 +30,7 @@ class CheckModulationsManager(surveil_manager.SurveilManager):
"""Create a new checkmodulation."""
resp, body = self.http_client.json_request(
CheckModulationsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -39,7 +39,7 @@ class CheckModulationsManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
CheckModulationsManager.base_url + '/' +
checkmodulation_name, 'GET',
body=''
data=''
)
return body
@ -48,7 +48,7 @@ class CheckModulationsManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
CheckModulationsManager.base_url + '/' +
checkmodulation_name, 'PUT',
body=checkmodulation
data=checkmodulation
)
return body
@ -56,7 +56,6 @@ class CheckModulationsManager(surveil_manager.SurveilManager):
"""Delete a checkmodulation."""
resp, body = self.http_client.request(
CheckModulationsManager.base_url+"/" + checkmodulation_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -23,7 +23,7 @@ class CommandsManager(surveil_manager.SurveilManager):
query = query or {}
resp, body = self.http_client.json_request(
CommandsManager.base_url, 'POST',
body=query
data=query
)
return body
@ -31,7 +31,7 @@ class CommandsManager(surveil_manager.SurveilManager):
"""Create a new command."""
resp, body = self.http_client.json_request(
CommandsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -39,7 +39,7 @@ class CommandsManager(surveil_manager.SurveilManager):
"""Get a new command."""
resp, body = self.http_client.json_request(
CommandsManager.base_url + '/' + command_name, 'GET',
body=''
data=''
)
return body
@ -47,7 +47,7 @@ class CommandsManager(surveil_manager.SurveilManager):
"""Update a command."""
resp, body = self.http_client.json_request(
CommandsManager.base_url + '/' + command_name, 'PUT',
body=command
data=command
)
return body
@ -55,7 +55,6 @@ class CommandsManager(surveil_manager.SurveilManager):
"""Delete a command."""
resp, body = self.http_client.request(
CommandsManager.base_url + "/" + command_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -23,7 +23,7 @@ class ContactGroupsManager(surveil_manager.SurveilManager):
query = query or {}
resp, body = self.http_client.json_request(
ContactGroupsManager.base_url, 'POST',
body=query
data=query
)
return body
@ -31,7 +31,7 @@ class ContactGroupsManager(surveil_manager.SurveilManager):
"""Create a new contactgroup."""
resp, body = self.http_client.json_request(
ContactGroupsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -39,7 +39,7 @@ class ContactGroupsManager(surveil_manager.SurveilManager):
"""Get a new contactgroup."""
resp, body = self.http_client.json_request(
ContactGroupsManager.base_url + '/' + contactgroup_name, 'GET',
body=''
data=''
)
return body
@ -47,7 +47,7 @@ class ContactGroupsManager(surveil_manager.SurveilManager):
"""Update a contactgroup."""
resp, body = self.http_client.json_request(
ContactGroupsManager.base_url + '/' + contactgroup_name, 'PUT',
body=contactgroup
data=contactgroup
)
return body
@ -55,7 +55,6 @@ class ContactGroupsManager(surveil_manager.SurveilManager):
"""Delete a contactgroup."""
resp, body = self.http_client.request(
ContactGroupsManager.base_url + "/" + contactgroup_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -22,7 +22,7 @@ class ContactsManager(surveil_manager.SurveilManager):
"""Get a list of contacts."""
query = query or {}
resp, body = self.http_client.json_request(
ContactsManager.base_url, 'POST', body=query
ContactsManager.base_url, 'POST', data=query
)
return body
@ -30,7 +30,7 @@ class ContactsManager(surveil_manager.SurveilManager):
"""Create a new contact."""
resp, body = self.http_client.json_request(
ContactsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -38,7 +38,7 @@ class ContactsManager(surveil_manager.SurveilManager):
"""Get a new contact."""
resp, body = self.http_client.json_request(
ContactsManager.base_url + '/' + contact_name, 'GET',
body=''
data=''
)
return body
@ -46,7 +46,7 @@ class ContactsManager(surveil_manager.SurveilManager):
"""Update a contact."""
resp, body = self.http_client.json_request(
ContactsManager.base_url + '/' + contact_name, 'PUT',
body=contact
data=contact
)
return body
@ -54,7 +54,6 @@ class ContactsManager(surveil_manager.SurveilManager):
"""Delete a contact."""
resp, body = self.http_client.request(
ContactsManager.base_url + "/" + contact_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -23,7 +23,7 @@ class HostGroupsManager(surveil_manager.SurveilManager):
query = query or {}
resp, body = self.http_client.json_request(
HostGroupsManager.base_url, 'POST',
body=query
data=query
)
return body
@ -31,7 +31,7 @@ class HostGroupsManager(surveil_manager.SurveilManager):
"""Create a new hostgroup."""
resp, body = self.http_client.json_request(
HostGroupsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -39,7 +39,7 @@ class HostGroupsManager(surveil_manager.SurveilManager):
"""Get a new hostgroup."""
resp, body = self.http_client.json_request(
HostGroupsManager.base_url + '/' + hostgroup_name, 'GET',
body=''
data=''
)
return body
@ -47,7 +47,7 @@ class HostGroupsManager(surveil_manager.SurveilManager):
"""Update a hostgroup."""
resp, body = self.http_client.json_request(
HostGroupsManager.base_url + '/' + hostgroup_name, 'PUT',
body=hostgroup
data=hostgroup
)
return body
@ -55,7 +55,6 @@ class HostGroupsManager(surveil_manager.SurveilManager):
"""Delete a hostgroup."""
resp, body = self.http_client.request(
HostGroupsManager.base_url + "/" + hostgroup_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -36,7 +36,7 @@ class HostsManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
HostsManager.base_url, 'POST',
body=query
data=query
)
return body
@ -44,7 +44,7 @@ class HostsManager(surveil_manager.SurveilManager):
"""Create a new host."""
resp, body = self.http_client.json_request(
HostsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -52,7 +52,7 @@ class HostsManager(surveil_manager.SurveilManager):
"""Get a new host."""
resp, body = self.http_client.json_request(
HostsManager.base_url + '/' + host_name, 'GET',
body=''
data=''
)
return body
@ -60,14 +60,13 @@ class HostsManager(surveil_manager.SurveilManager):
"""Update a host."""
resp, body = self.http_client.json_request(
HostsManager.base_url + '/' + host_name, 'PUT',
body=host
data=host
)
return body
def delete(self, host_name):
"""Delete a host."""
resp, body = self.http_client.request(
HostsManager.base_url + '/' + host_name, 'DELETE',
body=''
HostsManager.base_url + '/' + host_name, 'DELETE'
)
return body

View File

@ -23,7 +23,7 @@ class MacroModulationsManager(surveil_manager.SurveilManager):
query = query or {}
resp, body = self.http_client.json_request(
MacroModulationsManager.base_url, 'POST',
body=query
data=query
)
return body
@ -31,7 +31,7 @@ class MacroModulationsManager(surveil_manager.SurveilManager):
"""Create a new macromodulation."""
resp, body = self.http_client.json_request(
MacroModulationsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -40,7 +40,7 @@ class MacroModulationsManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
MacroModulationsManager.base_url + '/' +
macromodulation_name, 'GET',
body=''
data=''
)
return body
@ -49,7 +49,7 @@ class MacroModulationsManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
MacroModulationsManager.base_url + '/' +
macromodulation_name, 'PUT',
body=macromodulation
data=macromodulation
)
return body
@ -57,7 +57,6 @@ class MacroModulationsManager(surveil_manager.SurveilManager):
"""Delete a macromodulation."""
resp, body = self.http_client.request(
MacroModulationsManager.base_url + "/" + macromodulation_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -23,7 +23,7 @@ class NotificationWaysManager(surveil_manager.SurveilManager):
query = query or {}
resp, body = self.http_client.json_request(
NotificationWaysManager.base_url, 'POST',
body=query
data=query
)
return body
@ -31,7 +31,7 @@ class NotificationWaysManager(surveil_manager.SurveilManager):
"""Create a new notificationway."""
resp, body = self.http_client.json_request(
NotificationWaysManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -39,8 +39,7 @@ class NotificationWaysManager(surveil_manager.SurveilManager):
"""Get a new notificationway."""
resp, body = self.http_client.json_request(
NotificationWaysManager.base_url + '/' +
notificationway_name, 'GET',
body=''
notificationway_name, 'GET'
)
return body
@ -49,7 +48,7 @@ class NotificationWaysManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
NotificationWaysManager.base_url + '/' +
notificationway_name, 'PUT',
body=notificationway
data=notificationway
)
return body
@ -57,7 +56,6 @@ class NotificationWaysManager(surveil_manager.SurveilManager):
"""Delete a command."""
resp, body = self.http_client.request(
NotificationWaysManager.base_url + "/" + notificationway_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -23,7 +23,7 @@ class RealmsManager(surveil_manager.SurveilManager):
query = query or {}
resp, body = self.http_client.json_request(
RealmsManager.base_url, 'POST',
body=query
data=query
)
return body
@ -31,7 +31,7 @@ class RealmsManager(surveil_manager.SurveilManager):
"""Create a new realm."""
resp, body = self.http_client.json_request(
RealmsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -39,7 +39,7 @@ class RealmsManager(surveil_manager.SurveilManager):
"""Get a new realm."""
resp, body = self.http_client.json_request(
RealmsManager.base_url + '/' + realm_name, 'GET',
body=''
data=''
)
return body
@ -47,7 +47,7 @@ class RealmsManager(surveil_manager.SurveilManager):
"""Update a realm."""
resp, body = self.http_client.json_request(
RealmsManager.base_url + '/' + realm_name, 'PUT',
body=realm
data=realm
)
return body
@ -55,7 +55,6 @@ class RealmsManager(surveil_manager.SurveilManager):
"""Delete a realm."""
resp, body = self.http_client.request(
RealmsManager.base_url + "/" + realm_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -23,7 +23,7 @@ class ServiceGroupsManager(surveil_manager.SurveilManager):
query = query or {}
resp, body = self.http_client.json_request(
ServiceGroupsManager.base_url, 'POST',
body=query
data=query
)
return body
@ -31,15 +31,14 @@ class ServiceGroupsManager(surveil_manager.SurveilManager):
"""Create a new servicegroup."""
resp, body = self.http_client.json_request(
ServiceGroupsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
def get(self, servicegroup_name):
"""Get a new servicegroup."""
resp, body = self.http_client.json_request(
ServiceGroupsManager.base_url + '/' + servicegroup_name, 'GET',
body=''
ServiceGroupsManager.base_url + '/' + servicegroup_name, 'GET'
)
return body
@ -47,7 +46,7 @@ class ServiceGroupsManager(surveil_manager.SurveilManager):
"""Update a servicegroup."""
resp, body = self.http_client.json_request(
ServiceGroupsManager.base_url + '/' + servicegroup_name, 'PUT',
body=servicegroup
data=servicegroup
)
return body
@ -55,7 +54,6 @@ class ServiceGroupsManager(surveil_manager.SurveilManager):
"""Delete a servicegroup."""
resp, body = self.http_client.request(
ServiceGroupsManager.base_url + "/" + servicegroup_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -36,7 +36,7 @@ class ServicesManager(surveil_manager.SurveilManager):
resp, body = self.http_client.json_request(
ServicesManager.base_url, 'POST',
body=query
data=query
)
return body
@ -44,7 +44,7 @@ class ServicesManager(surveil_manager.SurveilManager):
"""Create a new host."""
resp, body = self.http_client.json_request(
ServicesManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
@ -53,8 +53,7 @@ class ServicesManager(surveil_manager.SurveilManager):
resp, body = self.http_client.request(
'/config/hosts' + '/'
+ host_name + '/services/' + service_description,
'DELETE',
body=''
'DELETE'
)
return body
@ -64,6 +63,6 @@ class ServicesManager(surveil_manager.SurveilManager):
'/config/hosts/' + host_name +
'/services/' + service_description,
'GET',
body=''
data=''
)
return body

View File

@ -23,7 +23,7 @@ class TimePeriodsManager(surveil_manager.SurveilManager):
query = query or {}
resp, body = self.http_client.json_request(
TimePeriodsManager.base_url, 'POST',
body=query
data=query
)
return body
@ -31,15 +31,14 @@ class TimePeriodsManager(surveil_manager.SurveilManager):
"""Create a new timeperiod."""
resp, body = self.http_client.json_request(
TimePeriodsManager.base_url, 'PUT',
body=kwargs
data=kwargs
)
return body
def get(self, timeperiod_name):
"""Get a new timeperiod."""
resp, body = self.http_client.json_request(
TimePeriodsManager.base_url + '/' + timeperiod_name, 'GET',
body=''
TimePeriodsManager.base_url + '/' + timeperiod_name, 'GET'
)
return body
@ -47,7 +46,7 @@ class TimePeriodsManager(surveil_manager.SurveilManager):
"""Update a timeperiod."""
resp, body = self.http_client.json_request(
TimePeriodsManager.base_url + '/' + timeperiod_name, 'PUT',
body=timeperiod
data=timeperiod
)
return body
@ -55,7 +54,6 @@ class TimePeriodsManager(surveil_manager.SurveilManager):
"""Delete a timeperiod."""
resp, body = self.http_client.request(
TimePeriodsManager.base_url + "/" + timeperiod_name,
'DELETE',
body=''
'DELETE'
)
return body
return body

View File

@ -22,6 +22,6 @@ class EventsManager(surveil_manager.SurveilManager):
"""List events."""
query = query or {}
resp, body = self.http_client.json_request(
EventsManager.base_url, 'POST', body=query
EventsManager.base_url, 'POST', data=query
)
return body

View File

@ -27,7 +27,7 @@ class HostsManager(surveil_manager.SurveilManager):
"""Get a list of hosts."""
query = query or {}
resp, body = self.http_client.json_request(
HostsManager.base_url, 'POST', body=query
HostsManager.base_url, 'POST', data=query
)
return body
@ -42,6 +42,6 @@ class HostsManager(surveil_manager.SurveilManager):
"""Submit a check result."""
resp, body = self.http_client.json_request(
HostsManager.base_url + '/' + host_name + '/results', 'POST',
body=kwargs
data=kwargs
)
return body

View File

@ -34,7 +34,7 @@ class MetricsManager(surveil_manager.SurveilManager):
query = query or {}
resp, body = self.http_client.json_request(
self._create_url(host_name, service_description, metric_name),
'POST', body=query)
'POST', data=query)
return body

View File

@ -22,7 +22,7 @@ class ServicesManager(surveil_manager.SurveilManager):
"""Get a list of services."""
query = query or {}
resp, body = self.http_client.json_request(
ServicesManager.base_url, 'POST', body=query
ServicesManager.base_url, 'POST', data=query
)
return body
@ -32,6 +32,6 @@ class ServicesManager(surveil_manager.SurveilManager):
'/status/hosts/%s/services/%s/results' % (host_name,
service_description),
'POST',
body=kwargs
data=kwargs
)
return body

View File

@ -4,4 +4,4 @@ sphinx
oslosphinx
testrepository
mox3>=0.7.0
httpretty==0.8.3
requests_mock