github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/label/label_test.go (about)

     1  // Copyright 2022 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 label
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  const longString = "a1234567890123456789012345678901234567890123456789012345678901234567890z"
    23  
    24  var cases = []struct {
    25  	str     string
    26  	allowed bool
    27  }{
    28  	{str: "123", allowed: true},
    29  	{str: "abc", allowed: true},
    30  	{str: "ABC", allowed: true},
    31  	{str: "09", allowed: true},
    32  	{str: "az", allowed: true},
    33  	{str: "AZ", allowed: true},
    34  	{str: "123abc", allowed: true},
    35  	{str: "123abcABC", allowed: true},
    36  	{str: "abcABC", allowed: true},
    37  	{str: "09AZaz", allowed: true},
    38  	{str: "09AZaz-_.", allowed: false},
    39  	{str: "-", allowed: false},
    40  	{str: "_", allowed: false},
    41  	{str: ".", allowed: false},
    42  	{str: ":", allowed: false},
    43  	{str: "~", allowed: false},
    44  	{str: string(invalidLabelKey), allowed: false},
    45  	{str: "你好", allowed: false},
    46  	{str: "", allowed: false},
    47  	{str: longString, allowed: false},
    48  }
    49  
    50  func TestNewKey(t *testing.T) {
    51  	t.Parallel()
    52  
    53  	for _, tc := range cases {
    54  		tc := tc
    55  		t.Run(tc.str, func(t *testing.T) {
    56  			t.Parallel()
    57  			res, err := NewKey(tc.str)
    58  			if tc.allowed {
    59  				require.NoError(t, err)
    60  				require.NotEqual(t, invalidLabelKey, res)
    61  				return
    62  			}
    63  			require.ErrorContains(t, err, "new key")
    64  		})
    65  	}
    66  }
    67  
    68  func TestNewValue(t *testing.T) {
    69  	t.Parallel()
    70  
    71  	for _, tc := range cases {
    72  		tc := tc
    73  		t.Run(tc.str, func(t *testing.T) {
    74  			t.Parallel()
    75  			res, err := NewValue(tc.str)
    76  			if tc.allowed {
    77  				require.NoError(t, err)
    78  				require.NotEqual(t, invalidLabelValue, res)
    79  				return
    80  			}
    81  			require.ErrorContains(t, err, "new value")
    82  		})
    83  	}
    84  }
    85  
    86  func TestSet(t *testing.T) {
    87  	t.Parallel()
    88  
    89  	set := NewSet()
    90  
    91  	require.True(t, set.Add("123", "abc"))
    92  	require.False(t, set.Add("123", "cde"))
    93  
    94  	key, ok := set.Get("123")
    95  	require.True(t, ok)
    96  	require.Equal(t, Value("abc"), key)
    97  
    98  	key, ok = set.Get("456")
    99  	require.False(t, ok)
   100  	require.Equal(t, invalidLabelValue, key)
   101  
   102  	require.Panics(t, func() {
   103  		set.Add(invalidLabelKey, "aaa")
   104  	})
   105  	require.Panics(t, func() {
   106  		set.Add("abc", invalidLabelValue)
   107  	})
   108  	require.Panics(t, func() {
   109  		set.Get(invalidLabelKey)
   110  	})
   111  }
   112  
   113  func TestNewSetFromMap(t *testing.T) {
   114  	t.Parallel()
   115  
   116  	_, err := NewSetFromMap(map[string]string{
   117  		"type":      "executor",
   118  		"09AZaz-_.": "asdf",
   119  	})
   120  	require.ErrorContains(t, err, "label string has wrong format")
   121  
   122  	_, err = NewSetFromMap(map[string]string{
   123  		"type": "executor",
   124  		"good": "~",
   125  	})
   126  	require.ErrorContains(t, err, "label string has wrong format")
   127  
   128  	set, err := NewSetFromMap(map[string]string{
   129  		"label1": "value1",
   130  		"label2": "value2",
   131  	})
   132  	require.NoError(t, err)
   133  	val, ok := set.Get("label1")
   134  	require.True(t, ok)
   135  	require.Equal(t, Value("value1"), val)
   136  
   137  	val, ok = set.Get("label2")
   138  	require.True(t, ok)
   139  	require.Equal(t, Value("value2"), val)
   140  }