buildfile_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. func TestBuild(t *testing.T) {
  15. runtime, err := newTestRuntime()
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. defer nuke(runtime)
  20. srv := &Server{runtime: runtime}
  21. buildfile := NewBuildFile(srv, &utils.NopWriter{})
  22. imgId, err := buildfile.Build(strings.NewReader(Dockerfile), nil)
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. builder := NewBuilder(runtime)
  27. container, err := builder.Create(
  28. &Config{
  29. Image: imgId,
  30. Cmd: []string{"cat", "/tmp/passwd"},
  31. },
  32. )
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. defer runtime.Destroy(container)
  37. output, err := container.Output()
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if string(output) != "root:testpass\n" {
  42. t.Fatalf("Unexpected output. Read '%s', expected '%s'", output, "root:testpass\n")
  43. }
  44. container2, err := builder.Create(
  45. &Config{
  46. Image: imgId,
  47. Cmd: []string{"ls", "-d", "/var/run/sshd"},
  48. },
  49. )
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. defer runtime.Destroy(container2)
  54. output, err = container2.Output()
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. if string(output) != "/var/run/sshd\n" {
  59. t.Fatal("/var/run/sshd has not been created")
  60. }
  61. }