config_reloader_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package config
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. "github.com/spf13/viper"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. var stringEnvConfigYaml = `
  10. pageTitle: ${{ INPUT }}
  11. `
  12. var stringEnvInterpolationConfigYaml = `
  13. pageTitle: Olivetin - ${{ INPUT }}
  14. `
  15. var boolEnvConfigYaml = `
  16. checkForUpdates: ${{ INPUT }}
  17. `
  18. var numericEnvConfigYaml = `
  19. logHistoryPageSize: ${{ INPUT }}
  20. `
  21. var argsSyntaxConfigYaml = `
  22. actions:
  23. - title: Ping host
  24. id: ping_host
  25. shell: ping {{ host }} -c ${{ INPUT }}
  26. icon: ping
  27. timeout: 100
  28. popupOnStart: execution-dialog-stdout-only
  29. arguments:
  30. - name: host
  31. title: Host
  32. type: ascii_identifier
  33. default: example.com
  34. description: The host that you want to ping
  35. `
  36. func pageTitleSelector(cfg *Config) any {
  37. return cfg.PageTitle
  38. }
  39. func checkForUpdatesSelector(cfg *Config) any {
  40. return cfg.CheckForUpdates
  41. }
  42. func logHistoryPageSizeSelector(cfg *Config) any {
  43. return cfg.LogHistoryPageSize
  44. }
  45. var envConfigTests = []struct {
  46. yaml string
  47. input string
  48. output any
  49. selector func(*Config) any
  50. }{
  51. // Test that it works for string type config fields, both standalone and as part of a larger string value.
  52. {stringEnvConfigYaml, "A Nice Title", "A Nice Title", pageTitleSelector},
  53. {stringEnvInterpolationConfigYaml, "A Nice Title", "Olivetin - A Nice Title", pageTitleSelector},
  54. // Test that unset variables turn into empty strings.
  55. {stringEnvConfigYaml, "", "", pageTitleSelector},
  56. // Test that it works for bool type config fields for intuitive bool->string conversions.
  57. {boolEnvConfigYaml, "FALSE", false, checkForUpdatesSelector},
  58. {boolEnvConfigYaml, "false", false, checkForUpdatesSelector},
  59. {boolEnvConfigYaml, "False", false, checkForUpdatesSelector},
  60. {boolEnvConfigYaml, "TRUE", true, checkForUpdatesSelector},
  61. {boolEnvConfigYaml, "true", true, checkForUpdatesSelector},
  62. {boolEnvConfigYaml, "True", true, checkForUpdatesSelector},
  63. {boolEnvConfigYaml, "0", false, checkForUpdatesSelector},
  64. {boolEnvConfigYaml, "1", true, checkForUpdatesSelector},
  65. // Test that unset variables turn into false bools.
  66. {boolEnvConfigYaml, "", false, checkForUpdatesSelector},
  67. // Test that it works for numeric type config fields.
  68. {numericEnvConfigYaml, "2048", int64(2048), logHistoryPageSizeSelector},
  69. // Test that unset variables turn into zero numbers.
  70. {numericEnvConfigYaml, "", int64(0), logHistoryPageSizeSelector},
  71. // Test that it doesn't interfere with similar arguments
  72. {argsSyntaxConfigYaml, "5", "ping {{ host }} -c 5", func(cfg *Config) any { return cfg.Actions[0].Shell }},
  73. }
  74. func TestEnvInConfig(t *testing.T) {
  75. viper.SetConfigType("yaml")
  76. for _, tt := range envConfigTests {
  77. err := viper.ReadConfig(strings.NewReader(tt.yaml))
  78. assert.Nil(t, err, "Viper read config file with environment variable syntax")
  79. if tt.input != "" {
  80. os.Setenv("INPUT", tt.input)
  81. }
  82. cfg := DefaultConfig()
  83. Reload(cfg)
  84. field := tt.selector(cfg)
  85. assert.Equal(t, tt.output, field, "Unmarshaled config field doesn't match expected value: env=\"%s\"", tt.input)
  86. os.Unsetenv("INPUT")
  87. }
  88. }