[TC Fixed] Modify clean method in LAUNCH test
[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(30)
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     p = run_tc_array(module_name, tc_array)
184     f = len(tc_array) - p
185     r = round(((p / len(tc_array)) * 100), 2)
186     print(f"--- {module_name} TCT Result ---\nFAIL : [{f}] / PASS : [{p}] - [{r}%]\n")
187
188 # Uninstall the application and restore to original state
189 def clean():
190     cmd(f"uninstall org.tizen.example.Launcher_TC_LAUNCH_01.Tizen")
191     cmd(f"uninstall org.tizen.example.Launcher_TC_LAUNCH_02")
192     cmd(f"uninstall org.tizen.example.Launcher_TC_LAUNCH_03.Tizen")
193
194     for tc in tc_array:
195         if f"{tc.__name__}" == "TC_04":
196             cmd(f"shell mount -o remount,rw /")
197             cmd(f"push dotnet.loader.origin /usr/share/aul/dotnet.loader")
198             cmd(f"shell chsmack -a _ /usr/share/aul/dotnet.loader")
199             cmd(f"shell chmod 644 /usr/share/aul/dotnet.loader")
200             cmd(f"shell reboot -f")
201
202             subprocess.run((f"rm dotnet.loader").split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
203             subprocess.run((f"rm dotnet.loader.origin").split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
204
205 # Main entry point
206 def main():
207     parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter)
208     parser.add_argument("TC_NUMBER", type=str, nargs="*", help="Individual excution")
209     args = parser.parse_args()
210
211     global tc_array
212     if args.TC_NUMBER and "TC_" in args.TC_NUMBER[0]:
213         tc_array = []
214         for tc_num in args.TC_NUMBER:
215             if tc_num not in funcMap:
216                 print(f"There is no {tc_num} test.")
217                 exit(1)
218             else:
219                 tc_array.append(funcMap[tc_num])
220     else:
221         tc_array = [TC_01, TC_02, TC_03, TC_04]
222
223     global serial
224     if len(sys.argv) >= 2 and "TC_" not in sys.argv[1]:
225         serial = read_serial(sys.argv[1])
226     else:
227         serial = read_serial(None)
228
229     if serial is None:
230         print("No connected device(s).")
231         exit(1)
232
233     device = get_device_type()
234     print(f"=== Dotnet-Launcher [{device}] Test Case - ({module_name}) ===")
235
236     run()
237     clean()
238
239
240 funcMap = {
241 'TC_01': TC_01, 'TC_02': TC_02, 'TC_03': TC_03, 'TC_04': TC_04,
242 'LAUNCH_TC_01': TC_01, 'LAUNCH_TC_02': TC_02, 'LAUNCH_TC_03': TC_03, 'LAUNCH_TC_04': TC_04
243 }
244
245
246 if __name__ == "__main__":
247     try:
248         main()
249     except KeyboardInterrupt:
250         print("\nExit (Pressed Ctrl+C)")
251         exit(1)