2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2015-07-09 22:12:36 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package runconfig // import "github.com/docker/docker/runconfig"
|
2015-07-01 08:16:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"os"
|
2015-07-01 08:16:36 +00:00
|
|
|
"testing"
|
2015-12-18 18:36:17 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-06-07 19:05:43 +00:00
|
|
|
"github.com/docker/docker/pkg/sysinfo"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2015-07-01 08:16:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeHostConfig(t *testing.T) {
|
|
|
|
fixtures := []struct {
|
|
|
|
file string
|
|
|
|
}{
|
2015-09-10 02:23:06 +00:00
|
|
|
{"fixtures/unix/container_hostconfig_1_14.json"},
|
|
|
|
{"fixtures/unix/container_hostconfig_1_19.json"},
|
2015-07-01 08:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range fixtures {
|
2021-08-24 10:10:50 +00:00
|
|
|
b, err := os.ReadFile(f.file)
|
2015-07-01 08:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-07-19 14:20:13 +00:00
|
|
|
c, err := decodeHostConfig(bytes.NewReader(b))
|
2015-07-01 08:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err))
|
|
|
|
}
|
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, !c.Privileged)
|
2015-07-01 08:16:36 +00:00
|
|
|
|
2015-07-12 17:12:48 +00:00
|
|
|
if l := len(c.Binds); l != 1 {
|
|
|
|
t.Fatalf("Expected 1 bind, found %d\n", l)
|
2015-07-01 08:16:36 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 11:28:37 +00:00
|
|
|
if len(c.CapAdd) != 1 && c.CapAdd[0] != "NET_ADMIN" {
|
2015-07-01 08:16:36 +00:00
|
|
|
t.Fatalf("Expected CapAdd NET_ADMIN, got %v", c.CapAdd)
|
|
|
|
}
|
|
|
|
|
2016-02-29 11:28:37 +00:00
|
|
|
if len(c.CapDrop) != 1 && c.CapDrop[0] != "NET_ADMIN" {
|
2016-12-01 09:07:38 +00:00
|
|
|
t.Fatalf("Expected CapDrop NET_ADMIN, got %v", c.CapDrop)
|
2015-07-01 08:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 19:05:43 +00:00
|
|
|
|
|
|
|
func TestValidateResources(t *testing.T) {
|
|
|
|
type resourceTest struct {
|
2020-05-22 21:18:06 +00:00
|
|
|
ConfigCPURealtimePeriod int64
|
|
|
|
ConfigCPURealtimeRuntime int64
|
|
|
|
SysInfoCPURealtime bool
|
|
|
|
ErrorExpected bool
|
|
|
|
FailureMsg string
|
2016-06-07 19:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []resourceTest{
|
|
|
|
{
|
2020-05-22 21:18:06 +00:00
|
|
|
ConfigCPURealtimePeriod: 1000,
|
|
|
|
ConfigCPURealtimeRuntime: 1000,
|
|
|
|
SysInfoCPURealtime: true,
|
|
|
|
ErrorExpected: false,
|
|
|
|
FailureMsg: "Expected valid configuration",
|
2016-06-07 19:05:43 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-22 21:18:06 +00:00
|
|
|
ConfigCPURealtimePeriod: 5000,
|
|
|
|
ConfigCPURealtimeRuntime: 5000,
|
|
|
|
SysInfoCPURealtime: false,
|
|
|
|
ErrorExpected: true,
|
|
|
|
FailureMsg: "Expected failure when cpu-rt-period is set but kernel doesn't support it",
|
2016-06-07 19:05:43 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-22 21:18:06 +00:00
|
|
|
ConfigCPURealtimePeriod: 5000,
|
|
|
|
ConfigCPURealtimeRuntime: 5000,
|
|
|
|
SysInfoCPURealtime: false,
|
|
|
|
ErrorExpected: true,
|
|
|
|
FailureMsg: "Expected failure when cpu-rt-runtime is set but kernel doesn't support it",
|
2016-06-07 19:05:43 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-22 21:18:06 +00:00
|
|
|
ConfigCPURealtimePeriod: 5000,
|
|
|
|
ConfigCPURealtimeRuntime: 10000,
|
|
|
|
SysInfoCPURealtime: true,
|
|
|
|
ErrorExpected: true,
|
|
|
|
FailureMsg: "Expected failure when cpu-rt-runtime is greater than cpu-rt-period",
|
2016-06-07 19:05:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rt := range tests {
|
|
|
|
var hc container.HostConfig
|
|
|
|
hc.Resources.CPURealtimePeriod = rt.ConfigCPURealtimePeriod
|
|
|
|
hc.Resources.CPURealtimeRuntime = rt.ConfigCPURealtimeRuntime
|
|
|
|
|
|
|
|
var si sysinfo.SysInfo
|
2020-05-22 21:18:06 +00:00
|
|
|
si.CPURealtime = rt.SysInfoCPURealtime
|
2016-06-07 19:05:43 +00:00
|
|
|
|
2017-03-10 17:39:22 +00:00
|
|
|
if err := validateResources(&hc, &si); (err != nil) != rt.ErrorExpected {
|
2016-06-07 19:05:43 +00:00
|
|
|
t.Fatal(rt.FailureMsg, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|