github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/flags/flags_test.go (about) 1 // Copyright 2021 iLogtail Authors 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 flags 16 17 import ( 18 "flag" 19 "os" 20 "testing" 21 ) 22 23 func TestLoadEnvToFlags(t *testing.T) { 24 tests := []struct { 25 name string 26 envs map[string]string 27 flags map[string]interface{} 28 expected map[string]interface{} 29 }{ 30 { 31 name: "string flag", 32 envs: map[string]string{ 33 LoongcollectorEnvPrefix + "CONFIG": "/etc/ilogtail/config.json", 34 }, 35 flags: map[string]interface{}{ 36 "config": "", 37 }, 38 expected: map[string]interface{}{ 39 "config": "/etc/ilogtail/config.json", 40 }, 41 }, 42 { 43 name: "bool flag", 44 envs: map[string]string{ 45 LoongcollectorEnvPrefix + "DEBUG": "true", 46 }, 47 flags: map[string]interface{}{ 48 "debug": false, 49 }, 50 expected: map[string]interface{}{ 51 "debug": true, 52 }, 53 }, 54 { 55 name: "int flag", 56 envs: map[string]string{ 57 LoongcollectorEnvPrefix + "PORT": "8080", 58 }, 59 flags: map[string]interface{}{ 60 "port": 0, 61 }, 62 expected: map[string]interface{}{ 63 "port": 8080, 64 }, 65 }, 66 { 67 name: "env not exist", 68 envs: map[string]string{}, 69 flags: map[string]interface{}{ 70 "config": "default", 71 }, 72 expected: map[string]interface{}{ 73 "config": "default", 74 }, 75 }, 76 { 77 name: "invalid bool value", 78 envs: map[string]string{ 79 LoongcollectorEnvPrefix + "DEBUG": "invalid", 80 }, 81 flags: map[string]interface{}{ 82 "debug": false, 83 }, 84 expected: map[string]interface{}{ 85 "debug": false, 86 }, 87 }, 88 { 89 name: "invalid int value", 90 envs: map[string]string{ 91 LoongcollectorEnvPrefix + "PORT": "invalid", 92 }, 93 flags: map[string]interface{}{ 94 "port": 0, 95 }, 96 expected: map[string]interface{}{ 97 "port": 0, 98 }, 99 }, 100 } 101 102 for _, tt := range tests { 103 t.Run(tt.name, func(t *testing.T) { 104 // Clean environment variables 105 os.Clearenv() 106 107 // Set environment variables 108 for k, v := range tt.envs { 109 os.Setenv(k, v) 110 } 111 112 // Create flags 113 flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) 114 for name, value := range tt.flags { 115 switch v := value.(type) { 116 case string: 117 flag.String(name, v, "test flag") 118 case bool: 119 flag.Bool(name, v, "test flag") 120 case int: 121 flag.Int(name, v, "test flag") 122 } 123 } 124 125 // Load environment variables to flags 126 LoadEnvToFlags() 127 128 found := false 129 // Verify flag values 130 flag.VisitAll(func(f *flag.Flag) { 131 found = true 132 expected := tt.expected[f.Name] 133 switch v := expected.(type) { 134 case string: 135 if f.Value.String() != v { 136 t.Errorf("flag %s = %s, want %s", f.Name, f.Value.String(), v) 137 } 138 case bool: 139 if f.Value.String() != "true" && v || f.Value.String() != "false" && !v { 140 t.Errorf("flag %s = %s, want %v", f.Name, f.Value.String(), v) 141 } 142 case int: 143 if f.Value.String() != "0" && v == 0 || f.Value.String() != "8080" && v == 8080 { 144 t.Errorf("flag %s = %s, want %d", f.Name, f.Value.String(), v) 145 } 146 } 147 }) 148 if !found { 149 t.Errorf("flag %s not found", tt) 150 } 151 }) 152 } 153 }