github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/externalresource/model/model_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 model
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestParseResource(t *testing.T) {
    23  	tp, suffix, err := ParseResourceID("/local/my-local-resource/a/b/c")
    24  	require.NoError(t, err)
    25  	require.Equal(t, ResourceTypeLocalFile, tp)
    26  	rawResName, err := DecodeResourceName(suffix)
    27  	require.NoError(t, err)
    28  	require.Equal(t, "my-local-resource/a/b/c", rawResName)
    29  	require.Equal(t, "/local/my-local-resource/a/b/c", BuildResourceID(tp, suffix))
    30  
    31  	tp, suffix, err = ParseResourceID("/s3/my-local-resource/a/b/c")
    32  	require.NoError(t, err)
    33  	require.Equal(t, ResourceTypeS3, tp)
    34  	rawResName, err = DecodeResourceName(suffix)
    35  	require.NoError(t, err)
    36  	require.Equal(t, "my-local-resource/a/b/c", rawResName)
    37  	require.Equal(t, "/s3/my-local-resource/a/b/c", BuildResourceID(tp, suffix))
    38  
    39  	tp, suffix, err = ParseResourceID("/gs/my-local-resource/a/b/c")
    40  	require.NoError(t, err)
    41  	require.Equal(t, ResourceTypeGCS, tp)
    42  	rawResName, err = DecodeResourceName(suffix)
    43  	require.NoError(t, err)
    44  	require.Equal(t, "my-local-resource/a/b/c", rawResName)
    45  	require.Equal(t, "/gs/my-local-resource/a/b/c", BuildResourceID(tp, suffix))
    46  
    47  	_, _, err = ParseResourceID("/gcs/my-local-resource/a/b/c")
    48  	require.Error(t, err)
    49  	_, _, err = ParseResourceID("/none/my-local-resource/a/b/c")
    50  	require.Error(t, err)
    51  }
    52  
    53  func FuzzEncodeResourceName(f *testing.F) {
    54  	testcases := []string{"resource-1", "resource-1/inner", "!resource-1+-%/*inner"}
    55  	for _, tc := range testcases {
    56  		f.Add(tc)
    57  	}
    58  	f.Fuzz(func(t *testing.T, rawResName string) {
    59  		resName := EncodeResourceName(rawResName)
    60  		decodedResName, err := DecodeResourceName(resName)
    61  		require.NoError(t, err)
    62  		require.Equal(t, rawResName, decodedResName)
    63  	})
    64  }