mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-30 04:34:27 +00:00
metrics: Add fio exec
This PR adds fio exec for the FIO benchmark. Signed-off-by: Gabriela Cervantes <gabriela.cervantes.tellez@intel.com>
This commit is contained in:
parent
ea198fddcc
commit
a45900324d
68
tests/metrics/storage/fio-k8s/pkg/exec/Exec.go
Normal file
68
tests/metrics/storage/fio-k8s/pkg/exec/Exec.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
// Copyright (c) 2021 Intel Corporation
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
package exec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
//logger interface for pkg
|
||||||
|
var log logger
|
||||||
|
|
||||||
|
type logger interface {
|
||||||
|
Infof(string, ...interface{})
|
||||||
|
Debugf(string, ...interface{})
|
||||||
|
Errorf(string, ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetLogger(l logger) {
|
||||||
|
log = l
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exec a command
|
||||||
|
// err != nil if command fails to execute
|
||||||
|
// output is a string with a combined stdout and stderr
|
||||||
|
func ExecCmd(c string, showInStdout bool) (stdout string, err error) {
|
||||||
|
if c == "" {
|
||||||
|
return "", errors.New("command is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("Exec: %s", c)
|
||||||
|
cmd := exec.Command("bash", "-o", "pipefail", "-c", c)
|
||||||
|
var stdBuffer bytes.Buffer
|
||||||
|
var writers []io.Writer
|
||||||
|
writers = append(writers, &stdBuffer)
|
||||||
|
if showInStdout {
|
||||||
|
writers = append(writers, os.Stdout)
|
||||||
|
}
|
||||||
|
mw := io.MultiWriter(writers...)
|
||||||
|
|
||||||
|
cmd.Stdout = mw
|
||||||
|
cmd.Stderr = mw
|
||||||
|
|
||||||
|
err = cmd.Run()
|
||||||
|
output := stdBuffer.String()
|
||||||
|
|
||||||
|
return stdBuffer.String(), errors.Wrap(err, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exec a command
|
||||||
|
// Send output to Stdout and Stderr
|
||||||
|
func ExecStdout(c string) error {
|
||||||
|
if c == "" {
|
||||||
|
return errors.New("command is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("Exec: %s", c)
|
||||||
|
cmd := exec.Command("bash", "-o", "pipefail", "-c", c)
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user