robot tcs, test charts, robot container added
[ta/cloudtaf.git] / testcases / cpu_pooling / tc_003_exclusive_pool_tests_more_cpu.py
1 import sys
2 import os
3 from robot.libraries.BuiltIn import BuiltIn
4 from robot.libraries.String import String
5 from robot.api import logger
6 from decorators_for_robot_functionalities import *
7 from test_constants import *
8
9 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '../libraries/common'))
10 import common_utils  # noqa
11
12
13 ex = BuiltIn().get_library_instance('execute_command')
14 cpupools = {}
15
16
17 def tc_003_exclusive_pool_tests_more_cpu():
18     steps = ['step1_with_two_process']
19     BuiltIn().run_keyword("tc_003_exclusive_pool_tests_more_cpu.Setup")
20     common_utils.keyword_runner(steps)
21
22
23 def Setup():
24     global cpupools, nodename
25     nodename = common_utils.decide_nodename()
26     cpupools = common_utils.get_cpupools()
27     logger.info("CPU pools: " + str(cpupools))
28     logger.info("Default nodename to deploy: " + nodename)
29
30
31 def step1_with_two_process():
32     try:
33         common_utils.helm_install(chart_name="default/cpu-pooling-exclusive3", release_name="cpu-pooling",
34                                   values="registry_url=" + reg + ",nodename=" + nodename)
35         common_utils.test_kubernetes_object_quality(kube_object=cpu_pooling_pod3,
36                                                     expected_result="1",
37                                                     filter=r'(Running)\s*[0]',
38                                                     timeout=10)
39
40         exclusive_cpus = cpupools[nodename]['exclusive_caas']
41
42         proc1_cpu, proc2_cpu = get_cpu_core_of_processes(cpu_pooling_pod3['obj_name'], "dumb-init -c sleep 1000")
43         if proc1_cpu not in exclusive_cpus:
44             raise Exception('{pod}: Proc1 running on non exclusive cpu core {cpu}!'
45                             .format(pod=cpu_pooling_pod3['obj_name'], cpu=proc1_cpu))
46         if proc2_cpu not in exclusive_cpus:
47             raise Exception('{pod}: Proc2 running on non exclusive cpu core {cpu}!'
48                             .format(pod=cpu_pooling_pod3['obj_name'], cpu=proc2_cpu))
49         if proc1_cpu == proc2_cpu:
50             raise Exception('{pod}: Two processes use same cpu core: {cpu}!'
51                             .format(pod=cpu_pooling_pod3['obj_name'], cpu=proc2_cpu))
52     finally:
53         common_utils.helm_delete("cpu-pooling")
54         common_utils.check_kubernetes_object(kube_object=cpu_pooling_pod3,
55                                              tester_function=common_utils.test_kubernetes_object_not_available,
56                                              timeout=60)
57
58
59 @robot_log
60 def get_cpu_core_of_processes(pod_name, command):
61     cpu_list = []
62     exact_pod_name = ex.execute_unix_command("kubectl get pod | grep {0} | awk '{{print $1}}'".format(pod_name))
63     bash_command = "ps | grep '{proc_name}' | grep -v grep | awk '{{print $1}}'".format(proc_name=command)
64     proc_ids = ex.execute_unix_command("kubectl exec {0} -- {1}".format(exact_pod_name, bash_command))
65     logger.info("PROC_IDS:" + proc_ids)
66     for id in proc_ids.splitlines():
67         bash_command = "cat /proc/{0}/stat | awk '{{print $39}}'".format(id)
68         command = "kubectl exec `kubectl get pod | grep {0} | awk '{{print $1}}'` -- {1}".format(pod_name, bash_command)
69         result = ex.execute_unix_command(command)
70         logger.info("CPU for pid " + id + "is: " + result)
71         cpu_list.append(int(result))
72     if len(cpu_list) == 1:
73         return cpu_list[0]
74     elif not cpu_list:
75         return ""
76     return cpu_list