Commit cd0fb8f8 authored by Łukasz Nowak's avatar Łukasz Nowak Committed by Łukasz Nowak

promise/plugin: Convert check_url_available to requests

It will be easier to improve it soon.
parent d5aaf706
...@@ -2,7 +2,7 @@ from zope import interface as zope_interface ...@@ -2,7 +2,7 @@ from zope import interface as zope_interface
from slapos.grid.promise import interface from slapos.grid.promise import interface
from slapos.grid.promise.generic import GenericPromise from slapos.grid.promise.generic import GenericPromise
import os import os
import pycurl import requests
class RunPromise(GenericPromise): class RunPromise(GenericPromise):
...@@ -21,33 +21,32 @@ class RunPromise(GenericPromise): ...@@ -21,33 +21,32 @@ class RunPromise(GenericPromise):
url = self.getConfig('url') url = self.getConfig('url')
timeout = int(self.getConfig('timeout', 20)) timeout = int(self.getConfig('timeout', 20))
expected_http_code = int(self.getConfig('http_code', '200')) expected_http_code = int(self.getConfig('http_code', '200'))
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.TIMEOUT, timeout)
curl.setopt(pycurl.FOLLOWLOCATION, True)
curl.setopt(pycurl.SSL_VERIFYPEER, False)
curl.setopt(pycurl.SSL_VERIFYHOST, False)
curl.setopt(pycurl.WRITEFUNCTION, lambda x: None)
ca_cert_file = self.getConfig('ca-cert-file') ca_cert_file = self.getConfig('ca-cert-file')
cert_file = self.getConfig('cert-file') cert_file = self.getConfig('cert-file')
key_file = self.getConfig('key-file') key_file = self.getConfig('key-file')
if key_file and cert_file and ca_cert_file:
# set certificate configuration if ca_cert_file:
curl.setopt(curl.CAINFO, ca_cert_file) verify = ca_cert_file
curl.setopt(curl.SSLCERT, cert_file) else:
curl.setopt(curl.SSLKEY, key_file) verify = False
if key_file and cert_file:
cert = (cert_file, key_file)
else:
cert = None
try: try:
curl.perform() result = requests.get(url, verify=verify, allow_redirects=True, timeout=timeout, cert=cert)
except pycurl.error, e: except requests.ConnectionError as e:
code, message = e self.logger.error(
self.logger.error("%s: %s" % (code, message)) "ERROR connection not possible while accessing %r" % (url, ))
return
except Exception, e:
self.logger.error("ERROR: %s" % (e,))
return return
http_code = curl.getinfo(pycurl.HTTP_CODE) http_code = result.status_code
check_secure = self.getConfig('check-secure') check_secure = self.getConfig('check-secure')
curl.close()
if http_code == 0: if http_code == 0:
self.logger.error("%s is not available (server not reachable)." % url) self.logger.error("%s is not available (server not reachable)." % url)
......
...@@ -59,7 +59,7 @@ extra_config_dict = { ...@@ -59,7 +59,7 @@ extra_config_dict = {
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
self.assertEqual(result['result']['failed'], True) self.assertEqual(result['result']['failed'], True)
self.assertEqual(result['result']['message'], "3: Bad URL") self.assertEqual(result['result']['message'], "ERROR: Invalid URL u'https://': No host supplied")
def test_check_url_malformed(self): def test_check_url_malformed(self):
content = self.base_content % { content = self.base_content % {
...@@ -73,7 +73,7 @@ extra_config_dict = { ...@@ -73,7 +73,7 @@ extra_config_dict = {
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
self.assertEqual(result['result']['failed'], True) self.assertEqual(result['result']['failed'], True)
self.assertEqual(result['result']['message'], "3: <url> malformed") self.assertEqual(result['result']['message'], "ERROR: Invalid URL '': No schema supplied. Perhaps you meant http://?")
def test_check_url_site_off(self): def test_check_url_site_off(self):
content = content = self.base_content % { content = content = self.base_content % {
...@@ -87,7 +87,7 @@ extra_config_dict = { ...@@ -87,7 +87,7 @@ extra_config_dict = {
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
self.assertEqual(result['result']['failed'], True) self.assertEqual(result['result']['failed'], True)
self.assertEqual(result['result']['message'], "7: Failed to connect to localhost port 56789: Connection refused") self.assertEqual(result['result']['message'], "ERROR connection not possible while accessing 'https://localhost:56789/site'")
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment