From 9bc0f3ec76ebd1a03c57e13c9a77cdc1694a75fd Mon Sep 17 00:00:00 2001 From: Liz Rice Date: Fri, 22 Feb 2019 16:14:02 +0000 Subject: [PATCH 1/4] Add tests for host discovery --- src/modules/discovery/test_hosts.py | 62 +++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/modules/discovery/test_hosts.py diff --git a/src/modules/discovery/test_hosts.py b/src/modules/discovery/test_hosts.py new file mode 100644 index 0000000..6486ccf --- /dev/null +++ b/src/modules/discovery/test_hosts.py @@ -0,0 +1,62 @@ +import requests_mock +import time +from Queue import Empty + +from hosts import FromPodHostDiscovery, RunningAsPodEvent, HostScanEvent, AzureMetadataApi +from ...core.events.types import Event, NewHostEvent +from ...core.events import handler +from __main__ import config + +def test_FromPodHostDiscovery(): + + with requests_mock.Mocker() as m: + e = RunningAsPodEvent() + + config.azure = False + config.remote = None + config.cidr = None + m.get("http://169.254.169.254/metadata/instance?api-version=2017-08-01", status_code=404) + f = FromPodHostDiscovery(e) + assert not f.is_azure_pod() + # TODO For now we don't test the traceroute discovery version + # f.execute() + + # Test that we generate NewHostEvent for the addresses reported by the Azure Metadata API + config.azure = True + m.get("http://169.254.169.254/metadata/instance?api-version=2017-08-01", \ + text='{"network":{"interface":[{"ipv4":{"subnet":[{"address": "3.4.5.6", "prefix": "255.255.255.252"}]}}]}}') + assert f.is_azure_pod() + f.execute() + + # Test that we don't trigger a HostScanEvent unless either config.remote or config.cidr are configured + config.remote = "1.2.3.4" + f.execute() + + config.azure = False + config.remote = None + config.cidr = "1.2.3.4/24" + f.execute() + + +# In this set of tests we should only trigger HostScanEvent when remote or cidr are set +@handler.subscribe(HostScanEvent) +class testHostDiscovery(object): + def __init__(self, event): + assert config.remote is not None or config.cidr is not None + assert config.remote == "1.2.3.4" or config.cidr == "1.2.3.4/24" + +# In this set of tests we should only get as far as finding a host if it's Azure +# because we're not running the code that would normally be triggered by a HostScanEvent +@handler.subscribe(NewHostEvent) +class testHostDiscoveryEvent(object): + def __init__(self, event): + assert config.azure + assert str(event.host).startswith("3.4.5.") + assert config.remote is None + assert config.cidr is None + +# Test that we only report this event for Azure hosts +@handler.subscribe(AzureMetadataApi) +class testAzureMetadataApi(object): + def __init__(self, event): + assert config.azure \ No newline at end of file From 8c0e2b00a9ac9e3a22243b1e457587ccc9ba5dd2 Mon Sep 17 00:00:00 2001 From: Liz Rice Date: Fri, 22 Feb 2019 20:17:31 +0000 Subject: [PATCH 2/4] Fail build if tests don't succeed --- requirements.txt | 1 + runtest.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 7c572ce..5cc2a79 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ requests PrettyTable urllib3 ruamel.yaml +requests_mock diff --git a/runtest.py b/runtest.py index d6fc3d8..3a1e239 100644 --- a/runtest.py +++ b/runtest.py @@ -16,7 +16,7 @@ parser.add_argument('--report', type=str, default='plain', help="set report type config = parser.parse_args() def main(): - pytest.main(['.']) + exit(pytest.main(['.'])) if __name__ == '__main__': From 730de60926c207cae720dac62a865d7c67cfa1a8 Mon Sep 17 00:00:00 2001 From: Liz Rice Date: Fri, 22 Feb 2019 20:30:39 +0000 Subject: [PATCH 3/4] Tests not currently working under Python 3 --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e1c8004..4d30ef2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,8 @@ language: python cache: pip matrix: allow_failures: - - python: 2.7 + # Python 3 tests are gailing due to an import error + - python: 3.6 include: - python: 2.7 #- python: 3.4 From 4323904e7f9d8b21958c1b42d60701f968b41581 Mon Sep 17 00:00:00 2001 From: Liz Rice Date: Fri, 22 Feb 2019 20:31:25 +0000 Subject: [PATCH 4/4] Comment typo --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4d30ef2..39358c7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ language: python cache: pip matrix: allow_failures: - # Python 3 tests are gailing due to an import error + # Python 3 tests are failing due to an import error - python: 3.6 include: - python: 2.7