Skip to content

Commit

Permalink
add more code
Browse files Browse the repository at this point in the history
Signed-off-by: jason yang <[email protected]>
  • Loading branch information
JasonYangShadow committed Oct 18, 2023
1 parent 0611436 commit 8656846
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/net v0.10.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
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/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
Expand Down Expand Up @@ -118,6 +123,7 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
toolman.org/net/peercred v0.6.1 h1:xAjw6yxNJRO2asnmqMPfbzOwKpb1wUJF3iKoTvqH0zk=
Expand Down
87 changes: 87 additions & 0 deletions internal/cgroup/cgroup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package cgroup_test

import (
"bytes"
"testing"

"github.com/apptainer/apptheus/internal/cgroup"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

type MockCgroupManager struct {
mock.Mock
}

func (m *MockCgroupManager) Apply(pid int) error {
return nil
}

func (m *MockCgroupManager) GetPids() ([]int, error) {
return []int{}, nil
}

func (m *MockCgroupManager) GetAllPids() ([]int, error) {
return []int{}, nil
}

func (m *MockCgroupManager) GetStats() (*cgroups.Stats, error) {
return cgroups.NewStats(), nil
}

func (m *MockCgroupManager) Freeze(state configs.FreezerState) error {
return nil
}

func (m *MockCgroupManager) Destroy() error {
return nil
}

func (m *MockCgroupManager) Path(path string) string {
return path
}

func (m *MockCgroupManager) Set(r *configs.Resources) error {
return nil
}

func (m *MockCgroupManager) GetPaths() map[string]string {
return map[string]string{}
}

func (m *MockCgroupManager) GetCgroups() (*configs.Cgroup, error) {
return nil, nil
}

func (m *MockCgroupManager) GetFreezerState() (configs.FreezerState, error) {
return "", nil
}

func (m *MockCgroupManager) Exists() bool {
return true
}

func (m *MockCgroupManager) OOMKillCount() (uint64, error) {
return 0, nil
}

func TestCgroup(t *testing.T) {
cgroup := &cgroup.CGroup{
Manager: &MockCgroupManager{},
}

has, err := cgroup.HasProcess()
require.NoError(t, err)
require.False(t, has)

funcs, err := cgroup.CreateStats()
require.NoError(t, err)
require.NotEmpty(t, funcs)
require.Len(t, funcs, 5)

var buffer bytes.Buffer
_, err = cgroup.Marshal(&buffer)
require.NoError(t, err)
}

0 comments on commit 8656846

Please sign in to comment.