github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/upgrade/version_test.go (about)

     1  // Copyright 2020 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 upgrade
    15  
    16  import (
    17  	. "github.com/pingcap/check"
    18  )
    19  
    20  func (t *testForEtcd) TestVersionJSON(c *C) {
    21  	v1 := NewVersion(1, "v2.0.0")
    22  	j, err := v1.toJSON()
    23  	c.Assert(err, IsNil)
    24  	c.Assert(j, Equals, `{"internal-no":1,"release-ver":"v2.0.0"}`)
    25  	c.Assert(j, Equals, v1.String())
    26  
    27  	v2, err := versionFromJSON(j)
    28  	c.Assert(err, IsNil)
    29  	c.Assert(v2, DeepEquals, v1)
    30  }
    31  
    32  func (t *testForEtcd) TestVersionFunc(c *C) {
    33  	var ve Version
    34  	c.Assert(ve.NotSet(), IsTrue)
    35  	c.Assert(CurrentVersion.NotSet(), IsFalse)
    36  
    37  	v1 := NewVersion(1, "v2.0.0")
    38  	v2 := NewVersion(2, "v2.1.0")
    39  	c.Assert(v1.Compare(v2), Equals, -1)
    40  	c.Assert(v2.Compare(v1), Equals, 1)
    41  
    42  	v11 := v1
    43  	c.Assert(v11.Compare(v1), Equals, 0)
    44  
    45  	// we only compare InternalNo now, if we should compare release version later, this should be fail.
    46  	v12 := NewVersion(v1.InternalNo, "another-release-ver")
    47  	c.Assert(v12.Compare(v1), Equals, 0)
    48  
    49  	// MinVersion should < any current version.
    50  	c.Assert(MinVersion.Compare(CurrentVersion), Equals, -1)
    51  }
    52  
    53  func (t *testForEtcd) TestVersionEtcd(c *C) {
    54  	defer clearTestData(c)
    55  
    56  	// try to get the version, but not exist.
    57  	ver, rev1, err := GetVersion(etcdTestCli)
    58  	c.Assert(err, IsNil)
    59  	c.Assert(rev1, Greater, int64(0))
    60  	c.Assert(ver.NotSet(), IsTrue)
    61  
    62  	// put current version into etcd.
    63  	rev2, err := PutVersion(etcdTestCli, CurrentVersion)
    64  	c.Assert(err, IsNil)
    65  	c.Assert(rev2, Greater, rev1)
    66  
    67  	// get the version back.
    68  	ver, rev3, err := GetVersion(etcdTestCli)
    69  	c.Assert(err, IsNil)
    70  	c.Assert(rev3, Equals, rev2)
    71  	c.Assert(ver, DeepEquals, CurrentVersion)
    72  }