8db4062ac1c9162979ad9a287b6047f34fd50cfd
[platform/core/dotnet/launcher.git] / tests / TCs / 7_LAUNCH / LAUNCH.py
1 #!/usr/bin/env python3
2 import os, subprocess, sys, argparse
3 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
4
5 from time import sleep
6 from Utils import *
7
8
9 module_name = "LAUNCH"
10
11 # The `Launcher_TC_LAUNCH_01` application should run in `candidate(dotnet-loader)` mode.
12 def TC_01():
13     sln_name = "Launcher_TC_LAUNCH_01.Tizen"
14
15     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
16     if tpk_path == None:
17         return f"FAIL : Get the tpk path for {sln_name}"
18
19     if "OK" not in app_install(f"{tpk_path}"):
20         return f"FAIL : Install the application for {tpk_path}"
21
22     pkg_id = "org.tizen.example.Launcher_TC_LAUNCH_01.Tizen"
23
24     root_path = get_root_path(f"{pkg_id}")
25     if root_path == "None":
26         return f"FAIL : Get the root path for {pkg_id}"
27
28     if "OK" not in prepare_candidate_process(f"dotnet-loader", f"{pkg_id}"):
29         return f"FAIL : Candidate process should have dotnet-loader"
30
31     pid = launch_and_get_pid(f"-s", f"{pkg_id}")
32     if 0 == pid:
33         return f"FAIL : Get the pid for {pkg_id}"
34
35     raw = cmd(f"shell cat /proc/{pid}/smaps | grep Launcher_TC_LAUNCH_01.Tizen")
36     if f"{root_path}/bin/Launcher_TC_LAUNCH_01.Tizen.dll" not in raw:
37         return "FAIL : The application is run as a candidate mode."
38
39     cmd(f"shell app_launcher -t {pkg_id}")
40
41     return "PASS"
42
43 # The `Launcher_TC_LAUNCH_01` application should run in `standalone(dotnet-launcher)` mode.
44 def TC_02():
45     sln_name = "Launcher_TC_LAUNCH_01.Tizen"
46
47     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
48     if tpk_path == None:
49         return f"FAIL : Get the tpk path for {sln_name}"
50
51     if "OK" not in app_install(f"{tpk_path}"):
52         return f"FAIL : Install the application for {tpk_path}"
53
54     pkg_id = "org.tizen.example.Launcher_TC_LAUNCH_01.Tizen"
55
56     root_path = get_root_path(f"{pkg_id}")
57     if root_path == "None":
58         return f"FAIL : Get the root path for {pkg_id}"
59
60     pid = launch_and_get_pid(f"-e", f"{pkg_id}")
61     if 0 == pid:
62         return f"FAIL : Get the pid for {pkg_id}"
63
64     raw = cmd(f"shell cat /proc/{pid}/smaps | grep Launcher_TC_LAUNCH_01.Tizen")
65     if f"{root_path}/bin/Launcher_TC_LAUNCH_01.Tizen.dll" not in raw:
66         return "FAIL : The application is run as a standalone mode"
67
68     cmd(f"shell app_launcher -t {pkg_id}")
69
70     return "PASS"
71
72 # The `Launcher_TC_LAUNCH_02` application should run in `candidate(dotnet-nui-loader)` mode.
73 def TC_03():
74     sln_name = "Launcher_TC_LAUNCH_02"
75
76     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
77     if tpk_path == None:
78         return f"FAIL : Get the tpk path for {sln_name}"
79
80     if "OK" not in app_install(f"{tpk_path}"):
81         return f"FAIL : Install the application for {tpk_path}"
82
83     pkg_id = "org.tizen.example.Launcher_TC_LAUNCH_02"
84
85     root_path = get_root_path(f"{pkg_id}")
86     if root_path == "None":
87         return f"FAIL : Get the root path for {pkg_id}"
88
89     if "OK" not in prepare_candidate_process(f"dotnet-nui-loader", f"{pkg_id}"):
90         return f"FAIL : Candidate process should have dotnet-nui-loader"
91
92     pid = launch_and_get_pid(f"-s", f"{pkg_id}")
93     if 0 == pid:
94         return f"FAIL : Get the pid for {pkg_id}"
95
96     raw = cmd(f"shell cat /proc/{pid}/smaps | grep Launcher_TC_LAUNCH_02")
97     if f"{root_path}/bin/Launcher_TC_LAUNCH_02.dll" not in raw:
98         return "FAIL : The application is run as a candidate mode"
99
100     cmd(f"shell app_launcher -t {pkg_id}")
101
102     return "PASS"
103
104 # The `Launcher_TC_LAUNCH_03` application should run in `hydra(dotnet-hydra-loader)` mode.
105 def TC_04():
106     sln_name = "Launcher_TC_LAUNCH_03.Tizen"
107
108     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
109     if tpk_path == None:
110         return f"FAIL : Get the tpk path for {sln_name}"
111
112     if "OK" not in app_install(f"{tpk_path}"):
113         return f"FAIL : Install the application for {tpk_path}"
114
115     pkg_id = "org.tizen.example.Launcher_TC_LAUNCH_03.Tizen"
116
117     root_path = get_root_path(f"{pkg_id}")
118     if root_path == "None":
119         return f"FAIL : Get the root path for {pkg_id}"
120
121     raw = cmd(f"pull /usr/share/aul/dotnet.loader dotnet.loader.origin")
122     if "1 file(s) pulled. 0 file(s) skipped." not in raw:
123         return "FAIL : Pull the dotnet.loader file from the target"
124
125     f1 = open("./dotnet.loader", "w")
126     f2 = open("./dotnet.loader.origin", "r")
127
128     for line in f2:
129         if line.startswith("EXE "):
130             f1.write(line.replace("EXE", "#EXE"))
131         elif line.startswith("#EXE "):
132             f1.write(line.replace("#EXE", "EXE"))
133         elif line.startswith("HYDRA "):
134             f1.write(line.replace("OFF", "ON"))
135         else:
136             f1.write(line)
137     f2.close()
138     f1.close()
139
140     raw = cmd(f"push dotnet.loader /usr/share/aul/")
141     if "1 file(s) pushed. 0 file(s) skipped." not in raw:
142         return "FAIL : Push the dotnet.loader file to the target"
143
144     cmd(f"shell chsmack -a _ /usr/share/aul/dotnet.loader")
145     cmd(f"shell chmod 644 /usr/share/aul/dotnet.loader")
146
147     cmd(f"shell reboot -f")
148     sleep(60)
149
150     if "." in serial:
151         subprocess.run((f"sdb connect {serial}").split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
152
153     cmd(f"root on")
154     cmd(f"shell mount -o remount,rw /")
155
156     if "OK" not in prepare_candidate_process(f"dotnet-loader", f"{pkg_id}"):
157         return f"FAIL : Candidate process should have dotnet-loader"
158
159     raw = cmd(f"shell ps -ef | grep dotnet-hydra-loader")
160     if "/usr/bin/dotnet-hydra-loader" not in raw:
161         return "FAIL : Candidate process should have dotnet-hydra-loader"
162
163     pid = launch_and_get_pid(f"-s", f"{pkg_id}")
164     if 0 == pid:
165         return f"FAIL : Get the pid for {pkg_id}"
166
167     raw = cmd(f"shell cat /proc/{pid}/smaps | grep Launcher_TC_LAUNCH_03.Tizen")
168     if f"{root_path}/bin/Launcher_TC_LAUNCH_03.Tizen.dll" not in raw:
169         return "FAIL : The application is run as a candidate mode"
170
171     cmd(f"shell app_launcher -t {pkg_id}")
172
173     return "PASS"
174
175 # Run the test
176 def run():
177     cmd(f"root on")
178     cmd(f"shell mount -o remount,rw /")
179
180     global tpk_list
181     tpk_list = search_tpk(f"{module_name}")
182
183     pn = run_tc_array(module_name, tc_array)
184     n = int(pn.split(":")[0])
185     f = int(pn.split(":")[1])
186     p = int(pn.split(":")[2])
187     r = 0.0
188     if (len(tc_array) - n) != 0:
189         r = round(((p / (len(tc_array) - n)) * 100), 2)
190     print(f"--- {module_name} TCT Result ---\nNONE : [{n}] / FAIL : [{f}] / PASS : [{p}] - [{r}%]\n")
191
192 # Uninstall the application and restore to original state
193 def clean():
194     cmd(f"uninstall org.tizen.example.Launcher_TC_LAUNCH_01.Tizen")
195     cmd(f"uninstall org.tizen.example.Launcher_TC_LAUNCH_02")
196     cmd(f"uninstall org.tizen.example.Launcher_TC_LAUNCH_03.Tizen")
197
198     for tc in tc_array:
199         if f"{tc.__name__}" == "TC_04":
200             cmd(f"shell mount -o remount,rw /")
201             cmd(f"push dotnet.loader.origin /usr/share/aul/dotnet.loader")
202             cmd(f"shell chsmack -a _ /usr/share/aul/dotnet.loader")
203             cmd(f"shell chmod 644 /usr/share/aul/dotnet.loader")
204             cmd(f"shell reboot -f")
205
206             subprocess.run((f"rm dotnet.loader").split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
207             subprocess.run((f"rm dotnet.loader.origin").split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
208
209 # Main entry point
210 def main():
211     parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter)
212     parser.add_argument("TC_NUMBER", type=str, nargs="*", help="Individual excution")
213     args = parser.parse_args()
214
215     global tc_array
216     if args.TC_NUMBER and "TC_" in args.TC_NUMBER[0]:
217         tc_array = []
218         for tc_num in args.TC_NUMBER:
219             if tc_num not in funcMap:
220                 print(f"There is no {tc_num} test.")
221                 exit(1)
222             else:
223                 tc_array.append(funcMap[tc_num])
224     else:
225         tc_array = [TC_01, TC_02, TC_03, TC_04]
226
227     global serial
228     if len(sys.argv) >= 2 and "TC_" not in sys.argv[1]:
229         serial = read_serial(sys.argv[1])
230     else:
231         serial = read_serial(None)
232
233     if serial is None:
234         print("No connected device(s).")
235         exit(1)
236
237     device = get_device_type()
238     print(f"=== Dotnet-Launcher [{device}] Test Case - ({module_name}) ===")
239
240     run()
241     clean()
242
243
244 funcMap = {
245 'TC_01': TC_01, 'TC_02': TC_02, 'TC_03': TC_03, 'TC_04': TC_04,
246 'LAUNCH_TC_01': TC_01, 'LAUNCH_TC_02': TC_02, 'LAUNCH_TC_03': TC_03, 'LAUNCH_TC_04': TC_04
247 }
248
249
250 if __name__ == "__main__":
251     try:
252         main()
253     except KeyboardInterrupt:
254         print("\nExit (Pressed Ctrl+C)")
255         exit(1)