Skip to content

Commit 95859c3

Browse files
Guest OS Imagescopybara-github
authored andcommitted
Improve readability and reproducibility of imageboot test suite error messages
PiperOrigin-RevId: 913867341
1 parent 46437f4 commit 95859c3

1 file changed

Lines changed: 33 additions & 33 deletions

File tree

test_suites/imageboot/image_boot_test.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package imageboot
1616

1717
import (
1818
"context"
19-
"errors"
2019
"fmt"
2120
"io/ioutil"
2221
"os"
@@ -61,6 +60,7 @@ const (
6160
secureBootFile = "/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c"
6261
setupModeFile = "/sys/firmware/efi/efivars/SetupMode-8be4df61-93ca-11d2-aa0d-00e098032b8c"
6362
defaultMaxBootTime = 20 // In seconds
63+
metadataCurlCmd = `curl -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/image`
6464
)
6565

6666
func mountEFIVarsCOS(t *testing.T) error {
@@ -73,14 +73,14 @@ func mountEFIVarsCOS(t *testing.T) error {
7373
ctx := utils.Context(t)
7474
image, err := utils.GetMetadata(utils.Context(t), "instance", "image")
7575
if err != nil {
76-
t.Fatalf("Couldn't get image from metadata %v", err)
76+
t.Fatalf("[FAILED] unable to get image from metadata with equivalent command %q: %v", metadataCurlCmd, err)
7777
}
7878

7979
if utils.IsCOS(image) {
8080
cmd := exec.CommandContext(ctx, "mount", "-t", "efivarfs", "efivarfs", "/sys/firmware/efi/efivars/")
8181
_, err := cmd.Output()
8282
if err != nil {
83-
return fmt.Errorf("Failed to mount EFI vars: %v", err)
83+
return fmt.Errorf("failed to mount EFI vars with command %q: %v", cmd.String(), err)
8484
}
8585
}
8686
return nil
@@ -95,13 +95,13 @@ func TestGuestReboot(t *testing.T) {
9595
if os.IsNotExist(err) {
9696
// first boot
9797
if err := os.MkdirAll(filepath.Dir(markerFile), 0755); err != nil {
98-
t.Fatalf("failed creating marker file directory: %v", err)
98+
t.Fatalf("[FAILED] unable to create marker file directory: %v", err)
9999
}
100100
if _, err := os.Create(markerFile); err != nil {
101-
t.Fatalf("failed creating marker file: %v", err)
101+
t.Fatalf("[FAILED] unable to create marker file: %v", err)
102102
}
103103
} else if err != nil {
104-
t.Fatalf("failed to stat marker file: %+v", err)
104+
t.Fatalf("[FAILED] unable to stat marker file: %+v", err)
105105
}
106106
// second boot
107107
t.Log("marker file exist signal the guest reboot successful")
@@ -112,10 +112,10 @@ func TestGuestRebootOnHost(t *testing.T) {
112112
if os.IsNotExist(err) {
113113
// first boot
114114
if err := os.MkdirAll(filepath.Dir(markerFile), 0755); err != nil {
115-
t.Fatalf("failed creating marker file directory: %v", err)
115+
t.Fatalf("[FAILED] unable to create marker file directory: %v", err)
116116
}
117117
if _, err := os.Create(markerFile); err != nil {
118-
t.Fatalf("failed creating marker file: %v", err)
118+
t.Fatalf("[FAILED] unable to create marker file: %v", err)
119119
}
120120
var cmd *exec.Cmd
121121
if utils.IsWindows() {
@@ -126,7 +126,7 @@ func TestGuestRebootOnHost(t *testing.T) {
126126
if err := cmd.Run(); err != nil {
127127
// check if error value is "signal: terminated" and if so, it means the reboot command was sent successfully
128128
if !(strings.Contains(err.Error(), "signal: terminated")) {
129-
t.Fatalf("failed to run reboot command: %v", err)
129+
t.Fatalf("[FAILED] error running reboot command %q: %v", cmd.String(), err)
130130
}
131131
}
132132
// sleep for a minute to ensure the guest has time to reboot
@@ -141,11 +141,11 @@ func TestGuestRebootOnHost(t *testing.T) {
141141
func TestGuestSecureBoot(t *testing.T) {
142142
if utils.IsWindows() {
143143
if err := testWindowsGuestSecureBoot(); err != nil {
144-
t.Fatalf("SecureBoot test failed with: %v", err)
144+
t.Fatalf("[FAILED] error running SecureBoot test: %v", err)
145145
}
146146
} else {
147147
if err := testLinuxGuestSecureBoot(t); err != nil {
148-
t.Fatalf("SecureBoot test failed with: %v", err)
148+
t.Fatalf("[FAILED] error running SecureBoot test: %v", err)
149149
}
150150
}
151151
}
@@ -156,22 +156,22 @@ func testLinuxGuestSecureBoot(t *testing.T) error {
156156
}
157157

158158
if _, err := os.Stat(secureBootFile); os.IsNotExist(err) {
159-
return errors.New("secureboot efi var is missing")
159+
return fmt.Errorf("failed to stat file %q: secureboot efi var is missing", secureBootFile)
160160
}
161161
data, err := ioutil.ReadFile(secureBootFile)
162162
if err != nil {
163-
return errors.New("failed reading secure boot file")
163+
return fmt.Errorf("failed reading secure boot file %q: %v", secureBootFile, err)
164164
}
165165
// https://www.kernel.org/doc/Documentation/ABI/stable/sysfs-firmware-efi-vars
166166
secureBootMode := data[len(data)-1]
167167
// https://uefi.org/specs/UEFI/2.9_A/32_Secure_Boot_and_Driver_Signing.html#firmware-os-key-exchange-creating-trust-relationships
168168
// If setup mode is not 0 secure boot isn't actually enabled because no PK is enrolled.
169169
if _, err = os.Stat(setupModeFile); os.IsNotExist(err) {
170-
return errors.New("setupmode efi var is missing")
170+
return fmt.Errorf("failed to stat file %q: setupmode efi var is missing", setupModeFile)
171171
}
172172
data, err = ioutil.ReadFile(setupModeFile)
173173
if err != nil {
174-
return errors.New("failed reading setup mode file")
174+
return fmt.Errorf("failed reading setup mode file %q: %v", setupModeFile, err)
175175
}
176176
setupMode := data[len(data)-1]
177177
if secureBootMode != 1 || setupMode != 0 {
@@ -185,13 +185,13 @@ func testWindowsGuestSecureBoot() error {
185185

186186
output, err := cmd.Output()
187187
if err != nil {
188-
return fmt.Errorf("failed to run SecureBoot command: %v", err)
188+
return fmt.Errorf("failed to run SecureBoot command %q: %v", cmd.String(), err)
189189
}
190190

191191
// The output will return a string that is either 'True' or 'False'
192192
// so we need to parse it and compare here.
193193
if trimmedOutput := strings.TrimSpace(string(output)); trimmedOutput != "True" {
194-
return errors.New("Secure boot is not enabled as expected")
194+
return fmt.Errorf("Secure boot is not enabled as expected, output of %q is %q", cmd.String(), trimmedOutput)
195195
}
196196

197197
return nil
@@ -201,15 +201,15 @@ func TestBootTime(t *testing.T) {
201201
ctx := utils.Context(t)
202202
image, err := utils.GetMetadata(ctx, "instance", "image")
203203
if err != nil {
204-
t.Errorf("utils.GetMetadata(ctx, instance, image) = err %v want nil", err)
204+
t.Errorf("[FAILED] unable to get image from metadata with equivalent command %q: %v", metadataCurlCmd, err)
205205
}
206206
startTime := findInstanceStartTime(ctx, t)
207207
essentialServiceStartTime := findEssentialServiceStartTime(ctx, t, image)
208208
bootTime := int(essentialServiceStartTime.Sub(startTime).Seconds())
209209
t.Logf("Instance start time: %s", startTime.Format(time.ANSIC))
210210
t.Logf("Service start time: %s", essentialServiceStartTime.Format(time.ANSIC))
211211
if bootTime < 0 {
212-
t.Fatalf("Invalid boot time, services started before boot.")
212+
t.Fatalf("[FAILED] Invalid boot time, services started before boot.")
213213
}
214214
maxBootTime := defaultMaxBootTime
215215
for _, threshold := range imageBootTimeThresholds {
@@ -219,7 +219,7 @@ func TestBootTime(t *testing.T) {
219219
}
220220
}
221221
if bootTime > maxBootTime {
222-
t.Errorf("Boot time of %d is greater than limit of %d", bootTime, maxBootTime)
222+
t.Errorf("[FAILED] Boot time of %d is greater than limit of %d", bootTime, maxBootTime)
223223
}
224224
if bootTime+10 < maxBootTime {
225225
t.Logf("Boot time of %d is more than 10 seconds below limit of %d. Consider lowering the limit if this is consistent.", bootTime, maxBootTime)
@@ -232,23 +232,23 @@ func findInstanceStartTime(ctx context.Context, t *testing.T) time.Time {
232232
cmd := "(Get-CimInstance Win32_OperatingSystem).LastBootUpTime"
233233
output, err := utils.RunPowershellCmd(cmd)
234234
if err != nil {
235-
t.Fatalf("utils.RunPowershellCmd(%s) = stderr: %v err: %v want err: nil", cmd, output.Stderr, err)
235+
t.Fatalf("[FAILED] utils.RunPowershellCmd(%s) = stderr: %v err: %v want err: nil", cmd, output.Stderr, err)
236236
}
237237
timestamp := strings.TrimSpace(output.Stdout)
238238
instanceStartTime, err := time.Parse(windowsTimeFormat, timestamp)
239239
if err != nil {
240-
t.Fatalf("time.Parse(windowsTimeFormat, %s) = %v want nil", timestamp, err)
240+
t.Fatalf("[FAILED] time.Parse(windowsTimeFormat, %s) = %v want nil", timestamp, err)
241241
}
242242
return instanceStartTime
243243
}
244244
uptimeData, err := os.ReadFile("/proc/uptime")
245245
if err != nil {
246-
t.Fatalf("os.ReadFile(/proc/uptime) = %v want nil", err)
246+
t.Fatalf("[FAILED] unable to read file %q: %v", "/proc/uptime", err)
247247
}
248248
fields := strings.Split(string(uptimeData), " ")
249249
uptime, err := strconv.ParseFloat(fields[0], 64)
250250
if err != nil {
251-
t.Fatalf("strconv.ParseFloat(%s, 64) = %v want nil", fields, err)
251+
t.Fatalf("[FAILED] strconv.ParseFloat(%s, 64) = %v want nil", fields, err)
252252
}
253253
instanceStartTime := time.Now().Add(time.Duration(-1*uptime) * time.Second)
254254
return instanceStartTime
@@ -279,7 +279,7 @@ func findEssentialServiceStartTime(ctx context.Context, t *testing.T, image stri
279279
}
280280
}
281281
if !foundOneRunningService && len(essentialServices) > 0 {
282-
t.Fatalf("Critical error: None of the essential services (%v) were found running for image %s.", essentialServices, image)
282+
t.Fatalf("[FAILED] Critical error: None of the essential services (%v) were found running for image %s.", essentialServices, image)
283283
}
284284
return latestStartTime
285285
}
@@ -301,33 +301,33 @@ func findServiceStartTime(ctx context.Context, t *testing.T, service string) tim
301301
}
302302
output, err := utils.RunPowershellCmd(fmt.Sprintf(`(Get-Service -Name "%s").Status`, service))
303303
if err != nil {
304-
t.Fatalf("utils.RunPowershellCmd((Get-Service -Name %q).Status) = stderr: %v err: %v want err: nil", service, output.Stderr, err)
304+
t.Fatalf("[FAILED] utils.RunPowershellCmd((Get-Service -Name %q).Status) = stderr: %v err: %v want err: nil", service, output.Stderr, err)
305305
}
306306
if strings.Contains(output.Stdout, "Running") {
307307
break
308308
}
309309
time.Sleep(time.Second)
310310
if ctx.Err() != nil {
311-
t.Fatalf("context expired before service %s was started: %v", service, ctx.Err())
311+
t.Fatalf("[FAILED] context expired before service %s was started: %v", service, ctx.Err())
312312
}
313313
}
314314
cmd := fmt.Sprintf(`(Get-Process -Id ((Get-CimInstance -ClassName Win32_Service | Where-Object {$_.Name -eq "%s"}).ProcessId)).StartTime`, service)
315315
output, err := utils.RunPowershellCmd(cmd)
316316
if err != nil {
317-
t.Fatalf("utils.RunPowershellCmd(%s) = stderr: %v err: %v want err: nil", cmd, output.Stderr, err)
317+
t.Fatalf("[FAILED] utils.RunPowershellCmd(%s) = stderr: %v err: %v want err: nil", cmd, output.Stderr, err)
318318
}
319319
timestamp := strings.TrimSpace(output.Stdout)
320320
serviceStartTime, err := time.Parse(windowsTimeFormat, timestamp)
321321
if err != nil {
322-
t.Fatalf("time.Parse(windowsServiceTimeFormat, %q) = %v want nil", timestamp, err)
322+
t.Fatalf("[FAILED] time.Parse(windowsServiceTimeFormat, %q) = %v want nil", timestamp, err)
323323
}
324324
return serviceStartTime
325325
}
326326
for {
327327
cmd := exec.CommandContext(ctx, "systemctl", "show", "--property=ActiveState,UnitFileState", service)
328328
output, err := cmd.Output()
329329
if err != nil {
330-
t.Fatalf("exec.CommandContext(ctx, %s) = %v want nil", cmd.String(), err)
330+
t.Fatalf("[FAILED] exec.CommandContext(ctx, %s) = %v want nil", cmd.String(), err)
331331
}
332332
if strings.Contains(string(output), "ActiveState=active") {
333333
break
@@ -338,18 +338,18 @@ func findServiceStartTime(ctx context.Context, t *testing.T, service string) tim
338338
}
339339
time.Sleep(time.Second)
340340
if ctx.Err() != nil {
341-
t.Fatalf("context expired before service %s was started: %v", service, ctx.Err())
341+
t.Fatalf("[FAILED] context expired before service %s was started: %v", service, ctx.Err())
342342
}
343343
}
344344
cmd := exec.CommandContext(ctx, "systemctl", "show", "--property=ActiveEnterTimestamp", service)
345345
output, err := cmd.Output()
346346
if err != nil {
347-
t.Fatalf("exec.CommandContext(ctx, %s) = %v want nil", cmd.String(), err)
347+
t.Fatalf("[FAILED] error while running command: %q: %v", cmd.String(), err)
348348
}
349349
timestamp := strings.TrimPrefix(strings.TrimSpace(string(output)), "ActiveEnterTimestamp=")
350350
serviceStartTime, err := time.Parse(systemdTimeFormat, timestamp)
351351
if err != nil {
352-
t.Fatalf("time.Parse(systemdTimeFormat, %q) = %v want nil", timestamp, err)
352+
t.Fatalf("[FAILED] time.Parse(systemdTimeFormat, %q) = %v want nil", timestamp, err)
353353
}
354354
return serviceStartTime
355355
}

0 commit comments

Comments
 (0)