Exemplo n.º 1
0
func TestReadProcessSchedStat(t *testing.T) {

	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	mfs := mockfs.NewMockFileSystem(mockCtrl)

	pid := 10

	stat := &ProcessSchedStat{
		NumProcesses:  1,
		Running:       100,
		RunWait:       120,
		NumTimeSlices: 130,
	}

	path := fmt.Sprintf("/proc/%v/schedstat", pid)
	content := fmt.Sprintf("%v %v %v\n", stat.Running, stat.RunWait, stat.NumTimeSlices)
	mockfs.AddTextFile(mfs, path, content)
	fs.ChangeFileSystem(mfs)

	receivedStat := &ProcessSchedStat{}
	err := receivedStat.Add(pid)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(receivedStat, stat) {
		t.Errorf("Received wrong schedstat: %+v", receivedStat)
	}
}
Exemplo n.º 2
0
func TestSchedDebugReader(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	for _, testCase := range schedDebugToLoadsPerContainerPerCore {
		mfs := mockfs.NewMockFileSystem(mockCtrl)
		path := "/proc/sched_debug"
		schedDebugContent := testCase.SchedDebugContent
		mockfs.AddTextFile(mfs, path, schedDebugContent)
		fs.ChangeFileSystem(mfs)
		loads, err := NewSchedulerLoadReader()
		if testCase.Error != nil {
			if err == nil {
				t.Fatal("expected error: %v", testCase.Error)
			}
		}
		if err != nil {
			t.Fatal(err)
		}
		containers, err := loads.AllContainers()
		if err != nil {
			t.Fatal(err)
		}
		if len(containers) != len(testCase.Loads) {
			t.Errorf("expected %v container's information; received %v", len(testCase.Loads), len(containers))
		}
		for _, container := range containers {
			l, err := loads.Load(container)
			if err != nil {
				t.Fatal(err)
			}
			if expected, ok := testCase.Loads[container]; ok {
				if !reflect.DeepEqual(expected, l) {
					t.Errorf("Wrong load for container %v; should be %v, received %v\nsched_debug:\n%v\n",
						container, expected, l, schedDebugContent)
					continue
				}
			} else {
				t.Errorf("unexpected container %v.\nsched_debug:\n%v\n", container, schedDebugContent)
			}
		}
	}
}
Exemplo n.º 3
0
func TestSplitName(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	for _, testCase := range initCgroupsToParentAndID {
		mfs := mockfs.NewMockFileSystem(mockCtrl)
		mockfs.AddTextFile(mfs, "/proc/1/cgroup", testCase.InitCgroupFileContent)
		fs.ChangeFileSystem(mfs)
		parent, id, err := SplitName(testCase.ContainerPath)
		if testCase.Error != nil {
			if err == nil {
				t.Fatalf("did not receive expected error.\ncontent:%v\n, path:%v\n, expected error:%v\n", testCase.InitCgroupFileContent, testCase.ContainerPath, testCase.Error)
			}
			continue
		}
		if testCase.Parent != parent || testCase.Id != id {
			t.Errorf("unexpected parent or id:\ncontent:%v\npath:%v\nexpected parent: %v; recevied parent: %v;\nexpected id: %v; received id: %v", testCase.InitCgroupFileContent, testCase.ContainerPath, testCase.Parent, parent, testCase.Id, id)
		}
	}
}