Skip to content

Commit

Permalink
Add initial testing for shared
Browse files Browse the repository at this point in the history
  • Loading branch information
robertjndw committed Apr 30, 2024
1 parent 9a3d31f commit ab7db4b
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 6 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Upload Go test results

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
directory: [ './HadesAPI', './HadesScheduler', './HadesCloneContainer' ]

steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.22
- name: Install dependencies
run: cd ${{ matrix.directory }} && go mod download
- name: Test with Go
run: go test ./${{ matrix.directory }}/... -json > TestResults-${{ matrix.directory }}.json
- name: Upload Go test results
uses: actions/upload-artifact@v4
with:
name: Go-results-${{ matrix.directory }}
path: TestResults-${{ matrix.directory }}.json
2 changes: 1 addition & 1 deletion shared/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ require (
)

require (
github.com/stretchr/testify v1.8.4 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/sys v0.16.0 // indirect
)
2 changes: 2 additions & 0 deletions shared/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
40 changes: 40 additions & 0 deletions shared/utils/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package utils

import (
"strconv"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGigabyte(t *testing.T) {
got, err := ParseMemoryLimit("1G")
assert.Nil(t, err)
assert.Equal(t, int64(1073741824), got)

got, err = ParseMemoryLimit("2g")
assert.Nil(t, err)
assert.Equal(t, int64(2147483648), got)
}

func TestMegabyte(t *testing.T) {
got, err := ParseMemoryLimit("1M")
assert.Nil(t, err)
assert.Equal(t, int64(1048576), got)

got, err = ParseMemoryLimit("2m")
assert.Nil(t, err)
assert.Equal(t, int64(2097152), got)
}

func TestUnknownUnit(t *testing.T) {
_, err := ParseMemoryLimit("1T")
assert.NotNil(t, err)
assert.Equal(t, "unknown unit: T", err.Error())
}

func TestInvalidNumber(t *testing.T) {
_, err := ParseMemoryLimit("abc")
assert.NotNil(t, err)
assert.IsType(t, &strconv.NumError{}, err)
}
12 changes: 7 additions & 5 deletions shared/utils/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ func FindLimit(x, y int) int {
// Gets two memory limits and returns the smaller one as number of bytes
func FindMemoryLimit(x, y string) int {
// Check the global RAM Limit
var global_ram_limit int64
var first_ram_limit int64
if x != "" {
bytes, err := ParseMemoryLimit(x)
if err != nil {
log.WithError(err).Errorf("Failed to parse global RAM limit %s", x)
first_ram_limit = 0
} else {
global_ram_limit = bytes
first_ram_limit = bytes
}
}
var step_ram_limit int64
var second_ram_limit int64
if y != "" {
bytes, err := ParseMemoryLimit(y)
if err != nil {
log.WithError(err).Errorf("Failed to parse step RAM limit %s", y)
second_ram_limit = 0
} else {
step_ram_limit = bytes
second_ram_limit = bytes
}
}
return FindLimit(int(step_ram_limit), int(global_ram_limit))
return FindLimit(int(second_ram_limit), int(first_ram_limit))
}
33 changes: 33 additions & 0 deletions shared/utils/limit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFindMemoryLimit(t *testing.T) {
// Test case 1: x and y are empty
result := FindMemoryLimit("", "")
assert.Equal(t, 0, result)

// Test case 2: x is valid, y is empty
result = FindMemoryLimit("1G", "")
assert.Equal(t, 1073741824, result)

// Test case 3: x is empty, y is valid
result = FindMemoryLimit("", "2M")
assert.Equal(t, 2097152, result)

// Test case 4: x and y are valid
result = FindMemoryLimit("1G", "2M")
assert.Equal(t, 2097152, result)

// Test case 5: x is invalid, y is valid
result = FindMemoryLimit("abc", "2M")
assert.Equal(t, 2097152, result)

// Test case 6: x is valid, y is invalid
result = FindMemoryLimit("1G", "xyz")
assert.Equal(t, 1073741824, result)
}
28 changes: 28 additions & 0 deletions shared/utils/prio_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestPrioFromInt(t *testing.T) {
tests := []struct {
priority int
expected string
}{
{10, "critical"},
{5, "critical"},
{4, "high"},
{3, "normal"},
{2, "low"},
{1, "minimal"},
{0, "minimal"},
{-1, "minimal"},
}

for _, test := range tests {
got := PrioFromInt(test.priority)
assert.Equal(t, test.expected, got)
}
}

0 comments on commit ab7db4b

Please sign in to comment.