|
| 1 | +package qemu |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/fs" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "regexp" |
| 12 | + "time" |
| 13 | + |
| 14 | + expect "github.com/google/goexpect" |
| 15 | + "github.com/linuxboot/contest/pkg/event" |
| 16 | + "github.com/linuxboot/contest/pkg/event/testevent" |
| 17 | + "github.com/linuxboot/contest/pkg/target" |
| 18 | + "github.com/linuxboot/contest/pkg/test" |
| 19 | + "github.com/linuxboot/contest/pkg/xcontext" |
| 20 | + "github.com/linuxboot/contest/plugins/teststeps" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + defaultTimeout = "10m" |
| 25 | + defaultNproc = "3" |
| 26 | + defaultMemory = "5000" |
| 27 | +) |
| 28 | + |
| 29 | +// Name of the plugin |
| 30 | +var Name = "Qemu" |
| 31 | + |
| 32 | +var Events = []event.Name{} |
| 33 | + |
| 34 | +type Qemu struct { |
| 35 | + executable *test.Param |
| 36 | + firmware *test.Param |
| 37 | + nproc *test.Param |
| 38 | + mem *test.Param |
| 39 | + image *test.Param |
| 40 | + logfile *test.Param |
| 41 | + timeout *test.Param |
| 42 | + steps []test.Param |
| 43 | +} |
| 44 | + |
| 45 | +// Needed for the Teststep interface. Returns a Teststep instance. |
| 46 | +func New() test.TestStep { |
| 47 | + return &Qemu{} |
| 48 | +} |
| 49 | + |
| 50 | +// Needed for the Teststep interface. Returns the Name, the New() Function and |
| 51 | +// the events the teststep can emit (which are no events). |
| 52 | +func Load() (string, test.TestStepFactory, []event.Name) { |
| 53 | + return Name, New, Events |
| 54 | +} |
| 55 | + |
| 56 | +// Name returns the name of the Step |
| 57 | +func (q Qemu) Name() string { |
| 58 | + return Name |
| 59 | +} |
| 60 | + |
| 61 | +// ValidateParameters validates the parameters that will be passed to the Run |
| 62 | +// and Resume methods of the test step. |
| 63 | +func (q *Qemu) ValidateParameters(_ xcontext.Context, params test.TestStepParameters) error { |
| 64 | + if q.executable = params.GetOne("executable"); q.executable.IsEmpty() { |
| 65 | + return fmt.Errorf("No Qemu executable given") |
| 66 | + } |
| 67 | + |
| 68 | + if q.firmware = params.GetOne("firmware"); q.firmware.IsEmpty() { |
| 69 | + return errors.New("Missing 'firmware' field in qemu parameters") |
| 70 | + } |
| 71 | + |
| 72 | + if q.nproc = params.GetOne("nproc"); q.nproc.IsEmpty() { |
| 73 | + q.nproc = test.NewParam(defaultNproc) |
| 74 | + } |
| 75 | + if q.mem = params.GetOne("mem"); q.mem.IsEmpty() { |
| 76 | + q.mem = test.NewParam(defaultMemory) |
| 77 | + } |
| 78 | + if q.timeout = params.GetOne("timeout"); q.timeout.IsEmpty() { |
| 79 | + q.timeout = test.NewParam(defaultTimeout) |
| 80 | + } |
| 81 | + |
| 82 | + q.image = params.GetOne("image") |
| 83 | + |
| 84 | + q.steps = params.Get("steps") |
| 85 | + |
| 86 | + q.logfile = params.GetOne("logfile") |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func (q *Qemu) validateAndPopulate(ctx xcontext.Context, params test.TestStepParameters) error { |
| 92 | + return q.ValidateParameters(ctx, params) |
| 93 | +} |
| 94 | + |
| 95 | +// Run starts the Qemu instance for each target and interacts with the qemu instance |
| 96 | +// through the expect and send steps. |
| 97 | +func (q *Qemu) Run(ctx xcontext.Context, |
| 98 | + ch test.TestStepChannels, |
| 99 | + ev testevent.Emitter, |
| 100 | + stepsVars test.StepsVariables, |
| 101 | + params test.TestStepParameters, |
| 102 | + resumeState json.RawMessage, |
| 103 | +) (json.RawMessage, error) { |
| 104 | + log := ctx.Logger() |
| 105 | + |
| 106 | + if err := q.validateAndPopulate(ctx, params); err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + f := func(ctx xcontext.Context, target *target.Target) error { |
| 110 | + targetTimeout, err := q.timeout.Expand(target, stepsVars) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + targetLogfile, err := q.logfile.Expand(target, stepsVars) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + targetImage, err := q.image.Expand(target, stepsVars) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + targetQemu, err := q.executable.Expand(target, stepsVars) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + // basic checks whether the executable is usable |
| 131 | + if abs := filepath.IsAbs(targetQemu); !abs { |
| 132 | + _, err := exec.LookPath(targetQemu) |
| 133 | + if err != nil { |
| 134 | + return fmt.Errorf("unable to find qemu executable in PATH: %w", err) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + targetFirmware, err := q.firmware.Expand(target, stepsVars) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + targetMem, err := q.mem.Expand(target, stepsVars) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + targetNproc, err := q.nproc.Expand(target, stepsVars) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + globalTimeout, err := time.ParseDuration(targetTimeout) |
| 154 | + if err != nil { |
| 155 | + return fmt.Errorf("Could not Parse %v as Timeout: %w", targetTimeout, err) |
| 156 | + } |
| 157 | + |
| 158 | + // no graphical output and no network access |
| 159 | + command := []string{targetQemu, "-nographic", "-nic", "none", "-bios", targetFirmware} |
| 160 | + qemuOpts := []string{"-m", targetMem, "-smp", targetNproc} |
| 161 | + |
| 162 | + command = append(command, qemuOpts...) |
| 163 | + if targetImage != "" { |
| 164 | + command = append(command, targetImage) |
| 165 | + } |
| 166 | + |
| 167 | + var logfile *os.File |
| 168 | + if targetLogfile != "" { |
| 169 | + |
| 170 | + logfile, err = os.Create(targetLogfile) |
| 171 | + if err != nil { |
| 172 | + return fmt.Errorf("Could not create Logfile: %w", err) |
| 173 | + } |
| 174 | + defer logfile.Close() |
| 175 | + } else { |
| 176 | + logfile, err = os.OpenFile("/dev/null", os.O_WRONLY, fs.ModeDevice) |
| 177 | + if err != nil { |
| 178 | + return fmt.Errorf("Could not redirect output to '/dev/null': %w", err) |
| 179 | + } |
| 180 | + defer logfile.Close() |
| 181 | + } |
| 182 | + |
| 183 | + gExpect, errchan, err := expect.SpawnWithArgs( |
| 184 | + command, |
| 185 | + globalTimeout, |
| 186 | + expect.Tee(logfile), |
| 187 | + expect.CheckDuration(time.Minute), |
| 188 | + expect.PartialMatch(false), |
| 189 | + expect.SendTimeout(globalTimeout), |
| 190 | + ) |
| 191 | + if err != nil { |
| 192 | + return fmt.Errorf("Could not start qemu: %w", err) |
| 193 | + } |
| 194 | + |
| 195 | + log.Infof("Started Qemu with command: %v", command) |
| 196 | + |
| 197 | + defer gExpect.Close() |
| 198 | + |
| 199 | + defer func() { |
| 200 | + select { |
| 201 | + case err = <-errchan: |
| 202 | + log.Errorf("Error from Qemu: %v", err) |
| 203 | + default: |
| 204 | + |
| 205 | + } |
| 206 | + }() |
| 207 | + |
| 208 | + // struct to capture expect and send strings from json. |
| 209 | + type expector struct { |
| 210 | + Expect string |
| 211 | + Send string |
| 212 | + Timeout string |
| 213 | + } |
| 214 | + |
| 215 | + // loop over all steps and expect/ send the given strings |
| 216 | + for _, interaction := range q.steps { |
| 217 | + |
| 218 | + dst := new(expector) |
| 219 | + |
| 220 | + jsString, err := interaction.Expand(target, stepsVars) |
| 221 | + if err != nil { |
| 222 | + return err |
| 223 | + } |
| 224 | + |
| 225 | + interactionParam := test.NewParam(jsString) |
| 226 | + js := interactionParam.JSON() |
| 227 | + |
| 228 | + if err := json.Unmarshal(js, dst); err != nil { |
| 229 | + return fmt.Errorf("Could not Unmarshal steps: %w", err) |
| 230 | + } |
| 231 | + |
| 232 | + // Expect and Send fields must not both be empty |
| 233 | + if dst.Expect+dst.Send == "" { |
| 234 | + return fmt.Errorf("%s is not a valid step statement", interaction.String()) |
| 235 | + } |
| 236 | + |
| 237 | + // process expect step |
| 238 | + if dst.Expect != "" { |
| 239 | + |
| 240 | + var timeout time.Duration |
| 241 | + if dst.Timeout == "" { |
| 242 | + timeout = globalTimeout |
| 243 | + } else { |
| 244 | + if timeout, err = time.ParseDuration(dst.Timeout); err != nil { |
| 245 | + return fmt.Errorf("Could not parse timeout '%s' for step: '%s'. %w", dst.Timeout, dst.Expect, err) |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + if _, _, err := gExpect.Expect(regexp.MustCompile(dst.Expect), timeout); err != nil { |
| 250 | + return fmt.Errorf("Error while expecting '%s': %w", dst.Expect, err) |
| 251 | + } |
| 252 | + |
| 253 | + log.Debugf("Completed expect step: '%v' with timeout: %v \n", dst.Expect, timeout.String()) |
| 254 | + |
| 255 | + } |
| 256 | + |
| 257 | + // process send step |
| 258 | + if dst.Send != "" { |
| 259 | + |
| 260 | + if err := gExpect.Send(dst.Send + "\n"); err != nil { |
| 261 | + return fmt.Errorf("Unable to send '%s': %w", dst.Send, err) |
| 262 | + } |
| 263 | + |
| 264 | + // notify the user if the timeout field is used incorrectly |
| 265 | + if dst.Expect == "" && dst.Timeout != "" { |
| 266 | + log.Warnf("The Timeout %v for send step: %v will be ignored.", dst.Timeout, dst.Send) |
| 267 | + } |
| 268 | + |
| 269 | + log.Debugf("Completed Send Step: '%v'", dst.Send) |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + log.Infof("Matching steps successful") |
| 274 | + return nil |
| 275 | + } |
| 276 | + return teststeps.ForEachTarget(Name, ctx, ch, f) |
| 277 | +} |
0 commit comments