Add case_name as constructor arg
[functest.git] / functest / opnfv_tests / openstack / vping / vping_userdata.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 import sys
11 import time
12
13 import argparse
14
15 import functest.utils.functest_logger as ft_logger
16 import vping_base
17
18
19 class VPingUserdata(vping_base.VPingBase):
20
21     def __init__(self, case_name='vping_userdata'):
22         super(VPingUserdata, self).__init__(case_name)
23         self.logger = ft_logger.Logger(self.case_name).getLogger()
24
25     def boot_vm_preparation(self, config, vmname, test_ip):
26         config['config_drive'] = True
27         if vmname == self.vm2_name:
28             u = ("#!/bin/sh\n\n"
29                  "while true; do\n"
30                  " ping -c 1 %s 2>&1 >/dev/null\n"
31                  " RES=$?\n"
32                  " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
33                  "  echo 'vPing OK'\n"
34                  "  break\n"
35                  " else\n"
36                  "  echo 'vPing KO'\n"
37                  " fi\n"
38                  " sleep 1\n"
39                  "done\n" % test_ip)
40             config['userdata'] = u
41
42     def do_vping(self, vm, test_ip):
43         self.logger.info("Waiting for ping...")
44         EXIT_CODE = -1
45         sec = 0
46         tries = 0
47
48         while True:
49             time.sleep(1)
50             p_console = vm.get_console_output()
51             if "vPing OK" in p_console:
52                 self.logger.info("vPing detected!")
53                 EXIT_CODE = 0
54                 break
55             elif "failed to read iid from metadata" in p_console or tries > 5:
56                 EXIT_CODE = -2
57                 break
58             elif sec == self.ping_timeout:
59                 self.logger.info("Timeout reached.")
60                 break
61             elif sec % 10 == 0:
62                 if "request failed" in p_console:
63                     self.logger.debug("It seems userdata is not supported "
64                                       "in nova boot. Waiting a bit...")
65                     tries += 1
66                 else:
67                     self.logger.debug("Pinging %s. Waiting for response..."
68                                       % test_ip)
69             sec += 1
70
71         return EXIT_CODE
72
73
74 if __name__ == '__main__':
75     args_parser = argparse.ArgumentParser()
76     args_parser.add_argument("-r", "--report",
77                              help="Create json result file",
78                              action="store_true")
79     args = vars(args_parser.parse_args())
80
81     sys.exit(vping_base.VPingMain(VPingUserdata).main(**args))