Commit 8db39d3b authored by Alain Takoudjou's avatar Alain Takoudjou

promises: fixup, first part

parent 3cbe5e3e
from zope import interface as zope_interface 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, TestResult
import re import re
import time import time
import os import os
...@@ -12,12 +12,12 @@ class RunPromise(GenericPromise): ...@@ -12,12 +12,12 @@ class RunPromise(GenericPromise):
def __init__(self, config): def __init__(self, config):
GenericPromise.__init__(self, config) GenericPromise.__init__(self, config)
# set periodicity to run the promise twice per day # set periodicity to run the promise twice per day
self.custom_frequency = 720 self.custom_frequency = int(self.getConfig('frequency', 720))
self.setPeriodicity(self.custom_frequency) self.setPeriodicity(self.custom_frequency)
def sense(self): def sense(self):
""" """
Check if frontend URL is available Check if http log file contain errors
""" """
log_file = self.getConfig('log-file') log_file = self.getConfig('log-file')
maximum_delay = int(self.getConfig('maximum-delay', 0)) maximum_delay = int(self.getConfig('maximum-delay', 0))
...@@ -82,3 +82,6 @@ class RunPromise(GenericPromise): ...@@ -82,3 +82,6 @@ class RunPromise(GenericPromise):
def anomaly(self): def anomaly(self):
# only check the result of the two latest sense call # only check the result of the two latest sense call
return self._test(result_count=2, failure_amount=2, latest_minute=self.custom_frequency*3) return self._test(result_count=2, failure_amount=2, latest_minute=self.custom_frequency*3)
def test(self):
return TestResult(message="")
from zope import interface as zope_interface 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, TestResult
import re import re
import time import time
from slapos.networkbench.ping import ping, ping6 from slapos.networkbench.ping import ping, ping6
...@@ -12,12 +12,12 @@ class RunPromise(GenericPromise): ...@@ -12,12 +12,12 @@ class RunPromise(GenericPromise):
def __init__(self, config): def __init__(self, config):
GenericPromise.__init__(self, config) GenericPromise.__init__(self, config)
# set periodicity to run the promise twice per day # set periodicity to run the promise twice per day
self.custom_frequency = 720 self.custom_frequency = int(self.getConfig('frequency', 720))
self.setPeriodicity(self.custom_frequency) self.setPeriodicity(self.custom_frequency)
def sense(self): def sense(self):
""" """
Check if frontend URL is available Check if there ICMP packets lost on given address
""" """
# Address to ping to # Address to ping to
address = self.getConfig('address') address = self.getConfig('address')
...@@ -26,6 +26,9 @@ class RunPromise(GenericPromise): ...@@ -26,6 +26,9 @@ class RunPromise(GenericPromise):
# Force use ipv4 protocol ? # Force use ipv4 protocol ?
ipv4 = self.getConfig('ipv4') in ('True', 'true', '1') ipv4 = self.getConfig('ipv4') in ('True', 'true', '1')
count = self.getConfig('count', 10) count = self.getConfig('count', 10)
threshold = int(self.getConfig('threshold', 0))
if threshold < 0:
raise ValueError("'threshold' value should be greater than 0.")
if ipv4: if ipv4:
result = ping(address, count=count) result = ping(address, count=count)
...@@ -33,7 +36,8 @@ class RunPromise(GenericPromise): ...@@ -33,7 +36,8 @@ class RunPromise(GenericPromise):
result = ping6(address, count=count) result = ping6(address, count=count)
message = "%s host=%s code=%s, result=%s, packet_lost_ratio=%s msg=%s" % result message = "%s host=%s code=%s, result=%s, packet_lost_ratio=%s msg=%s" % result
if result[4] != "0": packet_lost_ratio = int(result[4])
if packet_lost_ratio == -1 or packet_lost_ratio > threshold:
# Packet lost occurred # Packet lost occurred
self.logger.error(message) self.logger.error(message)
else: else:
...@@ -42,3 +46,6 @@ class RunPromise(GenericPromise): ...@@ -42,3 +46,6 @@ class RunPromise(GenericPromise):
def anomaly(self): def anomaly(self):
# only check the result of the two latest sense call # only check the result of the two latest sense call
return self._test(result_count=2, failure_amount=2, latest_minute=self.custom_frequency*3) return self._test(result_count=2, failure_amount=2, latest_minute=self.custom_frequency*3)
def test(self):
return TestResult(message="")
from zope import interface as zope_interface 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, TestResult
import re import re
import time import time
from slapos.networkbench.ping import ping, ping6 from slapos.networkbench.ping import ping, ping6
...@@ -12,12 +12,12 @@ class RunPromise(GenericPromise): ...@@ -12,12 +12,12 @@ class RunPromise(GenericPromise):
def __init__(self, config): def __init__(self, config):
GenericPromise.__init__(self, config) GenericPromise.__init__(self, config)
# set periodicity to run the promise twice per day # set periodicity to run the promise twice per day
self.custom_frequency = 720 self.custom_frequency = int(self.getConfig('frequency', 720))
self.setPeriodicity(self.custom_frequency) self.setPeriodicity(self.custom_frequency)
def sense(self): def sense(self):
""" """
Check if frontend URL is available Check re6st optimal status
""" """
# promise ipv6 and ipv4 address to compare. # promise ipv6 and ipv4 address to compare.
ipv4 = self.getConfig('ipv4') ipv4 = self.getConfig('ipv4')
...@@ -36,31 +36,34 @@ class RunPromise(GenericPromise): ...@@ -36,31 +36,34 @@ class RunPromise(GenericPromise):
if result_ipv4[3] == "failed" and result_ipv6[3] != "failed": if result_ipv4[3] == "failed" and result_ipv6[3] != "failed":
# IPv4 is unreacheable # IPv4 is unreacheable
self.logger.info("OK") self.logger.info("OK: IPv4 unreachable, IPv6 reachable")
return return
if result_ipv6[3] == "failed": if result_ipv6[3] == "failed":
# IPv6 is unreacheable # IPv6 is unreacheable
self.logger.error("FAILED") self.logger.error("FAILED: IPv4 reachable, IPv6 unreachable")
return return
latency4 = float(result_ipv4[3]) latency4 = float(result_ipv4[3])
latency6 = float(result_ipv6[3]) latency6 = float(result_ipv6[3])
# We can consider that at worst 1ms is added to # We can consider that at worst 1ms is added to
# ipv4 response, due the usage of openvpn. # ipv4 response, due the usage of openvpn.
acceptable_delay = 1 acceptable_delay = int(self.getConfig('acceptable-delay', 1))
# We can consider that we accept a certain increase # We can consider that we accept a certain increase
# on latency, if we are on a bit congested link. # on latency, if we are on a bit congested link.
# So 10% is reseonable enough. # So 10% is reseonable enough.
acceptable_lost = 0.10 acceptable_lost = int(self.getConfig('acceptable-lost', 0.10))
# Increase latency with the value. # Increase latency with the value.
latency4 += acceptable_delay + latency4 * acceptable_lost latency4 += acceptable_delay + latency4 * acceptable_lost
if latency4 < latency6: if latency4 < latency6:
self.logger.error("FAIL %s (latency4) > %s (latence6)" % (latency4, latency6)) self.logger.error("FAIL %s (latency4) > %s (latence6)" % (latency4, latency6))
else: else:
# Compare if both has Same working rate # Compare if both has Same working rate
self.logger.info("OK") self.logger.info("OK: IPv4 reachable, IPv6 reachable")
def anomaly(self): def anomaly(self):
# only check the result of the two latest sense call # only check the result of the two latest sense call
return self._test(result_count=2, failure_amount=2, latest_minute=self.custom_frequency*3) return self._test(result_count=2, failure_amount=2, latest_minute=self.custom_frequency*3)
def test(self):
return TestResult(message="")
...@@ -20,7 +20,6 @@ class RunPromise(GenericPromise): ...@@ -20,7 +20,6 @@ class RunPromise(GenericPromise):
""" """
validate_script = self.getConfig('verification-script') validate_script = self.getConfig('verification-script')
result = float(subprocess.check_output([validate_script]))
process = SlapPopen([validate_script]) process = SlapPopen([validate_script])
stdout, stderr = process.communicate() stdout, stderr = process.communicate()
if process.returncode != 0: if process.returncode != 0:
...@@ -29,4 +28,4 @@ class RunPromise(GenericPromise): ...@@ -29,4 +28,4 @@ class RunPromise(GenericPromise):
self.logger.error("%s\n%s" % (stdout, stderr)) self.logger.error("%s\n%s" % (stdout, stderr))
def anomaly(self): def anomaly(self):
return self._test(result_count=2, failure_amount=2) return self._test()
...@@ -86,7 +86,9 @@ extra_config_dict = { ...@@ -86,7 +86,9 @@ extra_config_dict = {
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher() self.configureLauncher(force=True, enable_anomaly=True)
self.launcher.run()
# run a second time to add more results
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
self.assertEqual(result['result']['failed'], False) self.assertEqual(result['result']['failed'], False)
...@@ -102,7 +104,6 @@ extra_config_dict = { ...@@ -102,7 +104,6 @@ extra_config_dict = {
if os.path.exists(self.promise_pyc): if os.path.exists(self.promise_pyc):
os.unlink(self.promise_pyc) os.unlink(self.promise_pyc)
# Ignore periodicity of the promise # Ignore periodicity of the promise
self.configureLauncher(force=True)
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
self.assertEqual(result['result']['failed'], False) self.assertEqual(result['result']['failed'], False)
...@@ -116,7 +117,8 @@ extra_config_dict = { ...@@ -116,7 +117,8 @@ extra_config_dict = {
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher() self.configureLauncher(force=True, enable_anomaly=True)
self.launcher.run()
with self.assertRaises(PromiseError): with self.assertRaises(PromiseError):
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
...@@ -133,7 +135,6 @@ extra_config_dict = { ...@@ -133,7 +135,6 @@ extra_config_dict = {
if os.path.exists(self.promise_pyc): if os.path.exists(self.promise_pyc):
os.unlink(self.promise_pyc) os.unlink(self.promise_pyc)
# Ignore periodicity of the promise # Ignore periodicity of the promise
self.configureLauncher(force=True, debug=True)
with self.assertRaises(PromiseError): with self.assertRaises(PromiseError):
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
...@@ -148,7 +149,8 @@ extra_config_dict = { ...@@ -148,7 +149,8 @@ extra_config_dict = {
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher() self.configureLauncher(force=True, enable_anomaly=True)
self.launcher.run()
with self.assertRaises(PromiseError): with self.assertRaises(PromiseError):
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
...@@ -165,7 +167,6 @@ extra_config_dict = { ...@@ -165,7 +167,6 @@ extra_config_dict = {
if os.path.exists(self.promise_pyc): if os.path.exists(self.promise_pyc):
os.unlink(self.promise_pyc) os.unlink(self.promise_pyc)
# Ignore periodicity of the promise # Ignore periodicity of the promise
self.configureLauncher(force=True)
with self.assertRaises(PromiseError): with self.assertRaises(PromiseError):
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
...@@ -180,7 +181,8 @@ extra_config_dict = { ...@@ -180,7 +181,8 @@ extra_config_dict = {
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher() self.configureLauncher(force=True, enable_anomaly=True)
self.launcher.run()
with self.assertRaises(PromiseError): with self.assertRaises(PromiseError):
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
...@@ -197,7 +199,6 @@ extra_config_dict = { ...@@ -197,7 +199,6 @@ extra_config_dict = {
if os.path.exists(self.promise_pyc): if os.path.exists(self.promise_pyc):
os.unlink(self.promise_pyc) os.unlink(self.promise_pyc)
# Ignore periodicity of the promise # Ignore periodicity of the promise
self.configureLauncher(force=True)
with self.assertRaises(PromiseError): with self.assertRaises(PromiseError):
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
......
...@@ -46,6 +46,7 @@ extra_config_dict = { ...@@ -46,6 +46,7 @@ extra_config_dict = {
'ipv4': '%(ipv4)s', 'ipv4': '%(ipv4)s',
'count': '%(count)s', 'count': '%(count)s',
'address': '%(address)s', 'address': '%(address)s',
'threshold': '%(threshold)s'
} }
""" """
...@@ -56,11 +57,14 @@ extra_config_dict = { ...@@ -56,11 +57,14 @@ extra_config_dict = {
content = self.base_content % { content = self.base_content % {
'address': "localhost", 'address': "localhost",
'count': 5, 'count': 5,
"ipv4": True "ipv4": True,
"threshold": 0
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher(timeout=20) self.configureLauncher(force=True, timeout=20, enable_anomaly=True)
self.launcher.run()
# run a second time to add more results
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
self.assertEqual(result['result']['failed'], False) self.assertEqual(result['result']['failed'], False)
...@@ -70,11 +74,13 @@ extra_config_dict = { ...@@ -70,11 +74,13 @@ extra_config_dict = {
content = self.base_content % { content = self.base_content % {
'address': "couscous", 'address': "couscous",
'count': 5, 'count': 5,
"ipv4": True "ipv4": True,
"threshold": 0
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher(timeout=20) self.configureLauncher(force=True, timeout=20, enable_anomaly=True)
self.launcher.run()
with self.assertRaises(PromiseError): with self.assertRaises(PromiseError):
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
...@@ -85,11 +91,14 @@ extra_config_dict = { ...@@ -85,11 +91,14 @@ extra_config_dict = {
content = self.base_content % { content = self.base_content % {
'address': "::1", 'address': "::1",
'count': 5, 'count': 5,
"ipv4": False "ipv4": False,
"threshold": 0
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher(timeout=20) self.configureLauncher(force=True, timeout=20, enable_anomaly=True)
self.launcher.run()
# run a second time to add more results
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
self.assertEqual(result['result']['failed'], False) self.assertEqual(result['result']['failed'], False)
...@@ -99,11 +108,14 @@ extra_config_dict = { ...@@ -99,11 +108,14 @@ extra_config_dict = {
content = self.base_content % { content = self.base_content % {
'address': "::1", 'address': "::1",
'count': 5, 'count': 5,
"ipv4": True "ipv4": True,
"threshold": 0
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher(timeout=20) self.configureLauncher(force=True, timeout=20, enable_anomaly=True)
self.launcher.run()
# run a second time to add more results
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
self.assertEqual(result['result']['failed'], False) self.assertEqual(result['result']['failed'], False)
...@@ -113,16 +125,35 @@ extra_config_dict = { ...@@ -113,16 +125,35 @@ extra_config_dict = {
content = self.base_content % { content = self.base_content % {
'address': "couscous", 'address': "couscous",
'count': 5, 'count': 5,
"ipv4": False "ipv4": False,
"threshold": 0
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher(timeout=20) self.configureLauncher(force=True, timeout=20, enable_anomaly=True)
self.launcher.run()
with self.assertRaises(PromiseError): with self.assertRaises(PromiseError):
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.assertTrue('packet_lost_ratio=-1' in result['result']['message']) self.assertTrue('packet_lost_ratio=-1' in result['result']['message'])
def test_packet_lost_less_than_threshold(self):
content = self.base_content % {
'address': "10.2.3.4",
'count': 5,
"ipv4": True,
"threshold": 110
}
self.writePromise(self.promise_name, content)
self.configureLauncher(force=True, timeout=20, enable_anomaly=True)
self.launcher.run()
# run a second time to add more results
self.launcher.run()
result = self.getPromiseResult(self.promise_name)
self.assertEqual(result['result']['failed'], False)
self.assertTrue('packet_lost_ratio=100' in result['result']['message'])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -55,12 +55,14 @@ extra_config_dict = { ...@@ -55,12 +55,14 @@ extra_config_dict = {
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher(timeout=20) self.configureLauncher(force=True, timeout=20, enable_anomaly=True)
self.launcher.run()
# run a second time to add more results
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
last_message = result['result']['message'].split('\n')[-1] last_message = result['result']['message'].split('\n')[-1]
self.assertEqual(result['result']['failed'], False) self.assertEqual(result['result']['failed'], False)
self.assertEqual(last_message, "OK") self.assertEqual(last_message, "OK: IPv4 reachable, IPv6 reachable")
def test_ipv4_is_faster(self): def test_ipv4_is_faster(self):
content = self.base_content % { content = self.base_content % {
...@@ -70,7 +72,8 @@ extra_config_dict = { ...@@ -70,7 +72,8 @@ extra_config_dict = {
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher(timeout=20) self.configureLauncher(force=True, timeout=20, enable_anomaly=True)
self.launcher.run()
with self.assertRaises(PromiseError): with self.assertRaises(PromiseError):
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
...@@ -86,12 +89,14 @@ extra_config_dict = { ...@@ -86,12 +89,14 @@ extra_config_dict = {
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher(timeout=20) self.configureLauncher(force=True, timeout=20, enable_anomaly=True)
self.launcher.run()
# run a second time to add more results
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
last_message = result['result']['message'].split('\n')[-1] last_message = result['result']['message'].split('\n')[-1]
self.assertEqual(result['result']['failed'], False) self.assertEqual(result['result']['failed'], False)
self.assertEqual(last_message, "OK") self.assertEqual(last_message, "OK: IPv4 unreachable, IPv6 reachable")
def test_ipv6_fail(self): def test_ipv6_fail(self):
content = self.base_content % { content = self.base_content % {
...@@ -101,13 +106,14 @@ extra_config_dict = { ...@@ -101,13 +106,14 @@ extra_config_dict = {
} }
self.writePromise(self.promise_name, content) self.writePromise(self.promise_name, content)
self.configureLauncher(timeout=20) self.configureLauncher(force=True, timeout=20, enable_anomaly=True)
self.launcher.run()
with self.assertRaises(PromiseError): with self.assertRaises(PromiseError):
self.launcher.run() self.launcher.run()
result = self.getPromiseResult(self.promise_name) result = self.getPromiseResult(self.promise_name)
last_message = result['result']['message'].split('\n')[-1] last_message = result['result']['message'].split('\n')[-1]
self.assertEqual(result['result']['failed'], True) self.assertEqual(result['result']['failed'], True)
self.assertEqual(last_message, "FAILED") self.assertEqual(last_message, "FAILED: IPv4 reachable, IPv6 unreachable")
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