File: memtotal_freebsd_test.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (49 lines) | stat: -rw-r--r-- 859 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package sysinfo

import (
	"errors"
	"os/exec"
	"regexp"
	"strconv"
	"testing"
)

var re = regexp.MustCompile(`hw\.physmem:\s*(\d+)`)

func freebsdSysctlMemoryBytes() (uint64, error) {
	out, err := exec.Command("/sbin/sysctl", "hw.physmem").Output()
	if err != nil {
		return 0, err
	}

	match := re.FindSubmatch(out)
	if match == nil {
		return 0, errors.New("memory size not found in sysctl output")
	}

	bts, err := strconv.ParseUint(string(match[1]), 10, 64)
	if err != nil {
		return 0, err
	}

	return bts, nil
}

func TestPhysicalMemoryBytes(t *testing.T) {
	mem, err := PhysicalMemoryBytes()
	if err != nil {
		t.Fatal(err)
	}

	mem2, err := freebsdSysctlMemoryBytes()
	if nil != err {
		t.Fatal(err)
	}

	if mem != mem2 {
		t.Error(mem, mem2)
	}
}