github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/lib/cgutil/cgutil_linux_test.go (about)

     1  //go:build linux
     2  
     3  package cgutil
     4  
     5  import (
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/nomad/ci"
    11  	"github.com/hashicorp/nomad/client/testutil"
    12  	"github.com/hashicorp/nomad/helper/testlog"
    13  	"github.com/hashicorp/nomad/helper/uuid"
    14  	"github.com/opencontainers/runc/libcontainer/cgroups"
    15  	"github.com/opencontainers/runc/libcontainer/cgroups/fs2"
    16  	"github.com/shoenig/test/must"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  func TestUtil_GetCgroupParent(t *testing.T) {
    21  	ci.Parallel(t)
    22  
    23  	t.Run("v1", func(t *testing.T) {
    24  		testutil.CgroupsCompatibleV1(t)
    25  		t.Run("default", func(t *testing.T) {
    26  			exp := "/nomad"
    27  			parent := GetCgroupParent("")
    28  			require.Equal(t, exp, parent)
    29  		})
    30  
    31  		t.Run("configured", func(t *testing.T) {
    32  			exp := "/bar"
    33  			parent := GetCgroupParent("/bar")
    34  			require.Equal(t, exp, parent)
    35  		})
    36  	})
    37  
    38  	t.Run("v2", func(t *testing.T) {
    39  		testutil.CgroupsCompatibleV2(t)
    40  		t.Run("default", func(t *testing.T) {
    41  			exp := "nomad.slice"
    42  			parent := GetCgroupParent("")
    43  			require.Equal(t, exp, parent)
    44  		})
    45  
    46  		t.Run("configured", func(t *testing.T) {
    47  			exp := "abc.slice"
    48  			parent := GetCgroupParent("abc.slice")
    49  			require.Equal(t, exp, parent)
    50  		})
    51  	})
    52  }
    53  
    54  func TestUtil_CreateCPUSetManager(t *testing.T) {
    55  	ci.Parallel(t)
    56  
    57  	logger := testlog.HCLogger(t)
    58  
    59  	t.Run("v1", func(t *testing.T) {
    60  		testutil.CgroupsCompatibleV1(t)
    61  		parent := "/" + uuid.Short()
    62  		manager := CreateCPUSetManager(parent, []uint16{0}, logger)
    63  		manager.Init()
    64  		_, ok := manager.(*cpusetManagerV1)
    65  		must.True(t, ok)
    66  		must.NoError(t, cgroups.RemovePath(filepath.Join(CgroupRoot, parent)))
    67  	})
    68  
    69  	t.Run("v2", func(t *testing.T) {
    70  		testutil.CgroupsCompatibleV2(t)
    71  		parent := uuid.Short() + ".slice"
    72  		manager := CreateCPUSetManager(parent, []uint16{0}, logger)
    73  		manager.Init()
    74  		_, ok := manager.(*cpusetManagerV2)
    75  		must.True(t, ok)
    76  		must.NoError(t, cgroups.RemovePath(filepath.Join(CgroupRoot, parent)))
    77  	})
    78  }
    79  
    80  func TestUtil_GetCPUsFromCgroup(t *testing.T) {
    81  	ci.Parallel(t)
    82  
    83  	t.Run("v2", func(t *testing.T) {
    84  		testutil.CgroupsCompatibleV2(t)
    85  		cpus, err := GetCPUsFromCgroup("system.slice") // thanks, systemd!
    86  		require.NoError(t, err)
    87  		require.NotEmpty(t, cpus)
    88  	})
    89  }
    90  
    91  func create(t *testing.T, name string) {
    92  	mgr, err := fs2.NewManager(nil, filepath.Join(CgroupRoot, name))
    93  	require.NoError(t, err)
    94  	if err = mgr.Apply(CreationPID); err != nil {
    95  		_ = cgroups.RemovePath(name)
    96  		t.Fatal("failed to create cgroup for test")
    97  	}
    98  }
    99  
   100  func cleanup(t *testing.T, name string) {
   101  	err := cgroups.RemovePath(name)
   102  	require.NoError(t, err)
   103  }
   104  
   105  func TestUtil_CopyCpuset(t *testing.T) {
   106  	ci.Parallel(t)
   107  
   108  	t.Run("v2", func(t *testing.T) {
   109  		testutil.CgroupsCompatibleV2(t)
   110  		source := uuid.Short() + ".scope"
   111  		create(t, source)
   112  		defer cleanup(t, source)
   113  		require.NoError(t, cgroups.WriteFile(filepath.Join(CgroupRoot, source), "cpuset.cpus", "0-1"))
   114  
   115  		destination := uuid.Short() + ".scope"
   116  		create(t, destination)
   117  		defer cleanup(t, destination)
   118  
   119  		err := CopyCpuset(
   120  			filepath.Join(CgroupRoot, source),
   121  			filepath.Join(CgroupRoot, destination),
   122  		)
   123  		require.NoError(t, err)
   124  
   125  		value, readErr := cgroups.ReadFile(filepath.Join(CgroupRoot, destination), "cpuset.cpus")
   126  		require.NoError(t, readErr)
   127  		require.Equal(t, "0-1", strings.TrimSpace(value))
   128  	})
   129  }