|
| 1 | +import sys |
| 2 | +import syslog |
| 3 | +from unittest.mock import patch |
| 4 | +import pytest |
| 5 | + |
| 6 | +sys.path.append("scripts") |
| 7 | +import disk_check |
| 8 | + |
| 9 | +disk_check.MOUNTS_FILE = "/tmp/proc_mounts" |
| 10 | + |
| 11 | +test_data = { |
| 12 | + "0": { |
| 13 | + "desc": "All good as /tmp is read-write", |
| 14 | + "args": ["", "-d", "/tmp"], |
| 15 | + "err": "" |
| 16 | + }, |
| 17 | + "1": { |
| 18 | + "desc": "Not good as /tmpx is not read-write; But fix skipped", |
| 19 | + "args": ["", "-d", "/tmpx", "-s"], |
| 20 | + "err": "/tmpx is not read-write" |
| 21 | + }, |
| 22 | + "2": { |
| 23 | + "desc": "Not good as /tmpx is not read-write; expect mount", |
| 24 | + "args": ["", "-d", "/tmpx"], |
| 25 | + "upperdir": "/tmp/tmpx", |
| 26 | + "workdir": "/tmp/tmpy", |
| 27 | + "mounts": "overlay_tmpx blahblah", |
| 28 | + "err": "/tmpx is not read-write|READ-ONLY: Mounted ['/tmpx'] to make Read-Write", |
| 29 | + "cmds": ['mount -t overlay overlay_tmpx -o lowerdir=/tmpx,upperdir=/tmp/tmpx,workdir=/tmp/tmpy /tmpx'] |
| 30 | + }, |
| 31 | + "3": { |
| 32 | + "desc": "Not good as /tmpx is not read-write; mount fail as create of upper fails", |
| 33 | + "args": ["", "-d", "/tmpx"], |
| 34 | + "upperdir": "/tmpx", |
| 35 | + "expect_ret": 1 |
| 36 | + }, |
| 37 | + "4": { |
| 38 | + "desc": "Not good as /tmpx is not read-write; mount fail as upper exist", |
| 39 | + "args": ["", "-d", "/tmpx"], |
| 40 | + "upperdir": "/tmp", |
| 41 | + "err": "/tmpx is not read-write|Already mounted", |
| 42 | + "expect_ret": 1 |
| 43 | + }, |
| 44 | + "5": { |
| 45 | + "desc": "/tmp is read-write, but as well mount exists; hence report", |
| 46 | + "args": ["", "-d", "/tmp"], |
| 47 | + "upperdir": "/tmp", |
| 48 | + "mounts": "overlay_tmp blahblah", |
| 49 | + "err": "READ-ONLY: Mounted ['/tmp'] to make Read-Write" |
| 50 | + }, |
| 51 | + "6": { |
| 52 | + "desc": "Test another code path for good case", |
| 53 | + "args": ["", "-d", "/tmp"], |
| 54 | + "upperdir": "/tmp" |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +err_data = "" |
| 59 | +cmds = [] |
| 60 | +current_tc = None |
| 61 | + |
| 62 | +def mount_file(d): |
| 63 | + with open(disk_check.MOUNTS_FILE, "w") as s: |
| 64 | + s.write(d) |
| 65 | + |
| 66 | + |
| 67 | +def report_err_msg(lvl, m): |
| 68 | + global err_data |
| 69 | + if lvl == syslog.LOG_ERR: |
| 70 | + if err_data: |
| 71 | + err_data += "|" |
| 72 | + err_data += m |
| 73 | + |
| 74 | + |
| 75 | +class proc: |
| 76 | + returncode = 0 |
| 77 | + stdout = None |
| 78 | + stderr = None |
| 79 | + |
| 80 | + def __init__(self, proc_upd = None): |
| 81 | + if proc_upd: |
| 82 | + self.returncode = proc_upd.get("ret", 0) |
| 83 | + self.stdout = proc_upd.get("stdout", None) |
| 84 | + self.stderr = proc_upd.get("stderr", None) |
| 85 | + |
| 86 | + |
| 87 | +def mock_subproc_run(cmd, shell, text, capture_output): |
| 88 | + global cmds |
| 89 | + |
| 90 | + upd = (current_tc["proc"][len(cmds)] |
| 91 | + if len(current_tc.get("proc", [])) > len(cmds) else None) |
| 92 | + cmds.append(cmd) |
| 93 | + |
| 94 | + return proc(upd) |
| 95 | + |
| 96 | + |
| 97 | +def init_tc(tc): |
| 98 | + global err_data, cmds, current_tc |
| 99 | + |
| 100 | + err_data = "" |
| 101 | + cmds = [] |
| 102 | + mount_file(tc.get("mounts", "")) |
| 103 | + current_tc = tc |
| 104 | + |
| 105 | + |
| 106 | +def swap_upper(tc): |
| 107 | + tmp_u = tc["upperdir"] |
| 108 | + tc["upperdir"] = disk_check.UPPER_DIR |
| 109 | + disk_check.UPPER_DIR = tmp_u |
| 110 | + |
| 111 | + |
| 112 | +def swap_work(tc): |
| 113 | + tmp_w = tc["workdir"] |
| 114 | + tc["upperdir"] = disk_check.WORK_DIR |
| 115 | + disk_check.WORK_DIR = tmp_w |
| 116 | + |
| 117 | + |
| 118 | +class TestDiskCheck(object): |
| 119 | + def setup(self): |
| 120 | + pass |
| 121 | + |
| 122 | + |
| 123 | + @patch("disk_check.syslog.syslog") |
| 124 | + @patch("disk_check.subprocess.run") |
| 125 | + def test_readonly(self, mock_proc, mock_log): |
| 126 | + global err_data, cmds |
| 127 | + |
| 128 | + mock_proc.side_effect = mock_subproc_run |
| 129 | + mock_log.side_effect = report_err_msg |
| 130 | + |
| 131 | + for i, tc in test_data.items(): |
| 132 | + print("-----------Start tc {}---------".format(i)) |
| 133 | + init_tc(tc) |
| 134 | + |
| 135 | + with patch('sys.argv', tc["args"]): |
| 136 | + if "upperdir" in tc: |
| 137 | + swap_upper(tc) |
| 138 | + |
| 139 | + if "workdir" in tc: |
| 140 | + # restore |
| 141 | + swap_work(tc) |
| 142 | + |
| 143 | + ret = disk_check.main() |
| 144 | + |
| 145 | + if "upperdir" in tc: |
| 146 | + # restore |
| 147 | + swap_upper(tc) |
| 148 | + |
| 149 | + if "workdir" in tc: |
| 150 | + # restore |
| 151 | + swap_work(tc) |
| 152 | + |
| 153 | + print("ret = {}".format(ret)) |
| 154 | + print("err_data={}".format(err_data)) |
| 155 | + print("cmds: {}".format(cmds)) |
| 156 | + |
| 157 | + assert ret == tc.get("expect_ret", 0) |
| 158 | + if "err" in tc: |
| 159 | + assert err_data == tc["err"] |
| 160 | + assert cmds == tc.get("cmds", []) |
| 161 | + print("-----------End tc {}-----------".format(i)) |
0 commit comments