12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package gupta
- import (
- "fmt"
- "testing"
- )
- func TestHello(t *testing.T) {
- got := Hello()
- want := fmt.Sprintf("Welcome to gupta version %s", version)
- if got != want {
- t.Errorf("got %q want %q", got, want)
- }
- }
- func TestRunGupta(t *testing.T) {
- // test that we get CPU load
- got := make([]float64, 3)
- got[0], got[1], got[2] = Run(true, true, true, "", "")
- want := []float64{0.2, 0.2, 70.0}
- for i, v := range got {
- if v != want[i] {
- t.Errorf("got %v want %v", v, want[i])
- }
- }
- // here it makes sense to return some kind of data structure
- // with properties filled.
- // having a data structure it would be possible to format
- // it to json ...
- }
- func TestGuptaHasReport(t *testing.T) {
- // test we can get some report struct from gupta
- report := GuptaReport{
- cpuLoad: 0.2,
- cpuUsage: 0.2,
- memoryUsage: 70.0,
- }
- got := report.GetCPULoad()
- want := "0.20"
- if got != want {
- t.Errorf("got %v want %v", got, want)
- }
- }
- func TestGetCPUUsage(t *testing.T) {
- cpustat := CPUStat{}
- cpustat.ReadInfo([]string{
- "4705",
- "356",
- "584",
- "3699",
- "23",
- "23",
- "0",
- "0",
- "0",
- "0"})
- if cpustat.User != 4705 {
- t.Errorf("Got cpustat.User %v want %v", cpustat.User, 4705)
- }
- }
- func TestNewCPUUsage(t *testing.T) {
- // 0 1 2 3 4 5 6 7 8 9 10
- // cpu user nice system idle iowait irq softirq steal guest guest_nice
- procstatLine := "cpu 4705 356 584 3699 23 23 0 0 0 0"
- cpustat := NewCPUStat(procstatLine)
- if cpustat.User != 4705 {
- t.Errorf("got %v want %v", cpustat.User, 4705)
- }
- }
- func TestTotalCPUTime(t *testing.T) {
- procstatLine := "cpu 4705 356 584 3699 23 23 0 0 0 0"
- cpustat := NewCPUStat(procstatLine)
- got := cpustat.TotalTime()
- want := cpustat.User + cpustat.nice + cpustat.system + cpustat.idle + cpustat.iowait +
- cpustat.irq + cpustat.softirq + cpustat.steal
- if got != want {
- t.Errorf("got total time %v want %v", got, want)
- }
- }
|