github.com/elastic/gosigar@v0.14.3/cgroup/util_test.go (about)

     1  package cgroup
     2  
     3  import (
     4  	"archive/zip"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  const dockerTestData = "testdata/docker.zip"
    15  
    16  func TestMain(m *testing.M) {
    17  	err := extractTestData(dockerTestData)
    18  	if err != nil {
    19  		fmt.Println(err)
    20  		os.Exit(1)
    21  	}
    22  	os.Exit(m.Run())
    23  }
    24  
    25  // extractTestData from zip file and write it in the same dir as the zip file.
    26  func extractTestData(path string) error {
    27  	r, err := zip.OpenReader(path)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	defer r.Close()
    32  
    33  	dest := filepath.Dir(path)
    34  
    35  	extractAndWriteFile := func(f *zip.File) error {
    36  		rc, err := f.Open()
    37  		if err != nil {
    38  			return err
    39  		}
    40  		defer rc.Close()
    41  
    42  		path := filepath.Join(dest, f.Name)
    43  		if found, err := exists(path); err != nil || found {
    44  			return err
    45  		}
    46  
    47  		if f.FileInfo().IsDir() {
    48  			os.MkdirAll(path, f.Mode())
    49  		} else {
    50  			destFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(0700))
    51  			if err != nil {
    52  				return err
    53  			}
    54  			defer destFile.Close()
    55  
    56  			_, err = io.Copy(destFile, rc)
    57  			if err != nil {
    58  				return err
    59  			}
    60  
    61  			os.Chmod(path, f.Mode())
    62  		}
    63  		return nil
    64  	}
    65  
    66  	for _, f := range r.File {
    67  		err := extractAndWriteFile(f)
    68  		if err != nil {
    69  			return err
    70  		}
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  // exists returns whether the given file or directory exists or not
    77  func exists(path string) (bool, error) {
    78  	_, err := os.Stat(path)
    79  	if err == nil {
    80  		return true, nil
    81  	}
    82  	if os.IsNotExist(err) {
    83  		return false, nil
    84  	}
    85  	return true, err
    86  }
    87  
    88  func TestSupportedSubsystems(t *testing.T) {
    89  	subsystems, err := SupportedSubsystems("testdata/docker")
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	assert.Len(t, subsystems, 11)
    95  	assertContains(t, subsystems, "cpuset")
    96  	assertContains(t, subsystems, "cpu")
    97  	assertContains(t, subsystems, "cpuacct")
    98  	assertContains(t, subsystems, "blkio")
    99  	assertContains(t, subsystems, "memory")
   100  	assertContains(t, subsystems, "devices")
   101  	assertContains(t, subsystems, "freezer")
   102  	assertContains(t, subsystems, "net_cls")
   103  	assertContains(t, subsystems, "perf_event")
   104  	assertContains(t, subsystems, "net_prio")
   105  	assertContains(t, subsystems, "pids")
   106  
   107  	_, found := subsystems["hugetlb"]
   108  	assert.False(t, found, "hugetlb should be missing because it's disabled")
   109  }
   110  
   111  func TestSupportedSubsystemsErrCgroupsMissing(t *testing.T) {
   112  	_, err := SupportedSubsystems("testdata/doesnotexist")
   113  	if err != ErrCgroupsMissing {
   114  		t.Fatalf("expected ErrCgroupsMissing, but got %v", err)
   115  	}
   116  }
   117  
   118  func TestSubsystemMountpoints(t *testing.T) {
   119  	subsystems := map[string]struct{}{}
   120  	subsystems["blkio"] = struct{}{}
   121  	subsystems["cpu"] = struct{}{}
   122  	subsystems["cpuacct"] = struct{}{}
   123  	subsystems["cpuset"] = struct{}{}
   124  	subsystems["devices"] = struct{}{}
   125  	subsystems["freezer"] = struct{}{}
   126  	subsystems["hugetlb"] = struct{}{}
   127  	subsystems["memory"] = struct{}{}
   128  	subsystems["perf_event"] = struct{}{}
   129  
   130  	mountpoints, err := SubsystemMountpoints("testdata/docker", subsystems)
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  
   135  	assert.Equal(t, "testdata/docker/sys/fs/cgroup/blkio", mountpoints["blkio"])
   136  	assert.Equal(t, "testdata/docker/sys/fs/cgroup/cpu", mountpoints["cpu"])
   137  	assert.Equal(t, "testdata/docker/sys/fs/cgroup/cpuacct", mountpoints["cpuacct"])
   138  	assert.Equal(t, "testdata/docker/sys/fs/cgroup/cpuset", mountpoints["cpuset"])
   139  	assert.Equal(t, "testdata/docker/sys/fs/cgroup/devices", mountpoints["devices"])
   140  	assert.Equal(t, "testdata/docker/sys/fs/cgroup/freezer", mountpoints["freezer"])
   141  	assert.Equal(t, "testdata/docker/sys/fs/cgroup/hugetlb", mountpoints["hugetlb"])
   142  	assert.Equal(t, "testdata/docker/sys/fs/cgroup/memory", mountpoints["memory"])
   143  	assert.Equal(t, "testdata/docker/sys/fs/cgroup/perf_event", mountpoints["perf_event"])
   144  }
   145  
   146  func TestProcessCgroupPaths(t *testing.T) {
   147  	paths, err := ProcessCgroupPaths("testdata/docker", 985)
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  
   152  	path := "/docker/b29faf21b7eff959f64b4192c34d5d67a707fe8561e9eaa608cb27693fba4242"
   153  	assert.Equal(t, path, paths["blkio"])
   154  	assert.Equal(t, path, paths["cpu"])
   155  	assert.Equal(t, path, paths["cpuacct"])
   156  	assert.Equal(t, path, paths["cpuset"])
   157  	assert.Equal(t, path, paths["devices"])
   158  	assert.Equal(t, path, paths["freezer"])
   159  	assert.Equal(t, path, paths["memory"])
   160  	assert.Equal(t, path, paths["net_cls"])
   161  	assert.Equal(t, path, paths["net_prio"])
   162  	assert.Equal(t, path, paths["perf_event"])
   163  	assert.Len(t, paths, 10)
   164  }
   165  
   166  func assertContains(t testing.TB, m map[string]struct{}, key string) {
   167  	_, contains := m[key]
   168  	if !contains {
   169  		t.Errorf("map is missing key %v, map=%+v", key, m)
   170  	}
   171  }
   172  
   173  func TestParseMountinfoLine(t *testing.T) {
   174  	lines := []string{
   175  		"30 24 0:25 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,blkio",
   176  		"30 24 0:25 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime shared:13 - cgroup cgroup rw,blkio",
   177  		"30 24 0:25 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime shared:13 master:1 - cgroup cgroup rw,blkio",
   178  		"30 24 0:25 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime shared:13 - cgroup cgroup rw,name=blkio",
   179  	}
   180  
   181  	for _, line := range lines {
   182  		mount, err := parseMountinfoLine(line)
   183  		if err != nil {
   184  			t.Fatal(err)
   185  		}
   186  
   187  		assert.Equal(t, "/sys/fs/cgroup/blkio", mount.mountpoint)
   188  		assert.Equal(t, "cgroup", mount.filesystemType)
   189  		assert.Len(t, mount.superOptions, 2)
   190  	}
   191  }