Let OnFailure handle abnormal VM termination

now that peg gracefully terminates the VM when `Destroy` is called.

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis 2023-02-14 16:14:16 +02:00
parent d390f77688
commit 0c236b6145
No known key found for this signature in database
GPG Key ID: 286DCAFD2C97DDE3
4 changed files with 31 additions and 36 deletions

2
go.mod
View File

@ -16,7 +16,7 @@ require (
github.com/onsi/ginkgo/v2 v2.8.1
github.com/onsi/gomega v1.26.0
github.com/pkg/errors v0.9.1
github.com/spectrocloud/peg v0.0.0-20230214084503-7d0e96a45ad3
github.com/spectrocloud/peg v0.0.0-20230214140930-4d6672f825b2
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.24.2
k8s.io/apimachinery v0.24.2

4
go.sum
View File

@ -700,8 +700,8 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spectrocloud/peg v0.0.0-20230214084503-7d0e96a45ad3 h1:dhGi7NDCKBEBNgyqhY20z9nV1icdRMi4L4l9Yg6XzJc=
github.com/spectrocloud/peg v0.0.0-20230214084503-7d0e96a45ad3/go.mod h1:L2fIdtZqbQEagjOOXwkwH3t7MjJUd7fbt52cLSQGDBg=
github.com/spectrocloud/peg v0.0.0-20230214140930-4d6672f825b2 h1:HPO7JRzIWhv9Fi36xOokcTJF/ylWSx1YVIun6tanTAk=
github.com/spectrocloud/peg v0.0.0-20230214140930-4d6672f825b2/go.mod h1:L2fIdtZqbQEagjOOXwkwH3t7MjJUd7fbt52cLSQGDBg=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=

View File

@ -1,7 +1,6 @@
package e2e_test
import (
"context"
"fmt"
"os"
"os/exec"
@ -23,36 +22,12 @@ var vm VM
var _ = Describe("local encrypted passphrase", func() {
var config string
var vmStillNeeded bool // When false, a stopped VM should stop execution
var ctx context.Context
BeforeEach(func() {
vmStillNeeded = true
RegisterFailHandler(printInstallationOutput)
ctx, vm = startVM()
_, vm = startVM()
fmt.Printf("\nvm.StateDir = %+v\n", vm.StateDir)
go func() {
defer GinkgoRecover()
<-ctx.Done()
if vmStillNeeded {
stdout, err := os.ReadFile(path.Join(vm.StateDir, "stdout"))
Expect(err).ToNot(HaveOccurred())
stderr, err := os.ReadFile(path.Join(vm.StateDir, "stderr"))
Expect(err).ToNot(HaveOccurred())
serialLog, err := os.ReadFile(path.Join(vm.StateDir, "serial.log"))
Expect(err).ToNot(HaveOccurred())
fmt.Printf("stdout: %s\n", stdout)
fmt.Printf("stderr: %s\n", stderr)
fmt.Printf("serial: %s\n", serialLog)
// Although we call `Fail`, ginkgo still waits for `EventuallyConnects`
// below to be done for some reason. Something to do with locks probably.
Fail("VM exited before the test was done")
}
}()
vm.EventuallyConnects(1200)
})
@ -72,7 +47,6 @@ var _ = Describe("local encrypted passphrase", func() {
})
AfterEach(func() {
vmStillNeeded = false // We are done. Don't fail when we exit the VM.
err := vm.Destroy(func(vm VM) {
// Stop TPM emulator
tpmPID, err := os.ReadFile(path.Join(vm.StateDir, "tpm", "pid"))

View File

@ -77,13 +77,34 @@ func startVM() (context.Context, VM) {
types.WithSSHUser(user()),
types.WithSSHPass(pass()),
types.OnFailure(func(p *process.Process) {
out, _ := os.ReadFile(p.StdoutPath())
err, _ := os.ReadFile(p.StderrPath())
status, _ := p.ExitCode()
defer GinkgoRecover()
// We are explicitly killing the qemu process. We don't treat that as an error
// but we just print the output just in case.
fmt.Printf("\nVM Aborted: %s %s Exit status: %s\n", out, err, status)
var stdout, stderr, serial, status string
if stdoutBytes, err := os.ReadFile(p.StdoutPath()); err != nil {
stdout = fmt.Sprintf("Error reading stdout file: %s\n", err)
} else {
stdout = string(stdoutBytes)
}
if stderrBytes, err := os.ReadFile(p.StderrPath()); err != nil {
stderr = fmt.Sprintf("Error reading stderr file: %s\n", err)
} else {
stderr = string(stderrBytes)
}
if status, err = p.ExitCode(); err != nil {
status = fmt.Sprintf("Error reading exit code file: %s\n", err)
}
if serialBytes, err := os.ReadFile(path.Join(p.StateDir(), "serial.log")); err != nil {
serial = fmt.Sprintf("Error reading serial log file: %s\n", err)
} else {
serial = string(serialBytes)
}
Fail(fmt.Sprintf("\nVM Aborted.\nstdout: %s\nstderr: %s\nserial: %s\nExit status: %s\n",
stdout, stderr, serial, status))
}),
types.WithStateDir(stateDir),
// Serial output to file: https://superuser.com/a/1412150