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