package gupta import ( "fmt" "testing" ) func TestHello(t *testing.T) { got := Hello() want := fmt.Sprintf("Welcome to gupta version %s", version) name := "welcome string" if got != want { t.Errorf("got %s %s want %s", name, 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 TestCPUStat(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" procstatLineNew := "cpu 4875 356 584 3779 23 23 0 0 0 0" cpustat := NewCPUStat(procstatLine, procstatLineNew) t.Run("Test that gupta hast Report", func(t *testing.T) { // test we can get some report struct from gupta report := GuptaReport{ LoadAvarage: 0.2, CPUUsage: &cpustat, MemoryUsage: &Memory{}, } got := report.CPUUsage.CPUUsage() want := float64(68.00) name := "CPULoad" if got != want { t.Errorf("got %s %.2f want %.2f", name, got, want) } }) t.Run("Test that CPUUsage calculates properly", func(t *testing.T) { cpustat = NewCPUStat("cpu 657046 84 232824 4025519 2874 0 40555 0 0 0", "cpu 657136 84 232952 4025695 2874 0 40555 0 0 0") got := fmt.Sprintf("%.2f", cpustat.CPUUsage()) want := "55.33" name := "CPUUsate" if got != want { t.Errorf("got %s %s want %s", name, got, want) } }) } func assert(t *testing.T, name string, got, want uint64) { if got != want { t.Errorf("got %s %v want %v", name, got, want) } }