github.com/cloudwego/kitex@v0.9.0/pkg/utils/yaml_test.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package utils 18 19 import ( 20 "os" 21 "runtime" 22 "testing" 23 "time" 24 25 "github.com/cloudwego/kitex/internal/test" 26 ) 27 28 const ( 29 TestYamlFile = "/tmp/test.yaml" 30 ) 31 32 var cfg *YamlConfig 33 34 func createTestYamlFile(t *testing.T, path string) { 35 file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm) 36 if err != nil { 37 t.Errorf("Open file: %s error, err: %s", path, err.Error()) 38 t.FailNow() 39 } 40 defer file.Close() 41 42 content := ` 43 Address: ":8888" 44 ExitWaitTimeout: 123ms 45 ReadWriteTimeout: 456ms 46 EnableMetrics: true 47 EnableTracing: true 48 EnableDebugServer: true 49 DebugServerPort: "18888" 50 Limit: 51 MaxQPS: 50 52 MaxConn: 100 53 Log: 54 Dir: log 55 Loggers: 56 - Name: default 57 Level: ERROR 58 EnableDyeLog: true 59 Outputs: 60 - File 61 - Console 62 - Agent 63 - Name: rpcAccess 64 Level: Info 65 Outputs: 66 - Agent 67 - Name: rpcCall 68 Level: warn 69 Outputs: 70 - File 71 MockInt: 12345 72 MockInt64: 123456789 73 MockFloat64: 123456.789` 74 _, err = file.WriteString(content) 75 if err != nil { 76 t.Errorf("Mock content into file: %s error, err: %s", path, err.Error()) 77 t.FailNow() 78 } 79 } 80 81 func deleteTestYamlFile(t *testing.T, path string) { 82 if err := os.Remove(path); err != nil { 83 t.Errorf("Remove file: %s error, err: %s", path, err.Error()) 84 } 85 } 86 87 func TestMain(m *testing.M) { 88 if runtime.GOOS == "windows" { 89 return 90 } 91 t := &testing.T{} 92 createTestYamlFile(t, TestYamlFile) 93 defer func() { 94 deleteTestYamlFile(t, TestYamlFile) 95 }() 96 97 cfg, _ = ReadYamlConfigFile(TestYamlFile) 98 } 99 100 func Test_ReadYamlConfigFile(t *testing.T) { 101 createTestYamlFile(t, TestYamlFile) 102 defer func() { 103 deleteTestYamlFile(t, TestYamlFile) 104 }() 105 106 cfg, err := ReadYamlConfigFile(TestYamlFile) 107 test.Assert(t, err == nil) 108 addr, ok := cfg.GetString("Address") 109 test.Assert(t, ok && addr == ":8888") 110 111 _, err = ReadYamlConfigFile("notExist.yaml") 112 test.Assert(t, err != nil) 113 } 114 115 func TestYamlConfig_Get(t *testing.T) { 116 // exist 117 val, ok := cfg.Get("Address") 118 test.Assert(t, ok && val != nil) 119 120 // not exist 121 _, ok = cfg.Get("NotExist") 122 test.Assert(t, !ok) 123 } 124 125 func TestYamlConfig_GetString(t *testing.T) { 126 // string 127 val, ok := cfg.GetString("Address") 128 test.Assert(t, ok && val != "") 129 130 // not string 131 _, ok = cfg.GetString("EnableMetrics") 132 test.Assert(t, !ok) 133 134 // not exist 135 _, ok = cfg.GetString("NotExist") 136 test.Assert(t, !ok) 137 } 138 139 func TestYamlConfig_GetBool(t *testing.T) { 140 // bool 141 val, ok := cfg.GetBool("EnableMetrics") 142 test.Assert(t, ok && val) 143 144 // not bool 145 _, ok = cfg.GetBool("Address") 146 test.Assert(t, !ok) 147 148 // not exist 149 _, ok = cfg.GetBool("NotExist") 150 test.Assert(t, !ok) 151 } 152 153 func TestYamlConfig_GetInt(t *testing.T) { 154 // int 155 val, ok := cfg.GetInt("MockInt") 156 test.Assert(t, ok && val == 12345) 157 158 // not int 159 _, ok = cfg.GetInt("EnableMetrics") 160 test.Assert(t, !ok) 161 162 // not exist 163 _, ok = cfg.GetString("NotExist") 164 test.Assert(t, !ok) 165 } 166 167 func TestYamlConfig_GetInt64(t *testing.T) { 168 // int64 169 val, ok := cfg.GetInt64("MockInt64") 170 test.Assert(t, ok && val == 123456789) 171 172 // not int64 173 _, ok = cfg.GetInt64("EnableMetrics") 174 test.Assert(t, !ok) 175 176 // not exist 177 _, ok = cfg.GetInt64("NotExist") 178 test.Assert(t, !ok) 179 } 180 181 func TestYamlConfig_GetFloat(t *testing.T) { 182 // float 183 val, ok := cfg.GetFloat("MockFloat64") 184 test.Assert(t, ok && val == 123456.789) 185 186 // not float 187 _, ok = cfg.GetFloat("EnableMetrics") 188 test.Assert(t, !ok) 189 190 // not exist 191 _, ok = cfg.GetFloat("NotExist") 192 test.Assert(t, !ok) 193 } 194 195 func TestYamlConfig_GetDuration(t *testing.T) { 196 // duration 197 val, ok := cfg.GetDuration("ExitWaitTimeout") 198 test.Assert(t, ok && val == time.Millisecond*123) 199 200 // not duration 201 _, ok = cfg.GetDuration("EnableMetrics") 202 test.Assert(t, !ok) 203 204 // not exist 205 _, ok = cfg.GetDuration("NotExist") 206 test.Assert(t, !ok) 207 }