builder_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. insert https://raw.github.com/dotcloud/docker/master/CHANGELOG.md /tmp/CHANGELOG.md
  14. `
  15. func TestBuild(t *testing.T) {
  16. runtime, err := newTestRuntime()
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. defer nuke(runtime)
  21. builder := NewBuilder(runtime)
  22. img, err := builder.Build(strings.NewReader(Dockerfile), &utils.NopWriter{})
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. container, err := builder.Create(
  27. &Config{
  28. Image: img.Id,
  29. Cmd: []string{"cat", "/tmp/passwd"},
  30. },
  31. )
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. defer runtime.Destroy(container)
  36. output, err := container.Output()
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if string(output) != "root:testpass\n" {
  41. t.Fatalf("Unexpected output. Read '%s', expected '%s'", output, "root:testpass\n")
  42. }
  43. container2, err := builder.Create(
  44. &Config{
  45. Image: img.Id,
  46. Cmd: []string{"ls", "-d", "/var/run/sshd"},
  47. },
  48. )
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. defer runtime.Destroy(container2)
  53. output, err = container2.Output()
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. if string(output) != "/var/run/sshd\n" {
  58. t.Fatal("/var/run/sshd has not been created")
  59. }
  60. container3, err := builder.Create(
  61. &Config{
  62. Image: img.Id,
  63. Cmd: []string{"cat", "/tmp/CHANGELOG.md"},
  64. },
  65. )
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. defer runtime.Destroy(container3)
  70. output, err = container3.Output()
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if len(output) == 0 {
  75. t.Fatal("/tmp/CHANGELOG.md has not been copied")
  76. }
  77. }