Skip to content

Commit

Permalink
Add sysinfo unit test (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
thom-at-redhat authored Sep 6, 2023
1 parent 93d2e12 commit e25f8ef
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

Expand All @@ -23,12 +23,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup up python
uses: actions/setup-python@v2
uses: actions/setup-python@v4

- name: Install tox
run: pip install tox
Expand All @@ -44,16 +44,16 @@ jobs:
go-version: ["1.19", "1.20"]
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: |
~/.cache/go-build
Expand Down Expand Up @@ -95,14 +95,14 @@ jobs:
run: find /tmp/receptor-testing -name controlsock -delete

- name: Artifact receptor data
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
if: ${{ failure() }}
with:
name: test-logs
path: /tmp/receptor-testing

- name: Archive receptor binary
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: receptor
path: /usr/local/bin/receptor
Expand All @@ -115,12 +115,12 @@ jobs:
python-version: [3.8, 3.9]
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Download receptor
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: receptor
path: /usr/local/bin/
Expand All @@ -129,7 +129,7 @@ jobs:
run: sudo chmod a+x /usr/local/bin/receptor

- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -143,12 +143,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4

- name: Install python dependencies
run: pip install build
Expand Down
48 changes: 48 additions & 0 deletions pkg/utils/sysinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils_test

import (
"os/exec"
"runtime"
"strconv"
"strings"
"testing"

"github.com/ansible/receptor/pkg/utils"
)

func TestGetSysCPUCount(t *testing.T) {
got := utils.GetSysCPUCount()
if got <= 0 {
t.Errorf("Non-positive CPU count: %d\n", got)
}

if runtime.GOOS == "linux" {
commandOutput, _ := exec.Command("nproc").CombinedOutput()

commandOutputWithout := strings.TrimSpace(string(commandOutput))
want, _ := strconv.Atoi(commandOutputWithout)

if got != want {
t.Errorf("Expected CPU count: %d, got %d\n", want, got)
}
}
}

func TestGetSysMemoryMiB(t *testing.T) {
got := utils.GetSysMemoryMiB()
if got <= 0 {
t.Errorf("Non-positive Memory: %d\n", got)
}

if runtime.GOOS == "linux" {
commandOutput, _ := exec.Command("sed", "-n", "s/^MemTotal:[[:space:]]*\\([[:digit:]]*\\).*/\\1/p", "/proc/meminfo").CombinedOutput()

commandOutputWithout := strings.TrimSpace(string(commandOutput))
wantKb, _ := strconv.ParseUint(commandOutputWithout, 10, 64)

want := wantKb / 1024
if got != want {
t.Errorf("Expected Memory: %d, got %d\n", want, got)
}
}
}

0 comments on commit e25f8ef

Please sign in to comment.