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.

129 lines
3.2 KiB

  1. package build_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "gerrit.wikimedia.org/r/blubber/build"
  6. )
  7. func TestApplyUser(t *testing.T) {
  8. instructions := []build.Instruction{
  9. build.Copy{[]string{"foo"}, "bar"},
  10. build.Copy{[]string{"baz"}, "qux"},
  11. build.CopyFrom{"foo", build.Copy{[]string{"a"}, "b"}},
  12. }
  13. assert.Equal(t,
  14. []build.Instruction{
  15. build.CopyAs{123, 223, build.Copy{[]string{"foo"}, "bar"}},
  16. build.CopyAs{123, 223, build.Copy{[]string{"baz"}, "qux"}},
  17. build.CopyAs{123, 223, build.CopyFrom{"foo", build.Copy{[]string{"a"}, "b"}}},
  18. },
  19. build.ApplyUser(123, 223, instructions),
  20. )
  21. }
  22. func TestChown(t *testing.T) {
  23. i := build.Chown(123, 124, "/foo")
  24. assert.Equal(t, []string{`chown "123":"124" "/foo"`}, i.Compile())
  25. }
  26. func TestCreateDirectories(t *testing.T) {
  27. i := build.CreateDirectories([]string{"/foo", "/bar"})
  28. assert.Equal(t, []string{`mkdir -p "/foo" "/bar"`}, i.Compile())
  29. }
  30. func TestCreateDirectory(t *testing.T) {
  31. i := build.CreateDirectory("/foo")
  32. assert.Equal(t, []string{`mkdir -p "/foo"`}, i.Compile())
  33. }
  34. func TestCreateUser(t *testing.T) {
  35. i := build.CreateUser("foo", 123, 124)
  36. if assert.Len(t, i, 2) {
  37. assert.Equal(t, []string{`groupadd -o -g "124" -r "foo"`}, i[0].Compile())
  38. assert.Equal(t, []string{`useradd -o -m -d "/home/foo" -r -g "foo" -u "123" "foo"`}, i[1].Compile())
  39. }
  40. }
  41. func TestHome(t *testing.T) {
  42. t.Run("root", func(t *testing.T) {
  43. assert.Equal(t,
  44. build.Env{map[string]string{"HOME": "/root"}},
  45. build.Home("root"),
  46. )
  47. })
  48. t.Run("non-root", func(t *testing.T) {
  49. assert.Equal(t,
  50. build.Env{map[string]string{"HOME": "/home/foo"}},
  51. build.Home("foo"),
  52. )
  53. })
  54. }
  55. func TestSortFilesByDir(t *testing.T) {
  56. files := []string{"foo", "./bar", "./d/d-foo", "./c/c/c-foo", "b/b-foo", "b/b-bar", "a/a-foo"}
  57. sortedDirs, filesByDir := build.SortFilesByDir(files)
  58. assert.Equal(t,
  59. []string{
  60. "./",
  61. "a/",
  62. "b/",
  63. "c/c/",
  64. "d/",
  65. },
  66. sortedDirs,
  67. )
  68. assert.Equal(t,
  69. map[string][]string{
  70. "./": []string{"foo", "bar"},
  71. "d/": []string{"d/d-foo"},
  72. "c/c/": []string{"c/c/c-foo"},
  73. "b/": []string{"b/b-foo", "b/b-bar"},
  74. "a/": []string{"a/a-foo"},
  75. },
  76. filesByDir,
  77. )
  78. }
  79. func TestSyncFiles(t *testing.T) {
  80. files := []string{"foo", "./bar", "./d/d-foo", "./c/c/c-foo", "b/b-foo", "b/b-bar", "a/a-foo"}
  81. assert.Equal(t,
  82. []build.Instruction{
  83. build.Run{"mkdir -p", []string{"a/", "b/", "c/c/", "d/"}},
  84. build.Copy{[]string{"foo", "bar"}, "./"},
  85. build.Copy{[]string{"a/a-foo"}, "a/"},
  86. build.Copy{[]string{"b/b-foo", "b/b-bar"}, "b/"},
  87. build.Copy{[]string{"c/c/c-foo"}, "c/c/"},
  88. build.Copy{[]string{"d/d-foo"}, "d/"},
  89. },
  90. build.SyncFiles(files, "."),
  91. )
  92. }
  93. func TestSyncFilesWithDestination(t *testing.T) {
  94. files := []string{"foo", "./bar", "./d/d-foo", "./c/c/c-foo", "b/b-foo", "b/b-bar", "a/a-foo"}
  95. assert.Equal(t,
  96. []build.Instruction{
  97. build.Run{"mkdir -p", []string{"/dir/a/", "/dir/b/", "/dir/c/c/", "/dir/d/"}},
  98. build.Copy{[]string{"foo", "bar"}, "/dir/"},
  99. build.Copy{[]string{"a/a-foo"}, "/dir/a/"},
  100. build.Copy{[]string{"b/b-foo", "b/b-bar"}, "/dir/b/"},
  101. build.Copy{[]string{"c/c/c-foo"}, "/dir/c/c/"},
  102. build.Copy{[]string{"d/d-foo"}, "/dir/d/"},
  103. },
  104. build.SyncFiles(files, "/dir"),
  105. )
  106. }