60eb9799ce8c9141e5e8396f74df5845164c76fe
[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         return "FAIL"
86     return "OK"
87
88 # Get the root path of the application
89 def get_root_path(pkg_id):
90     raw = cmd(f"shell pkginfo --pkg {pkg_id} | grep root_path")
91     lines = [l for l in raw.splitlines()]
92     if len(lines) == 0:
93         return "None"
94     return raw.split(": ")[1].strip()
95
96 # Application launch and get the pid
97 def launch_and_get_pid(mode, app_id):
98     raw = cmd(f"shell app_launcher {mode} {app_id}")
99     sleep(3)
100     if "successfully launched pid" not in raw:
101         return 0
102     return raw[raw.index("pid") + 6:raw.index("with") - 1]
103
104 # Get the device type
105 def get_device_type():
106     raw = cmd(f"shell cat /etc/config/model-config.xml | grep tizen.org/feature/profile")
107     return raw.split(">")[1].split("<")[0]
108
109 # Create the System.Private.CoreLib native image
110 def create_spc_ni():
111     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
112     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" not in raw:
113         raw = cmd(f"shell dotnettool --ni-dll {RUNTIME_DIR}{SPC_DLL}")
114         if "System.Private.CoreLib.ni.dll generated successfully." not in raw:
115             return "FAIL"
116
117     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
118     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" not in raw:
119         return "FAIL"
120     return "OK"
121
122 # Remove the platform native image
123 def remove_system_ni():
124     cmd(f"shell dotnettool --ni-reset-system")
125     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
126     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" in raw:
127         return "FAIL"
128     return "OK"
129
130 # Prepare the candidate process
131 def prepare_candidate_process(loader, pkg_id):
132     cmd(f"shell killall dotnet-launcher {loader}")
133     sleep(30)
134
135     raw = cmd(f"shell ps -ef | grep {loader}")
136     if f"/usr/bin/{loader}" not in raw:
137         cmd(f"shell app_launcher -s {pkg_id}")
138         sleep(30)
139         cmd(f"shell app_launcher -t {pkg_id}")
140
141     raw = cmd(f"shell ps -ef | grep {loader}")
142     if f"/usr/bin/{loader}" not in raw:
143         return "FAIL"
144
145     pid = raw.split()[1]
146     return f"OK : {pid}"
147
148 # Get the device arch
149 def get_device_arch():
150     raw = cmd("capability")
151     line = [l for l in raw.splitlines() if "cpu_arch" in l]
152     return line[0].split(":")[1]
153
154 # Check the library type
155 def check_library_arch(rootpath, library):
156     raw = cmd(f"pull {rootpath}/bin/{library} {library}")
157     if "1 file(s) pulled. 0 file(s) skipped." not in raw:
158         return "FAIL"
159
160     raw = subprocess.run((f"file ./{library}").split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
161     subprocess.run((f"rm ./{library}").split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
162
163     device_arch = get_device_arch()
164     if "armv7" == device_arch:
165         if "ARM" not in raw:
166             return "FAIL"
167     elif "x86" == device_arch:
168         if "Intel 80386" not in raw:
169             return "FAIL"
170     else:
171         return "FAIL"
172
173     return "OK"
174
175 # Check if the file or directory exists
176 def exist(path):
177     raw = cmd(f"shell find {path}")
178     if "No such file or directory" in raw:
179         return False
180     else:
181         return True
182
183 # Run the test array
184 def run_tc_array(module_name, tc_array):
185     p = 0
186     n = 0
187     for tc in tc_array:
188         print(f">{module_name}_{tc.__name__}...", end="\r")
189         ret = tc()
190         print(f">{module_name}_{tc.__name__} : {ret}")
191         if "PASS" == ret: p += 1
192         if "NONE" in ret: n += 1
193         sleep(3)
194     return str(p) + ":" + str(n)
195
196 def check_tc_support():
197     if "armv8" == get_device_arch():
198         return False
199     else:
200         return True