yportnova | a47b33e | 2015-03-05 20:20:31 +0200 | [diff] [blame] | 1 | import json |
| 2 | import sys |
| 3 | |
| 4 | import run_test |
| 5 | |
| 6 | |
| 7 | logger = run_test.logger |
| 8 | tool = None |
| 9 | |
| 10 | |
| 11 | class FakeVMContext(object): |
| 12 | def __init__(self, *args, **kwargs): |
| 13 | pass |
| 14 | |
| 15 | def __enter__(self): |
| 16 | return ["fake@fake"] |
| 17 | |
| 18 | def __exit__(self, *args, **kwargs): |
| 19 | pass |
| 20 | |
| 21 | |
| 22 | def fake_start_vms(*args, **kwargs): |
| 23 | return FakeVMContext |
| 24 | |
| 25 | |
| 26 | class FakeFD(object): |
| 27 | def __init__(self, content): |
| 28 | self.content = content |
| 29 | self.channel = FakeChannel() |
| 30 | |
| 31 | def read(self): |
| 32 | return self.content |
| 33 | |
| 34 | |
| 35 | class FakeChannel(object): |
| 36 | def recv_exit_status(self): |
| 37 | return 0 |
| 38 | |
| 39 | |
| 40 | def get_fake_out(cmd): |
| 41 | empty_fd = FakeFD("") |
| 42 | if "pgbench" == tool: |
| 43 | if "run" in cmd: |
| 44 | out = FakeFD("2 1:43\n2 1:42\n4 2:77") |
| 45 | else: |
| 46 | out = empty_fd |
| 47 | elif "iozone" == tool or "fio" == tool: |
| 48 | data = {'__meta__': { |
| 49 | 'direct_io': 1, |
| 50 | 'action': 'r', |
| 51 | 'concurence': 1, |
| 52 | 'blocksize': 1, |
| 53 | 'sync': 's'}, |
| 54 | 'bw_mean': 10} |
| 55 | out = FakeFD(json.dumps(data)) |
| 56 | else: |
| 57 | raise Exception("tool not found") |
| 58 | err = empty_fd |
| 59 | return empty_fd, out, err |
| 60 | |
| 61 | |
| 62 | def fake_ssh_connect(*args, **kwargs): |
| 63 | return FakeSSH() |
| 64 | |
| 65 | |
| 66 | class FakeSFTP(object): |
| 67 | def put(self, what, where): |
| 68 | logger.debug("Called sftp put with %s %s" % (what, where)) |
| 69 | |
| 70 | def chmod(self, f, mode): |
| 71 | logger.debug("called sftp chmod %s %s" % (mode, f)) |
| 72 | |
| 73 | def close(self): |
| 74 | logger.debug("called sftp close") |
| 75 | |
| 76 | |
| 77 | class FakeSSH(object): |
| 78 | def exec_command(self, cmd, **kwargs): |
| 79 | return get_fake_out(cmd) |
| 80 | |
| 81 | def close(self): |
| 82 | pass |
| 83 | |
| 84 | def open_sftp(self): |
| 85 | return FakeSFTP() |
| 86 | |
| 87 | |
| 88 | class FakePopen(object): |
| 89 | def __init__(self, cmd, |
| 90 | shell=True, |
| 91 | stdout=None, |
| 92 | stderr=None, |
| 93 | stdin=None): |
| 94 | print "Running subprocess command: %s" % cmd |
| 95 | self.stdin, self.stdout, self.stderr = get_fake_out(cmd) |
| 96 | |
| 97 | def wait(self): |
| 98 | return 0 |
| 99 | |
| 100 | |
| 101 | if __name__ == '__main__': |
| 102 | run_test.subprocess.Popen = FakePopen |
| 103 | run_test.start_test_vms = fake_start_vms() |
| 104 | run_test.ssh_runner.ssh_connect = fake_ssh_connect |
| 105 | opts = run_test.parse_args(sys.argv[1:]) |
| 106 | tool = opts.tool_type |
| 107 | exit(run_test.main(sys.argv[1:])) |