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

     1  // Copyright 2023 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 redo
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/google/uuid"
    22  	"github.com/pingcap/tidb/br/pkg/storage"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestParseLogFileName(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	type arg struct {
    30  		name string
    31  	}
    32  	tests := []struct {
    33  		name         string
    34  		args         arg
    35  		wantTs       uint64
    36  		wantFileType string
    37  		wantErr      string
    38  	}{
    39  		{
    40  			name: "happy row .log",
    41  			args: arg{
    42  				name: fmt.Sprintf(RedoLogFileFormatV1, "cp",
    43  					"test",
    44  					RedoRowLogFileType, 1, uuid.NewString(), LogEXT),
    45  			},
    46  			wantTs:       1,
    47  			wantFileType: RedoRowLogFileType,
    48  		},
    49  		{
    50  			name: "happy row .log",
    51  			args: arg{
    52  				name: fmt.Sprintf(RedoLogFileFormatV2, "cp",
    53  					"namespace", "test",
    54  					RedoRowLogFileType, 1, uuid.NewString(), LogEXT),
    55  			},
    56  			wantTs:       1,
    57  			wantFileType: RedoRowLogFileType,
    58  		},
    59  		{
    60  			name: "happy row .tmp",
    61  			args: arg{
    62  				name: fmt.Sprintf(RedoLogFileFormatV1, "cp",
    63  					"test",
    64  					RedoRowLogFileType, 1, uuid.NewString(), LogEXT) + TmpEXT,
    65  			},
    66  			wantTs:       1,
    67  			wantFileType: RedoRowLogFileType,
    68  		},
    69  		{
    70  			name: "happy row .tmp",
    71  			args: arg{
    72  				name: fmt.Sprintf(RedoLogFileFormatV2, "cp",
    73  					"namespace", "test",
    74  					RedoRowLogFileType, 1, uuid.NewString(), LogEXT) + TmpEXT,
    75  			},
    76  			wantTs:       1,
    77  			wantFileType: RedoRowLogFileType,
    78  		},
    79  		{
    80  			name: "happy ddl .log",
    81  			args: arg{
    82  				name: fmt.Sprintf(RedoLogFileFormatV1, "cp",
    83  					"test",
    84  					RedoDDLLogFileType, 1, uuid.NewString(), LogEXT),
    85  			},
    86  			wantTs:       1,
    87  			wantFileType: RedoDDLLogFileType,
    88  		},
    89  		{
    90  			name: "happy ddl .log",
    91  			args: arg{
    92  				name: fmt.Sprintf(RedoLogFileFormatV2, "cp",
    93  					"namespace", "test",
    94  					RedoDDLLogFileType, 1, uuid.NewString(), LogEXT),
    95  			},
    96  			wantTs:       1,
    97  			wantFileType: RedoDDLLogFileType,
    98  		},
    99  		{
   100  			name: "happy ddl .sort",
   101  			args: arg{
   102  				name: fmt.Sprintf(RedoLogFileFormatV2, "cp",
   103  					"default", "test",
   104  					RedoDDLLogFileType, 1, uuid.NewString(), LogEXT) + SortLogEXT,
   105  			},
   106  			wantTs:       1,
   107  			wantFileType: RedoDDLLogFileType,
   108  		},
   109  		{
   110  			name: "happy ddl .sort",
   111  			args: arg{
   112  				name: fmt.Sprintf(RedoLogFileFormatV2, "cp",
   113  					"namespace", "test",
   114  					RedoDDLLogFileType, 1, uuid.NewString(), LogEXT) + SortLogEXT,
   115  			},
   116  			wantTs:       1,
   117  			wantFileType: RedoDDLLogFileType,
   118  		},
   119  		{
   120  			name: "happy ddl .tmp",
   121  			args: arg{
   122  				name: fmt.Sprintf(RedoLogFileFormatV1, "cp",
   123  					"test",
   124  					RedoDDLLogFileType, 1, uuid.NewString(), LogEXT) + TmpEXT,
   125  			},
   126  			wantTs:       1,
   127  			wantFileType: RedoDDLLogFileType,
   128  		},
   129  		{
   130  			name: "happy ddl .tmp",
   131  			args: arg{
   132  				name: fmt.Sprintf(RedoLogFileFormatV2, "cp",
   133  					"namespace", "test",
   134  					RedoDDLLogFileType, 1, uuid.NewString(), LogEXT) + TmpEXT,
   135  			},
   136  			wantTs:       1,
   137  			wantFileType: RedoDDLLogFileType,
   138  		},
   139  		{
   140  			name: "happy .meta",
   141  			args: arg{
   142  				name: "sdfsdfsf" + MetaEXT,
   143  			},
   144  			wantTs:       0,
   145  			wantFileType: RedoMetaFileType,
   146  		},
   147  		{
   148  			name: "not supported fileType",
   149  			args: arg{
   150  				name: "sdfsdfsf.sfsf",
   151  			},
   152  		},
   153  		{
   154  			name: "err wrong format ddl .tmp",
   155  			args: arg{
   156  				name: fmt.Sprintf("%s_%s_%s_%s_%d%s%s", /* a wrong format */
   157  					"cp", "default", "test",
   158  					RedoDDLLogFileType, 1, uuid.NewString(), LogEXT) + TmpEXT,
   159  			},
   160  			wantErr: ".*bad log name*.",
   161  		},
   162  	}
   163  	for _, tt := range tests {
   164  		ts, fileType, err := ParseLogFileName(tt.args.name)
   165  		if tt.wantErr != "" {
   166  			require.Regexp(t, tt.wantErr, err, tt.name)
   167  		} else {
   168  			require.Nil(t, err, tt.name)
   169  			require.EqualValues(t, tt.wantTs, ts, tt.name)
   170  			require.Equal(t, tt.wantFileType, fileType, tt.name)
   171  		}
   172  	}
   173  }
   174  
   175  func TestInitExternalStorage(t *testing.T) {
   176  	t.Parallel()
   177  
   178  	dir := t.TempDir()
   179  	urls := []string{
   180  		fmt.Sprintf("file://%s/test", dir),
   181  	}
   182  
   183  	for _, urlStr := range urls {
   184  		url, err := storage.ParseRawURL(urlStr)
   185  		require.NoError(t, err)
   186  		_, err = InitExternalStorage(context.Background(), *url)
   187  		require.NoError(t, err)
   188  	}
   189  }