robot tcs, test charts, robot container added
[ta/cloudtaf.git] / testcases / HPA_check / HPA_check.py
1 import sys\r
2 import os\r
3 import time\r
4 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '../libraries/common'))\r
5 from datetime import datetime  # noqa\r
6 from datetime import timedelta  # noqa\r
7 from robot.libraries.BuiltIn import BuiltIn  # noqa\r
8 from robot.api import logger  # noqa\r
9 import common_utils  # noqa\r
10 from decorators_for_robot_functionalities import *  # noqa\r
11 from test_constants import *  # noqa\r
12 \r
13 \r
14 execute = BuiltIn().get_library_instance('execute_command')\r
15 stack_infos = BuiltIn().get_library_instance('stack_infos')\r
16 \r
17 \r
18 def HPA_check():\r
19     steps = ['step1_check_initial_replica_count',\r
20              'step2_check_scale_out',\r
21              'step3_check_scale_in']\r
22     BuiltIn().run_keyword("HPA_check.setup")\r
23     common_utils.keyword_runner(steps)\r
24 \r
25 \r
26 def setup():\r
27     common_utils.helm_install(chart_name="default/php-apache", release_name="crf01",\r
28                               values="registry_url={reg_url}".format(reg_url=reg))\r
29     common_utils.check_kubernetes_object(kube_object=php_apache_pod,\r
30                                          tester_function=common_utils.test_kubernetes_object_available,\r
31                                          additional_filter="Running",\r
32                                          timeout=90)\r
33     flags = ["--horizontal-pod-autoscaler-downscale-stabilization=10s", "--horizontal-pod-autoscaler-sync-period=10s"]\r
34     common_utils.modify_static_pod_config(common_utils.add_flag_to_command, "cm.yml", flags)\r
35     common_utils.helm_install(chart_name="default/load-generator-for-apache", release_name="load")\r
36     common_utils.check_kubernetes_object(kube_object=load_generator_for_apache,\r
37                                          tester_function=common_utils.test_kubernetes_object_available,\r
38                                          additional_filter="Running",\r
39                                          timeout=60)\r
40 \r
41 \r
42 def step1_check_initial_replica_count():\r
43     time.sleep(5)\r
44     replica_count = int(\r
45         execute.execute_unix_command("kubectl get hpa | grep php-apache-hpa | awk '{print $6}'"))\r
46     if replica_count == 1:\r
47         logger.info("number of php apache pod is 1")\r
48     else:\r
49         raise Exception("Expected initial replica count is not correct: expected: 1, got: " + str(replica_count))\r
50 \r
51 \r
52 def step2_check_scale_out():\r
53     check_scaling(expected_replicas="2", timeout=360)\r
54 \r
55 \r
56 def step3_check_scale_in():\r
57     check_scaling(expected_replicas="1", timeout=480)\r
58 \r
59 \r
60 def check_scaling(expected_replicas, timeout):\r
61     wait_until = datetime.now() + timedelta(seconds=timeout)\r
62     actual_replicas = execute.execute_unix_command("kubectl get hpa | grep php-apache-hpa | awk '{print $6}'")\r
63     while actual_replicas != expected_replicas:\r
64         time.sleep(5)\r
65         actual_replicas = execute.execute_unix_command("kubectl get hpa | grep php-apache-hpa | awk '{print $6}'")\r
66         if actual_replicas == expected_replicas:\r
67             logger.info("number of php apache pod is " + expected_replicas + ", scale out was successful")\r
68         elif wait_until < datetime.now():\r
69             raise Exception("Scaling did not happen in " + str(timeout) + " seconds, expected replica count is " +\r
70                             expected_replicas + ", got " + actual_replicas)\r