suite.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Licensed to the LF AI & Data foundation under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package integration
  17. import (
  18. "context"
  19. "flag"
  20. "os"
  21. "strings"
  22. "sync"
  23. "time"
  24. "github.com/stretchr/testify/suite"
  25. "go.etcd.io/etcd/server/v3/embed"
  26. "go.uber.org/zap/zapcore"
  27. "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
  28. "github.com/milvus-io/milvus/internal/util/hookutil"
  29. "github.com/milvus-io/milvus/pkg/log"
  30. "github.com/milvus-io/milvus/pkg/util/etcd"
  31. "github.com/milvus-io/milvus/pkg/util/paramtable"
  32. )
  33. var caseTimeout time.Duration
  34. func init() {
  35. flag.DurationVar(&caseTimeout, "caseTimeout", 10*time.Minute, "timeout duration for single case")
  36. }
  37. // EmbedEtcdSuite contains embed setup & teardown related logic
  38. type EmbedEtcdSuite struct {
  39. EtcdServer *embed.Etcd
  40. EtcdDir string
  41. }
  42. func (s *EmbedEtcdSuite) SetupEmbedEtcd() error {
  43. server, folder, err := etcd.StartTestEmbedEtcdServer()
  44. if err != nil {
  45. return err
  46. }
  47. s.EtcdServer = server
  48. s.EtcdDir = folder
  49. return nil
  50. }
  51. func (s *EmbedEtcdSuite) TearDownEmbedEtcd() {
  52. if s.EtcdServer != nil {
  53. s.EtcdServer.Server.Stop()
  54. }
  55. if s.EtcdDir != "" {
  56. os.RemoveAll(s.EtcdDir)
  57. }
  58. }
  59. type MiniClusterSuite struct {
  60. suite.Suite
  61. EmbedEtcdSuite
  62. Cluster *MiniClusterV2
  63. cancelFunc context.CancelFunc
  64. }
  65. func (s *MiniClusterSuite) SetupSuite() {
  66. s.Require().NoError(s.SetupEmbedEtcd())
  67. }
  68. func (s *MiniClusterSuite) TearDownSuite() {
  69. s.TearDownEmbedEtcd()
  70. }
  71. func (s *MiniClusterSuite) SetupTest() {
  72. log.SetLevel(zapcore.InfoLevel)
  73. s.T().Log("Setup test...")
  74. // setup mini cluster to use embed etcd
  75. endpoints := etcd.GetEmbedEtcdEndpoints(s.EtcdServer)
  76. val := strings.Join(endpoints, ",")
  77. // setup env value to init etcd source
  78. s.T().Setenv("etcd.endpoints", val)
  79. params = paramtable.Get()
  80. s.T().Log("Setup case timeout", caseTimeout)
  81. ctx, cancel := context.WithTimeout(context.Background(), caseTimeout)
  82. s.cancelFunc = cancel
  83. c, err := StartMiniClusterV2(ctx, func(c *MiniClusterV2) {
  84. // change config etcd endpoints
  85. c.params[params.EtcdCfg.Endpoints.Key] = val
  86. })
  87. s.Require().NoError(err)
  88. s.Cluster = c
  89. checkWg := sync.WaitGroup{}
  90. checkWg.Add(1)
  91. // start mini cluster
  92. nodeIDCheckReport := func() {
  93. defer checkWg.Done()
  94. timeoutCtx, cancelFunc := context.WithTimeout(ctx, 5*time.Second)
  95. defer cancelFunc()
  96. for {
  97. select {
  98. case <-timeoutCtx.Done():
  99. s.Fail("node id check timeout")
  100. case report := <-c.Extension.GetReportChan():
  101. reportInfo := report.(map[string]any)
  102. s.T().Log("node id report info: ", reportInfo)
  103. s.Equal(hookutil.OpTypeNodeID, reportInfo[hookutil.OpTypeKey])
  104. s.NotEqualValues(0, reportInfo[hookutil.NodeIDKey])
  105. return
  106. }
  107. }
  108. }
  109. go nodeIDCheckReport()
  110. s.Require().NoError(s.Cluster.Start())
  111. checkWg.Wait()
  112. }
  113. func (s *MiniClusterSuite) TearDownTest() {
  114. resp, err := s.Cluster.Proxy.ShowCollections(context.Background(), &milvuspb.ShowCollectionsRequest{
  115. Type: milvuspb.ShowType_InMemory,
  116. })
  117. if err == nil {
  118. for idx, collectionName := range resp.GetCollectionNames() {
  119. if resp.GetInMemoryPercentages()[idx] == 100 || resp.GetQueryServiceAvailable()[idx] {
  120. s.Cluster.Proxy.ReleaseCollection(context.Background(), &milvuspb.ReleaseCollectionRequest{
  121. CollectionName: collectionName,
  122. })
  123. }
  124. }
  125. }
  126. s.T().Log("Tear Down test...")
  127. defer s.cancelFunc()
  128. if s.Cluster != nil {
  129. s.Cluster.Stop()
  130. }
  131. }