refactor vping
[functest.git] / testcases / OpenStack / vPing / vping.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # 0.1: This script boots the VM1 and allocates IP address from Nova
11 # Later, the VM2 boots then execute cloud-init to ping VM1.
12 # After successful ping, both the VMs are deleted.
13 # 0.2: measure test duration and publish results under json format
14 # 0.3: adapt push 2 DB after Test API refacroting
15 #
16 #
17 import datetime
18 import time
19
20 import argparse
21 import functest.utils.functest_logger as ft_logger
22
23 import vping_util as util
24
25 parser = argparse.ArgumentParser()
26 image_exists = False
27
28 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
29 parser.add_argument("-r", "--report",
30                     help="Create json result file",
31                     action="store_true")
32 parser.add_argument("-m", "--mode", default='ssh',
33                     help="vPing mode: userdata or ssh",
34                     action="store")
35
36 args = parser.parse_args()
37
38 """ logging configuration """
39 logger = ft_logger.Logger("vping_userdata").getLogger()
40
41
42 def main():
43     if args.mode == 'ssh':
44         case = 'vping_ssh'
45     else:
46         case = 'vping_userdata'
47
48     util.init(logger)
49
50     util.check_repo_exist()
51
52     vmname_1 = util.get_vmname_1()
53     vmname_2 = util.get_vmname_2()
54
55     global image_exists
56     image_exists, image_id = util.create_image()
57
58     flavor = util.get_flavor()
59
60     network_id = util.create_network_full()
61
62     sg_id = util.create_security_group()
63
64     util.delete_exist_vms()
65
66     start_time = time.time()
67     logger.info("vPing Start Time:'%s'" % (
68         datetime.datetime.fromtimestamp(start_time).strftime(
69             '%Y-%m-%d %H:%M:%S')))
70
71     vm1 = util.boot_vm(case,
72                        vmname_1,
73                        image_id,
74                        flavor,
75                        network_id,
76                        None,
77                        sg_id)
78     test_ip = util.get_test_ip(vm1)
79     vm2 = util.boot_vm(case,
80                        vmname_2,
81                        image_id,
82                        flavor,
83                        network_id,
84                        test_ip,
85                        sg_id)
86
87     EXIT_CODE, stop_time = util.do_vping(case, vm2, test_ip)
88     details = util.check_result(EXIT_CODE,
89                                 start_time,
90                                 stop_time)
91     util.push_result(args.report,
92                      case,
93                      start_time,
94                      stop_time,
95                      details)
96
97     exit(EXIT_CODE)
98
99
100 if __name__ == '__main__':
101     main()