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.

123 lines
2.7 KiB

  1. package config_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "gerrit.wikimedia.org/r/blubber/config"
  7. )
  8. func ExampleResolveIncludes() {
  9. cfg, _ := config.ReadYAMLConfig([]byte(`---
  10. version: v4
  11. variants:
  12. varA: { includes: [varB, varC] }
  13. varB: { includes: [varD, varE] }
  14. varC: {}
  15. varD: { includes: [varF] }
  16. varE: {}
  17. varF: {}`))
  18. includes, _ := config.ResolveIncludes(cfg, "varA")
  19. fmt.Printf("%v\n", includes)
  20. // Output: [varF varD varE varB varC varA]
  21. }
  22. func TestReadYAMLConfigErrorsOnUnknownYAML(t *testing.T) {
  23. _, err := config.ReadYAMLConfig([]byte(`---
  24. version: v4
  25. newphone: whodis
  26. variants:
  27. foo: {}`))
  28. assert.EqualError(t,
  29. err,
  30. `json: unknown field "newphone"`)
  31. }
  32. func TestReadYAMLConfigValidateVersionBeforeStrictUnmarshal(t *testing.T) {
  33. _, err := config.ReadYAMLConfig([]byte(`---
  34. version: foo
  35. newphone: whodis
  36. variants:
  37. foo: {}`))
  38. if assert.True(t, config.IsValidationError(err)) {
  39. msg := config.HumanizeValidationError(err)
  40. assert.Equal(t, `version: config version "foo" is unsupported`, msg)
  41. }
  42. }
  43. func TestResolveIncludesPreventsInfiniteRecursion(t *testing.T) {
  44. cfg, err := config.ReadYAMLConfig([]byte(`---
  45. version: v4
  46. variants:
  47. varA: { includes: [varB] }
  48. varB: { includes: [varA] }`))
  49. assert.NoError(t, err)
  50. _, err2 := config.ResolveIncludes(cfg, "varA")
  51. assert.EqualError(t, err2, "variant expansion detected loop")
  52. }
  53. func TestMultiLevelIncludes(t *testing.T) {
  54. cfg, err := config.ReadYAMLConfig([]byte(`---
  55. version: v4
  56. base: foo-slim
  57. variants:
  58. build:
  59. base: foo-devel
  60. runs: { as: foo }
  61. development:
  62. includes: [build]
  63. runs: { uid: 123 }
  64. test:
  65. includes: [development]
  66. runs: { insecurely: true }`))
  67. if assert.NoError(t, err) {
  68. dev, _ := config.ExpandVariant(cfg, "development")
  69. assert.Equal(t, "foo-devel", dev.Base)
  70. assert.Equal(t, "foo", dev.Runs.As)
  71. assert.Equal(t, uint(123), dev.Runs.UID)
  72. test, _ := config.ExpandVariant(cfg, "test")
  73. assert.Equal(t, "foo-devel", test.Base)
  74. assert.Equal(t, "foo", test.Runs.As)
  75. assert.Equal(t, uint(123), test.Runs.UID)
  76. assert.True(t, test.Runs.Insecurely.True)
  77. }
  78. }
  79. func TestMultiIncludes(t *testing.T) {
  80. cfg, err := config.ReadYAMLConfig([]byte(`---
  81. version: v4
  82. variants:
  83. mammal:
  84. base: neutral
  85. human:
  86. base: moral
  87. includes: [mammal]
  88. lizard:
  89. base: immoral
  90. lizardman:
  91. includes: [human, lizard]`))
  92. if assert.NoError(t, err) {
  93. variant, err := config.ExpandVariant(cfg, "lizardman")
  94. if assert.NoError(t, err) {
  95. assert.Equal(t, "immoral", variant.Base)
  96. }
  97. }
  98. }