mirror of
https://github.com/datarhei/core.git
synced 2025-10-06 00:17:07 +08:00
Add internal mock modules
This commit is contained in:
@@ -35,7 +35,7 @@ func TestNewInvalidBinary(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
binary, err := testhelper.BuildBinary("ffmpeg", "../../internal/testhelper")
|
||||
binary, err := testhelper.BuildBinary("ffmpeg")
|
||||
require.NoError(t, err, "Failed to build helper program")
|
||||
|
||||
skills, err := New(binary)
|
||||
@@ -326,7 +326,7 @@ func TestEqualEmptySkills(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEqualSkills(t *testing.T) {
|
||||
binary, err := testhelper.BuildBinary("ffmpeg", "../../internal/testhelper")
|
||||
binary, err := testhelper.BuildBinary("ffmpeg")
|
||||
require.NoError(t, err, "Failed to build helper program")
|
||||
|
||||
s1, err := New(binary)
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/datarhei/core/v16/http/api"
|
||||
"github.com/datarhei/core/v16/http/mock"
|
||||
"github.com/datarhei/core/v16/internal/mock/restream"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -14,7 +15,7 @@ import (
|
||||
func getDummyAboutRouter() (*echo.Echo, error) {
|
||||
router := mock.DummyEcho()
|
||||
|
||||
rs, err := mock.DummyRestreamer("../../mock")
|
||||
rs, err := restream.New(nil, nil, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/datarhei/core/v16/iam"
|
||||
"github.com/datarhei/core/v16/iam/identity"
|
||||
"github.com/datarhei/core/v16/iam/policy"
|
||||
"github.com/datarhei/core/v16/internal/mock/restream"
|
||||
"github.com/datarhei/core/v16/io/fs"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -27,7 +28,7 @@ type Response struct {
|
||||
}
|
||||
|
||||
func getDummyRestreamHandler() (*ProcessHandler, error) {
|
||||
rs, err := mock.DummyRestreamer("../../mock")
|
||||
rs, err := restream.New(nil, nil, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -8,10 +8,11 @@ import (
|
||||
"github.com/datarhei/core/v16/encoding/json"
|
||||
"github.com/datarhei/core/v16/http/api"
|
||||
"github.com/datarhei/core/v16/http/mock"
|
||||
mockrs "github.com/datarhei/core/v16/internal/mock/restream"
|
||||
"github.com/datarhei/core/v16/restream"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func getDummyWidgetHandler(rs restream.Restreamer) (*WidgetHandler, error) {
|
||||
@@ -37,7 +38,7 @@ func getDummyWidgetRouter(rs restream.Restreamer) (*echo.Echo, error) {
|
||||
}
|
||||
|
||||
func TestWidget(t *testing.T) {
|
||||
rs, err := mock.DummyRestreamer("../../mock")
|
||||
rs, err := mockrs.New(nil, nil, nil, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
router, err := getDummyWidgetRouter(rs)
|
||||
|
@@ -2,25 +2,16 @@ package mock
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/datarhei/core/v16/encoding/json"
|
||||
"github.com/datarhei/core/v16/ffmpeg"
|
||||
"github.com/datarhei/core/v16/http/api"
|
||||
"github.com/datarhei/core/v16/http/errorhandler"
|
||||
"github.com/datarhei/core/v16/http/validator"
|
||||
"github.com/datarhei/core/v16/internal/testhelper"
|
||||
"github.com/datarhei/core/v16/io/fs"
|
||||
"github.com/datarhei/core/v16/resources"
|
||||
"github.com/datarhei/core/v16/resources/psutil"
|
||||
"github.com/datarhei/core/v16/restream"
|
||||
jsonstore "github.com/datarhei/core/v16/restream/store/json"
|
||||
|
||||
"github.com/invopop/jsonschema"
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -28,58 +19,6 @@ import (
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
)
|
||||
|
||||
func DummyRestreamer(pathPrefix string) (restream.Restreamer, error) {
|
||||
binary, err := testhelper.BuildBinary("ffmpeg", filepath.Join(pathPrefix, "../../internal/testhelper"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to build helper program: %w", err)
|
||||
}
|
||||
|
||||
memfs, err := fs.NewMemFilesystem(fs.MemConfig{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create memory filesystem: %w", err)
|
||||
}
|
||||
|
||||
store, err := jsonstore.New(jsonstore.Config{
|
||||
Filesystem: memfs,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
psutil, err := psutil.New("", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resources, err := resources.New(resources.Config{
|
||||
PSUtil: psutil,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ffmpeg, err := ffmpeg.New(ffmpeg.Config{
|
||||
Binary: binary,
|
||||
MaxLogLines: 100,
|
||||
LogHistoryLength: 3,
|
||||
Resource: resources,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rs, err := restream.New(restream.Config{
|
||||
Store: store,
|
||||
FFmpeg: ffmpeg,
|
||||
Filesystems: []fs.Filesystem{memfs},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
func DummyEcho() *echo.Echo {
|
||||
router := echo.New()
|
||||
router.HideBanner = true
|
||||
|
122
internal/mock/psutil/psutil.go
Normal file
122
internal/mock/psutil/psutil.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package psutil
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/datarhei/core/v16/resources/psutil"
|
||||
)
|
||||
|
||||
type MockPSUtil struct {
|
||||
Lock sync.Mutex
|
||||
|
||||
CPUInfo psutil.CPUInfo
|
||||
MemInfo psutil.MemoryInfo
|
||||
GPUInfo []psutil.GPUInfo
|
||||
}
|
||||
|
||||
func New(ngpu int) *MockPSUtil {
|
||||
u := &MockPSUtil{
|
||||
CPUInfo: psutil.CPUInfo{
|
||||
System: 10,
|
||||
User: 50,
|
||||
Idle: 35,
|
||||
Other: 5,
|
||||
},
|
||||
MemInfo: psutil.MemoryInfo{
|
||||
Total: 200,
|
||||
Available: 40,
|
||||
Used: 160,
|
||||
},
|
||||
}
|
||||
|
||||
for i := 0; i < ngpu; i++ {
|
||||
u.GPUInfo = append(u.GPUInfo, psutil.GPUInfo{
|
||||
Index: i,
|
||||
Name: "L4",
|
||||
MemoryTotal: 24 * 1024 * 1024 * 1024,
|
||||
MemoryUsed: uint64(12+i) * 1024 * 1024 * 1024,
|
||||
Usage: 50 - float64((i+1)*5),
|
||||
Encoder: 50 - float64((i+1)*10),
|
||||
Decoder: 50 - float64((i+1)*3),
|
||||
})
|
||||
}
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *MockPSUtil) Start() {}
|
||||
func (u *MockPSUtil) Cancel() {}
|
||||
|
||||
func (u *MockPSUtil) CPUCounts() (float64, error) {
|
||||
return 2, nil
|
||||
}
|
||||
|
||||
func (u *MockPSUtil) CPU() (*psutil.CPUInfo, error) {
|
||||
u.Lock.Lock()
|
||||
defer u.Lock.Unlock()
|
||||
|
||||
cpu := u.CPUInfo
|
||||
|
||||
return &cpu, nil
|
||||
}
|
||||
|
||||
func (u *MockPSUtil) Disk(path string) (*psutil.DiskInfo, error) {
|
||||
return &psutil.DiskInfo{}, nil
|
||||
}
|
||||
|
||||
func (u *MockPSUtil) Memory() (*psutil.MemoryInfo, error) {
|
||||
u.Lock.Lock()
|
||||
defer u.Lock.Unlock()
|
||||
|
||||
mem := u.MemInfo
|
||||
|
||||
return &mem, nil
|
||||
}
|
||||
|
||||
func (u *MockPSUtil) Network() ([]psutil.NetworkInfo, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (u *MockPSUtil) GPU() ([]psutil.GPUInfo, error) {
|
||||
u.Lock.Lock()
|
||||
defer u.Lock.Unlock()
|
||||
|
||||
gpu := []psutil.GPUInfo{}
|
||||
|
||||
gpu = append(gpu, u.GPUInfo...)
|
||||
|
||||
return gpu, nil
|
||||
}
|
||||
|
||||
func (u *MockPSUtil) Process(pid int32) (psutil.Process, error) {
|
||||
return &mockPSUtilProcess{}, nil
|
||||
}
|
||||
|
||||
type mockPSUtilProcess struct{}
|
||||
|
||||
func (p *mockPSUtilProcess) CPU() (*psutil.CPUInfo, error) {
|
||||
s := &psutil.CPUInfo{
|
||||
System: 1,
|
||||
User: 2,
|
||||
Idle: 0,
|
||||
Other: 3,
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (p *mockPSUtilProcess) Memory() (uint64, error) { return 42, nil }
|
||||
func (p *mockPSUtilProcess) GPU() (*psutil.GPUInfo, error) {
|
||||
return &psutil.GPUInfo{
|
||||
Index: 0,
|
||||
Name: "L4",
|
||||
MemoryTotal: 128,
|
||||
MemoryUsed: 42,
|
||||
Usage: 5,
|
||||
Encoder: 9,
|
||||
Decoder: 7,
|
||||
}, nil
|
||||
}
|
||||
func (p *mockPSUtilProcess) Cancel() {}
|
||||
func (p *mockPSUtilProcess) Suspend() error { return nil }
|
||||
func (p *mockPSUtilProcess) Resume() error { return nil }
|
14
internal/mock/resources/resources.go
Normal file
14
internal/mock/resources/resources.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"github.com/datarhei/core/v16/internal/mock/psutil"
|
||||
"github.com/datarhei/core/v16/resources"
|
||||
)
|
||||
|
||||
func New() resources.Resources {
|
||||
res, _ := resources.New(resources.Config{
|
||||
PSUtil: psutil.New(1),
|
||||
})
|
||||
|
||||
return res
|
||||
}
|
99
internal/mock/restream/restream.go
Normal file
99
internal/mock/restream/restream.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package restream
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/datarhei/core/v16/ffmpeg"
|
||||
"github.com/datarhei/core/v16/iam"
|
||||
iamidentity "github.com/datarhei/core/v16/iam/identity"
|
||||
"github.com/datarhei/core/v16/iam/policy"
|
||||
"github.com/datarhei/core/v16/internal/mock/resources"
|
||||
"github.com/datarhei/core/v16/internal/testhelper"
|
||||
"github.com/datarhei/core/v16/io/fs"
|
||||
"github.com/datarhei/core/v16/net"
|
||||
"github.com/datarhei/core/v16/restream"
|
||||
"github.com/datarhei/core/v16/restream/replace"
|
||||
"github.com/datarhei/core/v16/restream/rewrite"
|
||||
jsonstore "github.com/datarhei/core/v16/restream/store/json"
|
||||
)
|
||||
|
||||
func New(portrange net.Portranger, validatorIn, validatorOut ffmpeg.Validator, replacer replace.Replacer) (restream.Restreamer, error) {
|
||||
binary, err := testhelper.BuildBinary("ffmpeg")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to build helper program: %w", err)
|
||||
}
|
||||
|
||||
resources := resources.New()
|
||||
|
||||
ffmpeg, err := ffmpeg.New(ffmpeg.Config{
|
||||
Binary: binary,
|
||||
LogHistoryLength: 3,
|
||||
MaxLogLines: 100,
|
||||
Portrange: portrange,
|
||||
ValidatorInput: validatorIn,
|
||||
ValidatorOutput: validatorOut,
|
||||
Resource: resources,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
memfs, err := fs.NewMemFilesystem(fs.MemConfig{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
store, err := jsonstore.New(jsonstore.Config{
|
||||
Filesystem: memfs,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
policyAdapter, err := policy.NewJSONAdapter(memfs, "./policy.json", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
identityAdapter, err := iamidentity.NewJSONAdapter(memfs, "./users.json", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
iam, err := iam.New(iam.Config{
|
||||
PolicyAdapter: policyAdapter,
|
||||
IdentityAdapter: identityAdapter,
|
||||
Superuser: iamidentity.User{
|
||||
Name: "foobar",
|
||||
},
|
||||
JWTRealm: "",
|
||||
JWTSecret: "",
|
||||
Logger: nil,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
iam.AddPolicy("$anon", "$none", []string{"process"}, "*", []string{"CREATE", "GET", "DELETE", "UPDATE", "COMMAND", "PROBE", "METADATA", "PLAYOUT"})
|
||||
|
||||
rewriter, err := rewrite.New(rewrite.Config{
|
||||
IAM: iam,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rs, err := restream.New(restream.Config{
|
||||
Store: store,
|
||||
FFmpeg: ffmpeg,
|
||||
Replace: replacer,
|
||||
Filesystems: []fs.Filesystem{memfs},
|
||||
Rewrite: rewriter,
|
||||
Resources: resources,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rs, nil
|
||||
}
|
@@ -4,10 +4,12 @@ import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func BuildBinary(name, pathprefix string) (string, error) {
|
||||
dir := filepath.Join(pathprefix, name)
|
||||
func BuildBinary(name string) (string, error) {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
dir := filepath.Join(filepath.Dir(filename), name)
|
||||
aout := filepath.Join(dir, name)
|
||||
|
||||
err := exec.Command("go", "build", "-o", aout, dir).Run()
|
||||
|
@@ -235,7 +235,7 @@ func TestProcessFailed(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFFmpegWaitStop(t *testing.T) {
|
||||
binary, err := testhelper.BuildBinary("sigintwait", "../internal/testhelper")
|
||||
binary, err := testhelper.BuildBinary("sigintwait")
|
||||
require.NoError(t, err, "Failed to build helper program")
|
||||
|
||||
p, _ := New(Config{
|
||||
@@ -266,7 +266,7 @@ func TestFFmpegWaitStop(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFFmpegKill(t *testing.T) {
|
||||
binary, err := testhelper.BuildBinary("sigint", "../internal/testhelper")
|
||||
binary, err := testhelper.BuildBinary("sigint")
|
||||
require.NoError(t, err, "Failed to build helper program")
|
||||
|
||||
p, _ := New(Config{
|
||||
@@ -292,7 +292,7 @@ func TestFFmpegKill(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProcessForceKill(t *testing.T) {
|
||||
binary, err := testhelper.BuildBinary("ignoresigint", "../internal/testhelper")
|
||||
binary, err := testhelper.BuildBinary("ignoresigint")
|
||||
require.NoError(t, err, "Failed to build helper program")
|
||||
|
||||
p, _ := New(Config{
|
||||
@@ -326,7 +326,7 @@ func TestProcessForceKill(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProcessDuration(t *testing.T) {
|
||||
binary, err := testhelper.BuildBinary("sigint", "../internal/testhelper")
|
||||
binary, err := testhelper.BuildBinary("sigint")
|
||||
require.NoError(t, err, "Failed to build helper program")
|
||||
|
||||
p, err := New(Config{
|
||||
|
@@ -280,7 +280,7 @@ func TestWriterProcess(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNvidiaGPUCount(t *testing.T) {
|
||||
binary, err := testhelper.BuildBinary("nvidia-smi", "../../../../internal/testhelper")
|
||||
binary, err := testhelper.BuildBinary("nvidia-smi")
|
||||
require.NoError(t, err, "Failed to build helper program")
|
||||
|
||||
nv := New(binary)
|
||||
@@ -299,7 +299,7 @@ func TestNvidiaGPUCount(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNvidiaGPUStats(t *testing.T) {
|
||||
binary, err := testhelper.BuildBinary("nvidia-smi", "../../../../internal/testhelper")
|
||||
binary, err := testhelper.BuildBinary("nvidia-smi")
|
||||
require.NoError(t, err, "Failed to build helper program")
|
||||
|
||||
nv := New(binary)
|
||||
@@ -400,7 +400,7 @@ func TestNvidiaGPUStats(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNvidiaGPUProcess(t *testing.T) {
|
||||
binary, err := testhelper.BuildBinary("nvidia-smi", "../../../../internal/testhelper")
|
||||
binary, err := testhelper.BuildBinary("nvidia-smi")
|
||||
require.NoError(t, err, "Failed to build helper program")
|
||||
|
||||
nv := New(binary)
|
||||
|
@@ -6,129 +6,14 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/datarhei/core/v16/resources/psutil"
|
||||
"github.com/datarhei/core/v16/internal/mock/psutil"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type mockUtil struct {
|
||||
lock sync.Mutex
|
||||
|
||||
cpu psutil.CPUInfo
|
||||
mem psutil.MemoryInfo
|
||||
gpu []psutil.GPUInfo
|
||||
}
|
||||
|
||||
func newMockUtil(ngpu int) *mockUtil {
|
||||
u := &mockUtil{
|
||||
cpu: psutil.CPUInfo{
|
||||
System: 10,
|
||||
User: 50,
|
||||
Idle: 35,
|
||||
Other: 5,
|
||||
},
|
||||
mem: psutil.MemoryInfo{
|
||||
Total: 200,
|
||||
Available: 40,
|
||||
Used: 160,
|
||||
},
|
||||
}
|
||||
|
||||
for i := 0; i < ngpu; i++ {
|
||||
u.gpu = append(u.gpu, psutil.GPUInfo{
|
||||
Index: i,
|
||||
Name: "L4",
|
||||
MemoryTotal: 24 * 1024 * 1024 * 1024,
|
||||
MemoryUsed: uint64(12+i) * 1024 * 1024 * 1024,
|
||||
Usage: 50 - float64((i+1)*5),
|
||||
Encoder: 50 - float64((i+1)*10),
|
||||
Decoder: 50 - float64((i+1)*3),
|
||||
})
|
||||
}
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *mockUtil) Start() {}
|
||||
func (u *mockUtil) Cancel() {}
|
||||
|
||||
func (u *mockUtil) CPUCounts() (float64, error) {
|
||||
return 2, nil
|
||||
}
|
||||
|
||||
func (u *mockUtil) CPU() (*psutil.CPUInfo, error) {
|
||||
u.lock.Lock()
|
||||
defer u.lock.Unlock()
|
||||
|
||||
cpu := u.cpu
|
||||
|
||||
return &cpu, nil
|
||||
}
|
||||
|
||||
func (u *mockUtil) Disk(path string) (*psutil.DiskInfo, error) {
|
||||
return &psutil.DiskInfo{}, nil
|
||||
}
|
||||
|
||||
func (u *mockUtil) Memory() (*psutil.MemoryInfo, error) {
|
||||
u.lock.Lock()
|
||||
defer u.lock.Unlock()
|
||||
|
||||
mem := u.mem
|
||||
|
||||
return &mem, nil
|
||||
}
|
||||
|
||||
func (u *mockUtil) Network() ([]psutil.NetworkInfo, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (u *mockUtil) GPU() ([]psutil.GPUInfo, error) {
|
||||
u.lock.Lock()
|
||||
defer u.lock.Unlock()
|
||||
|
||||
gpu := []psutil.GPUInfo{}
|
||||
|
||||
gpu = append(gpu, u.gpu...)
|
||||
|
||||
return gpu, nil
|
||||
}
|
||||
|
||||
func (u *mockUtil) Process(pid int32) (psutil.Process, error) {
|
||||
return &mockProcess{}, nil
|
||||
}
|
||||
|
||||
type mockProcess struct{}
|
||||
|
||||
func (p *mockProcess) CPU() (*psutil.CPUInfo, error) {
|
||||
s := &psutil.CPUInfo{
|
||||
System: 1,
|
||||
User: 2,
|
||||
Idle: 0,
|
||||
Other: 3,
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (p *mockProcess) Memory() (uint64, error) { return 42, nil }
|
||||
func (p *mockProcess) GPU() (*psutil.GPUInfo, error) {
|
||||
return &psutil.GPUInfo{
|
||||
Index: 0,
|
||||
Name: "L4",
|
||||
MemoryTotal: 128,
|
||||
MemoryUsed: 42,
|
||||
Usage: 5,
|
||||
Encoder: 9,
|
||||
Decoder: 7,
|
||||
}, nil
|
||||
}
|
||||
func (p *mockProcess) Cancel() {}
|
||||
func (p *mockProcess) Suspend() error { return nil }
|
||||
func (p *mockProcess) Resume() error { return nil }
|
||||
|
||||
func TestConfigNoLimits(t *testing.T) {
|
||||
_, err := New(Config{
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
@@ -137,7 +22,7 @@ func TestConfigWrongLimits(t *testing.T) {
|
||||
_, err := New(Config{
|
||||
MaxCPU: 102,
|
||||
MaxMemory: 573,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
})
|
||||
require.Error(t, err)
|
||||
|
||||
@@ -146,7 +31,7 @@ func TestConfigWrongLimits(t *testing.T) {
|
||||
MaxMemory: 0,
|
||||
MaxGPU: 101,
|
||||
MaxGPUMemory: 103,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -155,7 +40,7 @@ func TestConfigWrongLimits(t *testing.T) {
|
||||
MaxMemory: 0,
|
||||
MaxGPU: 101,
|
||||
MaxGPUMemory: 103,
|
||||
PSUtil: newMockUtil(1),
|
||||
PSUtil: psutil.New(1),
|
||||
})
|
||||
require.Error(t, err)
|
||||
}
|
||||
@@ -164,7 +49,7 @@ func TestMemoryLimit(t *testing.T) {
|
||||
r, err := New(Config{
|
||||
MaxCPU: 100,
|
||||
MaxMemory: 150. / 200. * 100,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
Logger: nil,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -209,7 +94,7 @@ func TestMemoryLimit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMemoryUnlimit(t *testing.T) {
|
||||
util := newMockUtil(0)
|
||||
util := psutil.New(0)
|
||||
|
||||
r, err := New(Config{
|
||||
MaxCPU: 100,
|
||||
@@ -255,9 +140,9 @@ func TestMemoryUnlimit(t *testing.T) {
|
||||
_, limit, _ = r.ShouldLimit()
|
||||
require.True(t, limit)
|
||||
|
||||
util.lock.Lock()
|
||||
util.mem.Used = 140
|
||||
util.lock.Unlock()
|
||||
util.Lock.Lock()
|
||||
util.MemInfo.Used = 140
|
||||
util.Lock.Unlock()
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
@@ -296,7 +181,7 @@ func TestCPULimit(t *testing.T) {
|
||||
r, err := New(Config{
|
||||
MaxCPU: 50.,
|
||||
MaxMemory: 100,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
Logger: nil,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -341,7 +226,7 @@ func TestCPULimit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCPUUnlimit(t *testing.T) {
|
||||
util := newMockUtil(0)
|
||||
util := psutil.New(0)
|
||||
|
||||
r, err := New(Config{
|
||||
MaxCPU: 50.,
|
||||
@@ -387,9 +272,9 @@ func TestCPUUnlimit(t *testing.T) {
|
||||
limit, _, _ = r.ShouldLimit()
|
||||
require.True(t, limit)
|
||||
|
||||
util.lock.Lock()
|
||||
util.cpu.User = 20
|
||||
util.lock.Unlock()
|
||||
util.Lock.Lock()
|
||||
util.CPUInfo.User = 20
|
||||
util.Lock.Unlock()
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
@@ -430,7 +315,7 @@ func TestGPULimitMemory(t *testing.T) {
|
||||
MaxMemory: 100,
|
||||
MaxGPU: 100,
|
||||
MaxGPUMemory: 20,
|
||||
PSUtil: newMockUtil(2),
|
||||
PSUtil: psutil.New(2),
|
||||
Logger: nil,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -475,7 +360,7 @@ func TestGPULimitMemory(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGPUUnlimitMemory(t *testing.T) {
|
||||
util := newMockUtil(2)
|
||||
util := psutil.New(2)
|
||||
|
||||
r, err := New(Config{
|
||||
MaxCPU: 100,
|
||||
@@ -520,10 +405,10 @@ func TestGPUUnlimitMemory(t *testing.T) {
|
||||
|
||||
require.Contains(t, limit, true)
|
||||
|
||||
util.lock.Lock()
|
||||
util.gpu[0].MemoryUsed = 10
|
||||
util.gpu[1].MemoryUsed = 10
|
||||
util.lock.Unlock()
|
||||
util.Lock.Lock()
|
||||
util.GPUInfo[0].MemoryUsed = 10
|
||||
util.GPUInfo[1].MemoryUsed = 10
|
||||
util.Lock.Unlock()
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
@@ -564,7 +449,7 @@ func TestGPULimitMemorySome(t *testing.T) {
|
||||
MaxMemory: 100,
|
||||
MaxGPU: 100,
|
||||
MaxGPUMemory: 14. / 24. * 100.,
|
||||
PSUtil: newMockUtil(4),
|
||||
PSUtil: psutil.New(4),
|
||||
Logger: nil,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -614,7 +499,7 @@ func TestGPULimitUsage(t *testing.T) {
|
||||
MaxMemory: 100,
|
||||
MaxGPU: 40,
|
||||
MaxGPUMemory: 100,
|
||||
PSUtil: newMockUtil(3),
|
||||
PSUtil: psutil.New(3),
|
||||
Logger: nil,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -662,7 +547,7 @@ func TestGPULimitUsage(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGPUUnlimitUsage(t *testing.T) {
|
||||
util := newMockUtil(3)
|
||||
util := psutil.New(3)
|
||||
|
||||
r, err := New(Config{
|
||||
MaxCPU: 100,
|
||||
@@ -707,11 +592,11 @@ func TestGPUUnlimitUsage(t *testing.T) {
|
||||
|
||||
require.Equal(t, []bool{true, false, false}, limit)
|
||||
|
||||
util.lock.Lock()
|
||||
util.gpu[0].Usage = 30
|
||||
util.gpu[0].Encoder = 30
|
||||
util.gpu[0].Decoder = 30
|
||||
util.lock.Unlock()
|
||||
util.Lock.Lock()
|
||||
util.GPUInfo[0].Usage = 30
|
||||
util.GPUInfo[0].Encoder = 30
|
||||
util.GPUInfo[0].Decoder = 30
|
||||
util.Lock.Unlock()
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
@@ -749,7 +634,7 @@ func TestGPUUnlimitUsage(t *testing.T) {
|
||||
func TestRequestCPU(t *testing.T) {
|
||||
r, err := New(Config{
|
||||
MaxCPU: 70.,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -766,7 +651,7 @@ func TestRequestCPU(t *testing.T) {
|
||||
func TestRequestMemory(t *testing.T) {
|
||||
r, err := New(Config{
|
||||
MaxMemory: 170. / 200. * 100,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -784,7 +669,7 @@ func TestRequestNoGPU(t *testing.T) {
|
||||
r, err := New(Config{
|
||||
MaxCPU: 100,
|
||||
MaxMemory: 100,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -796,7 +681,7 @@ func TestRequestInvalidGPURequest(t *testing.T) {
|
||||
r, err := New(Config{
|
||||
MaxCPU: 100,
|
||||
MaxMemory: 100,
|
||||
PSUtil: newMockUtil(1),
|
||||
PSUtil: psutil.New(1),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -813,7 +698,7 @@ func TestRequestGPULimitsOneGPU(t *testing.T) {
|
||||
MaxMemory: 100,
|
||||
MaxGPU: 50,
|
||||
MaxGPUMemory: 60,
|
||||
PSUtil: newMockUtil(1),
|
||||
PSUtil: psutil.New(1),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -840,7 +725,7 @@ func TestRequestGPULimitsMoreGPU(t *testing.T) {
|
||||
MaxMemory: 100,
|
||||
MaxGPU: 60,
|
||||
MaxGPUMemory: 60,
|
||||
PSUtil: newMockUtil(2),
|
||||
PSUtil: psutil.New(2),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -856,7 +741,7 @@ func TestHasLimits(t *testing.T) {
|
||||
r, err := New(Config{
|
||||
MaxCPU: 70.,
|
||||
MaxMemory: 170. / 200. * 100,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
Logger: nil,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -866,7 +751,7 @@ func TestHasLimits(t *testing.T) {
|
||||
r, err = New(Config{
|
||||
MaxCPU: 100,
|
||||
MaxMemory: 100,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
Logger: nil,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -876,7 +761,7 @@ func TestHasLimits(t *testing.T) {
|
||||
r, err = New(Config{
|
||||
MaxCPU: 0,
|
||||
MaxMemory: 0,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
Logger: nil,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -887,7 +772,7 @@ func TestHasLimits(t *testing.T) {
|
||||
MaxCPU: 0,
|
||||
MaxMemory: 0,
|
||||
MaxGPU: 10,
|
||||
PSUtil: newMockUtil(1),
|
||||
PSUtil: psutil.New(1),
|
||||
Logger: nil,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -898,7 +783,7 @@ func TestHasLimits(t *testing.T) {
|
||||
MaxCPU: 0,
|
||||
MaxMemory: 0,
|
||||
MaxGPU: 10,
|
||||
PSUtil: newMockUtil(0),
|
||||
PSUtil: psutil.New(0),
|
||||
Logger: nil,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -912,7 +797,7 @@ func TestInfo(t *testing.T) {
|
||||
MaxMemory: 90,
|
||||
MaxGPU: 11,
|
||||
MaxGPUMemory: 50,
|
||||
PSUtil: newMockUtil(2),
|
||||
PSUtil: psutil.New(2),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
|
@@ -13,11 +13,10 @@ import (
|
||||
"github.com/datarhei/core/v16/iam"
|
||||
iamidentity "github.com/datarhei/core/v16/iam/identity"
|
||||
"github.com/datarhei/core/v16/iam/policy"
|
||||
"github.com/datarhei/core/v16/internal/mock/resources"
|
||||
"github.com/datarhei/core/v16/internal/testhelper"
|
||||
"github.com/datarhei/core/v16/io/fs"
|
||||
"github.com/datarhei/core/v16/net"
|
||||
"github.com/datarhei/core/v16/resources"
|
||||
"github.com/datarhei/core/v16/resources/psutil"
|
||||
"github.com/datarhei/core/v16/restream/app"
|
||||
rfs "github.com/datarhei/core/v16/restream/fs"
|
||||
"github.com/datarhei/core/v16/restream/replace"
|
||||
@@ -28,22 +27,12 @@ import (
|
||||
)
|
||||
|
||||
func getDummyRestreamer(portrange net.Portranger, validatorIn, validatorOut ffmpeg.Validator, replacer replace.Replacer) (Restreamer, error) {
|
||||
binary, err := testhelper.BuildBinary("ffmpeg", "../internal/testhelper")
|
||||
binary, err := testhelper.BuildBinary("ffmpeg")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to build helper program: %w", err)
|
||||
}
|
||||
|
||||
psutil, err := psutil.New("", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resources, err := resources.New(resources.Config{
|
||||
PSUtil: psutil,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resources := resources.New()
|
||||
|
||||
ffmpeg, err := ffmpeg.New(ffmpeg.Config{
|
||||
Binary: binary,
|
||||
|
Reference in New Issue
Block a user