container_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package libcontainer
  2. import (
  3. "encoding/json"
  4. "os"
  5. "testing"
  6. "github.com/dotcloud/docker/pkg/libcontainer/mount/nodes"
  7. )
  8. // Checks whether the expected capability is specified in the capabilities.
  9. func contains(expected string, values []string) bool {
  10. for _, v := range values {
  11. if v == expected {
  12. return true
  13. }
  14. }
  15. return false
  16. }
  17. func TestContainerJsonFormat(t *testing.T) {
  18. f, err := os.Open("container.json")
  19. if err != nil {
  20. t.Fatal("Unable to open container.json")
  21. }
  22. defer f.Close()
  23. var container *Container
  24. if err := json.NewDecoder(f).Decode(&container); err != nil {
  25. t.Fatalf("failed to decode container config: %s", err)
  26. }
  27. if container.Hostname != "koye" {
  28. t.Log("hostname is not set")
  29. t.Fail()
  30. }
  31. if !container.Tty {
  32. t.Log("tty should be set to true")
  33. t.Fail()
  34. }
  35. if !container.Namespaces["NEWNET"] {
  36. t.Log("namespaces should contain NEWNET")
  37. t.Fail()
  38. }
  39. if container.Namespaces["NEWUSER"] {
  40. t.Log("namespaces should not contain NEWUSER")
  41. t.Fail()
  42. }
  43. if contains("SYS_ADMIN", container.Capabilities) {
  44. t.Log("SYS_ADMIN should not be enabled in capabilities mask")
  45. t.Fail()
  46. }
  47. if !contains("MKNOD", container.Capabilities) {
  48. t.Log("MKNOD should be enabled in capabilities mask")
  49. t.Fail()
  50. }
  51. if contains("SYS_CHROOT", container.Capabilities) {
  52. t.Log("capabilities mask should not contain SYS_CHROOT")
  53. t.Fail()
  54. }
  55. for _, n := range nodes.DefaultNodes {
  56. if !contains(n, container.RequiredDeviceNodes) {
  57. t.Logf("devices should contain %s", n)
  58. t.Fail()
  59. }
  60. }
  61. }