[TC Fixed] Delete the .tpk file used for the test
[platform/core/dotnet/launcher.git] / tests / TCs / Utils.py
1 #!/usr/bin/env python3
2 import os, subprocess, sys
3 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
4
5 from time import sleep
6 from pathlib import Path
7
8 # Defined paths
9 RUNTIME_DIR = "/usr/share/dotnet.tizen/netcoreapp/"
10 FRAMEWORK_DIR = "/usr/share/dotnet.tizen/framework/"
11 PRELOAD_DIR = "/usr/share/dotnet.tizen/preload/"
12 IBCDATA_DIR = "/usr/share/dotnet.tizen/ibcdata/"
13 DOTNET_DIR = "/opt/usr/dotnet/"
14 OWNER_DIR = "/home/owner/"
15 SPC_DLL = "System.Private.CoreLib.dll"
16
17 # Check the sdb connection status and get a device serial number
18 def read_serial(serial):
19     global serial_num
20
21     if serial is not None:
22         serial_num = serial
23         return serial_num
24
25     raw = subprocess.run("sdb devices".split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
26     lines = [l for l in raw.splitlines() if not l.startswith("* ")]
27     if len(lines) <= 1:
28         serial_num = None
29     elif len(lines) == 2:
30         serial_num = lines[1].split("   ")[0].split(":")[0].strip()
31     else:
32         serials = []
33         for idx in range(1, len(lines)):
34             serial = lines[idx].split(" ")[0].split(":")[0].replace("device", "").strip()
35             serials.append(serial)
36             print(f"[{idx}] {serial} - {lines[idx].split('      ')[2].strip()}")
37         choice = input(f"Select a device [1-{len(lines) - 1}]: ")
38         serial_num = serials[int(choice) - 1].strip() if choice.isdigit() else None
39     return serial_num
40
41 # Execute a command and return the output as a string.
42 def cmd(command):
43     return subprocess.run((f"sdb -s {serial_num} " + command).split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
44
45 # Search the generated .tpk
46 tpk_lists = []
47 def search_tpk(module, dirname=None):
48     if dirname == None:
49         if os.path.isdir(os.path.join(os.getcwd(), "../../Apps")):
50             dirname = "../../Apps"
51         elif os.path.isdir(os.path.join(os.getcwd(), "../Apps")):
52             dirname = "../Apps"
53         elif os.path.isdir(os.path.join(os.getcwd(), "./Apps")):
54             dirname = "./Apps"
55         else:
56             dirname = "./tests/Apps"
57
58     filenames = os.listdir(dirname)
59     for filename in filenames:
60         full_filename = os.path.join(dirname, filename)
61         if os.path.isdir(full_filename):
62             search_tpk(module, full_filename)
63         else:
64             ext = os.path.splitext(full_filename)[-1]
65             if ext == ".tpk" and f"{module}" in full_filename:
66                 tpk_lists.append(full_filename)
67     return tpk_lists
68
69 # Get the .tpk path
70 def get_tpk_path(tpk_list, sln_name):
71     for tpk in tpk_list:
72         if sln_name in tpk:
73             return tpk
74     return None
75
76 # Install the application
77 def app_install(tpk_path):
78     raw = cmd(f"push {tpk_path} {OWNER_DIR}")
79     if "1 file(s) pushed. 0 file(s) skipped." not in raw:
80         return "FAIL"
81
82     tpk_name = Path(tpk_path).name
83     raw = cmd(f"shell pkgcmd -i -t tpk -p {OWNER_DIR}{tpk_name}")
84     if "key[end] val[ok]" not in raw:
85         cmd(f"shell rm {OWNER_DIR}{tpk_name}")
86         return "FAIL"
87
88     cmd(f"shell rm {OWNER_DIR}{tpk_name}")
89     return "OK"
90
91 # Get the root path of the application
92 def get_root_path(pkg_id):
93     raw = cmd(f"shell pkginfo --pkg {pkg_id} | grep root_path")
94     lines = [l for l in raw.splitlines()]
95     if len(lines) == 0:
96         return "None"
97     return raw.split(": ")[1].strip()
98
99 # Application launch and get the pid
100 def launch_and_get_pid(mode, app_id):
101     raw = cmd(f"shell app_launcher {mode} {app_id}")
102     sleep(3)
103     if "successfully launched pid" not in raw:
104         return 0
105     return raw[raw.index("pid") + 6:raw.index("with") - 1]
106
107 # Get the device type
108 def get_device_type():
109     raw = cmd(f"shell cat /etc/config/model-config.xml | grep tizen.org/feature/profile")
110     return raw.split(">")[1].split("<")[0]
111
112 # Create the System.Private.CoreLib native image
113 def create_spc_ni():
114     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
115     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" not in raw:
116         raw = cmd(f"shell dotnettool --ni-dll {RUNTIME_DIR}{SPC_DLL}")
117         if "System.Private.CoreLib.ni.dll generated successfully." not in raw:
118             return "FAIL"
119
120     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
121     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" not in raw:
122         return "FAIL"
123     return "OK"
124
125 # Remove the platform native image
126 def remove_system_ni():
127     cmd(f"shell dotnettool --ni-reset-system")
128     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
129     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" in raw:
130         return "FAIL"
131     return "OK"
132
133 # Prepare the candidate process
134 def prepare_candidate_process(loader, pkg_id):
135     cmd(f"shell killall dotnet-launcher {loader}")
136     sleep(30)
137
138     raw = cmd(f"shell ps -ef | grep {loader}")
139     if f"/usr/bin/{loader}" not in raw:
140         cmd(f"shell app_launcher -s {pkg_id}")
141         sleep(30)
142         cmd(f"shell app_launcher -t {pkg_id}")
143
144     raw = cmd(f"shell ps -ef | grep {loader}")
145     if f"/usr/bin/{loader}" not in raw:
146         return "FAIL"
147
148     pid = raw.split()[1]
149     return f"OK : {pid}"
150
151 # Get the device arch
152 def get_device_arch():
153     raw = cmd("capability")
154     line = [l for l in raw.splitlines() if "cpu_arch" in l]
155     return line[0].split(":")[1]
156
157 # Check the library type
158 def check_library_arch(rootpath, library):
159     raw = cmd(f"pull {rootpath}/bin/{library} {library}")
160     if "1 file(s) pulled. 0 file(s) skipped." not in raw:
161         return "FAIL"
162
163     raw = subprocess.run((f"file ./{library}").split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
164     subprocess.run((f"rm ./{library}").split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
165
166     device_arch = get_device_arch()
167     if "armv7" == device_arch:
168         if "ARM" not in raw:
169             return "FAIL"
170     elif "x86" == device_arch:
171         if "Intel 80386" not in raw:
172             return "FAIL"
173     else:
174         return "FAIL"
175
176     return "OK"
177
178 # Check if the file or directory exists
179 def exist(path):
180     raw = cmd(f"shell find {path}")
181     if "No such file or directory" in raw:
182         return False
183     else:
184         return True
185
186 # Run the test array
187 def run_tc_array(module_name, tc_array):
188     p = 0
189     n = 0
190     for tc in tc_array:
191         print(f">{module_name}_{tc.__name__}...", end="\r")
192         ret = tc()
193         print(f">{module_name}_{tc.__name__} : {ret}")
194         if "PASS" == ret: p += 1
195         if "NONE" in ret: n += 1
196         sleep(3)
197     return str(p) + ":" + str(n)
198
199 def check_tc_support():
200     if "armv8" == get_device_arch():
201         return False
202     else:
203         return True