buildfile_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package docker
  2. import (
  3. "github.com/dotcloud/docker/utils"
  4. "strings"
  5. "testing"
  6. )
  7. const Dockerfile = `
  8. # VERSION 0.1
  9. # DOCKER-VERSION 0.2
  10. from ` + unitTestImageName + `
  11. run sh -c 'echo root:testpass > /tmp/passwd'
  12. run mkdir -p /var/run/sshd
  13. `
  14. const DockerfileNoNewLine = `
  15. # VERSION 0.1
  16. # DOCKER-VERSION 0.2
  17. from ` + unitTestImageName + `
  18. run sh -c 'echo root:testpass > /tmp/passwd'
  19. run mkdir -p /var/run/sshd`
  20. func TestBuild(t *testing.T) {
  21. dockerfiles := []string{Dockerfile, DockerfileNoNewLine}
  22. for _, Dockerfile := range dockerfiles {
  23. runtime, err := newTestRuntime()
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. defer nuke(runtime)
  28. srv := &Server{runtime: runtime}
  29. buildfile := NewBuildFile(srv, &utils.NopWriter{})
  30. imgID, err := buildfile.Build(strings.NewReader(Dockerfile), nil)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. builder := NewBuilder(runtime)
  35. container, err := builder.Create(
  36. &Config{
  37. Image: imgID,
  38. Cmd: []string{"cat", "/tmp/passwd"},
  39. },
  40. )
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. defer runtime.Destroy(container)
  45. output, err := container.Output()
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. if string(output) != "root:testpass\n" {
  50. t.Fatalf("Unexpected output. Read '%s', expected '%s'", output, "root:testpass\n")
  51. }
  52. container2, err := builder.Create(
  53. &Config{
  54. Image: imgID,
  55. Cmd: []string{"ls", "-d", "/var/run/sshd"},
  56. },
  57. )
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. defer runtime.Destroy(container2)
  62. output, err = container2.Output()
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if string(output) != "/var/run/sshd\n" {
  67. t.Fatal("/var/run/sshd has not been created")
  68. }
  69. }
  70. }