github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/sysinfo/memtotal_test.go (about)

     1  package sysinfo
     2  
     3  import (
     4  	"os"
     5  	"regexp"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/lulzWill/go-agent/internal/crossagent"
    10  )
    11  
    12  func TestMemTotal(t *testing.T) {
    13  	var fileRe = regexp.MustCompile(`meminfo_([0-9]+)MB.txt$`)
    14  	var ignoreFile = regexp.MustCompile(`README\.md$`)
    15  
    16  	testCases, err := crossagent.ReadDir("proc_meminfo")
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	for _, testFile := range testCases {
    22  		if ignoreFile.MatchString(testFile) {
    23  			continue
    24  		}
    25  
    26  		matches := fileRe.FindStringSubmatch(testFile)
    27  
    28  		if matches == nil || len(matches) < 2 {
    29  			t.Error(testFile, matches)
    30  			continue
    31  		}
    32  
    33  		expect, err := strconv.ParseUint(matches[1], 10, 64)
    34  		if err != nil {
    35  			t.Error(err)
    36  			continue
    37  		}
    38  
    39  		input, err := os.Open(testFile)
    40  		if err != nil {
    41  			t.Error(err)
    42  			continue
    43  		}
    44  		bts, err := parseProcMeminfo(input)
    45  		input.Close()
    46  		mib := BytesToMebibytes(bts)
    47  		if err != nil {
    48  			t.Error(err)
    49  		} else if mib != expect {
    50  			t.Error(bts, expect)
    51  		}
    52  	}
    53  }