sven_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package paladin
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/library/conf/env"
  7. "github.com/naoina/toml"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. type testObj struct {
  11. Bool bool
  12. Int int64
  13. Float float64
  14. String string
  15. }
  16. func (t *testObj) Set(text string) error {
  17. return toml.Unmarshal([]byte(text), t)
  18. }
  19. type testConf struct {
  20. Bool bool
  21. Int int64
  22. Float float64
  23. String string
  24. Object *testObj
  25. }
  26. func (t *testConf) Set(text string) error {
  27. return toml.Unmarshal([]byte(text), t)
  28. }
  29. func TestSven(t *testing.T) {
  30. svenHost = "config.bilibili.co"
  31. svenVersion = "server-1"
  32. svenPath = "/tmp"
  33. svenToken = "1afe5efaf45e11e7b3f8c6cd4f230d8c"
  34. svenAppoint = ""
  35. svenTreeid = "2888"
  36. env.Region = "sh"
  37. env.Zone = "sh001"
  38. env.Hostname = "test"
  39. env.DeployEnv = "dev"
  40. env.AppID = "main.common-arch.msm-service"
  41. sven, err := NewSven()
  42. assert.Nil(t, err)
  43. testSvenMap(t, sven)
  44. testSvenValue(t, sven)
  45. testWatch(t, sven)
  46. }
  47. func testSvenMap(t *testing.T, cli Client) {
  48. m := Map{}
  49. text, err := cli.Get("test.toml").String()
  50. assert.Nil(t, err)
  51. assert.Nil(t, m.Set(text), text)
  52. b, err := m.Get("bool").Bool()
  53. assert.Nil(t, err)
  54. assert.Equal(t, b, true, "bool")
  55. // int64
  56. i, err := m.Get("int").Int64()
  57. assert.Nil(t, err)
  58. assert.Equal(t, i, int64(100), "int64")
  59. // float64
  60. f, err := m.Get("float").Float64()
  61. assert.Nil(t, err)
  62. assert.Equal(t, f, 100.1, "float64")
  63. // string
  64. s, err := m.Get("string").String()
  65. assert.Nil(t, err)
  66. assert.Equal(t, s, "text", "string")
  67. // error
  68. n, err := m.Get("not_exsit").String()
  69. assert.NotNil(t, err)
  70. assert.Equal(t, n, "", "not_exsit")
  71. obj := new(testObj)
  72. text, err = m.Get("object").Raw()
  73. assert.Nil(t, err)
  74. assert.Nil(t, obj.Set(text))
  75. assert.Equal(t, obj.Bool, true, "bool")
  76. assert.Equal(t, obj.Int, int64(100), "int64")
  77. assert.Equal(t, obj.Float, 100.1, "float64")
  78. assert.Equal(t, obj.String, "text", "string")
  79. }
  80. func testSvenValue(t *testing.T, cli Client) {
  81. v := new(testConf)
  82. text, err := cli.Get("test.toml").Raw()
  83. assert.Nil(t, err)
  84. assert.Nil(t, v.Set(text))
  85. assert.Equal(t, v.Bool, true, "bool")
  86. assert.Equal(t, v.Int, int64(100), "int64")
  87. assert.Equal(t, v.Float, 100.1, "float64")
  88. assert.Equal(t, v.String, "text", "string")
  89. assert.Equal(t, v.Object.Bool, true, "bool")
  90. assert.Equal(t, v.Object.Int, int64(100), "int64")
  91. assert.Equal(t, v.Object.Float, 100.1, "float64")
  92. assert.Equal(t, v.Object.String, "text", "string")
  93. }
  94. func testWatch(t *testing.T, cli Client) {
  95. ch := cli.WatchEvent(context.Background())
  96. select {
  97. case <-time.After(time.Second):
  98. t.Log("watch timeout")
  99. case e := <-ch:
  100. s, err := cli.Get("static").String()
  101. assert.Nil(t, err)
  102. assert.Equal(t, s, e.Value, "watch value")
  103. t.Logf("watch event:%+v", e)
  104. }
  105. }