client_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package v2
  2. import (
  3. "context"
  4. "testing"
  5. "go-common/app/infra/config/conf"
  6. "go-common/app/infra/config/model"
  7. "github.com/BurntSushi/toml"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func svr(t *testing.T) *Service {
  11. var (
  12. confPath = "../../cmd/config-service-example.toml"
  13. conf *conf.Config
  14. )
  15. Convey("get apply", t, func() {
  16. _, err := toml.DecodeFile(confPath, &conf)
  17. So(err, ShouldBeNil)
  18. })
  19. return New(conf)
  20. }
  21. func TestService_CheckVersion(t *testing.T) {
  22. var (
  23. c = context.TODO()
  24. svrName = "2888_dev_sh001"
  25. hostname = "test_host"
  26. bver = "server-1"
  27. ip = "123"
  28. version = int64(-1)
  29. token = "47fbbf237b7f11e8992154e1ad15006a"
  30. appoint = int64(-1)
  31. )
  32. svr := svr(t)
  33. Convey("AppService", t, func() {
  34. tmp, err := svr.AppService("sh001", "dev", "47fbbf237b7f11e8992154e1ad15006a")
  35. So(err, ShouldBeNil)
  36. So(tmp, ShouldEqual, svrName)
  37. })
  38. rhost := &model.Host{Service: svrName, Name: hostname, BuildVersion: bver, IP: ip, ConfigVersion: version, Appoint: appoint, Customize: "test"}
  39. Convey("get tag id by name", t, func() {
  40. event, err := svr.CheckVersion(c, rhost, token)
  41. So(err, ShouldBeNil)
  42. So(event, ShouldNotBeEmpty)
  43. Convey("get tag id by name", func() {
  44. e := <-event
  45. So(e, ShouldNotBeEmpty)
  46. })
  47. })
  48. }
  49. func TestService_Hosts(t *testing.T) {
  50. var (
  51. svrName = "2888_dev_sh001"
  52. )
  53. svr := svr(t)
  54. Convey("AppService", t, func() {
  55. tmp, err := svr.AppService("sh001", "dev", "47fbbf237b7f11e8992154e1ad15006a")
  56. So(err, ShouldBeNil)
  57. So(tmp, ShouldEqual, svrName)
  58. })
  59. Convey("should get hosts", t, func() {
  60. _, err := svr.Hosts(context.TODO(), svrName)
  61. So(err, ShouldBeNil)
  62. })
  63. }
  64. func TestService_Config(t *testing.T) {
  65. var (
  66. c = context.TODO()
  67. svrName = "2888_dev_sh001"
  68. version = int64(49)
  69. token = "47fbbf237b7f11e8992154e1ad15006a"
  70. )
  71. svr := svr(t)
  72. Convey("AppService", t, func() {
  73. tmp, err := svr.AppService("sh001", "dev", "47fbbf237b7f11e8992154e1ad15006a")
  74. So(err, ShouldBeNil)
  75. So(tmp, ShouldEqual, svrName)
  76. })
  77. Convey("should get hosts", t, func() {
  78. conf, err := svr.Config(c, svrName, token, version, nil)
  79. So(err, ShouldBeNil)
  80. So(conf, ShouldNotBeEmpty)
  81. })
  82. }
  83. func TestService_Push(t *testing.T) {
  84. var (
  85. c = context.TODO()
  86. svrName = "2888_dev_sh001"
  87. bver = "server-1"
  88. version = int64(49)
  89. )
  90. svr := svr(t)
  91. Convey("AppService", t, func() {
  92. tmp, err := svr.AppService("sh001", "dev", "47fbbf237b7f11e8992154e1ad15006a")
  93. So(err, ShouldBeNil)
  94. So(tmp, ShouldEqual, svrName)
  95. })
  96. service := &model.Service{Name: svrName, BuildVersion: bver, Version: version}
  97. Convey("should get Config2", t, func() {
  98. err := svr.Push(c, service)
  99. So(err, ShouldBeNil)
  100. })
  101. }
  102. func TestService_SetToken(t *testing.T) {
  103. var (
  104. svrName = "2888_dev_sh001"
  105. token = "47fbbf237b7f11e8992154e1ad15006a"
  106. )
  107. svr := svr(t)
  108. Convey("AppService", t, func() {
  109. tmp, err := svr.AppService("sh001", "dev", "47fbbf237b7f11e8992154e1ad15006a")
  110. So(err, ShouldBeNil)
  111. So(tmp, ShouldEqual, svrName)
  112. })
  113. Convey("should get Config2", t, func() {
  114. svr.SetToken(svrName, token)
  115. })
  116. }
  117. func TestService_ClearHost(t *testing.T) {
  118. var (
  119. c = context.TODO()
  120. svrName = "2888_dev_sh001"
  121. )
  122. svr := svr(t)
  123. Convey("AppService", t, func() {
  124. tmp, err := svr.AppService("sh001", "dev", "47fbbf237b7f11e8992154e1ad15006a")
  125. So(err, ShouldBeNil)
  126. So(tmp, ShouldEqual, svrName)
  127. })
  128. Convey("should clear host", t, func() {
  129. err := svr.ClearHost(c, svrName)
  130. So(err, ShouldBeNil)
  131. })
  132. }
  133. func TestService_VersionSuccess(t *testing.T) {
  134. var (
  135. c = context.TODO()
  136. svrName = "2888_dev_sh001"
  137. bver = "server-1"
  138. )
  139. svr := svr(t)
  140. Convey("AppService", t, func() {
  141. tmp, err := svr.AppService("sh001", "dev", "47fbbf237b7f11e8992154e1ad15006a")
  142. So(err, ShouldBeNil)
  143. So(tmp, ShouldEqual, svrName)
  144. })
  145. Convey("should clear host", t, func() {
  146. vers, err := svr.VersionSuccess(c, svrName, bver)
  147. So(err, ShouldBeNil)
  148. So(vers, ShouldNotBeEmpty)
  149. })
  150. }
  151. func TestService_Builds(t *testing.T) {
  152. var (
  153. c = context.TODO()
  154. svrName = "2888_dev_sh001"
  155. err error
  156. builds []string
  157. tmp string
  158. )
  159. svr := svr(t)
  160. Convey("AppService", t, func() {
  161. tmp, err = svr.AppService("sh001", "dev", "47fbbf237b7f11e8992154e1ad15006a")
  162. So(err, ShouldBeNil)
  163. So(tmp, ShouldEqual, svrName)
  164. })
  165. Convey("should clear host", t, func() {
  166. builds, err = svr.Builds(c, svrName)
  167. So(err, ShouldBeNil)
  168. So(builds, ShouldNotBeEmpty)
  169. })
  170. }
  171. func TestService_File(t *testing.T) {
  172. var (
  173. c = context.TODO()
  174. svrName = "2888_dev_sh001"
  175. bver = "server-1"
  176. fileName = "test.toml"
  177. token = "47fbbf237b7f11e8992154e1ad15006a"
  178. ver = int64(49)
  179. err error
  180. tmp string
  181. )
  182. svr := svr(t)
  183. Convey("AppService", t, func() {
  184. tmp, err = svr.AppService("sh001", "dev", "47fbbf237b7f11e8992154e1ad15006a")
  185. So(err, ShouldBeNil)
  186. So(tmp, ShouldEqual, svrName)
  187. })
  188. service := &model.Service{Name: svrName, BuildVersion: bver, File: fileName, Token: token, Version: ver}
  189. Convey("should clear host", t, func() {
  190. _, err = svr.File(c, service)
  191. So(err, ShouldBeNil)
  192. })
  193. }