service_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package service
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "go-common/app/admin/main/cache/conf"
  9. "go-common/app/admin/main/cache/model"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. var (
  13. svr *Service
  14. )
  15. func TestMain(m *testing.M) {
  16. var (
  17. err error
  18. )
  19. dir, _ := filepath.Abs("../cmd/test.toml")
  20. if err = flag.Set("conf", dir); err != nil {
  21. panic(err)
  22. }
  23. if err = conf.Init(); err != nil {
  24. panic(err)
  25. }
  26. svr = New(conf.Conf)
  27. os.Exit(m.Run())
  28. }
  29. func TestCluster(t *testing.T) {
  30. Convey("test cluster ", t, func() {
  31. req := &model.ClusterReq{
  32. PN: 1,
  33. PS: 10,
  34. }
  35. resp, err := svr.Clusters(context.TODO(), req)
  36. So(err, ShouldBeNil)
  37. So(resp, ShouldNotBeNil)
  38. t.Logf("resp %v", resp.Clusters[0])
  39. })
  40. }
  41. func TestAddCluster(t *testing.T) {
  42. Convey("test add cluster ", t, func() {
  43. req := &model.AddClusterReq{
  44. Type: "memcache",
  45. AppID: "test",
  46. HashMethod: "sha1",
  47. HashDistribution: "ketama",
  48. }
  49. _, err := svr.AddCluster(context.TODO(), req)
  50. So(err, ShouldBeNil)
  51. })
  52. }
  53. func TestSearchCluster(t *testing.T) {
  54. Convey("test search cluster", t, func() {
  55. req := &model.ClusterReq{
  56. AppID: "test",
  57. }
  58. resp, err := svr.Cluster(context.TODO(), req)
  59. So(err, ShouldBeNil)
  60. So(resp, ShouldNotBeNil)
  61. t.Logf("search resp %+v", resp)
  62. })
  63. }
  64. func TestModifyCluster(t *testing.T) {
  65. Convey("test add cluster nodes", t, func() {
  66. req := &model.ModifyClusterReq{
  67. ID: 1,
  68. Action: 1,
  69. Nodes: `[{"addr":"11","alias":"test1","weight":1}]`,
  70. }
  71. _, err := svr.ModifyCluster(context.TODO(), req)
  72. So(err, ShouldBeNil)
  73. })
  74. Convey("test get cluster detail", t, func() {
  75. req := &model.ClusterDtlReq{
  76. ID: 1,
  77. }
  78. resp, err := svr.ClusterDtl(context.TODO(), req)
  79. So(err, ShouldBeNil)
  80. So(len(resp.Nodes), ShouldEqual, 2)
  81. })
  82. }