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.

147 lines
3.6 KiB

  1. package config_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "gerrit.wikimedia.org/r/blubber/build"
  6. "gerrit.wikimedia.org/r/blubber/config"
  7. )
  8. func TestLivesConfigYAML(t *testing.T) {
  9. cfg, err := config.ReadYAMLConfig([]byte(`---
  10. version: v4
  11. base: foo
  12. lives:
  13. in: /some/directory
  14. as: foouser
  15. uid: 123
  16. gid: 223
  17. variants:
  18. development: {}`))
  19. if assert.NoError(t, err) {
  20. assert.Equal(t, "/some/directory", cfg.Lives.In)
  21. assert.Equal(t, "foouser", cfg.Lives.As)
  22. assert.Equal(t, uint(123), cfg.Lives.UID)
  23. assert.Equal(t, uint(223), cfg.Lives.GID)
  24. variant, err := config.ExpandVariant(cfg, "development")
  25. if assert.NoError(t, err) {
  26. assert.Equal(t, "/some/directory", variant.Lives.In)
  27. assert.Equal(t, "foouser", variant.Lives.As)
  28. assert.Equal(t, uint(123), variant.Lives.UID)
  29. assert.Equal(t, uint(223), variant.Lives.GID)
  30. }
  31. }
  32. }
  33. func TestLivesConfigDefaults(t *testing.T) {
  34. cfg, err := config.ReadYAMLConfig([]byte(`---
  35. version: v4
  36. base: foo`))
  37. if assert.NoError(t, err) {
  38. assert.Equal(t, "somebody", cfg.Lives.As)
  39. assert.Equal(t, uint(65533), cfg.Lives.UID)
  40. assert.Equal(t, uint(65533), cfg.Lives.GID)
  41. }
  42. }
  43. func TestLivesConfigInstructions(t *testing.T) {
  44. cfg := config.LivesConfig{
  45. In: "/some/directory",
  46. UserConfig: config.UserConfig{
  47. As: "foouser",
  48. UID: 123,
  49. GID: 223,
  50. },
  51. }
  52. t.Run("PhasePrivileged", func(t *testing.T) {
  53. assert.Equal(t,
  54. []build.Instruction{build.RunAll{[]build.Run{
  55. {"groupadd -o -g %s -r", []string{"223", "foouser"}},
  56. {"useradd -o -m -d %s -r -g %s -u %s", []string{"/home/foouser", "foouser", "123", "foouser"}},
  57. {"mkdir -p", []string{"/some/directory"}},
  58. {"chown %s:%s", []string{"123", "223", "/some/directory"}},
  59. {"mkdir -p", []string{"/opt/lib"}},
  60. {"chown %s:%s", []string{"123", "223", "/opt/lib"}},
  61. }}},
  62. cfg.InstructionsForPhase(build.PhasePrivileged),
  63. )
  64. })
  65. t.Run("PhasePrivilegeDropped", func(t *testing.T) {
  66. assert.Equal(t,
  67. []build.Instruction{
  68. build.WorkingDirectory{"/some/directory"},
  69. },
  70. cfg.InstructionsForPhase(build.PhasePrivilegeDropped),
  71. )
  72. })
  73. t.Run("PhasePreInstall", func(t *testing.T) {
  74. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePreInstall))
  75. })
  76. t.Run("PhasePostInstall", func(t *testing.T) {
  77. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePostInstall))
  78. })
  79. }
  80. func TestLivesConfigValidation(t *testing.T) {
  81. t.Run("in", func(t *testing.T) {
  82. t.Run("ok", func(t *testing.T) {
  83. err := config.Validate(config.LivesConfig{
  84. In: "/foo",
  85. })
  86. assert.False(t, config.IsValidationError(err))
  87. })
  88. t.Run("optional", func(t *testing.T) {
  89. err := config.Validate(config.LivesConfig{})
  90. assert.False(t, config.IsValidationError(err))
  91. })
  92. t.Run("non-root", func(t *testing.T) {
  93. err := config.Validate(config.LivesConfig{
  94. In: "/",
  95. })
  96. if assert.True(t, config.IsValidationError(err)) {
  97. msg := config.HumanizeValidationError(err)
  98. assert.Equal(t, `in: "/" is not a valid absolute non-root path`, msg)
  99. }
  100. })
  101. t.Run("non-root tricky", func(t *testing.T) {
  102. err := config.Validate(config.LivesConfig{
  103. In: "/foo/..",
  104. })
  105. if assert.True(t, config.IsValidationError(err)) {
  106. msg := config.HumanizeValidationError(err)
  107. assert.Equal(t, `in: "/foo/.." is not a valid absolute non-root path`, msg)
  108. }
  109. })
  110. t.Run("absolute", func(t *testing.T) {
  111. err := config.Validate(config.LivesConfig{
  112. In: "foo/bar",
  113. })
  114. if assert.True(t, config.IsValidationError(err)) {
  115. msg := config.HumanizeValidationError(err)
  116. assert.Equal(t, `in: "foo/bar" is not a valid absolute non-root path`, msg)
  117. }
  118. })
  119. })
  120. }