env_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package env
  2. import (
  3. "encoding/json"
  4. "os"
  5. "testing"
  6. "time"
  7. "gogs.baozhida.cn/zoie/OAuth-core/config/source"
  8. )
  9. func TestEnv_Read(t *testing.T) {
  10. expected := map[string]map[string]string{
  11. "database": {
  12. "host": "localhost",
  13. "password": "password",
  14. "datasource": "user:password@tcp(localhost:port)/db?charset=utf8mb4&parseTime=True&loc=Local",
  15. },
  16. }
  17. os.Setenv("DATABASE_HOST", "localhost")
  18. os.Setenv("DATABASE_PASSWORD", "password")
  19. os.Setenv("DATABASE_DATASOURCE", "user:password@tcp(localhost:port)/db?charset=utf8mb4&parseTime=True&loc=Local")
  20. source := NewSource()
  21. c, err := source.Read()
  22. if err != nil {
  23. t.Error(err)
  24. }
  25. var actual map[string]interface{}
  26. if err := json.Unmarshal(c.Data, &actual); err != nil {
  27. t.Error(err)
  28. }
  29. actualDB := actual["database"].(map[string]interface{})
  30. for k, v := range expected["database"] {
  31. a := actualDB[k]
  32. if a != v {
  33. t.Errorf("expected %v got %v", v, a)
  34. }
  35. }
  36. }
  37. func TestEnvvar_Prefixes(t *testing.T) {
  38. os.Setenv("APP_DATABASE_HOST", "localhost")
  39. os.Setenv("APP_DATABASE_PASSWORD", "password")
  40. os.Setenv("VAULT_ADDR", "vault:1337")
  41. os.Setenv("GO_ADMIN_CORE_REGISTRY", "mdns")
  42. var prefixtests = []struct {
  43. prefixOpts []source.Option
  44. expectedKeys []string
  45. }{
  46. {[]source.Option{WithPrefix("APP", "GO_ADMIN_CORE")}, []string{"app", "go_admin_core"}},
  47. {[]source.Option{WithPrefix("GO_ADMIN_CORE"), WithStrippedPrefix("APP")}, []string{"database", "go_admin_core"}},
  48. {[]source.Option{WithPrefix("GO_ADMIN_CORE"), WithStrippedPrefix("APP")}, []string{"database", "go_admin_core"}},
  49. }
  50. for _, pt := range prefixtests {
  51. source := NewSource(pt.prefixOpts...)
  52. c, err := source.Read()
  53. if err != nil {
  54. t.Error(err)
  55. }
  56. var actual map[string]interface{}
  57. if err := json.Unmarshal(c.Data, &actual); err != nil {
  58. t.Error(err)
  59. }
  60. // assert other prefixes ignored
  61. if l := len(actual); l != len(pt.expectedKeys) {
  62. t.Errorf("expected %v top keys, got %v", len(pt.expectedKeys), l)
  63. }
  64. for _, k := range pt.expectedKeys {
  65. if !containsKey(actual, k) {
  66. t.Errorf("expected key %v, not found", k)
  67. }
  68. }
  69. }
  70. }
  71. func TestEnvvar_WatchNextNoOpsUntilStop(t *testing.T) {
  72. src := NewSource(WithStrippedPrefix("GO_ADMIN_CORE_"))
  73. w, err := src.Watch()
  74. if err != nil {
  75. t.Error(err)
  76. }
  77. go func() {
  78. time.Sleep(50 * time.Millisecond)
  79. w.Stop()
  80. }()
  81. if _, err := w.Next(); err != source.ErrWatcherStopped {
  82. t.Errorf("expected watcher stopped error, got %v", err)
  83. }
  84. }
  85. func containsKey(m map[string]interface{}, s string) bool {
  86. for k := range m {
  87. if k == s {
  88. return true
  89. }
  90. }
  91. return false
  92. }