github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/externalresource/model/config_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  	"path/filepath"
    18  	"testing"
    19  
    20  	brStorage "github.com/pingcap/tidb/br/pkg/storage"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestAdjust(t *testing.T) {
    25  	t.Parallel()
    26  	dirs := []string{"", "/tmp/dfe-storage", "/var/engine/", "/a/b", "/a/b/c"}
    27  	for _, dir := range dirs {
    28  		cfg := Config{
    29  			Local: LocalFileConfig{
    30  				BaseDir: dir,
    31  			},
    32  		}
    33  		if dir == "" {
    34  			dir = defaultLocalStorageDirPrefix
    35  		}
    36  		oldCfg := cfg
    37  		cfg.Adjust("test-executor")
    38  		require.Equal(t, oldCfg.S3, cfg.S3, "inputBaseDir: %s", dir)
    39  		require.NotEqual(t, oldCfg.Local, cfg.Local, "inputBaseDir: %s", dir)
    40  
    41  		expected := filepath.Join(dir, "test-executor")
    42  		require.Equal(t, expected, cfg.Local.BaseDir, "inputBaseDir: %s", dir)
    43  	}
    44  }
    45  
    46  func TestToBrBackendOptions(t *testing.T) {
    47  	t.Parallel()
    48  	cases := []struct {
    49  		config         *Config
    50  		expectedOpts   *brStorage.BackendOptions
    51  		expectedBucket string
    52  		expectedPrefix string
    53  		expectedType   ResourceType
    54  	}{
    55  		{
    56  			config:         &Config{},
    57  			expectedOpts:   &brStorage.BackendOptions{},
    58  			expectedBucket: "",
    59  			expectedPrefix: "",
    60  			expectedType:   ResourceTypeNone,
    61  		},
    62  		{
    63  			config: &Config{
    64  				S3: S3Config{
    65  					Bucket: "s3-bucket",
    66  					Prefix: "pe",
    67  				},
    68  				GCS: GCSConfig{
    69  					Prefix: "pe1",
    70  				},
    71  			},
    72  			expectedOpts:   &brStorage.BackendOptions{},
    73  			expectedBucket: "s3-bucket",
    74  			expectedPrefix: "pe",
    75  			expectedType:   ResourceTypeS3,
    76  		},
    77  		{
    78  			config: &Config{
    79  				GCS: GCSConfig{
    80  					Bucket: "gcs-bucket",
    81  					Prefix: "pe1",
    82  				},
    83  			},
    84  			expectedOpts:   &brStorage.BackendOptions{},
    85  			expectedBucket: "gcs-bucket",
    86  			expectedPrefix: "pe1",
    87  			expectedType:   ResourceTypeGCS,
    88  		},
    89  	}
    90  
    91  	for _, cs := range cases {
    92  		opts, bucket, prefix, tp := cs.config.ToBrBackendOptions()
    93  		require.Equal(t, cs.expectedOpts, opts)
    94  		require.Equal(t, cs.expectedBucket, bucket)
    95  		require.Equal(t, cs.expectedPrefix, prefix)
    96  		require.Equal(t, cs.expectedType, tp)
    97  	}
    98  }