github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/pkg/config/config_test.go (about)

     1  // Copyright (c) 2021 Terminus, 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package config
    16  
    17  import (
    18  	"bytes"
    19  	"os"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"gopkg.in/yaml.v3"
    24  )
    25  
    26  func Test_Multi_Non_Bool_Env_Replace(t *testing.T) {
    27  	content := `
    28  example:
    29  	name: "${ES|CK:foo}"
    30  `
    31  	want := `
    32  example:
    33  	name: "ck"
    34  `
    35  	_ = os.Setenv("CK", "ck")
    36  	result := EscapeEnv([]byte(content))
    37  
    38  	assert.Equal(t, want, string(result))
    39  	_ = os.Unsetenv("CK")
    40  
    41  	want = `
    42  example:
    43  	name: "foo"
    44  `
    45  	result = EscapeEnv([]byte(content))
    46  
    47  	assert.Equal(t, want, string(result))
    48  
    49  	want = `
    50  example:
    51  	name: "es"
    52  `
    53  
    54  	_ = os.Setenv("ES", "es")
    55  	result = EscapeEnv([]byte(content))
    56  
    57  	assert.Equal(t, want, string(result))
    58  	_ = os.Unsetenv("ES")
    59  
    60  	want = `
    61  example:
    62  	name: "es"
    63  `
    64  
    65  	_ = os.Setenv("ES", "es")
    66  	_ = os.Setenv("CK", "ck")
    67  	result = EscapeEnv([]byte(content))
    68  
    69  	assert.Equal(t, want, string(result))
    70  	_ = os.Unsetenv("ES")
    71  	_ = os.Unsetenv("CK")
    72  
    73  }
    74  
    75  func Test_Multi_Bool_Env_Replace(t *testing.T) {
    76  	content := `
    77  example:
    78  	name: "${ES|CK:default}"
    79  `
    80  	want := `
    81  example:
    82  	name: "false"
    83  `
    84  	_ = os.Setenv("CK", "false")
    85  	result := EscapeEnv([]byte(content))
    86  
    87  	assert.Equal(t, want, string(result))
    88  	_ = os.Unsetenv("CK")
    89  
    90  	want = `
    91  example:
    92  	name: "false"
    93  `
    94  
    95  	_ = os.Setenv("ES", "false")
    96  	result = EscapeEnv([]byte(content))
    97  
    98  	assert.Equal(t, want, string(result))
    99  	_ = os.Unsetenv("ES")
   100  
   101  	want = `
   102  example:
   103  	name: "True"
   104  `
   105  	_ = os.Setenv("ES", "false")
   106  	_ = os.Setenv("CK", "True")
   107  	result = EscapeEnv([]byte(content))
   108  
   109  	assert.Equal(t, want, string(result))
   110  	_ = os.Unsetenv("ES")
   111  	_ = os.Unsetenv("CK")
   112  
   113  }
   114  
   115  func Test_Multi_Mix_Bool_Env_Replace(t *testing.T) {
   116  	content := `
   117  example:
   118  	name: "${ES|CK:default}"
   119  `
   120  
   121  	want := `
   122  example:
   123  	name: "false"
   124  `
   125  	_ = os.Setenv("ES", "false")
   126  	_ = os.Setenv("CK", "ddd")
   127  	result := EscapeEnv([]byte(content))
   128  
   129  	assert.Equal(t, want, string(result))
   130  	_ = os.Unsetenv("ES")
   131  	_ = os.Unsetenv("CK")
   132  
   133  }
   134  
   135  func Test_polishBuffer(t *testing.T) {
   136  	const envLocale = "LOCALE"
   137  	content := `
   138  i18n:
   139    LOCALE: "${LOCALE:zh-CN}"
   140    num: 1
   141  `
   142  
   143  	// init buf
   144  	buf := bytes.NewBufferString(content)
   145  	// polish buf
   146  	assert.NoError(t, polishBuffer(buf))
   147  	// unmarshal
   148  	cfg := map[string]interface{}{}
   149  	err := yaml.Unmarshal(buf.Bytes(), &cfg)
   150  	assert.NoError(t, err)
   151  	i18nCfg, ok := cfg["i18n"]
   152  	assert.True(t, ok)
   153  	c, ok := i18nCfg.(map[string]interface{})
   154  	assert.True(t, ok)
   155  	assert.True(t, c[envLocale] == "zh-CN")
   156  	assert.True(t, c["num"] == 1)
   157  
   158  	// set env
   159  	assert.NoError(t, os.Setenv(envLocale, "en-US"))
   160  	defer func() { _ = os.Unsetenv(envLocale) }()
   161  	// init buf
   162  	buf = bytes.NewBufferString(content)
   163  	// polish buf
   164  	assert.NoError(t, polishBuffer(buf))
   165  	// unmarshal
   166  	cfg = map[string]interface{}{}
   167  	err = yaml.Unmarshal(buf.Bytes(), &cfg)
   168  	assert.NoError(t, err)
   169  	i18nCfg, ok = cfg["i18n"]
   170  	assert.True(t, ok)
   171  	c, ok = i18nCfg.(map[string]interface{})
   172  	assert.True(t, ok)
   173  	assert.True(t, c[envLocale] == "en-US")
   174  	assert.True(t, c["num"] == 1)
   175  }
   176  
   177  func TestUnmarshalToMap(t *testing.T) {
   178  	const envLocale = "LOCALE"
   179  	content := `
   180  i18n:
   181    LOCALE: "${LOCALE:zh-CN}"
   182    num: 1
   183  `
   184  
   185  	// init buf
   186  	buf := bytes.NewBufferString(content)
   187  	cfg := make(map[string]interface{})
   188  	// parse conf
   189  	assert.NoError(t, UnmarshalToMap(buf, "yaml", cfg))
   190  	i18nCfg, ok := cfg["i18n"]
   191  	assert.True(t, ok)
   192  	c, ok := i18nCfg.(map[string]interface{})
   193  	assert.True(t, ok)
   194  	assert.True(t, c[envLocale] == "zh-CN")
   195  	assert.True(t, c["num"] == 1)
   196  
   197  	// set env
   198  	assert.NoError(t, os.Setenv(envLocale, "en-US"))
   199  	defer func() { _ = os.Unsetenv(envLocale) }()
   200  	// init buf
   201  	buf = bytes.NewBufferString(content)
   202  	cfg = make(map[string]interface{})
   203  	// parse conf
   204  	assert.NoError(t, UnmarshalToMap(buf, "yaml", cfg))
   205  	i18nCfg, ok = cfg["i18n"]
   206  	assert.True(t, ok)
   207  	c, ok = i18nCfg.(map[string]interface{})
   208  	assert.True(t, ok)
   209  	assert.True(t, c[envLocale] == "en-US")
   210  	assert.True(t, c["num"] == 1)
   211  }