90f6645681372887bf7b55ae8c044108b20ffa3c
[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 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
27 parser.add_argument("-r", "--report",
28                     help="Create json result file",
29                     action="store_true")
30 parser.add_argument("-m", "--mode", default='ssh',
31                     help="vPing mode: userdata or ssh",
32                     action="store")
33
34 args = parser.parse_args()
35
36
37 def main():
38     if args.mode == 'ssh':
39         case = 'vping_ssh'
40     else:
41         case = 'vping_userdata'
42
43     logger = ft_logger.Logger(case).getLogger()
44
45     util.init(logger)
46
47     util.check_repo_exist()
48
49     vmname_1 = util.get_vmname_1()
50     vmname_2 = util.get_vmname_2()
51
52     image_id = util.create_image()
53
54     flavor = util.get_flavor()
55
56     network_id = util.create_network_full()
57
58     sg_id = util.create_security_group()
59
60     util.delete_exist_vms()
61
62     start_time = time.time()
63     logger.info("vPing Start Time:'%s'" % (
64         datetime.datetime.fromtimestamp(start_time).strftime(
65             '%Y-%m-%d %H:%M:%S')))
66
67     vm1 = util.boot_vm(case,
68                        vmname_1,
69                        image_id,
70                        flavor,
71                        network_id,
72                        None,
73                        sg_id)
74     test_ip = util.get_test_ip(vm1)
75     vm2 = util.boot_vm(case,
76                        vmname_2,
77                        image_id,
78                        flavor,
79                        network_id,
80                        test_ip,
81                        sg_id)
82
83     EXIT_CODE, stop_time = util.do_vping(case, vm2, test_ip)
84     details = util.check_result(EXIT_CODE,
85                                 start_time,
86                                 stop_time)
87     util.push_result(args.report,
88                      case,
89                      start_time,
90                      stop_time,
91                      details)
92
93     exit(EXIT_CODE)
94
95
96 if __name__ == '__main__':
97     main()