github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/lightning/backend/tikv_test.go (about)

     1  package backend_test
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"sort"
     8  	"sync"
     9  
    10  	. "github.com/pingcap/check"
    11  	"github.com/pingcap/kvproto/pkg/import_sstpb"
    12  
    13  	kv "github.com/pingcap/tidb-lightning/lightning/backend"
    14  	"github.com/pingcap/tidb-lightning/lightning/common"
    15  )
    16  
    17  type tikvSuite struct{}
    18  
    19  var _ = Suite(&tikvSuite{})
    20  
    21  func (s *tikvSuite) TestForAllStores(c *C) {
    22  	server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    23  		w.Write([]byte(`
    24  			{
    25  				"count": 5,
    26  				"stores": [
    27  					{
    28  						"store": {
    29  							"id": 1,
    30  							"address": "127.0.0.1:20160",
    31  							"version": "3.0.0-beta.1",
    32  							"state_name": "Up"
    33  						},
    34  						"status": {}
    35  					},
    36  					{
    37  						"store": {
    38  							"id": 2,
    39  							"address": "127.0.0.1:20161",
    40  							"version": "3.0.0-rc.1",
    41  							"state_name": "Down"
    42  						},
    43  						"status": {}
    44  					},
    45  					{
    46  						"store": {
    47  							"id": 3,
    48  							"address": "127.0.0.1:20162",
    49  							"version": "3.0.0-rc.2",
    50  							"state_name": "Disconnected"
    51  						},
    52  						"status": {}
    53  					},
    54  					{
    55  						"store": {
    56  							"id": 4,
    57  							"address": "127.0.0.1:20163",
    58  							"version": "3.0.0",
    59  							"state_name": "Tombstone"
    60  						},
    61  						"status": {}
    62  					},
    63  					{
    64  						"store": {
    65  							"id": 5,
    66  							"address": "127.0.0.1:20164",
    67  							"version": "3.0.1",
    68  							"state_name": "Offline"
    69  						},
    70  						"status": {}
    71  					}
    72  				]
    73  			}
    74  		`))
    75  	}))
    76  	defer server.Close()
    77  
    78  	ctx := context.Background()
    79  	var (
    80  		allStoresLock sync.Mutex
    81  		allStores     []*kv.Store
    82  	)
    83  	tls := common.NewTLSFromMockServer(server)
    84  	err := kv.ForAllStores(ctx, tls, kv.StoreStateDown, func(c2 context.Context, store *kv.Store) error {
    85  		allStoresLock.Lock()
    86  		allStores = append(allStores, store)
    87  		allStoresLock.Unlock()
    88  		return nil
    89  	})
    90  	c.Assert(err, IsNil)
    91  
    92  	sort.Slice(allStores, func(i, j int) bool { return allStores[i].Address < allStores[j].Address })
    93  
    94  	c.Assert(allStores, DeepEquals, []*kv.Store{
    95  		{
    96  			Address: "127.0.0.1:20160",
    97  			Version: "3.0.0-beta.1",
    98  			State:   kv.StoreStateUp,
    99  		},
   100  		{
   101  			Address: "127.0.0.1:20161",
   102  			Version: "3.0.0-rc.1",
   103  			State:   kv.StoreStateDown,
   104  		},
   105  		{
   106  			Address: "127.0.0.1:20162",
   107  			Version: "3.0.0-rc.2",
   108  			State:   kv.StoreStateDisconnected,
   109  		},
   110  		{
   111  			Address: "127.0.0.1:20164",
   112  			Version: "3.0.1",
   113  			State:   kv.StoreStateOffline,
   114  		},
   115  	})
   116  }
   117  
   118  func (s *tikvSuite) TestFetchModeFromMetrics(c *C) {
   119  	testCases := []struct {
   120  		metrics string
   121  		mode    import_sstpb.SwitchMode
   122  		isErr   bool
   123  	}{
   124  		{
   125  			metrics: `tikv_config_rocksdb{cf="default",name="hard_pending_compaction_bytes_limit"} 274877906944`,
   126  			mode:    import_sstpb.SwitchMode_Normal,
   127  		},
   128  		{
   129  			metrics: `tikv_config_rocksdb{cf="default",name="hard_pending_compaction_bytes_limit"} 0`,
   130  			mode:    import_sstpb.SwitchMode_Import,
   131  		},
   132  		{
   133  			metrics: ``,
   134  			isErr:   true,
   135  		},
   136  	}
   137  
   138  	for _, tc := range testCases {
   139  		comment := Commentf("test case '%s'", tc.metrics)
   140  		mode, err := kv.FetchModeFromMetrics(tc.metrics)
   141  		if tc.isErr {
   142  			c.Assert(err, NotNil, comment)
   143  		} else {
   144  			c.Assert(err, IsNil, comment)
   145  			c.Assert(mode, Equals, tc.mode, comment)
   146  		}
   147  	}
   148  }