builder_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package dockerfile
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/builder/dockerfile/parser"
  8. )
  9. func TestBuildProcessLabels(t *testing.T) {
  10. dockerfile := "FROM scratch"
  11. d := parser.Directive{}
  12. parser.SetEscapeToken(parser.DefaultEscapeToken, &d)
  13. n, err := parser.Parse(strings.NewReader(dockerfile), &d)
  14. if err != nil {
  15. t.Fatalf("Error when parsing Dockerfile: %s", err)
  16. }
  17. options := &types.ImageBuildOptions{
  18. Labels: map[string]string{
  19. "org.e": "cli-e",
  20. "org.d": "cli-d",
  21. "org.c": "cli-c",
  22. "org.b": "cli-b",
  23. "org.a": "cli-a",
  24. },
  25. }
  26. b := &Builder{
  27. runConfig: &container.Config{},
  28. options: options,
  29. directive: d,
  30. dockerfile: n,
  31. }
  32. err = b.processLabels()
  33. if err != nil {
  34. t.Fatalf("Error when processing labels: %s", err)
  35. }
  36. expected := []string{
  37. "FROM scratch",
  38. `LABEL "org.a"='cli-a' "org.b"='cli-b' "org.c"='cli-c' "org.d"='cli-d' "org.e"='cli-e'`,
  39. }
  40. if len(b.dockerfile.Children) != 2 {
  41. t.Fatalf("Expect 2, got %d", len(b.dockerfile.Children))
  42. }
  43. for i, v := range b.dockerfile.Children {
  44. if v.Original != expected[i] {
  45. t.Fatalf("Expect '%s' for %dth children, got, '%s'", expected[i], i, v.Original)
  46. }
  47. }
  48. }