config_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package runconfig // import "github.com/docker/docker/runconfig"
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types/container"
  11. networktypes "github.com/docker/docker/api/types/network"
  12. "github.com/docker/docker/api/types/strslice"
  13. "github.com/docker/docker/pkg/sysinfo"
  14. )
  15. type f struct {
  16. file string
  17. entrypoint strslice.StrSlice
  18. }
  19. func TestDecodeContainerConfig(t *testing.T) {
  20. var (
  21. fixtures []f
  22. image string
  23. )
  24. if runtime.GOOS != "windows" {
  25. image = "ubuntu"
  26. fixtures = []f{
  27. {"fixtures/unix/container_config_1_14.json", strslice.StrSlice{}},
  28. {"fixtures/unix/container_config_1_17.json", strslice.StrSlice{"bash"}},
  29. {"fixtures/unix/container_config_1_19.json", strslice.StrSlice{"bash"}},
  30. }
  31. } else {
  32. image = "windows"
  33. fixtures = []f{
  34. {"fixtures/windows/container_config_1_19.json", strslice.StrSlice{"cmd"}},
  35. }
  36. }
  37. for _, f := range fixtures {
  38. f := f
  39. t.Run(f.file, func(t *testing.T) {
  40. b, err := os.ReadFile(f.file)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. c, h, _, err := decodeContainerConfig(bytes.NewReader(b), sysinfo.New())
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if c.Image != image {
  49. t.Fatalf("Expected %s image, found %s", image, c.Image)
  50. }
  51. if len(c.Entrypoint) != len(f.entrypoint) {
  52. t.Fatalf("Expected %v, found %v", f.entrypoint, c.Entrypoint)
  53. }
  54. if h != nil && h.Memory != 1000 {
  55. t.Fatalf("Expected memory to be 1000, found %d", h.Memory)
  56. }
  57. })
  58. }
  59. }
  60. // TestDecodeContainerConfigIsolation validates isolation passed
  61. // to the daemon in the hostConfig structure. Note this is platform specific
  62. // as to what level of container isolation is supported.
  63. func TestDecodeContainerConfigIsolation(t *testing.T) {
  64. // An Invalid isolation level
  65. if _, _, _, err := callDecodeContainerConfigIsolation("invalid"); err != nil {
  66. if !strings.Contains(err.Error(), `Invalid isolation: "invalid"`) {
  67. t.Fatal(err)
  68. }
  69. }
  70. // Blank isolation (== default)
  71. if _, _, _, err := callDecodeContainerConfigIsolation(""); err != nil {
  72. t.Fatal("Blank isolation should have succeeded")
  73. }
  74. // Default isolation
  75. if _, _, _, err := callDecodeContainerConfigIsolation("default"); err != nil {
  76. t.Fatal("default isolation should have succeeded")
  77. }
  78. // Process isolation (Valid on Windows only)
  79. if runtime.GOOS == "windows" {
  80. if _, _, _, err := callDecodeContainerConfigIsolation("process"); err != nil {
  81. t.Fatal("process isolation should have succeeded")
  82. }
  83. } else {
  84. if _, _, _, err := callDecodeContainerConfigIsolation("process"); err != nil {
  85. if !strings.Contains(err.Error(), `Invalid isolation: "process"`) {
  86. t.Fatal(err)
  87. }
  88. }
  89. }
  90. // Hyper-V Containers isolation (Valid on Windows only)
  91. if runtime.GOOS == "windows" {
  92. if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil {
  93. t.Fatal("hyperv isolation should have succeeded")
  94. }
  95. } else {
  96. if _, _, _, err := callDecodeContainerConfigIsolation("hyperv"); err != nil {
  97. if !strings.Contains(err.Error(), `Invalid isolation: "hyperv"`) {
  98. t.Fatal(err)
  99. }
  100. }
  101. }
  102. }
  103. // callDecodeContainerConfigIsolation is a utility function to call
  104. // DecodeContainerConfig for validating isolation
  105. func callDecodeContainerConfigIsolation(isolation string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
  106. var (
  107. b []byte
  108. err error
  109. )
  110. w := ContainerConfigWrapper{
  111. Config: &container.Config{},
  112. HostConfig: &container.HostConfig{
  113. NetworkMode: "none",
  114. Isolation: container.Isolation(isolation),
  115. },
  116. }
  117. if b, err = json.Marshal(w); err != nil {
  118. return nil, nil, nil, fmt.Errorf("Error on marshal %s", err.Error())
  119. }
  120. return decodeContainerConfig(bytes.NewReader(b), sysinfo.New())
  121. }