1
+ import logging
2
+ import time
3
+ import os
4
+ import re
5
+ import base64
6
+ import random
7
+ import string
8
+ import json
9
+ import threading
10
+
11
+ import aexpect
12
+
13
+ from distutils .util import strtobool
14
+ from avocado .utils import genio
15
+ from avocado .utils import path as avo_path
16
+ from avocado .utils import process
17
+ from avocado .core import exceptions
18
+ from aexpect .exceptions import ShellTimeoutError
19
+
20
+ from virttest import error_context
21
+ from virttest import guest_agent
22
+ from virttest import utils_misc
23
+ from virttest import utils_disk
24
+ from virttest import env_process
25
+ from virttest import utils_net
26
+ from virttest import data_dir
27
+ from virttest import storage
28
+ from virttest import qemu_migration
29
+ from virttest .utils_version import VersionInterval
30
+
31
+ from virttest .utils_windows import virtio_win
32
+ from provider .win_driver_installer_test import (uninstall_gagent ,
33
+ run_installer_with_interaction )
34
+
35
+ LOG_JOB = logging .getLogger ('avocado.test' )
36
+
37
+
38
+ class BaseVirtTest (object ):
39
+
40
+ def __init__ (self , test , params , env ):
41
+ self .test = test
42
+ self .params = params
43
+ self .env = env
44
+
45
+ def initialize (self , test , params , env ):
46
+ if test :
47
+ self .test = test
48
+ if params :
49
+ self .params = params
50
+ if env :
51
+ self .env = env
52
+ start_vm = self .params ["start_vm" ]
53
+ self .start_vm = start_vm
54
+ if self .start_vm == "yes" :
55
+ vm = self .env .get_vm (params ["main_vm" ])
56
+ vm .verify_alive ()
57
+ self .vm = vm
58
+
59
+ def setup (self , test , params , env ):
60
+ if test :
61
+ self .test = test
62
+ if params :
63
+ self .params = params
64
+ if env :
65
+ self .env = env
66
+
67
+ def run_once (self , test , params , env ):
68
+ if test :
69
+ self .test = test
70
+ if params :
71
+ self .params = params
72
+ if env :
73
+ self .env = env
74
+
75
+ def before_run_once (self , test , params , env ):
76
+ pass
77
+
78
+ def after_run_once (self , test , params , env ):
79
+ pass
80
+
81
+ def cleanup (self , test , params , env ):
82
+ pass
83
+
84
+ def execute (self , test , params , env ):
85
+ self .initialize (test , params , env )
86
+ self .setup (test , params , env )
87
+ try :
88
+ self .before_run_once (test , params , env )
89
+ self .run_once (test , params , env )
90
+ self .after_run_once (test , params , env )
91
+ finally :
92
+ self .cleanup (test , params , env )
93
+
94
+
95
+ class WinDebugToolTest (BaseVirtTest ):
96
+ def __init__ (self , test , params , env ):
97
+ super ().__init__ (test , params , env )
98
+ self ._open_session_list = []
99
+ self .vm = None
100
+ self .script_dir = None
101
+ self .script_name = params .get ("script_name" , "" ) # Assuming script is named CollectSystemInfo.ps1
102
+ self .script_path = params .get ("script_path" , "" )
103
+ self .tmp_dir = params .get ("test_tmp_dir" , "" )
104
+
105
+ def _get_session (self , params , vm ):
106
+ if not vm :
107
+ vm = self .vm
108
+ vm .verify_alive ()
109
+ timeout = int (params .get ("login_timeout" , 360 ))
110
+ session = vm .wait_for_login (timeout = timeout )
111
+ return session
112
+
113
+ def _cleanup_open_session (self ):
114
+ try :
115
+ for s in self ._open_session_list :
116
+ if s :
117
+ s .close ()
118
+ except Exception :
119
+ pass
120
+
121
+ def run_once (self , test , params , env ):
122
+ BaseVirtTest .run_once (self , test , params , env )
123
+ if self .start_vm == "yes" :
124
+ pass
125
+
126
+ def cleanup (self , test , params , env ):
127
+ self ._cleanup_open_session ()
128
+
129
+ @error_context .context_aware
130
+ def setup (self , test , params , env ):
131
+ BaseVirtTest .setup (self , test , params , env )
132
+ if self .start_vm == "yes" :
133
+ session = self ._get_session (params , self .vm )
134
+ self ._open_session_list .append (session )
135
+
136
+ error_context .context ("Check whether debug tool exists." , LOG_JOB .info )
137
+ cmd_get_debug_tool_script = params ['cmd_search_file_global' ] % self .script_name
138
+ script_path = str (session .cmd_output (cmd_get_debug_tool_script , timeout = 360 )).strip ()
139
+ print ("script_path: %s\n " % script_path )
140
+ if script_path :
141
+ self .script_path = script_path
142
+ self .script_dir = self .script_path .replace (self .script_name , "" ).rstrip ("\\ " )
143
+ self .script_path = "%TEMP%\CollectSystemInfo.ps1"
144
+ else :
145
+ test .error ("The tool script file CollectSystemInfo.ps1 was not found. Please check." )
146
+
147
+ error_context .context ("Create tmp work dir since testing "
148
+ "would create lots of dir and files." , LOG_JOB .info )
149
+ session .cmd_output (params ['cmd_create_dir' ] % self .tmp_dir )
150
+
151
+ def _check_generated_files (self , session , path , sensitive_data = False ):
152
+ output = str (session .cmd_output (f"dir /b { path } " )).strip ()
153
+ print (output )
154
+
155
+ target_files = (
156
+ self .params ["target_dump_files" ] if sensitive_data
157
+ else self .params ["target_files" ]
158
+ ).split (',' )
159
+
160
+ for target_file in target_files :
161
+ if target_file not in output :
162
+ self .test .error (f"{ target_file } is not included, please check it." )
163
+
164
+ def _get_path (self , output , session , sensitive_data = False ):
165
+ log_folder_path = re .search (r"Log folder path: (.+)" , output ).group (1 )
166
+ self ._check_generated_files (session , log_folder_path )
167
+ print (f"Log folder path: { log_folder_path } " )
168
+ log_zip_path = f"{ log_folder_path } .zip"
169
+ print (f"Log zip path: { log_zip_path } " )
170
+
171
+ if sensitive_data :
172
+ dump_folder_match = re .search (r"Dump folder path: (.+)" , output )
173
+ if dump_folder_match :
174
+ dump_folder_path = dump_folder_match .group (1 )
175
+ self ._check_generated_files (session , dump_folder_path , sensitive_data = True )
176
+ print (f"Dump folder path: { dump_folder_path } " )
177
+ dump_zip_path = f"{ dump_folder_path } .zip"
178
+ print (f"Dump zip path: { dump_zip_path } " )
179
+ return log_folder_path , log_zip_path , dump_folder_path , dump_zip_path
180
+
181
+ return log_folder_path , log_zip_path
182
+
183
+
184
+ class WinDebugToolTestBasicCheck (WinDebugToolTest ):
185
+ def __init__ (self , test , params , env ):
186
+ super ().__init__ (test , params , env )
187
+
188
+ @error_context .context_aware
189
+ def run_tool_scripts (self , session , return_zip_path = False ):
190
+ error_context .context ("Run Debug tool script to Query original info or value." , LOG_JOB .info )
191
+ # Running the PowerShell script on the VM
192
+ include_sensitive_data = bool (strtobool (self .params ['include_sensitive_data' ]))
193
+ print (include_sensitive_data )
194
+ sensitive_data_flag = "-IncludeSensitiveData" if include_sensitive_data else ""
195
+
196
+ # Execute the command on the VM
197
+ cmd_run_deg_tool = f"powershell.exe -ExecutionPolicy Bypass -File { self .script_path } { sensitive_data_flag } "
198
+ s , o = session .cmd_status_output (cmd_run_deg_tool , timeout = 360 )
199
+
200
+ paths = self ._get_path (o , session , sensitive_data = include_sensitive_data )
201
+ if include_sensitive_data :
202
+ log_folder_path , log_zip_path , dump_folder_path , dump_zip_path = paths
203
+ if not all (paths ):
204
+ test .fail ("Debug tool run failed, please check it." )
205
+ return paths if return_zip_path else (log_folder_path , dump_folder_path )
206
+ else :
207
+ log_folder_path , log_zip_path = paths
208
+ if not all (paths ):
209
+ test .fail ("Debug tool run failed, please check it." )
210
+ return paths if return_zip_path else log_folder_path
211
+
212
+ @error_context .context_aware
213
+ def windegtool_check_script_execution (self , test , params , env ):
214
+ if not self .vm :
215
+ self .vm = env .get_vm (params ["main_vm" ])
216
+ self .vm .verify_alive ()
217
+
218
+ session = self ._get_session (params , self .vm )
219
+ self ._open_session_list .append (session )
220
+ session .cmd ("cd %s" % self .tmp_dir )
221
+
222
+ log_folder_path , log_zip_path = self .run_tool_scripts (session , return_zip_path = True )
223
+ if not (log_zip_path and log_folder_path ):
224
+ test .fail ("debug tool run failed, please check it." )
225
+
226
+ def run_once (self , test , params , env ):
227
+ WinDebugToolTest .run_once (self , test , params , env )
228
+
229
+ windegtool_check_type = self .params ["windegtool_check_type" ]
230
+ chk_type = "windegtool_check_%s" % windegtool_check_type
231
+ if hasattr (self , chk_type ):
232
+ func = getattr (self , chk_type )
233
+ func (test , params , env )
234
+ else :
235
+ test .error ("Could not find matching test, check your config file" )
236
+
237
+
238
+ def run (test , params , env ):
239
+ """
240
+ Test CollectSystemInfo.ps1 tool, this case will:
241
+ 1) Start VM with virtio-win rpm package.
242
+ 2) Execute CollectSystemInfo.ps1 with&without param
243
+ '-IncludeSensitiveData'.
244
+ 3) Run some basic test for CollectSystemInfo.ps1.
245
+
246
+ :param test: kvm test object
247
+ :param params: Dictionary with the test parameters
248
+ :param env: Dictionary with test environmen.
249
+ """
250
+
251
+ collectinfotool_test = WinDebugToolTestBasicCheck (test , params , env )
252
+ collectinfotool_test .execute (test , params , env )
0 commit comments