github.com/newrelic/go-agent@v3.26.0+incompatible/internal/sysinfo/memtotal_darwin_test.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package sysinfo 5 6 import ( 7 "errors" 8 "os/exec" 9 "regexp" 10 "strconv" 11 "testing" 12 ) 13 14 var re = regexp.MustCompile(`hw\.memsize:\s*(\d+)`) 15 16 func darwinSysctlMemoryBytes() (uint64, error) { 17 out, err := exec.Command("/usr/sbin/sysctl", "hw.memsize").Output() 18 if err != nil { 19 return 0, err 20 } 21 22 match := re.FindSubmatch(out) 23 if match == nil { 24 return 0, errors.New("memory size not found in sysctl output") 25 } 26 27 bts, err := strconv.ParseUint(string(match[1]), 10, 64) 28 if err != nil { 29 return 0, err 30 } 31 32 return bts, nil 33 } 34 35 func TestPhysicalMemoryBytes(t *testing.T) { 36 mem, err := PhysicalMemoryBytes() 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 mem2, err := darwinSysctlMemoryBytes() 42 if nil != err { 43 t.Fatal(err) 44 } 45 46 if mem != mem2 { 47 t.Error(mem, mem2) 48 } 49 }