github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/lightning/common/version_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 common_test 15 16 import ( 17 "github.com/coreos/go-semver/semver" 18 . "github.com/pingcap/check" 19 20 "github.com/pingcap/tidb-lightning/lightning/common" 21 ) 22 23 func (s *utilSuite) TestVersion(c *C) { 24 common.ReleaseVersion = "ReleaseVersion" 25 common.BuildTS = "BuildTS" 26 common.GitHash = "GitHash" 27 common.GitBranch = "GitBranch" 28 common.GoVersion = "GoVersion" 29 30 version := common.GetRawInfo() 31 c.Assert(version, Equals, `Release Version: ReleaseVersion 32 Git Commit Hash: GitHash 33 Git Branch: GitBranch 34 UTC Build Time: BuildTS 35 Go Version: GoVersion 36 `) 37 common.PrintInfo("test", func() { 38 common.ReleaseVersion = "None" 39 common.BuildTS = "None" 40 common.GitHash = "None" 41 common.GitBranch = "None" 42 common.GoVersion = "None" 43 }) 44 45 version = common.GetRawInfo() 46 c.Assert(version, Equals, `Release Version: None 47 Git Commit Hash: None 48 Git Branch: None 49 UTC Build Time: None 50 Go Version: None 51 `) 52 } 53 54 func (s *utilSuite) TestExtractTiDBVersion(c *C) { 55 vers, err := common.ExtractTiDBVersion("5.7.10-TiDB-v2.1.0-rc.1-7-g38c939f") 56 c.Assert(err, IsNil) 57 c.Assert(*vers, Equals, *semver.New("2.1.0-rc.1")) 58 59 vers, err = common.ExtractTiDBVersion("5.7.10-TiDB-v2.0.4-1-g06a0bf5") 60 c.Assert(err, IsNil) 61 c.Assert(*vers, Equals, *semver.New("2.0.4")) 62 63 vers, err = common.ExtractTiDBVersion("5.7.10-TiDB-v2.0.7") 64 c.Assert(err, IsNil) 65 c.Assert(*vers, Equals, *semver.New("2.0.7")) 66 67 vers, err = common.ExtractTiDBVersion("8.0.12-TiDB-v3.0.5-beta.12") 68 c.Assert(err, IsNil) 69 c.Assert(*vers, Equals, *semver.New("3.0.5-beta.12")) 70 71 vers, err = common.ExtractTiDBVersion("5.7.25-TiDB-v3.0.0-beta-211-g09beefbe0-dirty") 72 c.Assert(err, IsNil) 73 c.Assert(*vers, Equals, *semver.New("3.0.0-beta")) 74 75 vers, err = common.ExtractTiDBVersion("8.0.12-TiDB-v3.0.5-dirty") 76 c.Assert(err, IsNil) 77 c.Assert(*vers, Equals, *semver.New("3.0.5")) 78 79 vers, err = common.ExtractTiDBVersion("8.0.12-TiDB-v3.0.5-beta.12-dirty") 80 c.Assert(err, IsNil) 81 c.Assert(*vers, Equals, *semver.New("3.0.5-beta.12")) 82 83 vers, err = common.ExtractTiDBVersion("5.7.10-TiDB-v2.1.0-rc.1-7-g38c939f-dirty") 84 c.Assert(err, IsNil) 85 c.Assert(*vers, Equals, *semver.New("2.1.0-rc.1")) 86 87 _, err = common.ExtractTiDBVersion("") 88 c.Assert(err, ErrorMatches, "not a valid TiDB version.*") 89 90 _, err = common.ExtractTiDBVersion("8.0.12") 91 c.Assert(err, ErrorMatches, "not a valid TiDB version.*") 92 93 _, err = common.ExtractTiDBVersion("not-a-valid-version") 94 c.Assert(err, NotNil) 95 }