diff options
author | Cameron Moberg <cjmoberg@google.com> | 2017-08-07 15:59:44 -0700 |
---|---|---|
committer | Cameron Moberg <cjmoberg@google.com> | 2017-08-07 15:59:44 -0700 |
commit | a95211ec97455ffe8238996a43ab46d64a5f010e (patch) | |
tree | 1c0036a4de6509ba4f145893ec7cc10a5059987e | |
parent | 2e7d258a282caa14851062cbc4e43d4b66be2584 (diff) | |
download | platform_tools_test_connectivity-a95211ec97455ffe8238996a43ab46d64a5f010e.tar.gz platform_tools_test_connectivity-a95211ec97455ffe8238996a43ab46d64a5f010e.tar.bz2 platform_tools_test_connectivity-a95211ec97455ffe8238996a43ab46d64a5f010e.zip |
Added HealthyIfStartsWith class for more accurate testing
Bug: None
Test: see tests/constant_health_analyzer_test.py
Change-Id: I4ccff7de3e15e8aaec79147444122b734eca372b
-rw-r--r-- | tools/lab/config.json | 6 | ||||
-rw-r--r-- | tools/lab/health/constant_health_analyzer.py | 13 | ||||
-rw-r--r-- | tools/lab/health_checker.py | 4 | ||||
-rw-r--r-- | tools/lab/tests/constant_health_analyzer_test.py | 24 |
4 files changed, 46 insertions, 1 deletions
diff --git a/tools/lab/config.json b/tools/lab/config.json index fced46edac..5ef8ff08ca 100644 --- a/tools/lab/config.json +++ b/tools/lab/config.json @@ -45,6 +45,12 @@ "compare": "EQUALS" } }, + "kernel_version": { + "kernel_release": { + "constant": "3.19", + "compare": "STARTS_WITH" + } + }, "zombie": { "num_adb_zombies": { "constant": 0, diff --git a/tools/lab/health/constant_health_analyzer.py b/tools/lab/health/constant_health_analyzer.py index 97e92a99a8..d7f9794c11 100644 --- a/tools/lab/health/constant_health_analyzer.py +++ b/tools/lab/health/constant_health_analyzer.py @@ -75,3 +75,16 @@ class HealthyIfEquals(ConstantHealthAnalyzer): True if result is equal to constant """ return metric_results[self.key] == self._constant + + +class HealthyIfStartsWith(ConstantHealthAnalyzer): + def is_healthy(self, metric_results): + """Returns whether result starts with a constant + + Args: + metric_results: a dictionary of metric results. + + Returns: + True if result starts with a constant + """ + return metric_results[self.key].startswith(str(self._constant)) diff --git a/tools/lab/health_checker.py b/tools/lab/health_checker.py index c935197c5c..6614220b04 100644 --- a/tools/lab/health_checker.py +++ b/tools/lab/health_checker.py @@ -19,6 +19,7 @@ import logging from health.constant_health_analyzer import HealthyIfGreaterThanConstantNumber from health.constant_health_analyzer import HealthyIfLessThanConstantNumber from health.constant_health_analyzer import HealthyIfEquals +from health.constant_health_analyzer import HealthyIfStartsWith from health.custom_health_analyzer import HealthyIfNotIpAddress from health.constant_health_analyzer_wrapper import HealthyIfValsEqual @@ -49,7 +50,8 @@ class HealthChecker(object): 'LESS_THAN': lambda k, c: HealthyIfLessThanConstantNumber(k, c), 'EQUALS': lambda k, c: HealthyIfEquals(k, c), 'IP_ADDR': lambda k, c: HealthyIfNotIpAddress(k), - 'EQUALS_DICT': lambda k, c: HealthyIfValsEqual(k, c) + 'EQUALS_DICT': lambda k, c: HealthyIfValsEqual(k, c), + 'STARTS_WITH': lambda k, c: HealthyIfStartsWith(k, c) } def __init__(self, config): diff --git a/tools/lab/tests/constant_health_analyzer_test.py b/tools/lab/tests/constant_health_analyzer_test.py index ebf38b2340..756b4889ec 100644 --- a/tools/lab/tests/constant_health_analyzer_test.py +++ b/tools/lab/tests/constant_health_analyzer_test.py @@ -61,5 +61,29 @@ class HealthyIfEqualsTest(unittest.TestCase): self.assertFalse(analyzer.is_healthy(sample_metric)) +class HealthIfStartsWithTest(unittest.TestCase): + def test_starts_with_true_str(self): + sample_metric = {'kernel_release': "3.19-generic-random-12"} + analyzer = ha.HealthyIfStartsWith( + key='kernel_release', constant="3.19") + self.assertTrue(analyzer.is_healthy(sample_metric)) + + def test_starts_with_false_str(self): + sample_metric = {'kernel_release': "3.19-generic-random-12"} + analyzer = ha.HealthyIfStartsWith( + key='kernel_release', constant="4.04") + self.assertFalse(analyzer.is_healthy(sample_metric)) + + def test_starts_with_true_non_str(self): + sample_metric = {'kernel_release': "3.19-generic-random-12"} + analyzer = ha.HealthyIfStartsWith(key='kernel_release', constant=3.19) + self.assertTrue(analyzer.is_healthy(sample_metric)) + + def test_starts_with_false_non_str(self): + sample_metric = {'kernel_release': "3.19-generic-random-12"} + analyzer = ha.HealthyIfStartsWith(key='kernel_release', constant=4.04) + self.assertFalse(analyzer.is_healthy(sample_metric)) + + if __name__ == '__main__': unittest.main() |