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.

109 lines
2.6 KiB

  1. package config_test
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "gerrit.wikimedia.org/r/blubber/build"
  7. "gerrit.wikimedia.org/r/blubber/config"
  8. )
  9. func TestAptConfigYAML(t *testing.T) {
  10. cfg, err := config.ReadYAMLConfig([]byte(`---
  11. version: v4
  12. apt:
  13. packages:
  14. - libfoo
  15. - libbar
  16. variants:
  17. build:
  18. apt:
  19. packages:
  20. - libfoo-dev`))
  21. assert.Nil(t, err)
  22. assert.Equal(t, []string{"libfoo", "libbar"}, cfg.Apt.Packages)
  23. variant, err := config.ExpandVariant(cfg, "build")
  24. assert.Nil(t, err)
  25. assert.Equal(t, []string{"libfoo", "libbar", "libfoo-dev"}, variant.Apt.Packages)
  26. }
  27. func TestAptConfigInstructions(t *testing.T) {
  28. cfg := config.AptConfig{Packages: []string{"libfoo", "libbar"}}
  29. t.Run("PhasePrivileged", func(t *testing.T) {
  30. assert.Equal(t,
  31. []build.Instruction{
  32. build.Env{map[string]string{
  33. "DEBIAN_FRONTEND": "noninteractive",
  34. }},
  35. build.RunAll{[]build.Run{
  36. {"apt-get update", []string{}},
  37. {"apt-get install -y", []string{"libfoo", "libbar"}},
  38. {"rm -rf /var/lib/apt/lists/*", []string{}},
  39. }},
  40. },
  41. cfg.InstructionsForPhase(build.PhasePrivileged),
  42. )
  43. })
  44. t.Run("PhasePrivilegeDropped", func(t *testing.T) {
  45. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePrivilegeDropped))
  46. })
  47. t.Run("PhasePreInstall", func(t *testing.T) {
  48. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePreInstall))
  49. })
  50. t.Run("PhasePostInstall", func(t *testing.T) {
  51. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePostInstall))
  52. })
  53. }
  54. func TestAptConfigValidation(t *testing.T) {
  55. t.Run("packages", func(t *testing.T) {
  56. t.Run("ok", func(t *testing.T) {
  57. err := config.Validate(config.AptConfig{
  58. Packages: []string{
  59. "f1",
  60. "foo-fighter",
  61. "bar+b.az",
  62. "bar+b.az=0:0.1~foo1-1",
  63. "bar+b.az/stable",
  64. "bar+b.az/jessie-wikimedia",
  65. },
  66. })
  67. assert.False(t, config.IsValidationError(err))
  68. })
  69. t.Run("bad", func(t *testing.T) {
  70. err := config.Validate(config.AptConfig{
  71. Packages: []string{
  72. "f1",
  73. "foo fighter",
  74. "bar_baz",
  75. "bar=0.1*bad version",
  76. "bar/0bad_release",
  77. },
  78. })
  79. if assert.True(t, config.IsValidationError(err)) {
  80. msg := config.HumanizeValidationError(err)
  81. assert.Equal(t, strings.Join([]string{
  82. `packages[1]: "foo fighter" is not a valid Debian package name`,
  83. `packages[2]: "bar_baz" is not a valid Debian package name`,
  84. `packages[3]: "bar=0.1*bad version" is not a valid Debian package name`,
  85. `packages[4]: "bar/0bad_release" is not a valid Debian package name`,
  86. }, "\n"), msg)
  87. }
  88. })
  89. })
  90. }