github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/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/stretchr/testify/require"
    20  )
    21  
    22  func TestEtcdKey(t *testing.T) {
    23  	key := NewEtcdKey("/a/b/c")
    24  	require.Equal(t, NewEtcdKeyFromBytes([]byte("/a/b/c")), key)
    25  	require.Equal(t, "/a/b/c", key.String())
    26  	require.Equal(t, []byte("/a/b/c"), key.Bytes())
    27  	require.Equal(t, "/a", key.Head().String())
    28  	require.Equal(t, "/b/c", key.Tail().String())
    29  	require.Equal(t, "/c", key.RemovePrefix(&EtcdPrefix{"/a/b"}).String())
    30  	require.Equal(t, "/a/b/c", key.AsRelKey().String())
    31  }
    32  
    33  func TestEtcdRelKey(t *testing.T) {
    34  	key := NewEtcdRelKey("/a/b/c")
    35  	require.Equal(t, NewEtcdRelKeyFromBytes([]byte("/a/b/c")), key)
    36  	require.Equal(t, "/a/b/c", key.String())
    37  	require.Equal(t, []byte("/a/b/c"), key.Bytes())
    38  	require.Equal(t, "/a", key.Head().String())
    39  	require.Equal(t, "/b/c", key.Tail().String())
    40  	require.Equal(t, "/c", key.RemovePrefix(&EtcdRelPrefix{EtcdPrefix{"/a/b"}}).String())
    41  	require.Equal(t, "/a/b/c", key.AsPrefix().String())
    42  }
    43  
    44  func TestEtcdPrefix(t *testing.T) {
    45  	prefix := NewEtcdPrefix("/aa/bb/cc")
    46  	require.Equal(t, NewEtcdPrefixFromBytes([]byte("/aa/bb/cc")), prefix)
    47  	require.Equal(t, "/aa/bb/cc", prefix.String())
    48  	require.Equal(t, []byte("/aa/bb/cc"), prefix.Bytes())
    49  	require.Equal(t, "/bb/cc", prefix.Tail().String())
    50  	require.Equal(t, "/aa", prefix.Head().String())
    51  	require.Equal(t, "/aa/bb/cc/dd", prefix.FullKey(NewEtcdRelKey("/dd")).String())
    52  }
    53  
    54  func TestEtcdRelPrefix(t *testing.T) {
    55  	prefix := NewEtcdRelPrefix("/aa/bb/cc")
    56  	require.Equal(t, NewEtcdRelPrefixFromBytes([]byte("/aa/bb/cc")), prefix)
    57  	require.Equal(t, "/aa/bb/cc", prefix.String())
    58  	require.Equal(t, []byte("/aa/bb/cc"), prefix.Bytes())
    59  	require.Equal(t, "/bb/cc", prefix.Tail().String())
    60  	require.Equal(t, "/aa", prefix.Head().String())
    61  }
    62  
    63  func TestNormalizePrefix(t *testing.T) {
    64  	require.Equal(t, NewEtcdPrefix("/aaa"), NormalizePrefix("aaa"))
    65  	require.Equal(t, NewEtcdPrefix("/aaa"), NormalizePrefix("aaa/"))
    66  	require.Equal(t, NewEtcdPrefix("/aaa"), NormalizePrefix("/aaa"))
    67  	require.Equal(t, NewEtcdPrefix("/aaa"), NormalizePrefix("/aaa/"))
    68  }
    69  
    70  func TestCornerCases(t *testing.T) {
    71  	require.Panics(t, func() { NewEtcdPrefix("").Head() }, "Empty EtcdPrefix")
    72  	require.Panics(t, func() { NewEtcdPrefix("").Tail() }, "Empty EtcdPrefix")
    73  	require.Equal(t, NewEtcdPrefix(""), NewEtcdPrefix("aaa").Head())
    74  	require.Equal(t, NewEtcdRelPrefix("aaa"), NewEtcdPrefix("aaa").Tail())
    75  
    76  	require.Panics(t, func() { NewEtcdKey("").Head() }, "Empty EtcdKey")
    77  	require.Panics(t, func() { NewEtcdKey("").Tail() }, "Empty EtcdKey")
    78  	require.Equal(t, NewEtcdPrefix(""), NewEtcdKey("aaa").Head())
    79  	require.Equal(t, NewEtcdRelKey("aaa"), NewEtcdKey("aaa").Tail())
    80  }