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.

166 lines
4.1 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 TestRunsConfigYAML(t *testing.T) {
  9. cfg, err := config.ReadYAMLConfig([]byte(`---
  10. version: v4
  11. base: foo
  12. runs:
  13. as: someuser
  14. insecurely: true
  15. uid: 666
  16. gid: 777
  17. environment: { FOO: bar }
  18. variants:
  19. development: {}`))
  20. assert.Nil(t, err)
  21. variant, err := config.ExpandVariant(cfg, "development")
  22. assert.Nil(t, err)
  23. assert.Equal(t, "someuser", variant.Runs.As)
  24. assert.Equal(t, true, variant.Runs.Insecurely.True)
  25. assert.Equal(t, uint(666), variant.Runs.UID)
  26. assert.Equal(t, uint(777), variant.Runs.GID)
  27. assert.Equal(t, map[string]string{"FOO": "bar"}, variant.Runs.Environment)
  28. }
  29. func TestRunsConfigInstructions(t *testing.T) {
  30. cfg := config.RunsConfig{
  31. UserConfig: config.UserConfig{
  32. As: "someuser",
  33. UID: 666,
  34. GID: 777,
  35. },
  36. Environment: map[string]string{
  37. "fooname": "foovalue",
  38. "barname": "barvalue",
  39. },
  40. }
  41. t.Run("PhasePrivileged", func(t *testing.T) {
  42. assert.Equal(t,
  43. []build.Instruction{build.RunAll{[]build.Run{
  44. {"groupadd -o -g %s -r", []string{"777", "someuser"}},
  45. {"useradd -o -m -d %s -r -g %s -u %s", []string{"/home/someuser", "someuser", "666", "someuser"}},
  46. }}},
  47. cfg.InstructionsForPhase(build.PhasePrivileged),
  48. )
  49. })
  50. t.Run("PhasePrivilegeDropped", func(t *testing.T) {
  51. assert.Equal(t,
  52. []build.Instruction{
  53. build.Env{map[string]string{"barname": "barvalue", "fooname": "foovalue"}},
  54. },
  55. cfg.InstructionsForPhase(build.PhasePrivilegeDropped),
  56. )
  57. t.Run("with empty Environment", func(t *testing.T) {
  58. cfg.Environment = map[string]string{}
  59. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePrivilegeDropped))
  60. })
  61. })
  62. t.Run("PhasePreInstall", func(t *testing.T) {
  63. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePreInstall))
  64. })
  65. t.Run("PhasePostInstall", func(t *testing.T) {
  66. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePostInstall))
  67. })
  68. }
  69. func TestRunsConfigValidation(t *testing.T) {
  70. t.Run("environment", func(t *testing.T) {
  71. t.Run("ok", func(t *testing.T) {
  72. err := config.Validate(config.RunsConfig{
  73. Environment: map[string]string{
  74. "foo": "bar",
  75. "f1oo": "bar",
  76. "FOO": "bar",
  77. "foo_fighter": "bar",
  78. "FOO_FIGHTER": "bar",
  79. "_FOO_FIGHTER": "bar",
  80. },
  81. })
  82. assert.False(t, config.IsValidationError(err))
  83. })
  84. t.Run("optional", func(t *testing.T) {
  85. err := config.Validate(config.RunsConfig{})
  86. assert.False(t, config.IsValidationError(err))
  87. })
  88. t.Run("bad", func(t *testing.T) {
  89. t.Run("spaces", func(t *testing.T) {
  90. err := config.Validate(config.RunsConfig{
  91. Environment: map[string]string{
  92. "foo fighter": "bar",
  93. },
  94. })
  95. if assert.True(t, config.IsValidationError(err)) {
  96. msg := config.HumanizeValidationError(err)
  97. assert.Equal(t, `environment: contains invalid environment variable names`, msg)
  98. }
  99. })
  100. t.Run("dashes", func(t *testing.T) {
  101. err := config.Validate(config.RunsConfig{
  102. Environment: map[string]string{
  103. "foo-fighter": "bar",
  104. },
  105. })
  106. if assert.True(t, config.IsValidationError(err)) {
  107. msg := config.HumanizeValidationError(err)
  108. assert.Equal(t, `environment: contains invalid environment variable names`, msg)
  109. }
  110. })
  111. t.Run("dots", func(t *testing.T) {
  112. err := config.Validate(config.RunsConfig{
  113. Environment: map[string]string{
  114. "foo.fighter": "bar",
  115. },
  116. })
  117. if assert.True(t, config.IsValidationError(err)) {
  118. msg := config.HumanizeValidationError(err)
  119. assert.Equal(t, `environment: contains invalid environment variable names`, msg)
  120. }
  121. })
  122. t.Run("starts with number", func(t *testing.T) {
  123. err := config.Validate(config.RunsConfig{
  124. Environment: map[string]string{
  125. "1foo": "bar",
  126. },
  127. })
  128. if assert.True(t, config.IsValidationError(err)) {
  129. msg := config.HumanizeValidationError(err)
  130. assert.Equal(t, `environment: contains invalid environment variable names`, msg)
  131. }
  132. })
  133. })
  134. })
  135. }