config_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package runconfig
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  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. )
  14. type f struct {
  15. file string
  16. entrypoint strslice.StrSlice
  17. }
  18. func TestDecodeContainerConfig(t *testing.T) {
  19. var (
  20. fixtures []f
  21. image string
  22. )
  23. //TODO: Should run for Solaris
  24. if runtime.GOOS == "solaris" {
  25. t.Skip()
  26. }
  27. if runtime.GOOS != "windows" {
  28. image = "ubuntu"
  29. fixtures = []f{
  30. {"fixtures/unix/container_config_1_14.json", strslice.StrSlice{}},
  31. {"fixtures/unix/container_config_1_17.json", strslice.StrSlice{"bash"}},
  32. {"fixtures/unix/container_config_1_19.json", strslice.StrSlice{"bash"}},
  33. }
  34. } else {
  35. image = "windows"
  36. fixtures = []f{
  37. {"fixtures/windows/container_config_1_19.json", strslice.StrSlice{"cmd"}},
  38. }
  39. }
  40. for _, f := range fixtures {
  41. b, err := ioutil.ReadFile(f.file)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. c, h, _, err := DecodeContainerConfig(bytes.NewReader(b))
  46. if err != nil {
  47. t.Fatal(fmt.Errorf("Error parsing %s: %v", f, err))
  48. }
  49. if c.Image != image {
  50. t.Fatalf("Expected %s image, found %s\n", image, c.Image)
  51. }
  52. if len(c.Entrypoint) != len(f.entrypoint) {
  53. t.Fatalf("Expected %v, found %v\n", f.entrypoint, c.Entrypoint)
  54. }
  55. if h != nil && h.Memory != 1000 {
  56. t.Fatalf("Expected memory to be 1000, found %d\n", h.Memory)
  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. if b, err = json.Marshal(w); err != nil {
  117. return nil, nil, nil, fmt.Errorf("Error on marshal %s", err.Error())
  118. }
  119. return DecodeContainerConfig(bytes.NewReader(b))
  120. }