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.

184 lines
4.5 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 TestNodeConfigYAML(t *testing.T) {
  9. cfg, err := config.ReadYAMLConfig([]byte(`---
  10. version: v4
  11. base: foo
  12. node:
  13. requirements: [package.json]
  14. env: foo
  15. variants:
  16. build:
  17. node:
  18. requirements: []
  19. env: bar`))
  20. if assert.NoError(t, err) {
  21. assert.Equal(t, []string{"package.json"}, cfg.Node.Requirements)
  22. assert.Equal(t, "foo", cfg.Node.Env)
  23. variant, err := config.ExpandVariant(cfg, "build")
  24. if assert.NoError(t, err) {
  25. assert.Empty(t, variant.Node.Requirements)
  26. assert.Equal(t, "bar", variant.Node.Env)
  27. }
  28. }
  29. }
  30. func TestNodeConfigInstructionsNoRequirements(t *testing.T) {
  31. cfg := config.NodeConfig{}
  32. t.Run("PhasePrivileged", func(t *testing.T) {
  33. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePrivileged))
  34. })
  35. t.Run("PhasePrivilegeDropped", func(t *testing.T) {
  36. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePrivilegeDropped))
  37. })
  38. t.Run("PhasePreInstall", func(t *testing.T) {
  39. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePreInstall))
  40. })
  41. t.Run("PhasePostInstall", func(t *testing.T) {
  42. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePostInstall))
  43. })
  44. }
  45. func TestNodeConfigInstructionsNonProduction(t *testing.T) {
  46. cfg := config.NodeConfig{Requirements: []string{"package.json"}, Env: "foo"}
  47. t.Run("PhasePrivileged", func(t *testing.T) {
  48. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePrivileged))
  49. })
  50. t.Run("PhasePrivilegeDropped", func(t *testing.T) {
  51. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePrivilegeDropped))
  52. })
  53. t.Run("PhasePreInstall", func(t *testing.T) {
  54. assert.Equal(t,
  55. []build.Instruction{
  56. build.Copy{[]string{"package.json"}, "./"},
  57. build.RunAll{[]build.Run{
  58. {"npm install", []string{}},
  59. }},
  60. },
  61. cfg.InstructionsForPhase(build.PhasePreInstall),
  62. )
  63. })
  64. t.Run("PhasePostInstall", func(t *testing.T) {
  65. assert.Equal(t,
  66. []build.Instruction{
  67. build.Env{map[string]string{
  68. "NODE_ENV": "foo",
  69. }},
  70. },
  71. cfg.InstructionsForPhase(build.PhasePostInstall),
  72. )
  73. })
  74. }
  75. func TestNodeConfigInstructionsProduction(t *testing.T) {
  76. cfg := config.NodeConfig{Requirements: []string{"package.json", "package-lock.json"}, Env: "production"}
  77. t.Run("PhasePrivileged", func(t *testing.T) {
  78. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePrivileged))
  79. })
  80. t.Run("PhasePrivilegeDropped", func(t *testing.T) {
  81. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePrivilegeDropped))
  82. })
  83. t.Run("PhasePreInstall", func(t *testing.T) {
  84. assert.Equal(t,
  85. []build.Instruction{
  86. build.Copy{[]string{"package.json", "package-lock.json"}, "./"},
  87. build.RunAll{[]build.Run{
  88. {"npm install", []string{"--production"}},
  89. {"npm dedupe", []string{}},
  90. }},
  91. },
  92. cfg.InstructionsForPhase(build.PhasePreInstall),
  93. )
  94. })
  95. t.Run("PhasePostInstall", func(t *testing.T) {
  96. assert.Equal(t,
  97. []build.Instruction{
  98. build.Env{map[string]string{
  99. "NODE_ENV": "production",
  100. }},
  101. },
  102. cfg.InstructionsForPhase(build.PhasePostInstall),
  103. )
  104. })
  105. }
  106. func TestNodeConfigInstructionsEnvironmentOnly(t *testing.T) {
  107. cfg := config.NodeConfig{Env: "production"}
  108. t.Run("PhasePrivileged", func(t *testing.T) {
  109. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePrivileged))
  110. })
  111. t.Run("PhasePrivilegeDropped", func(t *testing.T) {
  112. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePrivilegeDropped))
  113. })
  114. t.Run("PhasePreInstall", func(t *testing.T) {
  115. assert.Empty(t, cfg.InstructionsForPhase(build.PhasePreInstall))
  116. })
  117. t.Run("PhasePostInstall", func(t *testing.T) {
  118. assert.Equal(t,
  119. []build.Instruction{
  120. build.Env{map[string]string{
  121. "NODE_ENV": "production",
  122. }},
  123. },
  124. cfg.InstructionsForPhase(build.PhasePostInstall),
  125. )
  126. })
  127. }
  128. func TestNodeConfigValidation(t *testing.T) {
  129. t.Run("env", func(t *testing.T) {
  130. t.Run("ok", func(t *testing.T) {
  131. err := config.Validate(config.NodeConfig{
  132. Env: "production",
  133. })
  134. assert.False(t, config.IsValidationError(err))
  135. })
  136. t.Run("optional", func(t *testing.T) {
  137. err := config.Validate(config.NodeConfig{})
  138. assert.False(t, config.IsValidationError(err))
  139. })
  140. t.Run("bad", func(t *testing.T) {
  141. err := config.Validate(config.NodeConfig{
  142. Env: "foo bar",
  143. })
  144. if assert.True(t, config.IsValidationError(err)) {
  145. msg := config.HumanizeValidationError(err)
  146. assert.Equal(t, `env: "foo bar" is not a valid Node environment name`, msg)
  147. }
  148. })
  149. })
  150. }