2020-05-30 17:50:20 +02:00
|
|
|
package commands_test
|
2020-05-09 14:42:23 +02:00
|
|
|
|
|
|
|
import (
|
2020-06-29 13:27:21 +02:00
|
|
|
"errors"
|
2020-05-09 14:42:23 +02:00
|
|
|
"testing"
|
2020-06-29 13:27:21 +02:00
|
|
|
"time"
|
2020-05-30 17:50:20 +02:00
|
|
|
|
2020-05-31 16:31:18 +02:00
|
|
|
"github.com/hamburghammer/gstat/args"
|
2020-05-30 17:50:20 +02:00
|
|
|
"github.com/hamburghammer/gstat/commands"
|
2020-05-30 13:44:37 +02:00
|
|
|
)
|
2020-05-09 14:42:23 +02:00
|
|
|
|
2020-06-29 13:27:21 +02:00
|
|
|
func TestExec(t *testing.T) {
|
|
|
|
t.Run("should not run if no args are given", func(t *testing.T) {
|
|
|
|
args := args.Arguments{CPU: false}
|
2020-05-30 12:38:43 +02:00
|
|
|
|
2020-06-29 13:27:21 +02:00
|
|
|
got, err := commands.CPU{}.Exec(args)
|
2020-05-30 12:38:43 +02:00
|
|
|
|
2020-06-29 13:27:21 +02:00
|
|
|
assertNoError(err, t)
|
|
|
|
if len(got) != 0 {
|
|
|
|
t.Error("Got something even though it was not expected")
|
2020-05-30 12:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2020-06-29 13:27:21 +02:00
|
|
|
t.Run("should return one float", func(t *testing.T) {
|
|
|
|
args := args.Arguments{CPU: true}
|
2020-05-30 21:41:48 +02:00
|
|
|
|
2020-06-29 13:27:21 +02:00
|
|
|
readCPUStat := func(interval time.Duration, percpu bool) ([]float64, error) {
|
|
|
|
return []float64{float64(0)}, nil
|
|
|
|
}
|
2020-05-30 21:41:48 +02:00
|
|
|
|
2020-06-29 13:27:21 +02:00
|
|
|
got, err := commands.CPU{ReadCPUStat: readCPUStat}.Exec(args)
|
|
|
|
want := "{\"CPU\":0}"
|
2020-05-30 20:14:49 +02:00
|
|
|
|
2020-06-29 13:27:21 +02:00
|
|
|
assertNoError(err, t)
|
2020-05-30 20:14:49 +02:00
|
|
|
|
2020-06-29 13:27:21 +02:00
|
|
|
if string(got) != want {
|
|
|
|
t.Errorf("Want '%s' but got '%s'", want, string(got))
|
|
|
|
}
|
|
|
|
})
|
2020-05-30 20:14:49 +02:00
|
|
|
|
2020-06-29 13:27:21 +02:00
|
|
|
t.Run("should return wrapped error", func(t *testing.T) {
|
|
|
|
args := args.Arguments{CPU: true}
|
2020-05-30 20:14:49 +02:00
|
|
|
|
2020-06-29 13:27:21 +02:00
|
|
|
readCPUStat := func(interval time.Duration, percpu bool) ([]float64, error) {
|
|
|
|
return []float64{}, errors.New("Testing error")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, got := commands.CPU{ReadCPUStat: readCPUStat}.Exec(args)
|
|
|
|
want := "CPUReading failed because of: Testing error"
|
|
|
|
|
|
|
|
assertEqualString(got.Error(), want, t)
|
|
|
|
})
|
2020-05-30 20:14:49 +02:00
|
|
|
}
|