github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/orchestrator/util/key_utils_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 util
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/pingcap/check"
    20  	"github.com/pingcap/ticdc/pkg/util/testleak"
    21  )
    22  
    23  func Test(t *testing.T) { check.TestingT(t) }
    24  
    25  var _ = check.Suite(&keyUtilsSuite{})
    26  
    27  type keyUtilsSuite struct {
    28  }
    29  
    30  func (s *keyUtilsSuite) TestEtcdKey(c *check.C) {
    31  	defer testleak.AfterTest(c)()
    32  	key := NewEtcdKey("/a/b/c")
    33  	c.Assert(key, check.Equals, NewEtcdKeyFromBytes([]byte("/a/b/c")))
    34  	c.Assert(key.String(), check.Equals, "/a/b/c")
    35  	c.Assert(key.Bytes(), check.BytesEquals, []byte("/a/b/c"))
    36  	c.Assert(key.Head().String(), check.Equals, "/a")
    37  	c.Assert(key.Tail().String(), check.Equals, "/b/c")
    38  	c.Assert(key.RemovePrefix(&EtcdPrefix{"/a/b"}).String(), check.Equals, "/c")
    39  	c.Assert(key.AsRelKey().String(), check.Equals, "/a/b/c")
    40  }
    41  
    42  func (s *keyUtilsSuite) TestEtcdRelKey(c *check.C) {
    43  	defer testleak.AfterTest(c)()
    44  	key := NewEtcdRelKey("/a/b/c")
    45  	c.Assert(key, check.Equals, NewEtcdRelKeyFromBytes([]byte("/a/b/c")))
    46  	c.Assert(key.String(), check.Equals, "/a/b/c")
    47  	c.Assert(key.Bytes(), check.BytesEquals, []byte("/a/b/c"))
    48  	c.Assert(key.Head().String(), check.Equals, "/a")
    49  	c.Assert(key.Tail().String(), check.Equals, "/b/c")
    50  	c.Assert(key.RemovePrefix(&EtcdRelPrefix{EtcdPrefix{"/a/b"}}).String(), check.Equals, "/c")
    51  	c.Assert(key.AsPrefix().String(), check.Equals, "/a/b/c")
    52  }
    53  
    54  func (s *keyUtilsSuite) TestEtcdPrefix(c *check.C) {
    55  	defer testleak.AfterTest(c)()
    56  	prefix := NewEtcdPrefix("/aa/bb/cc")
    57  	c.Assert(prefix, check.Equals, NewEtcdPrefixFromBytes([]byte("/aa/bb/cc")))
    58  	c.Assert(prefix.String(), check.Equals, "/aa/bb/cc")
    59  	c.Assert(prefix.Bytes(), check.BytesEquals, []byte("/aa/bb/cc"))
    60  	c.Assert(prefix.Tail().String(), check.Equals, "/bb/cc")
    61  	c.Assert(prefix.Head().String(), check.Equals, "/aa")
    62  	c.Assert(prefix.FullKey(NewEtcdRelKey("/dd")).String(), check.Equals, "/aa/bb/cc/dd")
    63  }
    64  
    65  func (s *keyUtilsSuite) TestEtcdRelPrefix(c *check.C) {
    66  	defer testleak.AfterTest(c)()
    67  	prefix := NewEtcdRelPrefix("/aa/bb/cc")
    68  	c.Assert(prefix, check.Equals, NewEtcdRelPrefixFromBytes([]byte("/aa/bb/cc")))
    69  	c.Assert(prefix.String(), check.Equals, "/aa/bb/cc")
    70  	c.Assert(prefix.Bytes(), check.BytesEquals, []byte("/aa/bb/cc"))
    71  	c.Assert(prefix.Tail().String(), check.Equals, "/bb/cc")
    72  	c.Assert(prefix.Head().String(), check.Equals, "/aa")
    73  }
    74  
    75  func (s *keyUtilsSuite) TestNormalizePrefix(c *check.C) {
    76  	defer testleak.AfterTest(c)()
    77  	c.Assert(NormalizePrefix("aaa"), check.Equals, NewEtcdPrefix("/aaa"))
    78  	c.Assert(NormalizePrefix("aaa/"), check.Equals, NewEtcdPrefix("/aaa"))
    79  	c.Assert(NormalizePrefix("/aaa"), check.Equals, NewEtcdPrefix("/aaa"))
    80  	c.Assert(NormalizePrefix("/aaa/"), check.Equals, NewEtcdPrefix("/aaa"))
    81  }
    82  
    83  func (s *keyUtilsSuite) TestCornerCases(c *check.C) {
    84  	defer testleak.AfterTest(c)()
    85  	c.Assert(func() { NewEtcdPrefix("").Head() }, check.Panics, "Empty EtcdPrefix")
    86  	c.Assert(func() { NewEtcdPrefix("").Tail() }, check.Panics, "Empty EtcdPrefix")
    87  	c.Assert(NewEtcdPrefix("aaa").Head(), check.Equals, NewEtcdPrefix(""))
    88  	c.Assert(NewEtcdPrefix("aaa").Tail(), check.Equals, NewEtcdRelPrefix("aaa"))
    89  
    90  	c.Assert(func() { NewEtcdKey("").Head() }, check.Panics, "Empty EtcdKey")
    91  	c.Assert(func() { NewEtcdKey("").Tail() }, check.Panics, "Empty EtcdKey")
    92  	c.Assert(NewEtcdKey("aaa").Head(), check.Equals, NewEtcdPrefix(""))
    93  	c.Assert(NewEtcdKey("aaa").Tail(), check.Equals, NewEtcdRelKey("aaa"))
    94  }