-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathtest.py
More file actions
220 lines (177 loc) · 6.65 KB
/
Copy pathtest.py
File metadata and controls
220 lines (177 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Functions for testing built images on a VM via SSH."""
import os
import time
from .shell_commands import execute_and_stream_cmd_output
BUILD_DIR = "/build/tmp-glibc/deploy/images/x64/"
SAFEMODE_IMAGE = "nilrt-safemode-rootfs-x64.tar.gz"
RUNMODE_IMAGE = "nilrt-base-system-image-x64.tar"
def install_and_test_image(ssh_connection):
"""
perform the following steps to test the built images on the target machine:
1. set safemode
2. reboot
3. format the machine, enable sshd, and reboot
4. current os version
5. install safemode image
6. verify os version
7. install runmode image
8. verify os version
"""
print("\nStarting OS test...")
# Step 1: Set safemode
print("Setting safemode...")
result = execute_and_stream_cmd_output(
f'ssh -o StrictHostKeyChecking=no {ssh_connection} "/etc/init.d/nisetbootmode force-safemode"'
)
if result[0] != 0:
return result
# Step 2: Reboot
print("Rebooting after setting safemode...")
reboot = reboot_machine(ssh_connection)
if reboot[0] != 0:
return reboot
time.sleep(90) # Wait for reboot
# Step 3: Format, enable sshd, and reboot
result = format_and_enable_ssh(ssh_connection)
if result[0] != 0:
return result
# Step 4: Get current OS version
print("Verifying current OS version...")
current_os = verify_os_version(ssh_connection)
if current_os[0] != 0:
return current_os
print(f"Current OS version:\n{current_os[1]}")
# Step 5: Install safemode image
print("Installing safemode image...")
safemode_install = test_safemode_installation(ssh_connection)
if safemode_install[0] != 0:
return safemode_install
# Step 6: Verify OS version after safemode install
print("Verifying OS version after safemode installation...")
os_after_safemode = verify_os_version(ssh_connection)
if os_after_safemode[0] != 0:
return os_after_safemode
print(f"OS version after safemode installation:\n{os_after_safemode[1]}")
# Step 7: Install runmode image
print("Installing runmode image...")
runmode_install = test_runmode_installation(ssh_connection)
if runmode_install[0] != 0:
return runmode_install
# Step 8: Verify OS version after runmode install
print("Verifying OS version after runmode installation...")
os_after_runmode = verify_os_version(ssh_connection)
if os_after_runmode[0] != 0:
return os_after_runmode
print(f"OS version after runmode installation:\n{os_after_runmode[1]}")
return os_after_runmode
def format_and_enable_ssh(ssh_connection):
"""
Format the machine, enable sshd, and reboot.
"""
print("Formatting the machine, enabling sshd, and rebooting...")
result = execute_and_stream_cmd_output(
f"ssh -o StrictHostKeyChecking=no {ssh_connection} \"nisystemformat -f -t ext4 -n all && "
"sed -i 's/^sshd.enabled=.*/sshd.enabled=True/' "
"/etc/natinst/share/ni-rt.ini && reboot\""
)
if result[0] != 0:
return result
time.sleep(90) # Wait for reboot
return (0, None)
def test_safemode_installation(ssh_connection):
"""
Copy, extract, and install the safemode image on the target machine.
"""
current_directory = os.getcwd()
copy = execute_and_stream_cmd_output(
f"scp {current_directory}{BUILD_DIR}{SAFEMODE_IMAGE} "
f"{ssh_connection}:/home/root"
)
if copy[0] != 0:
return copy
extract_and_install = extract_and_install_safemode_image(ssh_connection)
if extract_and_install[0] != 0:
return extract_and_install
reboot = reboot_machine(ssh_connection)
if reboot[0] != 0:
return reboot
time.sleep(90) # Wait for the machine to reboot
return (0, None)
def test_runmode_installation(ssh_connection):
"""
Copy, extract, and install the runmode image on the target machine.
"""
current_directory = os.getcwd()
copy = execute_and_stream_cmd_output(
f"scp -o StrictHostKeyChecking=no {current_directory}{BUILD_DIR}{RUNMODE_IMAGE} "
f"{ssh_connection}:/mnt/userfs"
)
if copy[0] != 0:
return copy
extract_and_install = extract_and_install_runmode_image(ssh_connection)
if extract_and_install[0] != 0:
return extract_and_install
reboot = reboot_machine(ssh_connection)
if reboot[0] != 0:
return reboot
time.sleep(300) # Wait for the machine to reboot
return (0, None)
def extract_and_install_safemode_image(ssh_connection):
"""
Extract and install the safemode image on the target machine.
"""
print("Extracting safemode image on target machine...")
return execute_and_stream_cmd_output(
f'ssh -o StrictHostKeyChecking=no {ssh_connection} "tar xf {SAFEMODE_IMAGE} -C /boot/.safe/"'
)
def extract_and_install_runmode_image(ssh_connection):
"""
Extract and install the runmode image on the target machine.
"""
print("Extracting runmode image on target machine...")
# Change to /mnt/userfs and extract the tar
first_cmd = execute_and_stream_cmd_output(
f'ssh -o StrictHostKeyChecking=no {ssh_connection} "cd /mnt/userfs && tar xf {RUNMODE_IMAGE}"'
)
if first_cmd[0] != 0:
return first_cmd
# Extract data.tar.gz and run postinst
second_cmd = execute_and_stream_cmd_output(
f'ssh -o StrictHostKeyChecking=no {ssh_connection} "cd /mnt/userfs && tar xf data.tar.gz -C '
f'/mnt/userfs && ./postinst"'
)
if second_cmd[0] != 0:
return second_cmd
# Remove the tar, data.tar.gz, and postinst files
cleanup_cmd = execute_and_stream_cmd_output(
f'ssh -o StrictHostKeyChecking=no {ssh_connection} "cd /mnt/userfs && rm -f {RUNMODE_IMAGE} '
f'data.tar.gz postinst"'
)
if cleanup_cmd[0] != 0:
return cleanup_cmd
return (0, None)
def reboot_machine(ssh_connection):
"""
Reboot the target machine.
"""
print("Rebooting the machine...")
return execute_and_stream_cmd_output(f'ssh -o StrictHostKeyChecking=no {ssh_connection} "reboot"')
def verify_os_version(ssh_connection):
"""
Verify the OS version on the target machine.
"""
print("Verifying OS version...")
return execute_and_stream_cmd_output(
f'ssh -o StrictHostKeyChecking=no {ssh_connection} "cat /etc/os-release"'
)
if __name__ == "__main__":
# Example usage:
SSH_CONNECTION = ""
status_code, message = install_and_test_image(SSH_CONNECTION)
if status_code == 0:
print("\n OS test completed successfully.")
else:
print(
f"\n OS test failed with status code {status_code}. "
f"Error: {message}"
)