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 TestGetCPULoad(t *testing.T) { cpustat := CPUStat{} cpustat.ReadInfo([]string{ "cpu", "4705", "356", "584", "3699", "23", "23", "0", "0", "0", "0"}) if cpustat.user != 4705 { t.Errorf("got %v want %v", cpustat.user, 4705) } } func TestCalculateCPUUsage(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) } }