hostconfig_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //go:build !windows
  2. package runconfig // import "github.com/docker/docker/runconfig"
  3. import (
  4. "bytes"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/pkg/sysinfo"
  10. "gotest.tools/v3/assert"
  11. )
  12. func TestDecodeHostConfig(t *testing.T) {
  13. fixtures := []struct {
  14. file string
  15. }{
  16. {"fixtures/unix/container_hostconfig_1_14.json"},
  17. {"fixtures/unix/container_hostconfig_1_19.json"},
  18. }
  19. for _, f := range fixtures {
  20. b, err := os.ReadFile(f.file)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. c, err := decodeHostConfig(bytes.NewReader(b))
  25. if err != nil {
  26. t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err))
  27. }
  28. assert.Check(t, !c.Privileged)
  29. if l := len(c.Binds); l != 1 {
  30. t.Fatalf("Expected 1 bind, found %d\n", l)
  31. }
  32. if len(c.CapAdd) != 1 && c.CapAdd[0] != "NET_ADMIN" {
  33. t.Fatalf("Expected CapAdd NET_ADMIN, got %v", c.CapAdd)
  34. }
  35. if len(c.CapDrop) != 1 && c.CapDrop[0] != "NET_ADMIN" {
  36. t.Fatalf("Expected CapDrop NET_ADMIN, got %v", c.CapDrop)
  37. }
  38. }
  39. }
  40. func TestValidateResources(t *testing.T) {
  41. type resourceTest struct {
  42. ConfigCPURealtimePeriod int64
  43. ConfigCPURealtimeRuntime int64
  44. SysInfoCPURealtime bool
  45. ErrorExpected bool
  46. FailureMsg string
  47. }
  48. tests := []resourceTest{
  49. {
  50. ConfigCPURealtimePeriod: 1000,
  51. ConfigCPURealtimeRuntime: 1000,
  52. SysInfoCPURealtime: true,
  53. ErrorExpected: false,
  54. FailureMsg: "Expected valid configuration",
  55. },
  56. {
  57. ConfigCPURealtimePeriod: 5000,
  58. ConfigCPURealtimeRuntime: 5000,
  59. SysInfoCPURealtime: false,
  60. ErrorExpected: true,
  61. FailureMsg: "Expected failure when cpu-rt-period is set but kernel doesn't support it",
  62. },
  63. {
  64. ConfigCPURealtimePeriod: 5000,
  65. ConfigCPURealtimeRuntime: 5000,
  66. SysInfoCPURealtime: false,
  67. ErrorExpected: true,
  68. FailureMsg: "Expected failure when cpu-rt-runtime is set but kernel doesn't support it",
  69. },
  70. {
  71. ConfigCPURealtimePeriod: 5000,
  72. ConfigCPURealtimeRuntime: 10000,
  73. SysInfoCPURealtime: true,
  74. ErrorExpected: true,
  75. FailureMsg: "Expected failure when cpu-rt-runtime is greater than cpu-rt-period",
  76. },
  77. }
  78. for _, rt := range tests {
  79. var hc container.HostConfig
  80. hc.Resources.CPURealtimePeriod = rt.ConfigCPURealtimePeriod
  81. hc.Resources.CPURealtimeRuntime = rt.ConfigCPURealtimeRuntime
  82. var si sysinfo.SysInfo
  83. si.CPURealtime = rt.SysInfoCPURealtime
  84. if err := validateResources(&hc, &si); (err != nil) != rt.ErrorExpected {
  85. t.Fatal(rt.FailureMsg, err)
  86. }
  87. }
  88. }