You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
3.5 KiB

  1. package docker_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "gerrit.wikimedia.org/r/blubber/build"
  6. "gerrit.wikimedia.org/r/blubber/docker"
  7. )
  8. func TestRun(t *testing.T) {
  9. i := build.Run{"echo", []string{"hello"}}
  10. di, err := docker.NewInstruction(i)
  11. if assert.NoError(t, err) {
  12. assert.Equal(t, "RUN echo \"hello\"\n", di.Compile())
  13. }
  14. }
  15. func TestRunAll(t *testing.T) {
  16. i := build.RunAll{[]build.Run{
  17. {"echo", []string{"hello"}},
  18. {"echo", []string{"yo"}},
  19. }}
  20. di, err := docker.NewInstruction(i)
  21. if assert.NoError(t, err) {
  22. assert.Equal(t, "RUN echo \"hello\" && echo \"yo\"\n", di.Compile())
  23. }
  24. }
  25. func TestCopy(t *testing.T) {
  26. i := build.Copy{[]string{"foo1", "foo2"}, "bar"}
  27. di, err := docker.NewInstruction(i)
  28. if assert.NoError(t, err) {
  29. assert.Equal(t, "COPY [\"foo1\", \"foo2\", \"bar/\"]\n", di.Compile())
  30. }
  31. }
  32. func TestCopyAs(t *testing.T) {
  33. t.Run("with Copy", func(t *testing.T) {
  34. i := build.CopyAs{123, 124, build.Copy{[]string{"foo1", "foo2"}, "bar"}}
  35. di, err := docker.NewInstruction(i)
  36. if assert.NoError(t, err) {
  37. assert.Equal(t, "COPY --chown=123:124 [\"foo1\", \"foo2\", \"bar/\"]\n", di.Compile())
  38. }
  39. })
  40. t.Run("with CopyFrom", func(t *testing.T) {
  41. i := build.CopyAs{123, 124, build.CopyFrom{"foo", build.Copy{[]string{"foo1", "foo2"}, "bar"}}}
  42. di, err := docker.NewInstruction(i)
  43. if assert.NoError(t, err) {
  44. assert.Equal(t, "COPY --chown=123:124 --from=foo [\"foo1\", \"foo2\", \"bar/\"]\n", di.Compile())
  45. }
  46. })
  47. }
  48. func TestCopyFrom(t *testing.T) {
  49. i := build.CopyFrom{"foo", build.Copy{[]string{"foo1", "foo2"}, "bar"}}
  50. di, err := docker.NewInstruction(i)
  51. if assert.NoError(t, err) {
  52. assert.Equal(t, "COPY --from=foo [\"foo1\", \"foo2\", \"bar/\"]\n", di.Compile())
  53. }
  54. }
  55. func TestEntryPoint(t *testing.T) {
  56. i := build.EntryPoint{[]string{"foo", "bar"}}
  57. di, err := docker.NewInstruction(i)
  58. if assert.NoError(t, err) {
  59. assert.Equal(t, "ENTRYPOINT [\"foo\", \"bar\"]\n", di.Compile())
  60. }
  61. }
  62. func TestEnv(t *testing.T) {
  63. i := build.Env{map[string]string{"foo": "bar", "bar": "foo"}}
  64. di, err := docker.NewInstruction(i)
  65. if assert.NoError(t, err) {
  66. assert.Equal(t, "ENV bar=\"foo\" foo=\"bar\"\n", di.Compile())
  67. }
  68. }
  69. func TestLabel(t *testing.T) {
  70. i := build.Label{map[string]string{"foo": "bar", "bar": "foo"}}
  71. di, err := docker.NewInstruction(i)
  72. if assert.NoError(t, err) {
  73. assert.Equal(t, "LABEL bar=\"foo\" foo=\"bar\"\n", di.Compile())
  74. }
  75. }
  76. func TestUser(t *testing.T) {
  77. i := build.User{"foo"}
  78. di, err := docker.NewInstruction(i)
  79. if assert.NoError(t, err) {
  80. assert.Equal(t, "USER \"foo\"\n", di.Compile())
  81. }
  82. }
  83. func TestWorkingDirectory(t *testing.T) {
  84. i := build.WorkingDirectory{"/foo/dir"}
  85. di, err := docker.NewInstruction(i)
  86. if assert.NoError(t, err) {
  87. assert.Equal(t, "WORKDIR \"/foo/dir\"\n", di.Compile())
  88. }
  89. }
  90. func TestEscapeRun(t *testing.T) {
  91. i := build.Run{"/bin/true\nRUN echo HACKED!", []string{}}
  92. di, err := docker.NewInstruction(i)
  93. if assert.NoError(t, err) {
  94. assert.Equal(t, "RUN /bin/true\\nRUN echo HACKED!\n", di.Compile())
  95. }
  96. }
  97. func TestEscapeCopy(t *testing.T) {
  98. i := build.Copy{[]string{"file.a", "file.b"}, "dest"}
  99. di, err := docker.NewInstruction(i)
  100. if assert.NoError(t, err) {
  101. assert.Equal(t, "COPY [\"file.a\", \"file.b\", \"dest/\"]\n", di.Compile())
  102. }
  103. }
  104. func TestEscapeEnv(t *testing.T) {
  105. i := build.Env{map[string]string{"a": "b\nRUN echo HACKED!"}}
  106. di, err := docker.NewInstruction(i)
  107. if assert.NoError(t, err) {
  108. assert.Equal(t, "ENV a=\"b\\nRUN echo HACKED!\"\n", di.Compile())
  109. }
  110. }