From dfb2b3fdd552a5157aecffb55f1663f1896be7d2 Mon Sep 17 00:00:00 2001 From: Paul Neumann Date: Mon, 9 Jul 2018 15:15:31 +0200 Subject: [PATCH] ios_facts: Report space of file systems (#41850) * ios_facts: Report file system space Parse total and free space from dir output. For this, add a hash filesystems_info containing the keys spacetotal_kb and spacefree_kb. * ios_facts: Add unit test for file system space reporting * ios_facts: Add integration test for file system space reporting --- lib/ansible/modules/network/ios/ios_facts.py | 20 ++++++++++++++++ .../ios_facts/tests/cli/all_facts.yaml | 4 ++++ .../ios_facts/tests/cli/default_facts.yaml | 4 ++++ .../ios_facts/tests/cli/not_hardware.yaml | 1 + .../network/ios/fixtures/ios_facts_dir | 23 +++++++++++++++++++ .../fixtures/ios_facts_show_memory_statistics | 0 .../modules/network/ios/test_ios_facts.py | 10 ++++++++ 7 files changed, 62 insertions(+) create mode 100644 test/units/modules/network/ios/fixtures/ios_facts_dir create mode 100644 test/units/modules/network/ios/fixtures/ios_facts_show_memory_statistics diff --git a/lib/ansible/modules/network/ios/ios_facts.py b/lib/ansible/modules/network/ios/ios_facts.py index 805c635a376..45b7e4bcd94 100644 --- a/lib/ansible/modules/network/ios/ios_facts.py +++ b/lib/ansible/modules/network/ios/ios_facts.py @@ -105,6 +105,10 @@ ansible_net_filesystems: description: All file system names available on the device returned: when hardware is configured type: list +ansible_net_filesystems_info: + description: A hash of all file systems containing info about each file system (e.g. free and total space) + returned: when hardware is configured + type: dict ansible_net_memfree_mb: description: The available free memory on the remote device in Mb returned: when hardware is configured @@ -225,6 +229,7 @@ class Hardware(FactsBase): data = self.responses[0] if data: self.facts['filesystems'] = self.parse_filesystems(data) + self.facts['filesystems_info'] = self.parse_filesystems_info(data) data = self.responses[1] if data: @@ -241,6 +246,21 @@ class Hardware(FactsBase): def parse_filesystems(self, data): return re.findall(r'^Directory of (\S+)/', data, re.M) + def parse_filesystems_info(self, data): + facts = dict() + fs = '' + for line in data.split('\n'): + match = re.match(r'^Directory of (\S+)/', line) + if match: + fs = match.group(1) + facts[fs] = dict() + continue + match = re.match(r'^(\d+) bytes total \((\d+) bytes free\)', line) + if match: + facts[fs]['spacetotal_kb'] = int(match.group(1)) / 1024 + facts[fs]['spacefree_kb'] = int(match.group(2)) / 1024 + return facts + class Config(FactsBase): diff --git a/test/integration/targets/ios_facts/tests/cli/all_facts.yaml b/test/integration/targets/ios_facts/tests/cli/all_facts.yaml index 2596f76a8fe..5f2a754487d 100644 --- a/test/integration/targets/ios_facts/tests/cli/all_facts.yaml +++ b/test/integration/targets/ios_facts/tests/cli/all_facts.yaml @@ -28,4 +28,8 @@ - "result.ansible_facts.ansible_net_memfree_mb > 1" - "result.ansible_facts.ansible_net_memtotal_mb > 1" +- assert: + that: "{{ item.value.spacetotal_kb }} > {{ item.value.spacefree_kb }}" + loop: "{{ lookup('dict', result.ansible_facts.ansible_net_filesystems_info, wantlist=True) }}" + - debug: msg="END cli/all_facts.yaml on connection={{ ansible_connection }}" diff --git a/test/integration/targets/ios_facts/tests/cli/default_facts.yaml b/test/integration/targets/ios_facts/tests/cli/default_facts.yaml index 3efffb87d71..11cb9e518f7 100644 --- a/test/integration/targets/ios_facts/tests/cli/default_facts.yaml +++ b/test/integration/targets/ios_facts/tests/cli/default_facts.yaml @@ -28,4 +28,8 @@ # ... and not present - "result.ansible_facts.ansible_net_config is not defined" # config +- assert: + that: "{{ item.value.spacetotal_kb }} > {{ item.value.spacefree_kb }}" + loop: "{{ lookup('dict', result.ansible_facts.ansible_net_filesystems_info, wantlist=True) }}" + - debug: msg="END cli/default.yaml on connection={{ ansible_connection }}" diff --git a/test/integration/targets/ios_facts/tests/cli/not_hardware.yaml b/test/integration/targets/ios_facts/tests/cli/not_hardware.yaml index b8111988943..da46f2cd90b 100644 --- a/test/integration/targets/ios_facts/tests/cli/not_hardware.yaml +++ b/test/integration/targets/ios_facts/tests/cli/not_hardware.yaml @@ -26,5 +26,6 @@ - "result.ansible_facts.ansible_net_interfaces | length > 1" # more than one interface returned # ... and not present - "result.ansible_facts.ansible_net_filesystems is not defined" + - "result.ansible_facts.ansible_net_filesystems_info is not defined" - debug: msg="END cli/not_hardware_facts.yaml on connection={{ ansible_connection }}" diff --git a/test/units/modules/network/ios/fixtures/ios_facts_dir b/test/units/modules/network/ios/fixtures/ios_facts_dir new file mode 100644 index 00000000000..3adc44adba7 --- /dev/null +++ b/test/units/modules/network/ios/fixtures/ios_facts_dir @@ -0,0 +1,23 @@ +Directory of bootflash:/ + + 11 drwx 16384 Jun 1 2017 13:03:27 +00:00 lost+found +325121 drwx 4096 Jun 1 2017 13:03:54 +00:00 .super.iso.dir + 12 -rw- 31 Jun 22 2018 15:17:06 +00:00 .CsrLxc_LastInstall + 13 -rw- 69 Jun 1 2017 13:05:53 +00:00 virtual-instance.conf +438913 drwx 4096 Jun 1 2017 13:04:57 +00:00 core + 15 -rw- 125736960 Jun 1 2017 13:03:54 +00:00 iosxe-remote-mgmt.16.03.04.ova +105667 -rw- 292164568 Jun 1 2017 13:04:04 +00:00 csr1000v-mono-universalk9.16.03.04.SPA.pkg +105668 -rw- 34370768 Jun 1 2017 13:04:10 +00:00 csr1000v-rpboot.16.03.04.SPA.pkg +105666 -rw- 5317 Jun 1 2017 13:04:10 +00:00 packages.conf +195073 drwx 4096 Jun 1 2017 13:04:51 +00:00 .prst_sync +414529 drwx 4096 Jun 1 2017 13:04:57 +00:00 .rollback_timer + 16 -rw- 0 Jun 1 2017 13:05:00 +00:00 tracelogs.kZn +16257 drwx 24576 Jun 22 2018 16:03:11 +00:00 tracelogs +349505 drwx 4096 Jun 1 2017 13:05:08 +00:00 .installer +292609 drwx 4096 Jun 1 2017 13:05:59 +00:00 virtual-instance + 17 -rw- 30 Jun 22 2018 15:17:59 +00:00 throughput_monitor_params +48769 drwx 4096 Jun 1 2017 13:06:04 +00:00 onep + 19 -rw- 376 Jun 22 2018 15:18:11 +00:00 csrlxc-cfg.log + 20 -rw- 0 Jun 22 2018 15:17:59 +00:00 cvac.log + +7897796608 bytes total (6608056320 bytes free) diff --git a/test/units/modules/network/ios/fixtures/ios_facts_show_memory_statistics b/test/units/modules/network/ios/fixtures/ios_facts_show_memory_statistics new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/modules/network/ios/test_ios_facts.py b/test/units/modules/network/ios/test_ios_facts.py index 9557b7d9066..b6d0a711c03 100644 --- a/test/units/modules/network/ios/test_ios_facts.py +++ b/test/units/modules/network/ios/test_ios_facts.py @@ -77,3 +77,13 @@ class TestIosFactsModule(TestIosModule): self.assertIsNone( result['ansible_facts']['ansible_net_interfaces']['Tunnel1110']['macaddress'] ) + + def test_ios_facts_filesystems_info(self): + set_module_args(dict(gather_subset='hardware')) + result = self.execute_module() + self.assertEqual( + result['ansible_facts']['ansible_net_filesystems_info']['bootflash:']['spacetotal_kb'], 7712692.0 + ) + self.assertEqual( + result['ansible_facts']['ansible_net_filesystems_info']['bootflash:']['spacefree_kb'], 6453180.0 + )