0

I have a Go project which creates an endpoint, to consume system information like CPU temps and clock speed etc. (For debian based distros)

I am able to build and test the project successfully in my local environment.

In Github Actions the tests fail because the Shell script returns with error code 1 - Permission Denied.

The Go Function:

//GetTemp returns the current CPU temperature in C
func GetTemp() (temp float64, err error) {
    cmd := exec.Command("bash", "-c", "/usr/bin/vcgencmd measure_temp | egrep -o '[0-9]*\\.[0-9]*'")
    out, err := cmd.Output()
    if err != nil {
        return -1, err
    }
    res := strings.TrimSuffix(string(out), "\n")
    temp, _ = strconv.ParseFloat(res, 64)
    return temp, nil
}

Test for that function:

func TestTempRange(t *testing.T) {
    expectedMin, expectedMax := 0.0, 120.0
    actual, err := GetTemp()
    if err != nil {
        t.Fatalf("Error retrieving cpu temp\nError: %v", err)
    }

    if actual < expectedMin || actual > expectedMax {
        t.Fatalf("return temp outside of acceptable range.\nExpected temp within range [%v, %v]\nRecieved %v", expectedMin, expectedMax, actual)
    }
}

Action Build steps:

jobs:

  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go-version: [ '1.14', '1.15', '1.16.x' ]
        
    steps:
    - uses: actions/checkout@v3

    - name: Set up Go
      uses: actions/setup-go@v3
      with:
        go-version: ${{ matrix.go-version }}
        
    - name: Install dependencies
      run: go get .

    - name: Build
      run: go build -v ./...

    - name: Test with Go
      run: go test -json > TestResults-${{ matrix.go-version }}.json
      
    - name: Upload Go test results
      uses: actions/upload-artifact@v3
      with:
        name: Go-results-${{ matrix.go-version }}
        path: TestResults-${{ matrix.go-version }}.json

Output from Github Action:

Run go test `go list ./... | grep -v examples` -coverprofile=coverage.txt -covermode=atomic
?       github.com/rafaelzasas/rpiSystemMonitor [no test files]
--- FAIL: TestTempRange (0.02s)
    cpu_test.go:9: Error retrieving cpu temp
        Error: exit status 1
--- FAIL: TestFreqRange (0.03s)
    cpu_test.go:22: Error retrieving cpu freq
        Error: exit status 1
FAIL
coverage: 57.1% of statements
FAIL    github.com/rafaelzasas/rpiSystemMonitor/cpu 0.047s
ok      github.com/rafaelzasas/rpiSystemMonitor/mem 0.044s  coverage: 42.9% of statements
FAIL
Error: Process completed with exit code 1.
Rafael Zasas
  • 891
  • 8
  • 27
  • Isn't `vcgencmd` a raspberry-pi specific command? Assuming that's correct, you're not going to be able to use that in a github action. – larsks Oct 13 '22 at 01:06
  • I do feel quite silly now. If you leave that as an answer I will mark it as the correct solution. I managed to get around this by using my PI as a `self hosted` github actions runner. It is not the ideal solution since this package will only work for Raspberry PI's but I would like to avoid downloading extra packages or reading system files a lot. Please let me know if you have an alternate solution. – Rafael Zasas Oct 26 '22 at 17:00

0 Answers0