diff --git a/jc/parsers/sfdisk.py b/jc/parsers/sfdisk.py index edd0c1c0..46341b3f 100644 --- a/jc/parsers/sfdisk.py +++ b/jc/parsers/sfdisk.py @@ -271,7 +271,6 @@ def parse(data, raw=False, quiet=False): if jc.utils.has_data(data): for line in data.splitlines(): - # deprecated - only for older versions of util-linux if line.startswith('# partition table of'): if item: @@ -363,6 +362,7 @@ def parse(data, raw=False, quiet=False): item['units'] = line.split(':')[1].strip() continue + # sfdisk -F if line.startswith('Unpartitioned space'): line = line.replace(':', '').replace(',', '') fields = line.split() @@ -372,6 +372,7 @@ def parse(data, raw=False, quiet=False): item['free_sectors'] = fields[7] continue + # partition lines if 'Device' in line and 'Boot' in line and 'Start' in line and 'End' in line: section = 'partitions' partitions.append(line.lower().replace('#', ' ')) @@ -387,6 +388,12 @@ def parse(data, raw=False, quiet=False): partitions = [] continue + # get final partitions if there are any left over + if section == 'partitions' and option != 'd' and partitions: + item['partitions'] = jc.parsers.universal.sparse_table_parse(partitions) + section = '' + partitions = [] + if item: raw_output.append(item) diff --git a/tests/fixtures/centos-8/sfdisk-F.json b/tests/fixtures/centos-8/sfdisk-F.json new file mode 100644 index 00000000..21258374 --- /dev/null +++ b/tests/fixtures/centos-8/sfdisk-F.json @@ -0,0 +1 @@ +[{"disk":"/dev/sda","free_disk_size":"0 B","free_bytes":0,"free_sectors":0,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512}] diff --git a/tests/fixtures/centos-8/sfdisk-l.json b/tests/fixtures/centos-8/sfdisk-l.json new file mode 100644 index 00000000..c97342c3 --- /dev/null +++ b/tests/fixtures/centos-8/sfdisk-l.json @@ -0,0 +1 @@ +[{"disk":"/dev/sda","disk_size":"20 GiB","bytes":21474836480,"sectors":41943040,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"min_io_size":512,"optimal_io_size":512,"disk_label_type":"dos","disk_identifier":"0x94988ac4","partitions":[{"device":"/dev/sda1","boot":true,"start":2048,"end":2099199,"sectors":2097152,"size":1,"id":"83","type":"Linux"},{"device":"/dev/sda2","boot":false,"start":2099200,"end":41943039,"sectors":39843840,"size":19,"id":"8e","type":"Linux LVM"}]},{"disk":"/dev/mapper/cl-root","disk_size":"17 GiB","bytes":18249416704,"sectors":35643392,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"min_io_size":512,"optimal_io_size":512},{"disk":"/dev/mapper/cl-swap","disk_size":"2 GiB","bytes":2147483648,"sectors":4194304,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"min_io_size":512,"optimal_io_size":512}] diff --git a/tests/fixtures/debian10/sfdisk-F.json b/tests/fixtures/debian10/sfdisk-F.json new file mode 100644 index 00000000..21258374 --- /dev/null +++ b/tests/fixtures/debian10/sfdisk-F.json @@ -0,0 +1 @@ +[{"disk":"/dev/sda","free_disk_size":"0 B","free_bytes":0,"free_sectors":0,"units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512}] diff --git a/tests/fixtures/debian10/sfdisk-l.json b/tests/fixtures/debian10/sfdisk-l.json new file mode 100644 index 00000000..8cd16a65 --- /dev/null +++ b/tests/fixtures/debian10/sfdisk-l.json @@ -0,0 +1 @@ +[{"disk":"/dev/sda","disk_size":"20 GiB","bytes":21474836480,"sectors":41943040,"disk_model":"VMware Virtual S","units":"sectors of 1 * 512 = 512 bytes","logical_sector_size":512,"physical_sector_size":512,"min_io_size":512,"optimal_io_size":512,"disk_label_type":"dos","disk_identifier":"0x3719ef0f","partitions":[{"device":"/dev/sda1","boot":true,"start":2048,"end":39942143,"sectors":39940096,"size":19,"id":"83","type":"Linux"},{"device":"/dev/sda2","boot":false,"start":39944190,"end":41940991,"sectors":1996802,"size":975,"id":"5","type":"Extended"},{"device":"/dev/sda5","boot":false,"start":39944192,"end":41940991,"sectors":1996800,"size":975,"id":"82","type":"Linux swap / Solaris"}]}] diff --git a/tests/test_sfdisk.py b/tests/test_sfdisk.py index 9466ae77..92618e46 100644 --- a/tests/test_sfdisk.py +++ b/tests/test_sfdisk.py @@ -31,6 +31,24 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-luS.out'), 'r', encoding='utf-8') as f: self.centos_7_7_sfdisk_luS = f.read() + + + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-8/sfdisk-l.out'), 'r', encoding='utf-8') as f: + self.centos_8_sfdisk_l = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-8/sfdisk-F.out'), 'r', encoding='utf-8') as f: + self.centos_8_sfdisk_F = f.read() + + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-l.out'), 'r', encoding='utf-8') as f: + self.debian_10_sfdisk_l = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-F.out'), 'r', encoding='utf-8') as f: + self.debian_10_sfdisk_F = f.read() + + + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-l.json'), 'r', encoding='utf-8') as f: self.centos_7_7_sfdisk_l_json = json.loads(f.read()) @@ -53,6 +71,20 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/sfdisk-luS.json'), 'r', encoding='utf-8') as f: self.centos_7_7_sfdisk_luS_json = json.loads(f.read()) + + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-8/sfdisk-l.json'), 'r', encoding='utf-8') as f: + self.centos_8_sfdisk_l_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-8/sfdisk-F.json'), 'r', encoding='utf-8') as f: + self.centos_8_sfdisk_F_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-l.json'), 'r', encoding='utf-8') as f: + self.debian_10_sfdisk_l_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/sfdisk-F.json'), 'r', encoding='utf-8') as f: + self.debian_10_sfdisk_F_json = json.loads(f.read()) + def test_sfdisk_nodata(self): """ Test 'sfdisk' with no data @@ -102,5 +134,30 @@ class MyTests(unittest.TestCase): self.assertEqual(jc.parsers.sfdisk.parse(self.centos_7_7_sfdisk_luS, quiet=True), self.centos_7_7_sfdisk_luS_json) + def test_sfdisk_l_centos_8(self): + """ + Test 'sfdisk -l' on Centos 8 + """ + self.assertEqual(jc.parsers.sfdisk.parse(self.centos_8_sfdisk_l, quiet=True), self.centos_8_sfdisk_l_json) + + def test_sfdisk_F_centos_8(self): + """ + Test 'sfdisk -F' on Centos 8 + """ + self.assertEqual(jc.parsers.sfdisk.parse(self.centos_8_sfdisk_F, quiet=True), self.centos_8_sfdisk_F_json) + + def test_sfdisk_l_debian_10(self): + """ + Test 'sfdisk -l' on Debian 10 + """ + self.assertEqual(jc.parsers.sfdisk.parse(self.debian_10_sfdisk_l, quiet=True), self.debian_10_sfdisk_l_json) + + def test_sfdisk_F_debian10(self): + """ + Test 'sfdisk -F' on Debian 10 + """ + self.assertEqual(jc.parsers.sfdisk.parse(self.debian_10_sfdisk_F, quiet=True), self.debian_10_sfdisk_F_json) + + if __name__ == '__main__': unittest.main()