Example #1
0
func TestValidateResources(t *testing.T) {
	type resourceTest struct {
		ConfigCPURealtimePeriod   int64
		ConfigCPURealtimeRuntime  int64
		SysInfoCPURealtimePeriod  bool
		SysInfoCPURealtimeRuntime bool
		ErrorExpected             bool
		FailureMsg                string
	}

	tests := []resourceTest{
		{
			ConfigCPURealtimePeriod:   1000,
			ConfigCPURealtimeRuntime:  1000,
			SysInfoCPURealtimePeriod:  true,
			SysInfoCPURealtimeRuntime: true,
			ErrorExpected:             false,
			FailureMsg:                "Expected valid configuration",
		},
		{
			ConfigCPURealtimePeriod:   5000,
			ConfigCPURealtimeRuntime:  5000,
			SysInfoCPURealtimePeriod:  false,
			SysInfoCPURealtimeRuntime: true,
			ErrorExpected:             true,
			FailureMsg:                "Expected failure when cpu-rt-period is set but kernel doesn't support it",
		},
		{
			ConfigCPURealtimePeriod:   5000,
			ConfigCPURealtimeRuntime:  5000,
			SysInfoCPURealtimePeriod:  true,
			SysInfoCPURealtimeRuntime: false,
			ErrorExpected:             true,
			FailureMsg:                "Expected failure when cpu-rt-runtime is set but kernel doesn't support it",
		},
		{
			ConfigCPURealtimePeriod:   5000,
			ConfigCPURealtimeRuntime:  10000,
			SysInfoCPURealtimePeriod:  true,
			SysInfoCPURealtimeRuntime: false,
			ErrorExpected:             true,
			FailureMsg:                "Expected failure when cpu-rt-runtime is greater than cpu-rt-period",
		},
	}

	for _, rt := range tests {
		var hc container.HostConfig
		hc.Resources.CPURealtimePeriod = rt.ConfigCPURealtimePeriod
		hc.Resources.CPURealtimeRuntime = rt.ConfigCPURealtimeRuntime

		var si sysinfo.SysInfo
		si.CPURealtimePeriod = rt.SysInfoCPURealtimePeriod
		si.CPURealtimeRuntime = rt.SysInfoCPURealtimeRuntime

		if err := ValidateResources(&hc, &si); (err != nil) != rt.ErrorExpected {
			t.Fatal(rt.FailureMsg, err)
		}
	}
}