container_test.go 1.3 KB

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