github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/lightning/backend/checkreq_test.go (about) 1 // Copyright 2019 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package backend 15 16 import ( 17 "context" 18 "encoding/json" 19 "fmt" 20 "net/http" 21 "net/http/httptest" 22 "net/url" 23 24 "github.com/coreos/go-semver/semver" 25 . "github.com/pingcap/check" 26 27 "github.com/pingcap/tidb-lightning/lightning/common" 28 ) 29 30 var _ = Suite(&checkReqSuite{}) 31 32 type checkReqSuite struct{} 33 34 func (s *checkReqSuite) TestCheckVersion(c *C) { 35 err := checkVersion("TiNB", *semver.New("2.1.0"), *semver.New("2.3.5")) 36 c.Assert(err, IsNil) 37 38 err = checkVersion("TiNB", *semver.New("2.3.5"), *semver.New("2.1.0")) 39 c.Assert(err, ErrorMatches, "TiNB version too old.*") 40 } 41 42 func (s *checkReqSuite) TestCheckTiDBVersion(c *C) { 43 var version string 44 ctx := context.Background() 45 46 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 47 c.Assert(req.URL.Path, Equals, "/status") 48 w.WriteHeader(http.StatusOK) 49 err := json.NewEncoder(w).Encode(map[string]interface{}{ 50 "version": version, 51 }) 52 c.Assert(err, IsNil) 53 })) 54 55 tls := common.NewTLSFromMockServer(mockServer) 56 57 version = "5.7.25-TiDB-v9999.0.0" 58 c.Assert(checkTiDBVersionByTLS(ctx, tls, requiredTiDBVersion), IsNil) 59 60 version = "5.7.25-TiDB-v1.0.0" 61 c.Assert(checkTiDBVersionByTLS(ctx, tls, requiredTiDBVersion), ErrorMatches, "TiDB version too old.*") 62 } 63 64 func (s *checkReqSuite) TestCheckPDVersion(c *C) { 65 var version string 66 ctx := context.Background() 67 68 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 69 c.Assert(req.URL.Path, Equals, "/pd/api/v1/config/cluster-version") 70 w.WriteHeader(http.StatusOK) 71 err := json.NewEncoder(w).Encode(version) 72 c.Assert(err, IsNil) 73 })) 74 mockURL, err := url.Parse(mockServer.URL) 75 c.Assert(err, IsNil) 76 77 tls := common.NewTLSFromMockServer(mockServer) 78 79 version = "9999.0.0" 80 c.Assert(checkPDVersion(ctx, tls, mockURL.Host, requiredPDVersion), IsNil) 81 82 version = "1.0.0" 83 c.Assert(checkPDVersion(ctx, tls, mockURL.Host, requiredPDVersion), ErrorMatches, "PD version too old.*") 84 } 85 86 func (s *checkReqSuite) TestCheckTiKVVersion(c *C) { 87 var versions []string 88 ctx := context.Background() 89 90 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 91 c.Assert(req.URL.Path, Equals, "/pd/api/v1/stores") 92 w.WriteHeader(http.StatusOK) 93 94 stores := make([]map[string]interface{}, 0, len(versions)) 95 for i, v := range versions { 96 stores = append(stores, map[string]interface{}{ 97 "store": map[string]interface{}{ 98 "address": fmt.Sprintf("tikv%d.test:20160", i), 99 "version": v, 100 }, 101 }) 102 } 103 err := json.NewEncoder(w).Encode(map[string]interface{}{ 104 "count": len(versions), 105 "stores": stores, 106 }) 107 c.Assert(err, IsNil) 108 })) 109 mockURL, err := url.Parse(mockServer.URL) 110 c.Assert(err, IsNil) 111 112 tls := common.NewTLSFromMockServer(mockServer) 113 114 versions = []string{"9999.0.0", "9999.0.0"} 115 c.Assert(checkTiKVVersion(ctx, tls, mockURL.Host, requiredTiKVVersion), IsNil) 116 117 versions = []string{"4.1.0", "v4.1.0-alpha-9-ga27a7dd"} 118 c.Assert(checkTiKVVersion(ctx, tls, mockURL.Host, requiredTiKVVersion), IsNil) 119 120 versions = []string{"9999.0.0", "1.0.0"} 121 c.Assert(checkTiKVVersion(ctx, tls, mockURL.Host, requiredTiKVVersion), ErrorMatches, `TiKV \(at tikv1\.test:20160\) version too old.*`) 122 }